Skip to content

Commit 78b4b83

Browse files
authored
Merge pull request #9674 from sharifelgamal/auth-env
Use special credential file location for cloud shell
2 parents 296d887 + c01e106 commit 78b4b83

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/addons/gcpauth/enable.go

+18
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package gcpauth
1919
import (
2020
"bytes"
2121
"context"
22+
"io/ioutil"
2223
"os"
2324
"os/exec"
25+
"path"
2426
"strconv"
2527

2628
"github.com/pkg/errors"
@@ -63,6 +65,22 @@ func enableAddon(cfg *config.ClusterConfig) error {
6365
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.")
6466
}
6567

68+
if creds.JSON == nil {
69+
// Cloud Shell sends credential files to an unusual location, let's check that location
70+
// For example, CLOUDSDK_CONFIG=/tmp/tmp.cflmvysoQE
71+
if e := os.Getenv("CLOUDSDK_CONFIG"); e != "" {
72+
credFile := path.Join(e, "application_default_credentials.json")
73+
b, err := ioutil.ReadFile(credFile)
74+
if err != nil {
75+
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.")
76+
}
77+
creds.JSON = b
78+
} else {
79+
// We don't currently support authentication through the metadata server
80+
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.")
81+
}
82+
}
83+
6684
f := assets.NewMemoryAssetTarget(creds.JSON, credentialsPath, "0444")
6785

6886
err = r.Copy(f)

0 commit comments

Comments
 (0)