- Notifications
You must be signed in to change notification settings - Fork919
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Add support for gcp access token#1578
base:main
Are you sure you want to change the base?
Changes fromall commits
af64307
5c01b44
0f7dad5
5cd41b2
caf7ac2
f5c24b9
b97969a
6b52c79
0bd5399
bb4bd8c
6719977
3f7920a
7e43137
8e336b4
6b407ea
043d87a
5413fd2
8ad26e8
6f55e70
180d647
c288864
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -12,6 +12,7 @@ import ( | ||||||||||||||||||||||
kms "cloud.google.com/go/kms/apiv1" | ||||||||||||||||||||||
"cloud.google.com/go/kms/apiv1/kmspb" | ||||||||||||||||||||||
"github.com/sirupsen/logrus" | ||||||||||||||||||||||
"golang.org/x/oauth2" | ||||||||||||||||||||||
"google.golang.org/api/option" | ||||||||||||||||||||||
"google.golang.org/grpc" | ||||||||||||||||||||||
@@ -23,6 +24,9 @@ const ( | ||||||||||||||||||||||
// a path to a credentials file, or directly as the variable's value in JSON | ||||||||||||||||||||||
// format. | ||||||||||||||||||||||
SopsGoogleCredentialsEnv = "GOOGLE_CREDENTIALS" | ||||||||||||||||||||||
// SopsGoogleCredentialsOAuthToken is the environment variable used for the | ||||||||||||||||||||||
// GCP Oauth2 Token. | ||||||||||||||||||||||
SopsGoogleCredentialsOAuthToken = "GOOGLE_OAUTH_ACCESS_TOKEN" | ||||||||||||||||||||||
Comment on lines +27 to +29 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
// KeyTypeIdentifier is the string used to identify a GCP KMS MasterKey. | ||||||||||||||||||||||
KeyTypeIdentifier = "gcp_kms" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
@@ -203,8 +207,8 @@ func (key *MasterKey) TypeToIdentifier() string { | ||||||||||||||||||||||
return KeyTypeIdentifier | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// newKMSClient returns a GCP KMS client configured with the credentialJSON, | ||||||||||||||||||||||
//tokenSourceand/or grpcConn, falling back to environmental defaults. | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't think the GCP authentication method introduced in this PR is quite what you would call "token source". A token source method would be similar to the equivalents of AWS and Azure: Lines 72 to 76 in5355c24
Lines 58 to 62 in5355c24
| ||||||||||||||||||||||
// It returns an error if the ResourceID is invalid, or if the setup of the | ||||||||||||||||||||||
// client fails. | ||||||||||||||||||||||
func (key *MasterKey) newKMSClient() (*kms.KeyManagementClient, error) { | ||||||||||||||||||||||
@@ -219,37 +223,62 @@ func (key *MasterKey) newKMSClient() (*kms.KeyManagementClient, error) { | ||||||||||||||||||||||
case key.credentialJSON != nil: | ||||||||||||||||||||||
opts = append(opts, option.WithCredentialsJSON(key.credentialJSON)) | ||||||||||||||||||||||
default: | ||||||||||||||||||||||
credentials, err_credentials_file := getGoogleCredentials() | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
The Go idiom for variable names is camel case | ||||||||||||||||||||||
if credentials != nil { | ||||||||||||||||||||||
opts = append(opts, option.WithCredentialsJSON(credentials)) | ||||||||||||||||||||||
break | ||||||||||||||||||||||
} | ||||||||||||||||||||||
at_credentials, err_credentials_token := getGoogleOAuthToken() | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
if at_credentials != nil { | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
opts = append(opts, option.WithTokenSource(at_credentials)) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
} | ||||||||||||||||||||||
if err_credentials_file != nil && err_credentials_token != nil { | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
return nil, fmt.Errorf("credentials: failed to get credentials for gcp kms, add default credentials or oauth access token") | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
if key.grpcConn != nil { | ||||||||||||||||||||||
opts = append(opts, option.WithGRPCConn(key.grpcConn)) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
ctx := context.Background() | ||||||||||||||||||||||
client,err_credentials := kms.NewKeyManagementClient(ctx, opts...) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
iferr_credentials != nil { | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
return nil,err_credentials | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
} | ||||||||||||||||||||||
return client, nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// getGoogleCredentials returns the SopsGoogleCredentialsEnv variable, as | ||||||||||||||||||||||
// either the file contents of the path of a credentials file, or as value in | ||||||||||||||||||||||
// JSON format. | ||||||||||||||||||||||
// It returns an error and a nil byte slice if the environment variable is not set, | ||||||||||||||||||||||
// or the file cannot be read. | ||||||||||||||||||||||
func getGoogleCredentials() ([]byte, error) { | ||||||||||||||||||||||
if defaultCredentials, ok := os.LookupEnv(SopsGoogleCredentialsEnv); ok && len(defaultCredentials) > 0 { | ||||||||||||||||||||||
if _, err := os.Stat(defaultCredentials); err == nil { | ||||||||||||||||||||||
return os.ReadFile(defaultCredentials) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return []byte(defaultCredentials), nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return nil, fmt.Errorf("could not find Google credential file") | ||||||||||||||||||||||
Comment on lines +265 to +268 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Can you update the function doc comment to reflect behavior change? Right now, it incorrectly reads There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I also changed the description of the SopsGoogleCredentialsOAuthToken, where i forgot to update after removing the possibility of using a file path! Thank you for all of your comments, and time! 🌻 | ||||||||||||||||||||||
} | ||||||||||||||||||||||
// getGoogleOAuthToken returns the SopsGoogleCredentialsOauthToken variable, | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
// as the oauth token. | ||||||||||||||||||||||
// It returns an error and a nil byte slice if the envrionment variable is not set. | ||||||||||||||||||||||
func getGoogleOAuthToken() (oauth2.TokenSource, error) { | ||||||||||||||||||||||
Comment on lines +271 to +274 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||
if token, isSet := os.LookupEnv(SopsGoogleCredentialsOAuthToken); isSet { | ||||||||||||||||||||||
tokenSource := oauth2.StaticTokenSource( | ||||||||||||||||||||||
&oauth2.Token{AccessToken: token}, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
return tokenSource, nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return nil, fmt.Errorf("could not find Google OAuth token") | ||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module github.com/getsops/sops/v3 | ||
go 1.22 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Can you undo this change? Thanks. | ||
toolchain go1.23.6 | ||
require ( | ||
@@ -36,6 +37,7 @@ require ( | ||
github.com/urfave/cli v1.22.16 | ||
golang.org/x/crypto v0.35.0 | ||
golang.org/x/net v0.35.0 | ||
golang.org/x/oauth2 v0.26.0 | ||
golang.org/x/sys v0.30.0 | ||
golang.org/x/term v0.29.0 | ||
google.golang.org/api v0.223.0 | ||
@@ -139,7 +141,6 @@ require ( | ||
go.opentelemetry.io/otel/sdk v1.34.0 // indirect | ||
go.opentelemetry.io/otel/sdk/metric v1.33.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.34.0 // indirect | ||
golang.org/x/sync v0.11.0 // indirect | ||
golang.org/x/text v0.22.0 // indirect | ||
golang.org/x/time v0.10.0 // indirect | ||