Skip to content

Commit

Permalink
issue-176: setup root kms client & provider (#2582)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpeye authored Nov 28, 2024
1 parent db47e2e commit 40e9574
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 13 deletions.
19 changes: 19 additions & 0 deletions cloud/blockstore/apps/server/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cloud/blockstore/config/root_kms.pb.h>
#include <cloud/blockstore/libs/daemon/ydb/bootstrap.h>
#include <cloud/blockstore/libs/kms/iface/compute_client.h>
#include <cloud/blockstore/libs/kms/iface/kms_client.h>
Expand All @@ -7,6 +8,8 @@
#include <cloud/blockstore/libs/rdma/impl/client.h>
#include <cloud/blockstore/libs/rdma/impl/server.h>
#include <cloud/blockstore/libs/rdma/impl/verbs.h>
#include <cloud/blockstore/libs/root_kms/iface/client.h>
#include <cloud/blockstore/libs/root_kms/impl/client.h>
#include <cloud/blockstore/libs/service/device_handler.h>
#include <cloud/blockstore/libs/spdk/iface/env_stub.h>

Expand Down Expand Up @@ -76,6 +79,22 @@ int main(int argc, char** argv)
return NCloud::NBlockStore::CreateKmsClientStub();
};

serverModuleFactories->RootKmsClientFactory = [] (
const NProto::TRootKmsConfig& config,
NCloud::ILoggingServicePtr logging)
{
if (config.GetAddress()) {
return NCloud::NBlockStore::CreateRootKmsClient(
std::move(logging),
{.Address = config.GetAddress(),
.RootCertsFile = config.GetRootCertsFile(),
.CertChainFile = config.GetCertChainFile(),
.PrivateKeyFile = config.GetPrivateKeyFile()});
}

return NCloud::NBlockStore::CreateRootKmsClientStub();
};

