github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/ccl/testutils.cc (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Licensed as a CockroachDB Enterprise file under the Cockroach Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
     8  
     9  #include "testutils.h"
    10  #include <gtest/gtest.h>
    11  #include "ccl/baseccl/encryption_options.pb.h"
    12  #include "crypto_utils.h"
    13  
    14  namespace testutils {
    15  
    16  std::string MakePlaintextExtraOptions() {
    17    cockroach::ccl::baseccl::EncryptionOptions opts;
    18    opts.mutable_key_files()->set_current_key("plain");
    19    opts.mutable_key_files()->set_old_key("plain");
    20  
    21    std::string ret;
    22    opts.SerializeToString(&ret);
    23    return ret;
    24  }
    25  
    26  enginepbccl::SecretKey* MakeAES128Key(rocksdb::Env* env) {
    27    int64_t now;
    28    env->GetCurrentTime(&now);
    29  
    30    auto key = new enginepbccl::SecretKey();
    31    // Random key.
    32    key->set_key(RandomBytes(16));
    33  
    34    auto info = key->mutable_info();
    35    info->set_encryption_type(enginepbccl::AES128_CTR);
    36    info->set_creation_time(now);
    37    // Random key ID.
    38    info->set_key_id(HexString(RandomBytes(kKeyIDLength)));
    39  
    40    return key;
    41  }
    42  
    43  rocksdb::Status WriteAES128KeyFile(rocksdb::Env* env, const std::string& filename) {
    44    return rocksdb::WriteStringToFile(env, RandomBytes(16 + kKeyIDLength), filename,
    45                                      true /* should_sync */);
    46  }
    47  
    48  MemKeyManager::~MemKeyManager() {}
    49  
    50  std::shared_ptr<enginepbccl::SecretKey> MemKeyManager::CurrentKey() {
    51    if (key_ != nullptr) {
    52      return std::shared_ptr<enginepbccl::SecretKey>(new enginepbccl::SecretKey(*key_.get()));
    53    }
    54    return nullptr;
    55  }
    56  
    57  std::shared_ptr<enginepbccl::SecretKey> MemKeyManager::GetKey(const std::string& id) {
    58    if (key_ != nullptr && key_->info().key_id() == id) {
    59      return std::shared_ptr<enginepbccl::SecretKey>(new enginepbccl::SecretKey(*key_.get()));
    60    }
    61    return nullptr;
    62  }
    63  
    64  void MemKeyManager::set_key(enginepbccl::SecretKey* key) { key_.reset(key); }
    65  
    66  }  // namespace testutils