Skip to content

Commit bfd7c7c

Browse files
committed
gcpkms: tweak obtaining of credentials
This ensures the error which can occur due to e.g. an invalid file path being set still surfaces, while simultaneously addressing the other concerns from review comments. Signed-off-by: Hidde Beydals <hidde@hhh.computer>
1 parent 542779f commit bfd7c7c

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

gcpkms/keysource.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,21 @@ func (key *MasterKey) newKMSClient() (*kms.KeyManagementClient, error) {
246246
case key.credentialJSON != nil:
247247
opts = append(opts, option.WithCredentialsJSON(key.credentialJSON))
248248
default:
249-
credentials, errCredentialsFile := getGoogleCredentials()
249+
credentials, err := getGoogleCredentials()
250+
if err != nil {
251+
return nil, fmt.Errorf("credentials: failed to obtain credentials from %q: %w", SopsGoogleCredentialsEnv, err)
252+
}
250253
if credentials != nil {
251254
opts = append(opts, option.WithCredentialsJSON(credentials))
252255
break
253256
}
254257

255-
atCredentials, errCredentialsToken := getGoogleOAuthTokenFromEnv()
256-
if atCredentials != nil {
258+
if atCredentials := getGoogleOAuthTokenFromEnv(); atCredentials != nil {
257259
opts = append(opts, option.WithTokenSource(atCredentials))
260+
break
258261
}
259262

260-
if errCredentialsFile != nil && errCredentialsToken != nil {
261-
return nil, fmt.Errorf("credentials: failed to get credentials for gcp kms, add default credentials or oauth access token from env")
262-
}
263+
return nil, fmt.Errorf("credentials: none found: missing %q or %q environment variable", SopsGoogleCredentialsEnv, SopsGoogleCredentialsOAuthTokenEnv)
263264
}
264265

265266
if key.grpcConn != nil {
@@ -278,30 +279,26 @@ func (key *MasterKey) newKMSClient() (*kms.KeyManagementClient, error) {
278279
// getGoogleCredentials returns the SopsGoogleCredentialsEnv variable, as
279280
// either the file contents of the path of a credentials file, or as value in
280281
// JSON format.
281-
// It returns an error and a nil byte slice if the environment variable is not set,
282-
// or the file cannot be read.
282+
// It returns an error and a nil byte slice if the file cannot be read.
283283
func getGoogleCredentials() ([]byte, error) {
284284
if defaultCredentials, ok := os.LookupEnv(SopsGoogleCredentialsEnv); ok && len(defaultCredentials) > 0 {
285285
if _, err := os.Stat(defaultCredentials); err == nil {
286286
return os.ReadFile(defaultCredentials)
287287
}
288-
289288
return []byte(defaultCredentials), nil
290289
}
291-
return nil, fmt.Errorf("could not find Google credential file")
290+
return nil, nil
292291
}
293292

294293
// getGoogleOAuthTokenFromEnv returns the SopsGoogleCredentialsOauthTokenEnv variable,
295294
// as the OAauth 2.0 token.
296295
// It returns an error and a nil byte slice if the envrionment variable is not set.
297-
func getGoogleOAuthTokenFromEnv() (oauth2.TokenSource, error) {
296+
func getGoogleOAuthTokenFromEnv() oauth2.TokenSource {
298297
if token, isSet := os.LookupEnv(SopsGoogleCredentialsOAuthTokenEnv); isSet && len(token) > 0 {
299298
tokenSource := oauth2.StaticTokenSource(
300299
&oauth2.Token{AccessToken: token},
301300
)
302-
303-
return tokenSource, nil
301+
return tokenSource
304302
}
305-
306-
return nil, fmt.Errorf("could not find Google OAuth token")
303+
return nil
307304
}

gcpkms/keysource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestMasterKey_createCloudKMSService_withCredentialsFile(t *testing.T) {
150150
key: MasterKey{
151151
ResourceID: testResourceID,
152152
},
153-
errString: "credentials: failed to get credentials",
153+
errString: `credentials: failed to obtain credentials from "SOPS_GOOGLE_CREDENTIALS"`,
154154
},
155155
}
156156

0 commit comments

Comments
 (0)