forked from getsops/sops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeysource.go
More file actions
366 lines (321 loc) · 11.9 KB
/
keysource.go
File metadata and controls
366 lines (321 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package gcpkms // import "github.com/getsops/sops/v3/gcpkms"
import (
"context"
"encoding/base64"
"fmt"
"os"
"regexp"
"strings"
"time"
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"
"github.com/getsops/sops/v3/logging"
)
const (
// SopsGoogleCredentialsEnv can be set as an environment variable as either
// a path to a credentials file, or directly as the variable's value in JSON
// format.
SopsGoogleCredentialsEnv = "GOOGLE_CREDENTIALS"
// SopsGoogleCredentialsOAuthTokenEnv is the environment variable used for the
// GCP OAuth 2.0 Token.
SopsGoogleCredentialsOAuthTokenEnv = "GOOGLE_OAUTH_ACCESS_TOKEN"
// SopsGCPKMSClientTypeEnv is the environment variable used to specify the
// GCP KMS client type. Valid values are "grpc" (default) and "rest".
SopsGCPKMSClientTypeEnv = "SOPS_GCP_KMS_CLIENT_TYPE"
// KeyTypeIdentifier is the string used to identify a GCP KMS MasterKey.
KeyTypeIdentifier = "gcp_kms"
)
var (
// gcpkmsTTL is the duration after which a MasterKey requires rotation.
gcpkmsTTL = time.Hour * 24 * 30 * 6
// log is the global logger for any GCP KMS MasterKey.
log *logrus.Logger
)
func init() {
log = logging.NewLogger("GCPKMS")
}
// MasterKey is a GCP KMS key used to encrypt and decrypt the SOPS
// data key.
type MasterKey struct {
// ResourceID is the resource id used to refer to the gcp kms key.
// It can be retrieved using the `gcloud` command.
ResourceID string
// EncryptedKey is the string returned after encrypting with GCP KMS.
EncryptedKey string
// CreationDate is the creation timestamp of the MasterKey. Used
// for NeedsRotation.
CreationDate time.Time
// tokenSource contains the oauth2.TokenSource used by the GCP client.
// It can be injected by a (local) keyservice.KeyServiceServer using
// TokenSource.ApplyToMasterKey.
// If nil, the remaining authentication methods are attempted.
tokenSource oauth2.TokenSource
// credentialJSON is the Service Account credentials JSON used for
// authenticating towards the GCP KMS service.
credentialJSON []byte
// grpcConn can be used to inject a custom GCP client connection.
// Mostly useful for testing at present, to wire the client to a mock
// server.
grpcConn *grpc.ClientConn
// grpcDialOpts are the gRPC dial options used to create the gRPC connection.
grpcDialOpts []grpc.DialOption
// useRESTClient indicates whether to use the REST client for GCP KMS.
useRESTClient bool
// clientOpts are the client options used to create the GCP KMS client.
clientOpts []option.ClientOption
}
// NewMasterKeyFromResourceID creates a new MasterKey with the provided resource
// ID.
func NewMasterKeyFromResourceID(resourceID string) *MasterKey {
k := &MasterKey{}
resourceID = strings.Replace(resourceID, " ", "", -1)
k.ResourceID = resourceID
k.CreationDate = time.Now().UTC()
return k
}
// MasterKeysFromResourceIDString takes a comma separated list of GCP KMS
// resource IDs and returns a slice of new MasterKeys for them.
func MasterKeysFromResourceIDString(resourceID string) []*MasterKey {
var keys []*MasterKey
if resourceID == "" {
return keys
}
for _, s := range strings.Split(resourceID, ",") {
keys = append(keys, NewMasterKeyFromResourceID(s))
}
return keys
}
// TokenSource is an oauth2.TokenSource used for authenticating towards the
// GCP KMS service.
type TokenSource struct {
source oauth2.TokenSource
}
// NewTokenSource creates a new TokenSource from the provided oauth2.TokenSource.
func NewTokenSource(source oauth2.TokenSource) TokenSource {
return TokenSource{source: source}
}
// ApplyToMasterKey configures the TokenSource on the provided key.
func (t TokenSource) ApplyToMasterKey(key *MasterKey) {
key.tokenSource = t.source
}
// CredentialJSON is the Service Account credentials JSON used for authenticating
// towards the GCP KMS service.
type CredentialJSON []byte
// ApplyToMasterKey configures the CredentialJSON on the provided key.
func (c CredentialJSON) ApplyToMasterKey(key *MasterKey) {
key.credentialJSON = c
}
// DialOptions are the gRPC dial options used to create the gRPC connection.
type DialOptions []grpc.DialOption
// ApplyToMasterKey configures the DialOptions on the provided key.
func (d DialOptions) ApplyToMasterKey(key *MasterKey) {
key.grpcDialOpts = d
}
// UseRESTClient configures the MasterKey to use the REST client for GCP KMS.
type UseRESTClient struct{}
// ApplyToMasterKey configures the MasterKey to use the REST client for GCP KMS.
func (UseRESTClient) ApplyToMasterKey(key *MasterKey) {
key.useRESTClient = true
}
// ClientOptions are the client options used to create the GCP KMS client.
type ClientOptions []option.ClientOption
// ApplyToMasterKey configures the ClientOptions on the provided key.
func (c ClientOptions) ApplyToMasterKey(key *MasterKey) {
key.clientOpts = c
}
// Encrypt takes a SOPS data key, encrypts it with GCP KMS, and stores the
// result in the EncryptedKey field.
//
// Consider using EncryptContext instead.
func (key *MasterKey) Encrypt(dataKey []byte) error {
return key.EncryptContext(context.Background(), dataKey)
}
// EncryptContext takes a SOPS data key, encrypts it with GCP KMS, and stores the
// result in the EncryptedKey field.
func (key *MasterKey) EncryptContext(ctx context.Context, dataKey []byte) error {
service, err := key.newKMSClient(ctx)
if err != nil {
log.WithField("resourceID", key.ResourceID).Info("Encryption failed")
return fmt.Errorf("cannot create GCP KMS service: %w", err)
}
defer func() {
if err := service.Close(); err != nil {
log.Error("failed to close GCP KMS client connection")
}
}()
req := &kmspb.EncryptRequest{
Name: key.ResourceID,
Plaintext: dataKey,
}
resp, err := service.Encrypt(ctx, req)
if err != nil {
log.WithField("resourceID", key.ResourceID).Info("Encryption failed")
return fmt.Errorf("failed to encrypt sops data key with GCP KMS key: %w", err)
}
// NB: base64 encoding is for compatibility with SOPS <=3.8.x.
// The previous GCP KMS client used to work with base64 encoded
// strings.
key.EncryptedKey = base64.StdEncoding.EncodeToString(resp.Ciphertext)
log.WithField("resourceID", key.ResourceID).Info("Encryption succeeded")
return nil
}
// SetEncryptedDataKey sets the encrypted data key for this master key.
func (key *MasterKey) SetEncryptedDataKey(enc []byte) {
key.EncryptedKey = string(enc)
}
// EncryptedDataKey returns the encrypted data key this master key holds.
func (key *MasterKey) EncryptedDataKey() []byte {
return []byte(key.EncryptedKey)
}
// EncryptIfNeeded encrypts the provided SOPS data key, if it has not been
// encrypted yet.
func (key *MasterKey) EncryptIfNeeded(dataKey []byte) error {
if key.EncryptedKey == "" {
return key.Encrypt(dataKey)
}
return nil
}
// Decrypt decrypts the EncryptedKey field with GCP KMS and returns
// the result.
//
// Consider using DecryptContext instead.
func (key *MasterKey) Decrypt() ([]byte, error) {
return key.DecryptContext(context.Background())
}
// DecryptContext decrypts the EncryptedKey field with GCP KMS and returns
// the result.
func (key *MasterKey) DecryptContext(ctx context.Context) ([]byte, error) {
service, err := key.newKMSClient(ctx)
if err != nil {
log.WithField("resourceID", key.ResourceID).Info("Decryption failed")
return nil, fmt.Errorf("cannot create GCP KMS service: %w", err)
}
defer func() {
if err := service.Close(); err != nil {
log.Error("failed to close GCP KMS client connection")
}
}()
// NB: this is for compatibility with SOPS <=3.8.x. The previous GCP KMS
// client used to work with base64 encoded strings.
decodedCipher, err := base64.StdEncoding.DecodeString(string(key.EncryptedDataKey()))
if err != nil {
log.WithField("resourceID", key.ResourceID).Info("Decryption failed")
return nil, err
}
req := &kmspb.DecryptRequest{
Name: key.ResourceID,
Ciphertext: decodedCipher,
}
resp, err := service.Decrypt(ctx, req)
if err != nil {
log.WithField("resourceID", key.ResourceID).Info("Decryption failed")
return nil, fmt.Errorf("failed to decrypt sops data key with GCP KMS key: %w", err)
}
log.WithField("resourceID", key.ResourceID).Info("Decryption succeeded")
return resp.Plaintext, nil
}
// NeedsRotation returns whether the data key needs to be rotated or not.
func (key *MasterKey) NeedsRotation() bool {
return time.Since(key.CreationDate) > (gcpkmsTTL)
}
// ToString converts the key to a string representation.
func (key *MasterKey) ToString() string {
return key.ResourceID
}
// ToMap converts the MasterKey to a map for serialization purposes.
func (key MasterKey) ToMap() map[string]interface{} {
out := make(map[string]interface{})
out["resource_id"] = key.ResourceID
out["created_at"] = key.CreationDate.UTC().Format(time.RFC3339)
out["enc"] = key.EncryptedKey
return out
}
// TypeToIdentifier returns the string identifier for the MasterKey type.
func (key *MasterKey) TypeToIdentifier() string {
return KeyTypeIdentifier
}
// newKMSClient returns a GCP KMS client configured with the tokenSource
// or credentialJSON, and/or grpcConn, falling back to environmental defaults.
// It returns an error if the ResourceID is invalid, or if the setup of the
// client fails.
func (key *MasterKey) newKMSClient(ctx context.Context) (*kms.KeyManagementClient, error) {
re := regexp.MustCompile(`^projects/(?P<project>[^/]+)/locations/[^/]+/keyRings/[^/]+/cryptoKeys/[^/]+$`)
matches := re.FindStringSubmatch(key.ResourceID)
if matches == nil {
return nil, fmt.Errorf("no valid resource ID found in %q", key.ResourceID)
}
var opts []option.ClientOption
opts = append(opts, option.WithQuotaProject(matches[1]))
switch {
case key.tokenSource != nil:
opts = append(opts, option.WithTokenSource(key.tokenSource))
case key.credentialJSON != nil:
opts = append(opts, option.WithCredentialsJSON(key.credentialJSON))
default:
credentials, err := getGoogleCredentials()
if err != nil {
return nil, fmt.Errorf("credentials: failed to obtain credentials from %q: %w", SopsGoogleCredentialsEnv, err)
}
if credentials != nil {
opts = append(opts, option.WithCredentialsJSON(credentials))
break
}
if atCredentials := getGoogleOAuthTokenFromEnv(); atCredentials != nil {
opts = append(opts, option.WithTokenSource(atCredentials))
break
}
}
switch {
case key.grpcConn != nil:
opts = append(opts, option.WithGRPCConn(key.grpcConn))
case len(key.grpcDialOpts) > 0:
for _, opt := range key.grpcDialOpts {
opts = append(opts, option.WithGRPCDialOption(opt))
}
}
// Add extra options.
opts = append(opts, key.clientOpts...)
// Select client type based on inputs.
clientType := strings.ToLower(os.Getenv(SopsGCPKMSClientTypeEnv))
var client *kms.KeyManagementClient
var err error
switch {
case clientType == "rest", key.useRESTClient:
client, err = kms.NewKeyManagementRESTClient(ctx, opts...)
default:
client, err = kms.NewKeyManagementClient(ctx, opts...)
}
if err != nil {
return nil, err
}
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 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, nil
}
// getGoogleOAuthTokenFromEnv returns the SopsGoogleCredentialsOauthTokenEnv variable,
// as the OAauth 2.0 token.
// It returns an error and a nil byte slice if the envrionment variable is not set.
func getGoogleOAuthTokenFromEnv() oauth2.TokenSource {
if token, ok := os.LookupEnv(SopsGoogleCredentialsOAuthTokenEnv); ok && len(token) > 0 {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
return tokenSource
}
return nil
}