@@ -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