serverModuleFactories->SpdkFactory = [] (
NSpdk::TSpdkEnvConfigPtr config)
{
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/apps/server/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PEERDIR(
cloud/blockstore/libs/kms/impl
cloud/blockstore/libs/logbroker/iface
cloud/blockstore/libs/rdma/impl
cloud/blockstore/libs/root_kms/impl
cloud/blockstore/libs/service
cloud/blockstore/libs/spdk/iface

Expand Down
21 changes: 21 additions & 0 deletions cloud/blockstore/config/root_kms.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

package NCloud.NBlockStore.NProto;

option go_package = "github.com/ydb-platform/nbs/cloud/blockstore/config";

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

message TRootKmsConfig
{
// Address of the RootKMS server.
optional string Address = 1;

// Key encryption key identifier.
optional string KeyId = 2;

// mTLS.
optional string RootCertsFile = 3;
optional string CertChainFile = 4;
optional string PrivateKeyFile = 5;
}
1 change: 1 addition & 0 deletions cloud/blockstore/config/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SRCS(
notify.proto
plugin.proto
rdma.proto
root_kms.proto
server.proto
spdk.proto
storage.proto
Expand Down
2 changes: 2 additions & 0 deletions cloud/blockstore/libs/daemon/common/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ void TBootstrapBase::Start()
START_KIKIMR_COMPONENT(IamTokenClient);
START_KIKIMR_COMPONENT(ComputeClient);
START_KIKIMR_COMPONENT(KmsClient);
START_KIKIMR_COMPONENT(RootKmsClient);
START_KIKIMR_COMPONENT(YdbStorage);
START_KIKIMR_COMPONENT(StatsUploader);
START_COMMON_COMPONENT(Spdk);
Expand Down Expand Up @@ -957,6 +958,7 @@ void TBootstrapBase::Stop()
STOP_COMMON_COMPONENT(Spdk);
STOP_KIKIMR_COMPONENT(StatsUploader);
STOP_KIKIMR_COMPONENT(YdbStorage);
STOP_KIKIMR_COMPONENT(RootKmsClient);
STOP_KIKIMR_COMPONENT(KmsClient);
STOP_KIKIMR_COMPONENT(ComputeClient);
STOP_KIKIMR_COMPONENT(IamTokenClient);
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/libs/daemon/common/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class TBootstrapBase
virtual IStartable* GetIamTokenClient() = 0;
virtual IStartable* GetComputeClient() = 0;
virtual IStartable* GetKmsClient() = 0;
virtual IStartable* GetRootKmsClient() = 0;

virtual void InitSpdk() = 0;
virtual void InitRdmaClient() = 0;
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/libs/daemon/local/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TBootstrapLocal final
IStartable* GetIamTokenClient() override { return nullptr; }
IStartable* GetComputeClient() override { return nullptr; }
IStartable* GetKmsClient() override { return nullptr; }
IStartable* GetRootKmsClient() override { return nullptr; }

void InitSpdk() override;
void InitRdmaClient() override;
Expand Down
14 changes: 14 additions & 0 deletions cloud/blockstore/libs/daemon/ydb/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <cloud/blockstore/libs/rdma/iface/client.h>
#include <cloud/blockstore/libs/rdma/iface/config.h>
#include <cloud/blockstore/libs/rdma/iface/server.h>
#include <cloud/blockstore/libs/root_kms/iface/client.h>
#include <cloud/blockstore/libs/root_kms/iface/key_provider.h>
#include <cloud/blockstore/libs/server/config.h>
#include <cloud/blockstore/libs/service/service_auth.h>
#include <cloud/blockstore/libs/service_kikimr/auth_provider_kikimr.h>
Expand Down Expand Up @@ -133,6 +135,7 @@ IStartable* TBootstrapYdb::GetCgroupStatsFetcher() { return CgroupStatsFetcher.g
IStartable* TBootstrapYdb::GetIamTokenClient() { return IamTokenClient.get(); }
IStartable* TBootstrapYdb::GetComputeClient() { return ComputeClient.get(); }
IStartable* TBootstrapYdb::GetKmsClient() { return KmsClient.get(); }
IStartable* TBootstrapYdb::GetRootKmsClient() { return RootKmsClient.get(); }

void TBootstrapYdb::InitConfigs()
{
Expand All @@ -151,6 +154,7 @@ void TBootstrapYdb::InitConfigs()
Configs->InitNotifyConfig();
Configs->InitIamClientConfig();
Configs->InitKmsClientConfig();
Configs->InitRootKmsConfig();
Configs->InitComputeClientConfig();
}

Expand Down Expand Up @@ -363,6 +367,16 @@ void TBootstrapYdb::InitKikimrService()

STORAGE_INFO("KmsKeyProvider initialized");

RootKmsClient = ServerModuleFactories->RootKmsClientFactory(
Configs->RootKmsConfig,
logging);

RootKmsKeyProvider = CreateRootKmsKeyProvider(
RootKmsClient,
Configs->RootKmsConfig.GetKeyId());

STORAGE_INFO("RootKmsKeyProvider initialized");

auto discoveryConfig = Configs->DiscoveryConfig;
if (discoveryConfig->GetConductorGroups()
|| discoveryConfig->GetInstanceListFile())
Expand Down
16 changes: 13 additions & 3 deletions cloud/blockstore/libs/daemon/ydb/bootstrap.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "public.h"

#include <cloud/blockstore/config/grpc_client.pb.h>

#include <cloud/blockstore/libs/daemon/common/bootstrap.h>
#include <cloud/blockstore/libs/kms/iface/public.h>
#include <cloud/blockstore/libs/logbroker/iface/public.h>
#include <cloud/blockstore/libs/notify/public.h>
#include <cloud/blockstore/libs/rdma/iface/public.h>
#include <cloud/blockstore/libs/root_kms/iface/public.h>
#include <cloud/blockstore/libs/ydbstats/public.h>

#include <cloud/storage/core/libs/actors/public.h>
Expand All @@ -15,6 +14,11 @@

#include <contrib/ydb/core/driver_lib/run/factories.h>

namespace NCloud::NBlockStore::NProto {
class TGrpcClientConfig;
class TRootKmsConfig;
} // namespace NCloud::NBlockStore::NProto

namespace NCloud::NBlockStore::NServer {

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -46,6 +50,10 @@ struct TServerModuleFactories
NProto::TGrpcClientConfig config,
ILoggingServicePtr logging)> KmsClientFactory;

std::function<IRootKmsClientPtr(
const NProto::TRootKmsConfig& config,
ILoggingServicePtr logging)> RootKmsClientFactory;

std::function<TSpdkParts(NSpdk::TSpdkEnvConfigPtr config)> SpdkFactory;

std::function<NRdma::IServerPtr(
Expand Down Expand Up @@ -83,14 +91,15 @@ struct TBootstrapYdb final
NIamClient::IIamTokenClientPtr IamTokenClient;
IComputeClientPtr ComputeClient;
IKmsClientPtr KmsClient;
IRootKmsClientPtr RootKmsClient;
std::function<void(TLog& log)> SpdkLogInitializer;

public:
TBootstrapYdb(
std::shared_ptr<NKikimr::TModuleFactories> moduleFactories,
std::shared_ptr<TServerModuleFactories> serverModuleFactories,
IDeviceHandlerFactoryPtr deviceHandlerFactory);
~TBootstrapYdb();
~TBootstrapYdb() override;

TProgramShouldContinue& GetShouldContinue() override;

Expand All @@ -110,6 +119,7 @@ struct TBootstrapYdb final
IStartable* GetIamTokenClient() override;
IStartable* GetComputeClient() override;
IStartable* GetKmsClient() override;
IStartable* GetRootKmsClient() override;

void InitSpdk() override;
void InitRdmaClient() override;
Expand Down
26 changes: 25 additions & 1 deletion cloud/blockstore/libs/daemon/ydb/config_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using namespace NCloud::NBlockStore::NDiscovery;
////////////////////////////////////////////////////////////////////////////////

TConfigInitializerYdb::TConfigInitializerYdb(TOptionsYdbPtr options)
: TConfigInitializerCommon(options)
: TConfigInitializerCommon(options)
, NCloud::NStorage::TConfigInitializerYdbBase(options)
, Options(options)
{}
Expand Down Expand Up @@ -146,6 +146,21 @@ void TConfigInitializerYdb::InitKmsClientConfig()
KmsClientConfig = std::move(config);
}

void TConfigInitializerYdb::InitRootKmsConfig()
{
NProto::TRootKmsConfig config;

if (Options->RootKmsConfig) {
ParseProtoTextFromFile(Options->RootKmsConfig, config);
}

if (!config.GetRootCertsFile()) {
config.SetRootCertsFile(ServerConfig->GetRootCertsFile());
}

RootKmsConfig = std::move(config);
}

void TConfigInitializerYdb::InitComputeClientConfig()
{
NProto::TGrpcClientConfig config;
Expand Down Expand Up @@ -340,6 +355,14 @@ void TConfigInitializerYdb::ApplyKmsClientConfig(const TString& text)
KmsClientConfig = std::move(config);
}

void TConfigInitializerYdb::ApplyRootKmsConfig(const TString& text)
{
NProto::TRootKmsConfig config;
ParseProtoTextFromString(text, config);

RootKmsConfig = std::move(config);
}

void TConfigInitializerYdb::ApplyComputeClientConfig(const TString& text)
{
NProto::TGrpcClientConfig config;
Expand Down Expand Up @@ -384,6 +407,7 @@ void TConfigInitializerYdb::ApplyCustomCMSConfigs(const NKikimrConfig::TAppConfi
{ "YdbStatsConfig", &TSelf::ApplyYdbStatsConfig },
{ "IamClientConfig", &TSelf::ApplyIamClientConfig },
{ "KmsClientConfig", &TSelf::ApplyKmsClientConfig },
{ "RootKmsConfig", &TSelf::ApplyRootKmsConfig },
{ "ComputeClientConfig", &TSelf::ApplyComputeClientConfig },
};

Expand Down
5 changes: 5 additions & 0 deletions cloud/blockstore/libs/daemon/ydb/config_initializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "public.h"

#include <cloud/blockstore/config/grpc_client.pb.h>
#include <cloud/blockstore/config/root_kms.pb.h>

#include <cloud/blockstore/libs/client/config.h>
#include <cloud/blockstore/libs/client/throttling.h>
Expand All @@ -15,6 +16,7 @@
#include <cloud/blockstore/libs/kikimr/public.h>
#include <cloud/blockstore/libs/logbroker/iface/public.h>
#include <cloud/blockstore/libs/notify/public.h>
#include <cloud/blockstore/libs/root_kms/iface/public.h>
#include <cloud/blockstore/libs/server/public.h>
#include <cloud/blockstore/libs/service/public.h>
#include <cloud/blockstore/libs/spdk/iface/public.h>
Expand Down Expand Up @@ -59,6 +61,7 @@ struct TConfigInitializerYdb final
NIamClient::TIamClientConfigPtr IamClientConfig;
NProto::TGrpcClientConfig KmsClientConfig;
NProto::TGrpcClientConfig ComputeClientConfig;
NProto::TRootKmsConfig RootKmsConfig;

TConfigInitializerYdb(TOptionsYdbPtr options);

Expand All @@ -69,6 +72,7 @@ struct TConfigInitializerYdb final
void InitStorageConfig();
void InitIamClientConfig();
void InitKmsClientConfig();
void InitRootKmsConfig();
void InitComputeClientConfig();

bool GetUseNonreplicatedRdmaActor() const override;
Expand All @@ -92,6 +96,7 @@ struct TConfigInitializerYdb final
void ApplyYdbStatsConfig(const TString& text);
void ApplyIamClientConfig(const TString& text);
void ApplyKmsClientConfig(const TString& text);
void ApplyRootKmsConfig(const TString& text);
void ApplyComputeClientConfig(const TString& text);
};

Expand Down
4 changes: 4 additions & 0 deletions cloud/blockstore/libs/daemon/ydb/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ TOptionsYdb::TOptionsYdb()
.RequiredArgument("PATH")
.StoreResult(&KmsConfig);

Opts.AddLongOption("root-kms-file")
.RequiredArgument("PATH")
.StoreResult(&RootKmsConfig);

Opts.AddLongOption("compute-file")
.RequiredArgument("PATH")
.StoreResult(&ComputeConfig);
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/libs/daemon/ydb/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct TOptionsYdb final
TString NotifyConfig;
TString IamConfig;
TString KmsConfig;
TString RootKmsConfig;
TString ComputeConfig;

TOptionsYdb();
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/libs/daemon/ydb/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ PEERDIR(
cloud/blockstore/libs/logbroker/iface
cloud/blockstore/libs/notify
cloud/blockstore/libs/nvme
cloud/blockstore/libs/root_kms/iface
cloud/blockstore/libs/server
cloud/blockstore/libs/service
cloud/blockstore/libs/service_kikimr
Expand Down
6 changes: 3 additions & 3 deletions cloud/blockstore/libs/root_kms/impl/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ TRootKmsClient::~TRootKmsClient()
void TRootKmsClient::Start()
{
grpc::SslCredentialsOptions sslOpts{
.pem_root_certs = ReadFile(Params.RootCAPath),
.pem_private_key = ReadFile(Params.PrivateKeyPath),
.pem_cert_chain = ReadFile(Params.CertChainPath)
.pem_root_certs = ReadFile(Params.RootCertsFile),
.pem_private_key = ReadFile(Params.PrivateKeyFile),
.pem_cert_chain = ReadFile(Params.CertChainFile)
};

STORAGE_INFO("Connect to " << Params.Address);
Expand Down
6 changes: 3 additions & 3 deletions cloud/blockstore/libs/root_kms/impl/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace NCloud::NBlockStore {
struct TCreateRootKmsClientParams
{
TString Address;
TString RootCAPath;
TString CertChainPath;
TString PrivateKeyPath;
TString RootCertsFile;
TString CertChainFile;
TString PrivateKeyFile;
};

IRootKmsClientPtr CreateRootKmsClient(
Expand Down
6 changes: 3 additions & 3 deletions cloud/blockstore/libs/root_kms/impl/client_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ struct TFixture
Client = CreateRootKmsClient(
Logging,
{.Address = "localhost:" + GetEnv("FAKE_ROOT_KMS_PORT"),
.RootCAPath = GetEnv("FAKE_ROOT_KMS_CA"),
.CertChainPath = GetEnv("FAKE_ROOT_KMS_CLIENT_CRT"),
.PrivateKeyPath = GetEnv("FAKE_ROOT_KMS_CLIENT_KEY")});
.RootCertsFile = GetEnv("FAKE_ROOT_KMS_CA"),
.CertChainFile = GetEnv("FAKE_ROOT_KMS_CLIENT_CRT"),
.PrivateKeyFile = GetEnv("FAKE_ROOT_KMS_CLIENT_KEY")});
Client->Start();
}

Expand Down

0 comments on commit 40e9574

Please sign in to comment.