YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/encryption/universe_key_manager.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/encryption/universe_key_manager.h"
15
16
#include "yb/util/status_format.h"
17
18
namespace yb {
19
namespace encryption {
20
21
Result<std::unique_ptr<UniverseKeyManager>> UniverseKeyManager::FromKey(
22
6
    const std::string& key_id, const Slice& key_data) {
23
6
  auto universe_key_manager = std::make_unique<UniverseKeyManager>();
24
6
  UniverseKeyRegistryPB universe_key_registry;
25
6
  universe_key_registry.set_encryption_enabled(true);
26
6
  universe_key_registry.set_latest_version_id(key_id);
27
6
  auto encryption_params = VERIFY_RESULT(EncryptionParams::FromSlice(key_data));
28
0
  EncryptionParamsPB params_pb;
29
6
  encryption_params->ToEncryptionParamsPB(&params_pb);
30
6
  (*universe_key_registry.mutable_universe_keys())[key_id] = params_pb;
31
6
  universe_key_manager->SetUniverseKeyRegistry(universe_key_registry);
32
6
  return universe_key_manager;
33
6
}
34
35
0
void UniverseKeyManager::SetUniverseKeys(const UniverseKeysPB& universe_keys) {
36
0
  {
37
0
    std::unique_lock<std::mutex> l(mutex_);
38
0
    auto& keys_map = *universe_key_registry_.mutable_universe_keys();
39
0
    for (const auto& entry : universe_keys.map()) {
40
0
      auto encryption_params_res = EncryptionParams::FromSlice(entry.second);
41
0
      if (!encryption_params_res.ok()) {
42
0
        return;
43
0
      }
44
0
      EncryptionParamsPB params_pb;
45
0
      (*encryption_params_res)->ToEncryptionParamsPB(&params_pb);
46
0
      keys_map[entry.first] = params_pb;
47
0
    }
48
0
  }
49
0
  cond_.notify_all();
50
0
}
51
52
void UniverseKeyManager::SetUniverseKeyRegistry(
53
57
    const UniverseKeyRegistryPB& universe_key_registry) {
54
57
  {
55
57
    std::unique_lock<std::mutex> l(mutex_);
56
57
    universe_key_registry_ = universe_key_registry;
57
57
    received_universe_keys_ = true;
58
57
  }
59
57
  cond_.notify_all();
60
57
}
61
62
Result<EncryptionParamsPtr> UniverseKeyManager::GetUniverseParamsWithVersion(
63
35
    const UniverseKeyId& version_id) {
64
35
  std::unique_lock<std::mutex> l(mutex_);
65
35
  auto it = universe_key_registry_.universe_keys().find(version_id);
66
35
  if (it == universe_key_registry_.universe_keys().end()) {
67
0
    if (universe_key_registry_.universe_keys().empty()) {
68
0
      l.unlock();
69
0
      get_universe_keys_callback_();
70
0
      l.lock();
71
0
      it = universe_key_registry_.universe_keys().find(version_id);
72
0
    }
73
0
    if (it == universe_key_registry_.universe_keys().end()) {
74
0
      return STATUS_SUBSTITUTE(
75
0
          InvalidArgument, "Key with version number $0 does not exist", version_id);
76
0
    }
77
0
  }
78
35
  return EncryptionParams::FromEncryptionParamsPB(it->second);
79
35
}
80
81
39
Result<UniverseKeyParams> UniverseKeyManager::GetLatestUniverseParams() {
82
39
  std::unique_lock<std::mutex> l(mutex_);
83
39
  cond_.wait(l, [&] { return received_universe_keys_; });
84
39
  const auto it = universe_key_registry_.universe_keys().find(
85
39
      universe_key_registry_.latest_version_id());
86
39
  if (it == universe_key_registry_.universe_keys().end()) {
87
0
    return STATUS(IllegalState, "Could not find a latest universe key.");
88
0
  }
89
90
39
  UniverseKeyParams universe_key_params;
91
39
  universe_key_params.version_id = it->first;
92
39
  universe_key_params.params = VERIFY_RESULT(EncryptionParams::FromEncryptionParamsPB(it->second));
93
0
  return universe_key_params;
94
39
}
95
96
4.53M
bool UniverseKeyManager::IsEncryptionEnabled() {
97
4.53M
  std::unique_lock<std::mutex> l(mutex_);
98
4.53M
  return universe_key_registry_.encryption_enabled();
99
4.53M
}
100
101
0
bool UniverseKeyManager::ReceivedUniverseKeys() {
102
0
  std::unique_lock<std::mutex> l(mutex_);
103
0
  return received_universe_keys_;
104
0
}
105
106
} // namespace encryption
107
} // namespace yb