|
| 1 | +// Copyright 2026-Present Couchbase, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License included |
| 4 | +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified |
| 5 | +// in that file, in accordance with the Business Source License, use of this |
| 6 | +// software will be governed by the Apache License, Version 2.0, included in |
| 7 | +// the file licenses/APL2.txt. |
| 8 | + |
| 9 | +//go:build enterprise |
| 10 | + |
| 11 | +package keymgmt |
| 12 | + |
| 13 | +import ( |
| 14 | + "fmt" |
| 15 | + |
| 16 | + "github.com/couchbase/cbauth" |
| 17 | + "github.com/couchbase/query/encryption" |
| 18 | + "github.com/couchbase/query/errors" |
| 19 | + "github.com/couchbase/query/logging" |
| 20 | +) |
| 21 | + |
| 22 | +type NodeEncryptionManager struct { |
| 23 | + keyStore *nodeKeyStore |
| 24 | +} |
| 25 | + |
| 26 | +func NewNodeEncryptionManager() *NodeEncryptionManager { |
| 27 | + return &NodeEncryptionManager{ |
| 28 | + keyStore: newNodeKeyStore(), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Performs initialization of the manager with info about the provided key datatypes |
| 33 | +// Typically invoked during service startup to initialize the manager with required key data. |
| 34 | +func (this *NodeEncryptionManager) PrimeKeys(keyDataTypes []encryption.KeyDataType) errors.Error { |
| 35 | + logging.Infof("Priming encryption-at-rest manager with keys for all available key data types") |
| 36 | + this.keyStore.PrimeKeys(keyDataTypes) |
| 37 | + logging.Infof("Finished priming operation of encryption-at-rest manager with keys for all available key data types") |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +func (this *NodeEncryptionManager) UpdateKeys(dataType cbauth.KeyDataType, newInfo *cbauth.EncrKeysInfo, prime bool) errors.Error { |
| 42 | + return this.keyStore.UpdateKeys(dataType, newInfo, prime) |
| 43 | +} |
| 44 | + |
| 45 | +func (this *NodeEncryptionManager) GetActiveKey(dt encryption.KeyDataType) (*encryption.EaRKey, errors.Error) { |
| 46 | + return this.keyStore.GetActiveKey(dt) |
| 47 | +} |
| 48 | + |
| 49 | +func (this *NodeEncryptionManager) RegisterCbauthEncryptionCallbacks() { |
| 50 | + cbauth.RegisterEncryptionKeysCallbacks(this.RefreshKeysCallback, this.GetInUseKeysCallback, this.DropKeysCallback, |
| 51 | + this.SynchronizeKeyFilesCallback) |
| 52 | +} |
| 53 | + |
| 54 | +func (this *NodeEncryptionManager) RefreshKeysCallback(dt cbauth.KeyDataType) error { |
| 55 | + // Key info will always be present in cbauth when RefreshKeysCallback is called. |
| 56 | + // Thus call cbauth.GetEncryptionKeys() instead of GetEncryptionKeysBlocking(). |
| 57 | + newKeys, cbErr := cbauth.GetEncryptionKeys(dt) |
| 58 | + |
| 59 | + // In RefreshKeysCallback, any error returned by GetEncryptionKeys() is a hard error including cbauth.ErrKeysNotAvailable. |
| 60 | + // This is because key info should always be available when RefreshKeysCallback is called by cbauth. |
| 61 | + if cbErr != nil { |
| 62 | + logging.Errorf( |
| 63 | + "Error refreshing encryption-at-rest configuration for key data type %s. Failed to fetch configuration from cbauth: %v", |
| 64 | + cbauthTypeToDataType(dt).String(), cbErr) |
| 65 | + return cbErr |
| 66 | + } |
| 67 | + |
| 68 | + err := this.keyStore.UpdateKeys(dt, newKeys, false) |
| 69 | + if err != nil && err.Code() != errors.E_INVALID_ENCRYPTION_KEY_DATATYPE { |
| 70 | + logging.Errorf( |
| 71 | + "Error refreshing encryption-at-rest configuration for key data type %s. Failed to update local encryption manager: %v", |
| 72 | + cbauthTypeToDataType(dt).String(), err) |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func (this *NodeEncryptionManager) GetInUseKeysCallback(dt cbauth.KeyDataType) ([]string, error) { |
| 79 | + // EAR TODO |
| 80 | + return nil, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (this *NodeEncryptionManager) DropKeysCallback(dt cbauth.KeyDataType, KeyIdsToDrop []string) { |
| 84 | + // EAR TODO |
| 85 | +} |
| 86 | + |
| 87 | +func (this *NodeEncryptionManager) SynchronizeKeyFilesCallback(dt cbauth.KeyDataType) error { |
| 88 | + // Query has no requirement for this as of now |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func validateKeyDataType(dt cbauth.KeyDataType) (encryption.KeyDataType, errors.Error) { |
| 93 | + kdt := encryption.KeyDataType{ |
| 94 | + TypeName: dt.TypeName, |
| 95 | + BucketUUID: dt.BucketUUID, |
| 96 | + } |
| 97 | + |
| 98 | + if dt.BucketUUID != "" && dt.TypeName != encryption.BUCKET_KEY_DATATYPE { |
| 99 | + return encryption.KeyDataType{}, errors.NewEncryptionError(errors.E_INVALID_ENCRYPTION_KEY_DATATYPE, |
| 100 | + fmt.Errorf("bucketUUID is only valid when typeName is service_bucket"), kdt.String()) |
| 101 | + } |
| 102 | + |
| 103 | + if dt.TypeName == encryption.BUCKET_KEY_DATATYPE && dt.BucketUUID == "" { |
| 104 | + return encryption.KeyDataType{}, errors.NewEncryptionError(errors.E_INVALID_ENCRYPTION_KEY_DATATYPE, |
| 105 | + fmt.Errorf("bucketUUID is required when typeName is service_bucket"), kdt.String()) |
| 106 | + } |
| 107 | + |
| 108 | + switch dt.TypeName { |
| 109 | + case encryption.BUCKET_KEY_DATATYPE: |
| 110 | + if dt.BucketUUID == "" { |
| 111 | + return encryption.KeyDataType{}, errors.NewEncryptionError(errors.E_INVALID_ENCRYPTION_KEY_DATATYPE, |
| 112 | + fmt.Errorf("bucketUUID is required when typeName is service_bucket"), kdt.String()) |
| 113 | + } |
| 114 | + case encryption.LOG_KEY_DATATYPE, encryption.OTHER_KEY_DATATYPE: |
| 115 | + default: |
| 116 | + return encryption.KeyDataType{}, errors.NewEncryptionError(errors.E_INVALID_ENCRYPTION_KEY_DATATYPE, |
| 117 | + fmt.Errorf("unsupported key data type"), kdt.String()) |
| 118 | + } |
| 119 | + |
| 120 | + return kdt, nil |
| 121 | +} |
| 122 | + |
| 123 | +func dataTypeToCbauthType(dt encryption.KeyDataType) cbauth.KeyDataType { |
| 124 | + return cbauth.KeyDataType{ |
| 125 | + TypeName: dt.TypeName, |
| 126 | + BucketUUID: dt.BucketUUID, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func cbauthTypeToDataType(dt cbauth.KeyDataType) encryption.KeyDataType { |
| 131 | + return encryption.KeyDataType{ |
| 132 | + TypeName: dt.TypeName, |
| 133 | + BucketUUID: dt.BucketUUID, |
| 134 | + } |
| 135 | +} |
0 commit comments