-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathencryption.go
515 lines (456 loc) · 16.8 KB
/
encryption.go
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2022 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package backupencryption
import (
"bytes"
"context"
"crypto"
cryptorand "crypto/rand"
"fmt"
"net/url"
"strings"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/ccl/backupccl/backupbase"
"github.com/cockroachdb/cockroach/pkg/ccl/storageccl"
"github.com/cockroachdb/cockroach/pkg/cloud"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/isql"
"github.com/cockroachdb/cockroach/pkg/util/ioctx"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
)
const (
// backupEncryptionInfoFile is the file name used to store the serialized
// EncryptionInfo proto while the backup is in progress.
backupEncryptionInfoFile = "ENCRYPTION-INFO"
// BackupOptEncKMS is the option name in a BACKUP statement to specify a KMS
// URI for encryption.
BackupOptEncKMS = "kms"
// BackupOptEncPassphrase is the option name in a BACKUP statement to specify
// a passphrase for encryption.
BackupOptEncPassphrase = "encryption_passphrase"
)
// ErrEncryptionInfoRead is a special error returned when the ENCRYPTION-INFO
// file is not found.
var ErrEncryptionInfoRead = errors.New(`ENCRYPTION-INFO not found`)
// BackupKMSEnv is the environment in which a backup with KMS is configured and
// used.
type BackupKMSEnv struct {
// Settings refers to the cluster settings that apply to the BackupKMSEnv.
Settings *cluster.Settings
// Conf represents the ExternalIODirConfig that applies to the BackupKMSEnv.
Conf *base.ExternalIODirConfig
// DB is the database handle that applies to the BackupKMSEnv.
DB isql.DB
// Username is the use that applies to the BackupKMSEnv.
Username username.SQLUsername
}
// MakeBackupKMSEnv returns an instance of `BackupKMSEnv` that defines the
// environment in which KMS is configured and used.
func MakeBackupKMSEnv(
settings *cluster.Settings, conf *base.ExternalIODirConfig, db isql.DB, user username.SQLUsername,
) BackupKMSEnv {
return BackupKMSEnv{
Settings: settings,
Conf: conf,
DB: db,
Username: user,
}
}
var _ cloud.KMSEnv = &BackupKMSEnv{}
// ClusterSettings implements the cloud.KMSEnv interface.
func (p *BackupKMSEnv) ClusterSettings() *cluster.Settings {
return p.Settings
}
// KMSConfig implements the cloud.KMSEnv interface.
func (p *BackupKMSEnv) KMSConfig() *base.ExternalIODirConfig {
return p.Conf
}
// DBHandle implements the cloud.KMSEnv interface.
func (p *BackupKMSEnv) DBHandle() isql.DB {
return p.DB
}
// User returns the user associated with the KMSEnv.
func (p *BackupKMSEnv) User() username.SQLUsername {
return p.Username
}
type (
// PlaintextMasterKeyID is the plain text version of the master key ID.
PlaintextMasterKeyID string
// HashedMasterKeyID is the hashed version of the master key ID.
HashedMasterKeyID string
// EncryptedDataKeyMap is a mapping from the hashed version of the master key
// ID to the encrypted data key.
EncryptedDataKeyMap struct {
m map[HashedMasterKeyID][]byte
}
)
// NewEncryptedDataKeyMap returns a new EncryptedDataKeyMap.
func NewEncryptedDataKeyMap() *EncryptedDataKeyMap {
return &EncryptedDataKeyMap{make(map[HashedMasterKeyID][]byte)}
}
// NewEncryptedDataKeyMapFromProtoMap constructs an EncryptedDataKeyMap from the
// passed in protoDataKeyMap.
func NewEncryptedDataKeyMapFromProtoMap(protoDataKeyMap map[string][]byte) *EncryptedDataKeyMap {
encMap := &EncryptedDataKeyMap{make(map[HashedMasterKeyID][]byte)}
for k, v := range protoDataKeyMap {
encMap.m[HashedMasterKeyID(k)] = v
}
return encMap
}
// AddEncryptedDataKey adds an entry to the EncryptedDataKeyMap.
func (e *EncryptedDataKeyMap) AddEncryptedDataKey(
masterKeyID PlaintextMasterKeyID, encryptedDataKey []byte,
) {
// Hash the master key ID before writing to the map.
hasher := crypto.SHA256.New()
hasher.Write([]byte(masterKeyID))
hash := hasher.Sum(nil)
e.m[HashedMasterKeyID(hash)] = encryptedDataKey
}
func (e *EncryptedDataKeyMap) getEncryptedDataKey(
masterKeyID PlaintextMasterKeyID,
) ([]byte, error) {
// Hash the master key ID before reading from the map.
hasher := crypto.SHA256.New()
hasher.Write([]byte(masterKeyID))
hash := hasher.Sum(nil)
var encDataKey []byte
var ok bool
if encDataKey, ok = e.m[HashedMasterKeyID(hash)]; !ok {
return nil, errors.New("could not find an entry in the encryptedDataKeyMap")
}
return encDataKey, nil
}
// RangeOverMap iterates over the map and executes fn on every key-value pair.
func (e *EncryptedDataKeyMap) RangeOverMap(fn func(masterKeyID HashedMasterKeyID, dataKey []byte)) {
for k, v := range e.m {
fn(k, v)
}
}
// ValidateKMSURIsAgainstFullBackup ensures that the KMS URIs provided to an
// incremental BACKUP are a subset of those used during the full BACKUP. It does
// this by ensuring that the KMS master key ID of each KMS URI specified during
// the incremental BACKUP can be found in the map written to `encryption-info`
// during a base BACKUP.
//
// The method also returns the KMSInfo to be used for all subsequent
// encryption/decryption operations during this BACKUP. By default it is the
// first KMS URI passed during the incremental BACKUP.
func ValidateKMSURIsAgainstFullBackup(
ctx context.Context,
kmsURIs []string,
kmsMasterKeyIDToDataKey *EncryptedDataKeyMap,
kmsEnv cloud.KMSEnv,
) (*jobspb.BackupEncryptionOptions_KMSInfo, error) {
var defaultKMSInfo *jobspb.BackupEncryptionOptions_KMSInfo
for _, kmsURI := range kmsURIs {
kms, err := cloud.KMSFromURI(ctx, kmsURI, kmsEnv)
if err != nil {
return nil, err
}
defer func() {
_ = kms.Close()
}()
// Depending on the KMS specific implementation, this may or may not contact
// the remote KMS.
id, err := kms.MasterKeyID()
if err != nil {
return nil, err
}
encryptedDataKey, err := kmsMasterKeyIDToDataKey.getEncryptedDataKey(PlaintextMasterKeyID(id))
if err != nil {
return nil,
errors.Wrap(err,
"one of the provided URIs was not used when encrypting the base BACKUP")
}
if defaultKMSInfo == nil {
defaultKMSInfo = &jobspb.BackupEncryptionOptions_KMSInfo{
Uri: kmsURI,
EncryptedDataKey: encryptedDataKey,
}
}
}
return defaultKMSInfo, nil
}
// MakeNewEncryptionOptions returns a new jobspb.BackupEncryptionOptions based
// on the passed in encryption parameters.
func MakeNewEncryptionOptions(
ctx context.Context, encryptionParams jobspb.BackupEncryptionOptions, kmsEnv cloud.KMSEnv,
) (*jobspb.BackupEncryptionOptions, *jobspb.EncryptionInfo, error) {
var encryptionOptions *jobspb.BackupEncryptionOptions
var encryptionInfo *jobspb.EncryptionInfo
switch encryptionParams.Mode {
case jobspb.EncryptionMode_Passphrase:
salt, err := storageccl.GenerateSalt()
if err != nil {
return nil, nil, err
}
encryptionInfo = &jobspb.EncryptionInfo{Salt: salt}
encryptionOptions = &jobspb.BackupEncryptionOptions{
Mode: jobspb.EncryptionMode_Passphrase,
Key: storageccl.GenerateKey([]byte(encryptionParams.RawPassphrase), salt),
}
case jobspb.EncryptionMode_KMS:
// Generate a 32 byte/256-bit crypto-random number which will serve as
// the data key for encrypting the BACKUP data and manifest files.
plaintextDataKey := make([]byte, 32)
_, err := cryptorand.Read(plaintextDataKey)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to generate DataKey")
}
encryptedDataKeyByKMSMasterKeyID, defaultKMSInfo, err :=
GetEncryptedDataKeyByKMSMasterKeyID(ctx, encryptionParams.RawKmsUris, plaintextDataKey, kmsEnv)
if err != nil {
return nil, nil, err
}
encryptedDataKeyMapForProto := make(map[string][]byte)
encryptedDataKeyByKMSMasterKeyID.RangeOverMap(
func(masterKeyID HashedMasterKeyID, dataKey []byte) {
encryptedDataKeyMapForProto[string(masterKeyID)] = dataKey
})
encryptionInfo = &jobspb.EncryptionInfo{EncryptedDataKeyByKMSMasterKeyID: encryptedDataKeyMapForProto}
encryptionOptions = &jobspb.BackupEncryptionOptions{
Mode: jobspb.EncryptionMode_KMS,
KMSInfo: defaultKMSInfo,
}
}
return encryptionOptions, encryptionInfo, nil
}
// GetEncryptedDataKeyFromURI returns the encrypted data key from the KMS
// specified by kmsURI.
func GetEncryptedDataKeyFromURI(
ctx context.Context, plaintextDataKey []byte, kmsURI string, kmsEnv cloud.KMSEnv,
) (string, []byte, error) {
kms, err := cloud.KMSFromURI(ctx, kmsURI, kmsEnv)
if err != nil {
return "", nil, err
}
defer func() {
_ = kms.Close()
}()
kmsURL, err := url.ParseRequestURI(kmsURI)
if err != nil {
return "", nil, errors.Wrap(err, "cannot parse KMSURI")
}
encryptedDataKey, err := kms.Encrypt(ctx, plaintextDataKey)
if err != nil {
return "", nil, errors.Wrapf(err, "failed to encrypt data key for KMS scheme %s",
kmsURL.Scheme)
}
masterKeyID, err := kms.MasterKeyID()
if err != nil {
return "", nil, errors.Wrapf(err, "failed to get master key ID for KMS scheme %s",
kmsURL.Scheme)
}
return masterKeyID, encryptedDataKey, nil
}
// GetEncryptedDataKeyByKMSMasterKeyID constructs a mapping {MasterKeyID :
// EncryptedDataKey} for each KMS URI provided during a full BACKUP. The
// MasterKeyID is hashed before writing it to the map.
//
// The method also returns the KMSInfo to be used for all subsequent
// encryption/decryption operations during this BACKUP. By default it is the
// first KMS URI.
func GetEncryptedDataKeyByKMSMasterKeyID(
ctx context.Context, kmsURIs []string, plaintextDataKey []byte, kmsEnv cloud.KMSEnv,
) (*EncryptedDataKeyMap, *jobspb.BackupEncryptionOptions_KMSInfo, error) {
encryptedDataKeyByKMSMasterKeyID := NewEncryptedDataKeyMap()
// The coordinator node contacts every KMS and records the encrypted data
// key for each one.
var kmsInfo *jobspb.BackupEncryptionOptions_KMSInfo
for _, kmsURI := range kmsURIs {
masterKeyID, encryptedDataKey, err := GetEncryptedDataKeyFromURI(ctx,
plaintextDataKey, kmsURI, kmsEnv)
if err != nil {
return nil, nil, err
}
// By default we use the first KMS URI and encrypted data key for subsequent
// encryption/decryption operation during a BACKUP.
if kmsInfo == nil {
kmsInfo = &jobspb.BackupEncryptionOptions_KMSInfo{
Uri: kmsURI,
EncryptedDataKey: encryptedDataKey,
}
}
encryptedDataKeyByKMSMasterKeyID.AddEncryptedDataKey(PlaintextMasterKeyID(masterKeyID),
encryptedDataKey)
}
return encryptedDataKeyByKMSMasterKeyID, kmsInfo, nil
}
// WriteNewEncryptionInfoToBackup writes a versioned ENCRYPTION-INFO file to
// external storage.
func WriteNewEncryptionInfoToBackup(
ctx context.Context, opts *jobspb.EncryptionInfo, dest cloud.ExternalStorage, numFiles int,
) error {
// New encryption-info file name is in the format "ENCRYPTION-INFO-<version number>"
newEncryptionInfoFile := fmt.Sprintf("%s-%d", backupEncryptionInfoFile, numFiles+1)
buf, err := protoutil.Marshal(opts)
if err != nil {
return err
}
return cloud.WriteFile(ctx, dest, newEncryptionInfoFile, bytes.NewReader(buf))
}
// GetEncryptionFromBase retrieves the encryption options of the base backup. It
// is expected that incremental backups use the same encryption options as the
// base backups.
func GetEncryptionFromBase(
ctx context.Context,
user username.SQLUsername,
makeCloudStorage cloud.ExternalStorageFromURIFactory,
baseBackupURI string,
encryptionParams jobspb.BackupEncryptionOptions,
kmsEnv cloud.KMSEnv,
) (*jobspb.BackupEncryptionOptions, error) {
var encryptionOptions *jobspb.BackupEncryptionOptions
if encryptionParams.Mode != jobspb.EncryptionMode_None {
exportStore, err := makeCloudStorage(ctx, baseBackupURI, user)
if err != nil {
return nil, err
}
defer exportStore.Close()
opts, err := ReadEncryptionOptions(ctx, exportStore)
if err != nil {
return nil, err
}
switch encryptionParams.Mode {
case jobspb.EncryptionMode_Passphrase:
encryptionOptions = &jobspb.BackupEncryptionOptions{
Mode: jobspb.EncryptionMode_Passphrase,
Key: storageccl.GenerateKey([]byte(encryptionParams.RawPassphrase), opts[0].Salt),
}
case jobspb.EncryptionMode_KMS:
var defaultKMSInfo *jobspb.BackupEncryptionOptions_KMSInfo
for _, encFile := range opts {
defaultKMSInfo, err = ValidateKMSURIsAgainstFullBackup(ctx, encryptionParams.RawKmsUris,
NewEncryptedDataKeyMapFromProtoMap(encFile.EncryptedDataKeyByKMSMasterKeyID), kmsEnv)
if err == nil {
break
}
}
if err != nil {
return nil, err
}
encryptionOptions = &jobspb.BackupEncryptionOptions{
Mode: jobspb.EncryptionMode_KMS,
KMSInfo: defaultKMSInfo}
}
}
return encryptionOptions, nil
}
// GetEncryptionKey returns the decrypted plaintext data key to be used for
// encryption.
func GetEncryptionKey(
ctx context.Context, encryption *jobspb.BackupEncryptionOptions, kmsEnv cloud.KMSEnv,
) ([]byte, error) {
if encryption == nil {
return nil, errors.New("FileEncryptionOptions is nil when retrieving encryption key")
}
switch encryption.Mode {
case jobspb.EncryptionMode_Passphrase:
return encryption.Key, nil
case jobspb.EncryptionMode_KMS:
// Contact the selected KMS to derive the decrypted data key.
// TODO(pbardea): Add a check here if encryption.KMSInfo is unexpectedly nil
// here to avoid a panic, and return an error instead.
kms, err := cloud.KMSFromURI(ctx, encryption.KMSInfo.Uri, kmsEnv)
if err != nil {
return nil, err
}
defer func() {
_ = kms.Close()
}()
plaintextDataKey, err := kms.Decrypt(ctx, encryption.KMSInfo.EncryptedDataKey)
if err != nil {
return nil, errors.Wrap(err, "failed to decrypt data key")
}
return plaintextDataKey, nil
}
return nil, errors.New("invalid encryption mode")
}
// ReadEncryptionOptions takes in a backup location and tries to find
// and return all encryption option files in the backup. A backup
// normally only creates one encryption option file, but if the user
// uses ALTER BACKUP to add new keys, a new encryption option file
// will be placed side by side with the old one. Since the old file
// is still valid, as we never want to modify or delete an existing
// backup, we return both new and old files.
func ReadEncryptionOptions(
ctx context.Context, src cloud.ExternalStorage,
) ([]jobspb.EncryptionInfo, error) {
const encryptionReadErrorMsg = `could not find or read encryption information`
files, err := GetEncryptionInfoFiles(ctx, src)
if err != nil {
return nil, errors.Mark(errors.Wrap(err, encryptionReadErrorMsg), ErrEncryptionInfoRead)
}
var encInfo []jobspb.EncryptionInfo
// The user is more likely to pass in a KMS URI that was used to
// encrypt the backup recently, so we iterate the ENCRYPTION-INFO
// files from latest to oldest.
for i := len(files) - 1; i >= 0; i-- {
r, _, err := src.ReadFile(ctx, files[i], cloud.ReadOptions{NoFileSize: true})
if err != nil {
return nil, errors.Wrap(err, encryptionReadErrorMsg)
}
defer r.Close(ctx)
encInfoBytes, err := ioctx.ReadAll(ctx, r)
if err != nil {
return nil, errors.Wrap(err, encryptionReadErrorMsg)
}
var currentEncInfo jobspb.EncryptionInfo
if err := protoutil.Unmarshal(encInfoBytes, ¤tEncInfo); err != nil {
return nil, err
}
encInfo = append(encInfo, currentEncInfo)
}
return encInfo, nil
}
// GetEncryptionInfoFiles reads the ENCRYPTION-INFO files from external storage.
func GetEncryptionInfoFiles(ctx context.Context, dest cloud.ExternalStorage) ([]string, error) {
var files []string
// Look for all files in dest that start with "/ENCRYPTION-INFO"
// and return them.
err := dest.List(ctx, "", backupbase.ListingDelimDataSlash, func(p string) error {
paths := strings.Split(p, "/")
p = paths[len(paths)-1]
if match := strings.HasPrefix(p, backupEncryptionInfoFile); match {
files = append(files, p)
}
return nil
})
if len(files) < 1 {
return nil, errors.New("no ENCRYPTION-INFO files found")
}
return files, err
}
// WriteEncryptionInfoIfNotExists writes EncryptionInfo to external storage.
func WriteEncryptionInfoIfNotExists(
ctx context.Context, opts *jobspb.EncryptionInfo, dest cloud.ExternalStorage,
) error {
r, _, err := dest.ReadFile(ctx, backupEncryptionInfoFile, cloud.ReadOptions{NoFileSize: true})
if err == nil {
r.Close(ctx)
// If the file already exists, then we don't need to create a new one.
return nil
}
if !errors.Is(err, cloud.ErrFileDoesNotExist) {
return errors.Wrapf(err,
"returned an unexpected error when checking for the existence of %s file",
backupEncryptionInfoFile)
}
buf, err := protoutil.Marshal(opts)
if err != nil {
return err
}
if err := cloud.WriteFile(ctx, dest, backupEncryptionInfoFile, bytes.NewReader(buf)); err != nil {
return err
}
return nil
}