|
| 1 | +// Licensed to the LF AI & Data foundation under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package rootcoord |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "go.uber.org/zap" |
| 25 | + |
| 26 | + "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" |
| 27 | + "github.com/milvus-io/milvus/internal/metastore/model" |
| 28 | + "github.com/milvus-io/milvus/internal/types" |
| 29 | + "github.com/milvus-io/milvus/internal/util/hookutil" |
| 30 | + "github.com/milvus-io/milvus/pkg/v2/common" |
| 31 | + "github.com/milvus-io/milvus/pkg/v2/log" |
| 32 | + "github.com/milvus-io/milvus/pkg/v2/proto/querypb" |
| 33 | + "github.com/milvus-io/milvus/pkg/v2/proto/rootcoordpb" |
| 34 | + "github.com/milvus-io/milvus/pkg/v2/util" |
| 35 | + "github.com/milvus-io/milvus/pkg/v2/util/merr" |
| 36 | + "github.com/samber/lo" |
| 37 | +) |
| 38 | + |
| 39 | +type KeyManager struct { |
| 40 | + ctx context.Context |
| 41 | + meta IMetaTable |
| 42 | + mixCoord types.MixCoord |
| 43 | + enabled bool |
| 44 | +} |
| 45 | + |
| 46 | +func NewKeyManager( |
| 47 | + ctx context.Context, |
| 48 | + meta IMetaTable, |
| 49 | + mixCoord types.MixCoord, |
| 50 | +) *KeyManager { |
| 51 | + return &KeyManager{ |
| 52 | + ctx: ctx, |
| 53 | + meta: meta, |
| 54 | + mixCoord: mixCoord, |
| 55 | + enabled: hookutil.GetCipherWithState() != nil, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (km *KeyManager) Init() error { |
| 60 | + if !km.enabled { |
| 61 | + log.Info("KeyManager disabled (cipher plugin not loaded)") |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + hookutil.GetCipherWithState().RegisterRotationCallback(km.onKeyRotated) |
| 66 | + log.Info("KeyManager initialized") |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (km *KeyManager) GetDatabaseEzStates() ([]int64, error) { |
| 71 | + if !km.enabled { |
| 72 | + return nil, nil |
| 73 | + } |
| 74 | + |
| 75 | + currentStates, err := hookutil.GetEzStates() |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("failed to get cipher states: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + revokedDBs := make(map[int64]struct{}) |
| 81 | + for ezID, currentState := range currentStates { |
| 82 | + switch currentState { |
| 83 | + case hookutil.KeyStateDisabled, hookutil.KeyStatePendingDeletion: |
| 84 | + db, err := km.getDatabaseByEzID(ezID) |
| 85 | + if err != nil { |
| 86 | + log.Warn("KeyManager: failed to get database for ezID", zap.Int64("ezID", ezID), zap.Error(err)) |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + revokedDBs[db.ID] = struct{}{} |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + revokedDBIDs := lo.Keys(revokedDBs) |
| 95 | + if err := km.releaseLoadedCollections(revokedDBIDs); err != nil { |
| 96 | + log.Warn("KeyManager: failed to release collections for revoked databases", zap.Error(err)) |
| 97 | + } |
| 98 | + |
| 99 | + return revokedDBIDs, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (km *KeyManager) releaseLoadedCollections(revokedDBIDs []int64) error { |
| 103 | + if len(revokedDBIDs) == 0 { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + collectionIDsToCheck := make([]int64, 0) |
| 108 | + |
| 109 | + for _, dbID := range revokedDBIDs { |
| 110 | + db, err := km.meta.GetDatabaseByID(km.ctx, dbID, 0) |
| 111 | + if err != nil { |
| 112 | + log.Warn("KeyManager: failed to get database metadata", zap.Int64("dbID", dbID), zap.Error(err)) |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + colls, err := km.meta.ListCollections(km.ctx, db.Name, 0, true) |
| 117 | + if err != nil { |
| 118 | + log.Warn("KeyManager: failed to list collections for revoked database", |
| 119 | + zap.Int64("dbID", dbID), |
| 120 | + zap.String("dbName", db.Name), |
| 121 | + zap.Error(err)) |
| 122 | + continue |
| 123 | + } |
| 124 | + |
| 125 | + for _, coll := range colls { |
| 126 | + collectionIDsToCheck = append(collectionIDsToCheck, coll.CollectionID) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if len(collectionIDsToCheck) == 0 { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + resp, err := km.mixCoord.ShowLoadCollections(km.ctx, &querypb.ShowCollectionsRequest{ |
| 135 | + CollectionIDs: collectionIDsToCheck, |
| 136 | + }) |
| 137 | + if err := merr.CheckRPCCall(resp.GetStatus(), err); err != nil { |
| 138 | + if errors.Is(err, merr.ErrCollectionNotLoaded) { |
| 139 | + return nil |
| 140 | + } |
| 141 | + return fmt.Errorf("failed to get loaded collections: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + log.Info("KeyManager: releasing loaded collection for revoked database", |
| 145 | + zap.Int64s("collectionIDs", resp.GetCollectionIDs()), |
| 146 | + ) |
| 147 | + for _, collID := range resp.GetCollectionIDs() { |
| 148 | + req := &querypb.ReleaseCollectionRequest{CollectionID: collID} |
| 149 | + if _, err := km.mixCoord.ReleaseCollection(km.ctx, req); err != nil { |
| 150 | + log.Warn("KeyManager: failed to release collection", zap.Int64("collectionID", collID), zap.Error(err)) |
| 151 | + continue |
| 152 | + } |
| 153 | + } |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func (km *KeyManager) getDatabaseByEzID(ezID int64) (*model.Database, error) { |
| 158 | + db, err := km.meta.GetDatabaseByID(km.ctx, ezID, 0) |
| 159 | + if err != nil { |
| 160 | + return km.meta.GetDatabaseByID(km.ctx, util.DefaultDBID, 0) |
| 161 | + } |
| 162 | + return db, nil |
| 163 | +} |
| 164 | + |
| 165 | +func (km *KeyManager) onKeyRotated(ezID int64) error { |
| 166 | + if !km.enabled { |
| 167 | + return nil |
| 168 | + } |
| 169 | + |
| 170 | + log.Info("KeyManager: handling key rotation", zap.Int64("ezID", ezID)) |
| 171 | + db, err := km.getDatabaseByEzID(ezID) |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + req := &rootcoordpb.AlterDatabaseRequest{ |
| 177 | + DbName: db.Name, |
| 178 | + Properties: []*commonpb.KeyValuePair{ |
| 179 | + { |
| 180 | + Key: common.InternalCipherKeyRotatedKey, |
| 181 | + }, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + status, err := km.mixCoord.AlterDatabase(km.ctx, req) |
| 186 | + if err := merr.CheckRPCCall(status, err); err != nil { |
| 187 | + log.Error("KeyManager: failed to broadcast key rotation", |
| 188 | + zap.Int64("dbID", db.ID), |
| 189 | + zap.Int64("ezID", ezID), |
| 190 | + zap.String("dbName", db.Name), |
| 191 | + zap.Error(err)) |
| 192 | + return fmt.Errorf("failed to broadcast key rotation: %w", err) |
| 193 | + } |
| 194 | + |
| 195 | + log.Info("KeyManager: key rotation handled", zap.Int64("ezID", ezID)) |
| 196 | + return nil |
| 197 | +} |
0 commit comments