Skip to content

Commit

Permalink
issue-176: root kms client & key provider (#2574)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpeye authored Nov 27, 2024
1 parent 7a5fe83 commit 160a646
Show file tree
Hide file tree
Showing 13 changed files with 682 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cloud/blockstore/libs/root_kms/iface/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "client.h"

#include <cloud/blockstore/libs/encryption/encryption_key.h>
#include <cloud/blockstore/public/api/protos/encryption.pb.h>

namespace NCloud::NBlockStore {

using namespace NThreading;

namespace {

////////////////////////////////////////////////////////////////////////////////

class TRootKmsClientStub: public IRootKmsClient
{
public:
void Start() override
{}

void Stop() override
{}

auto Decrypt(const TString& keyId, const TString& ciphertext)
-> TFuture<TResultOrError<TEncryptionKey>> override
{
Y_UNUSED(keyId);
Y_UNUSED(ciphertext);
return MakeFuture<TResultOrError<TEncryptionKey>>(TErrorResponse(
E_NOT_IMPLEMENTED,
"RootKmsClientStub can't decrypt cyphertext"));
}

auto GenerateDataEncryptionKey(const TString& keyId)
-> TFuture<TResultOrError<NProto::TKmsKey>> override
{
Y_UNUSED(keyId);
return MakeFuture<TResultOrError<NProto::TKmsKey>>(TErrorResponse(
E_NOT_IMPLEMENTED,
"RootKmsClientStub can't generate DEK"));
}
};

} // namespace

////////////////////////////////////////////////////////////////////////////////

IRootKmsClientPtr CreateRootKmsClientStub()
{
return std::make_shared<TRootKmsClientStub>();
}

} // namespace NCloud::NBlockStore
38 changes: 38 additions & 0 deletions cloud/blockstore/libs/root_kms/iface/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "public.h"

#include <cloud/storage/core/libs/common/error.h>
#include <cloud/storage/core/libs/common/startable.h>

#include <library/cpp/threading/future/future.h>

namespace NCloud::NBlockStore {

////////////////////////////////////////////////////////////////////////////////

class TEncryptionKey;

namespace NProto {

class TKmsKey;

} // namespace NProto

////////////////////////////////////////////////////////////////////////////////

struct IRootKmsClient
: public IStartable
{
virtual auto Decrypt(const TString& keyId, const TString& ciphertext)
-> NThreading::TFuture<TResultOrError<TEncryptionKey>> = 0;

virtual auto GenerateDataEncryptionKey(const TString& keyId)
-> NThreading::TFuture<TResultOrError<NProto::TKmsKey>> = 0;
};

////////////////////////////////////////////////////////////////////////////////

IRootKmsClientPtr CreateRootKmsClientStub();

} // namespace NCloud::NBlockStore
66 changes: 66 additions & 0 deletions cloud/blockstore/libs/root_kms/iface/key_provider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "key_provider.h"

#include "client.h"

#include <cloud/blockstore/libs/encryption/encryption_key.h>
#include <cloud/storage/core/libs/coroutine/executor.h>

#include <library/cpp/string_utils/base64/base64.h>

#include <util/generic/string.h>
#include <util/string/builder.h>

namespace NCloud::NBlockStore {

using namespace NThreading;

namespace {

////////////////////////////////////////////////////////////////////////////////

class TRootKmsKeyProvider
: public IRootKmsKeyProvider
{
private:
const IRootKmsClientPtr Client;
const TString KeyId;

public:
TRootKmsKeyProvider(
IRootKmsClientPtr client,
TString keyId)
: Client(std::move(client))
, KeyId(std::move(keyId))
{}

auto GetKey(const NProto::TKmsKey& kmsKey, const TString& diskId)
-> TFuture<TResultOrError<TEncryptionKey>> override
{
Y_UNUSED(diskId);

return Client->Decrypt(kmsKey.GetKekId(), kmsKey.GetEncryptedDEK());
}

auto GenerateDataEncryptionKey(const TString& diskId)
-> TFuture<TResultOrError<NProto::TKmsKey>> override
{
Y_UNUSED(diskId);

return Client->GenerateDataEncryptionKey(KeyId);
}
};

} // namespace

////////////////////////////////////////////////////////////////////////////////

IRootKmsKeyProviderPtr CreateRootKmsKeyProvider(
IRootKmsClientPtr client,
TString keyId)
{
return std::make_shared<TRootKmsKeyProvider>(
std::move(client),
std::move(keyId));
}

} // namespace NCloud::NBlockStore
16 changes: 16 additions & 0 deletions cloud/blockstore/libs/root_kms/iface/key_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "public.h"

#include <cloud/blockstore/libs/encryption/public.h>
#include <cloud/storage/core/libs/common/error.h>

namespace NCloud::NBlockStore {

////////////////////////////////////////////////////////////////////////////////

IRootKmsKeyProviderPtr CreateRootKmsKeyProvider(
IRootKmsClientPtr client,
TString keyId);

} // namespace NCloud::NBlockStore
12 changes: 12 additions & 0 deletions cloud/blockstore/libs/root_kms/iface/public.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <memory>

namespace NCloud::NBlockStore {

////////////////////////////////////////////////////////////////////////////////

struct IRootKmsClient;
using IRootKmsClientPtr = std::shared_ptr<IRootKmsClient>;

} // namespace NCloud::NBlockStore
13 changes: 13 additions & 0 deletions cloud/blockstore/libs/root_kms/iface/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LIBRARY()

SRCS(
client.cpp
key_provider.cpp
)

PEERDIR(
cloud/blockstore/config
cloud/blockstore/libs/common
)

END()
Loading

0 comments on commit 160a646

Please sign in to comment.