Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto-detect gce and do not enable gcp auth addon #10730

Merged
merged 19 commits into from
Mar 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions pkg/addons/gcpauth/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package gcpauth
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -54,6 +53,10 @@ func EnableOrDisable(cfg *config.ClusterConfig, name string, val string) error {
}

func enableAddon(cfg *config.ClusterConfig) error {
if isGCE() {
exit.Message(reason.InternalCredsNotFound, "It seems like you are on GCE, which means authentication should work without the GCP Auth addon.")
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
}

// Grab command runner from running cluster
cc := mustload.Running(cfg.Name)
r := cc.CP.Runner
Expand All @@ -65,20 +68,9 @@ func enableAddon(cfg *config.ClusterConfig) error {
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
}

// Don't mount in empty credentials file
if creds.JSON == nil {
// Cloud Shell sends credential files to an unusual location, let's check that location
// For example, CLOUDSDK_CONFIG=/tmp/tmp.cflmvysoQE
if e := os.Getenv("CLOUDSDK_CONFIG"); e != "" {
credFile := path.Join(e, "application_default_credentials.json")
b, err := ioutil.ReadFile(credFile)
if err != nil {
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
}
creds.JSON = b
} else {
// We don't currently support authentication through the metadata server
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
}
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
medyagh marked this conversation as resolved.
Show resolved Hide resolved
}

f := assets.NewMemoryAssetTarget(creds.JSON, credentialsPath, "0444")
Expand Down Expand Up @@ -134,3 +126,16 @@ func disableAddon(cfg *config.ClusterConfig) error {

return nil
}

func isGCE() bool {
resp, err := http.Get("metadata.google.internal")
if err != nil {
return false
}

if resp.Header.Get("Metadata-Flavor") == "Google" {
return true
}

return false
}