2525// Session is an object representing session for GCP API.
2626type Session struct {
2727 Credentials * googleoauth.Credentials
28+
29+ // Path contains the filepath for provided credentials. When authenticating with
30+ // Default Application Credentials, Path will be empty.
31+ Path string
2832}
2933
3034// GetSession returns a GCP session by using credentials found in default locations in order:
@@ -35,17 +39,18 @@ type Session struct {
3539// gcloud cli defaults
3640// and, if no creds are found, asks for them and stores them on disk in a config file
3741func GetSession (ctx context.Context ) (* Session , error ) {
38- creds , err := loadCredentials (ctx )
42+ creds , path , err := loadCredentials (ctx )
3943 if err != nil {
4044 return nil , errors .Wrap (err , "failed to load credentials" )
4145 }
4246
4347 return & Session {
4448 Credentials : creds ,
49+ Path : path ,
4550 }, nil
4651}
4752
48- func loadCredentials (ctx context.Context ) (* googleoauth.Credentials , error ) {
53+ func loadCredentials (ctx context.Context ) (* googleoauth.Credentials , string , error ) {
4954 if len (credLoaders ) == 0 {
5055 for _ , authEnv := range authEnvs {
5156 credLoaders = append (credLoaders , & envLoader {env : authEnv })
@@ -66,30 +71,31 @@ func loadCredentials(ctx context.Context) (*googleoauth.Credentials, error) {
6671 onceLoggers [loader ].Do (func () {
6772 logrus .Infof ("Credentials loaded from %s" , loader )
6873 })
69- return creds , nil
74+ return creds , loader . Content (), nil
7075 }
7176 return getCredentials (ctx )
7277}
7378
74- func getCredentials (ctx context.Context ) (* googleoauth.Credentials , error ) {
79+ func getCredentials (ctx context.Context ) (* googleoauth.Credentials , string , error ) {
7580 creds , err := (& userLoader {}).Load (ctx )
7681 if err != nil {
77- return nil , err
82+ return nil , "" , err
7883 }
7984
8085 filePath := defaultAuthFilePath
8186 logrus .Infof ("Saving the credentials to %q" , filePath )
8287 if err := os .MkdirAll (filepath .Dir (filePath ), 0700 ); err != nil {
83- return nil , err
88+ return nil , "" , err
8489 }
8590 if err := os .WriteFile (filePath , creds .JSON , 0o600 ); err != nil {
86- return nil , err
91+ return nil , "" , err
8792 }
88- return creds , nil
93+ return creds , filePath , nil
8994}
9095
9196type credLoader interface {
9297 Load (context.Context ) (* googleoauth.Credentials , error )
98+ Content () string
9399}
94100
95101type envLoader struct {
@@ -115,19 +121,25 @@ func (e *envLoader) String() string {
115121 return strings .Join (path , ", " )
116122}
117123
124+ func (e * envLoader ) Content () string {
125+ envValue , found := os .LookupEnv (e .env )
126+ if ! found {
127+ return ""
128+ }
129+ return envValue
130+ }
131+
118132type fileOrContentLoader struct {
119133 pathOrContent string
120134 delegate credLoader
121135}
122136
123137func (fc * fileOrContentLoader ) Load (ctx context.Context ) (* googleoauth.Credentials , error ) {
124138 // if this is a path and we can stat it, assume it's ok
125- if _ , err := os .Stat (fc .pathOrContent ); err == nil {
126- fc .delegate = & fileLoader {path : fc .pathOrContent }
127- } else {
128- fc .delegate = & contentLoader {content : fc .pathOrContent }
139+ if _ , err := os .Stat (fc .pathOrContent ); err != nil {
140+ return nil , fmt .Errorf ("supplied value should be the path to a GCP credentials file: %w" , err )
129141 }
130-
142+ fc . delegate = & fileLoader { path : fc . pathOrContent }
131143 return fc .delegate .Load (ctx )
132144}
133145
@@ -138,6 +150,13 @@ func (fc *fileOrContentLoader) String() string {
138150 return "file or content"
139151}
140152
153+ func (fc * fileOrContentLoader ) Content () string {
154+ if _ , err := os .Stat (fc .pathOrContent ); err != nil {
155+ return ""
156+ }
157+ return fc .pathOrContent
158+ }
159+
141160type fileLoader struct {
142161 path string
143162}
@@ -154,6 +173,10 @@ func (f *fileLoader) String() string {
154173 return fmt .Sprintf ("file %q" , f .path )
155174}
156175
176+ func (f * fileLoader ) Content () string {
177+ return f .path
178+ }
179+
157180type contentLoader struct {
158181 content string
159182}
@@ -166,6 +189,10 @@ func (f *contentLoader) String() string {
166189 return "content <redacted>"
167190}
168191
192+ func (f * contentLoader ) Content () string {
193+ return ""
194+ }
195+
169196type cliLoader struct {}
170197
171198func (c * cliLoader ) Load (ctx context.Context ) (* googleoauth.Credentials , error ) {
@@ -176,14 +203,18 @@ func (c *cliLoader) String() string {
176203 return "gcloud CLI defaults"
177204}
178205
206+ func (c * cliLoader ) Content () string {
207+ return ""
208+ }
209+
179210type userLoader struct {}
180211
181212func (u * userLoader ) Load (ctx context.Context ) (* googleoauth.Credentials , error ) {
182213 var content string
183214 err := survey .Ask ([]* survey.Question {
184215 {
185216 Prompt : & survey.Multiline {
186- Message : "Service Account (absolute path to file or JSON content )" ,
217+ Message : "Service Account (absolute path to file)" ,
187218 // Due to a bug in survey pkg, help message is not rendered
188219 Help : "The location to file that contains the service account in JSON, or the service account in JSON format" ,
189220 },
@@ -193,5 +224,9 @@ func (u *userLoader) Load(ctx context.Context) (*googleoauth.Credentials, error)
193224 return nil , err
194225 }
195226 content = strings .TrimSpace (content )
196- return (& fileOrContentLoader {pathOrContent : content }).Load (ctx )
227+ return (& fileLoader {path : content }).Load (ctx )
228+ }
229+
230+ func (u * userLoader ) Content () string {
231+ return defaultAuthFilePath
197232}
0 commit comments