Skip to content

Commit fd95030

Browse files
authored
Merge pull request #1794 from matheuscscp/gcp-kms-token-source
Add support for `oauth2.TokenSource` in GCP KMS
2 parents 5355c24 + cac6e62 commit fd95030

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

gcpkms/keysource.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
kms "cloud.google.com/go/kms/apiv1"
1313
"cloud.google.com/go/kms/apiv1/kmspb"
1414
"github.com/sirupsen/logrus"
15+
"golang.org/x/oauth2"
1516
"google.golang.org/api/option"
1617
"google.golang.org/grpc"
1718

@@ -50,6 +51,11 @@ type MasterKey struct {
5051
// for NeedsRotation.
5152
CreationDate time.Time
5253

54+
// tokenSource contains the oauth2.TokenSource used by the GCP client.
55+
// It can be injected by a (local) keyservice.KeyServiceServer using
56+
// TokenSource.ApplyToMasterKey.
57+
// If nil, the remaining authentication methods are attempted.
58+
tokenSource oauth2.TokenSource
5359
// credentialJSON is the Service Account credentials JSON used for
5460
// authenticating towards the GCP KMS service.
5561
credentialJSON []byte
@@ -82,6 +88,22 @@ func MasterKeysFromResourceIDString(resourceID string) []*MasterKey {
8288
return keys
8389
}
8490

91+
// TokenSource is an oauth2.TokenSource used for authenticating towards the
92+
// GCP KMS service.
93+
type TokenSource struct {
94+
source oauth2.TokenSource
95+
}
96+
97+
// NewTokenSource creates a new TokenSource from the provided oauth2.TokenSource.
98+
func NewTokenSource(source oauth2.TokenSource) TokenSource {
99+
return TokenSource{source: source}
100+
}
101+
102+
// ApplyToMasterKey configures the TokenSource on the provided key.
103+
func (t TokenSource) ApplyToMasterKey(key *MasterKey) {
104+
key.tokenSource = t.source
105+
}
106+
85107
// CredentialJSON is the Service Account credentials JSON used for authenticating
86108
// towards the GCP KMS service.
87109
type CredentialJSON []byte
@@ -203,8 +225,8 @@ func (key *MasterKey) TypeToIdentifier() string {
203225
return KeyTypeIdentifier
204226
}
205227

206-
// newKMSClient returns a GCP KMS client configured with the credentialJSON
207-
// and/or grpcConn, falling back to environmental defaults.
228+
// newKMSClient returns a GCP KMS client configured with the tokenSource
229+
// or credentialJSON, and/or grpcConn, falling back to environmental defaults.
208230
// It returns an error if the ResourceID is invalid, or if the setup of the
209231
// client fails.
210232
func (key *MasterKey) newKMSClient() (*kms.KeyManagementClient, error) {
@@ -216,6 +238,8 @@ func (key *MasterKey) newKMSClient() (*kms.KeyManagementClient, error) {
216238

217239
var opts []option.ClientOption
218240
switch {
241+
case key.tokenSource != nil:
242+
opts = append(opts, option.WithTokenSource(key.tokenSource))
219243
case key.credentialJSON != nil:
220244
opts = append(opts, option.WithCredentialsJSON(key.credentialJSON))
221245
default:

gcpkms/keysource_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"cloud.google.com/go/kms/apiv1/kmspb"
1111
"github.com/stretchr/testify/assert"
12+
"golang.org/x/oauth2"
1213
"google.golang.org/grpc"
1314
"google.golang.org/grpc/credentials/insecure"
1415
)
@@ -38,6 +39,13 @@ func TestMasterKeysFromResourceIDString(t *testing.T) {
3839
}
3940
}
4041

42+
func TestTokenSource_ApplyToMasterKey(t *testing.T) {
43+
src := NewTokenSource(oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "some-token"}))
44+
key := &MasterKey{}
45+
src.ApplyToMasterKey(key)
46+
assert.Equal(t, src.source, key.tokenSource)
47+
}
48+
4149
func TestCredentialJSON_ApplyToMasterKey(t *testing.T) {
4250
key := &MasterKey{}
4351
credential := CredentialJSON("mock")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
github.com/urfave/cli v1.22.16
3737
golang.org/x/crypto v0.36.0
3838
golang.org/x/net v0.37.0
39+
golang.org/x/oauth2 v0.28.0
3940
golang.org/x/sys v0.31.0
4041
golang.org/x/term v0.30.0
4142
google.golang.org/api v0.227.0
@@ -138,7 +139,6 @@ require (
138139
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
139140
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
140141
go.opentelemetry.io/otel/trace v1.34.0 // indirect
141-
golang.org/x/oauth2 v0.28.0 // indirect
142142
golang.org/x/sync v0.12.0 // indirect
143143
golang.org/x/text v0.23.0 // indirect
144144
golang.org/x/time v0.11.0 // indirect

0 commit comments

Comments
 (0)