From 4f4124c69dacde0976f0834afe1049154c3c6271 Mon Sep 17 00:00:00 2001 From: Sharif Elgamal Date: Tue, 10 Nov 2020 15:53:54 -0800 Subject: [PATCH] try to create credentials file if using env to authenticate --- pkg/addons/gcpauth/enable.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/addons/gcpauth/enable.go b/pkg/addons/gcpauth/enable.go index a394d481e..ed32452ae 100644 --- a/pkg/addons/gcpauth/enable.go +++ b/pkg/addons/gcpauth/enable.go @@ -19,6 +19,7 @@ package gcpauth import ( "bytes" "context" + "encoding/json" "os" "os/exec" "strconv" @@ -63,6 +64,18 @@ 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.") } + if creds.JSON == nil { + // credentials were found from the surrounding environment instead of from a file, we need to create a file from these creds. + token, err := creds.TokenSource.Token() + if err != nil { + // well that's no good, the creds aren't valid + 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.") + } + tokenCredsBuffer := new(bytes.Buffer) + json.NewEncoder(tokenCredsBuffer).Encode(token) + creds.JSON = tokenCredsBuffer.Bytes() + } + f := assets.NewMemoryAssetTarget(creds.JSON, credentialsPath, "0444") err = r.Copy(f) -- GitLab