Skip to content

Commit 5ba30dc

Browse files
authored
Merge pull request #1973 from putsuka/feature/gcp-kms-client-type
2 parents 54c17cc + d667393 commit 5ba30dc

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ Or if you are logged in you can authorize by generating an access token:
309309
310310
$ export GOOGLE_OAUTH_ACCESS_TOKEN="$(gcloud auth print-access-token)"
311311
312+
By default, SOPS uses the gRPC client to communicate with GCP KMS. You can optionally
313+
switch to the REST client by setting the ``SOPS_GCP_KMS_CLIENT_TYPE`` environment variable:
314+
315+
.. code:: sh
316+
317+
$ export SOPS_GCP_KMS_CLIENT_TYPE=rest # Use REST client
318+
$ export SOPS_GCP_KMS_CLIENT_TYPE=grpc # Use gRPC client (default)
319+
312320
Encrypting/decrypting with GCP KMS requires a KMS ResourceID. You can use the
313321
cloud console the get the ResourceID or you can create one using the gcloud
314322
sdk:

gcpkms/keysource.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const (
2727
// SopsGoogleCredentialsOAuthTokenEnv is the environment variable used for the
2828
// GCP OAuth 2.0 Token.
2929
SopsGoogleCredentialsOAuthTokenEnv = "GOOGLE_OAUTH_ACCESS_TOKEN"
30+
// SopsGCPKMSClientTypeEnv is the environment variable used to specify the
31+
// GCP KMS client type. Valid values are "grpc" (default) and "rest".
32+
SopsGCPKMSClientTypeEnv = "SOPS_GCP_KMS_CLIENT_TYPE"
3033
// KeyTypeIdentifier is the string used to identify a GCP KMS MasterKey.
3134
KeyTypeIdentifier = "gcp_kms"
3235
)
@@ -68,6 +71,10 @@ type MasterKey struct {
6871
grpcConn *grpc.ClientConn
6972
// grpcDialOpts are the gRPC dial options used to create the gRPC connection.
7073
grpcDialOpts []grpc.DialOption
74+
// useRESTClient indicates whether to use the REST client for GCP KMS.
75+
useRESTClient bool
76+
// clientOpts are the client options used to create the GCP KMS client.
77+
clientOpts []option.ClientOption
7178
}
7279

7380
// NewMasterKeyFromResourceID creates a new MasterKey with the provided resource
@@ -126,6 +133,22 @@ func (d DialOptions) ApplyToMasterKey(key *MasterKey) {
126133
key.grpcDialOpts = d
127134
}
128135

136+
// UseRESTClient configures the MasterKey to use the REST client for GCP KMS.
137+
type UseRESTClient struct{}
138+
139+
// ApplyToMasterKey configures the MasterKey to use the REST client for GCP KMS.
140+
func (UseRESTClient) ApplyToMasterKey(key *MasterKey) {
141+
key.useRESTClient = true
142+
}
143+
144+
// ClientOptions are the client options used to create the GCP KMS client.
145+
type ClientOptions []option.ClientOption
146+
147+
// ApplyToMasterKey configures the ClientOptions on the provided key.
148+
func (c ClientOptions) ApplyToMasterKey(key *MasterKey) {
149+
key.clientOpts = c
150+
}
151+
129152
// Encrypt takes a SOPS data key, encrypts it with GCP KMS, and stores the
130153
// result in the EncryptedKey field.
131154
//
@@ -294,7 +317,19 @@ func (key *MasterKey) newKMSClient(ctx context.Context) (*kms.KeyManagementClien
294317
}
295318
}
296319

297-
client, err := kms.NewKeyManagementClient(ctx, opts...)
320+
// Add extra options.
321+
opts = append(opts, key.clientOpts...)
322+
323+
// Select client type based on inputs.
324+
clientType := strings.ToLower(os.Getenv(SopsGCPKMSClientTypeEnv))
325+
var client *kms.KeyManagementClient
326+
var err error
327+
switch {
328+
case clientType == "rest", key.useRESTClient:
329+
client, err = kms.NewKeyManagementRESTClient(ctx, opts...)
330+
default:
331+
client, err = kms.NewKeyManagementClient(ctx, opts...)
332+
}
298333
if err != nil {
299334
return nil, err
300335
}

0 commit comments

Comments
 (0)