Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NBS-4456: use iam-token-client in blockstore-client #245

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cloud/blockstore/apps/client/CMakeLists.darwin-x86_64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_link_libraries(blockstore-client PUBLIC
library-cpp-cpuid_check
library-cpp-getopt
apps-client-lib
libs-iam-iface
)
target_link_options(blockstore-client PRIVATE
-Wl,-platform_version,macos,11.0,11.0
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/apps/client/CMakeLists.linux-aarch64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_link_libraries(blockstore-client PUBLIC
yutil
library-cpp-getopt
apps-client-lib
libs-iam-iface
)
target_link_options(blockstore-client PRIVATE
-ldl
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/apps/client/CMakeLists.linux-x86_64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ target_link_libraries(blockstore-client PUBLIC
library-cpp-cpuid_check
library-cpp-getopt
apps-client-lib
libs-iam-iface
)
target_link_options(blockstore-client PRIVATE
-ldl
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/apps/client/CMakeLists.windows-x86_64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_link_libraries(blockstore-client PUBLIC
library-cpp-cpuid_check
library-cpp-getopt
apps-client-lib
libs-iam-iface
)
target_sources(blockstore-client PRIVATE
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/main.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_sources(apps-client-lib PRIVATE
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/app.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/assign_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/backup_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/bootstrap.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/command.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_checkpoint.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_placement_group.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ target_sources(apps-client-lib PRIVATE
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/app.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/assign_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/backup_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/bootstrap.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/command.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_checkpoint.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_placement_group.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ target_sources(apps-client-lib PRIVATE
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/app.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/assign_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/backup_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/bootstrap.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/command.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_checkpoint.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_placement_group.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_sources(apps-client-lib PRIVATE
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/app.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/assign_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/backup_volume.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/bootstrap.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/command.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_checkpoint.cpp
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/create_placement_group.cpp
Expand Down
8 changes: 7 additions & 1 deletion cloud/blockstore/apps/client/lib/app.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "app.h"
#include "bootstrap.h"

#include <library/cpp/getopt/small/last_getopt.h>

Expand Down Expand Up @@ -30,7 +31,10 @@ void TApp::Shutdown()
}
}

int TApp::Run(int argc, const char* argv[])
int TApp::Run(
std::shared_ptr<TClientFactories> clientFactories,
int argc,
const char* argv[])
{
TOpts opts;
opts.AddHelpOption('h');
Expand Down Expand Up @@ -66,6 +70,8 @@ int TApp::Run(int argc, const char* argv[])
ythrow yexception() << "unknown command: " << command;
}

Handler->SetClientFactories(clientFactories);

bool res = Handler->Run(argc, argv);
if (!res) {
return 1;
Expand Down
5 changes: 4 additions & 1 deletion cloud/blockstore/apps/client/lib/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class TApp
public:
static TApp& Instance();
void Shutdown();
int Run(int argc, const char* argv[]);
int Run(
std::shared_ptr<TClientFactories> clientFactories,
int argc,
const char* argv[]);
};

void Shutdown(int signum);
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/apps/client/lib/bootstrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "bootstrap.h"
20 changes: 20 additions & 0 deletions cloud/blockstore/apps/client/lib/bootstrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <cloud/storage/core/libs/common/public.h>
#include <cloud/storage/core/libs/diagnostics/public.h>
#include <cloud/storage/core/libs/iam/iface/public.h>

namespace NCloud::NBlockStore::NClient {

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

struct TClientFactories
{
std::function<NIamClient::IIamTokenClientPtr(
NIamClient::TIamClientConfigPtr config,
ILoggingServicePtr logging,
ISchedulerPtr scheduler,
ITimerPtr timer)> IamClientFactory;
};

} // namespace NCloud::NBlockStore::NClient
59 changes: 57 additions & 2 deletions cloud/blockstore/apps/client/lib/command.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "bootstrap.h"
#include "command.h"
#include "factory.h"

Expand Down Expand Up @@ -53,6 +54,7 @@ namespace {
////////////////////////////////////////////////////////////////////////////////

const TString DefaultConfigFile = "/Berkanavt/nbs-server/cfg/nbs-client.txt";
const TString DefaultIamConfigFile = "/Berkanavt/nbs-server/cfg/nbs-iam.txt";
const TString DefaultIamTokenFile = "~/.nbs-client/iam-token";

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -122,6 +124,13 @@ TCommand::TCommand(IBlockStorePtr client)
.RequiredArgument("STR")
.StoreResult(&ConfigFile);

Opts.AddLongOption("iam-config")
.Help(TStringBuilder()
<< "iam-config file name. Default is "
<< DefaultIamConfigFile)
.RequiredArgument("STR")
.StoreResult(&IamConfigFile);

Opts.AddLongOption("host", "connect host")
.RequiredArgument("STR")
.StoreResult(&Host);
Expand Down Expand Up @@ -284,6 +293,12 @@ void TCommand::SetOutputStream(std::shared_ptr<IOutputStream> os)
OutputStream = std::move(os);
}

void TCommand::SetClientFactories(
std::shared_ptr<TClientFactories> clientFactories)
{
ClientFactories = std::move(clientFactories);
}

TString TCommand::NormalizeCommand(TString command)
{
command.to_lower();
Expand Down Expand Up @@ -420,11 +435,12 @@ void TCommand::Parse(const int argc, const char* argv[])
void TCommand::Init()
{
InitLWTrace();
InitClientConfig();

Timer = CreateWallClockTimer();
Scheduler = CreateScheduler();

InitClientConfig(InitIamTokenClient());

const auto& logConfig = ClientConfig->GetLogConfig();
const auto& monConfig = ClientConfig->GetMonitoringConfig();

Expand Down Expand Up @@ -549,7 +565,39 @@ void TCommand::InitLWTrace()
probes.AddProbesList(LWTRACE_GET_PROBES(BLOCKSTORE_SERVER_PROVIDER));
}

void TCommand::InitClientConfig()
TString TCommand::InitIamTokenClient()
{
NProto::TIamClientConfig iamConfig;
if (IamConfigFile) {
ParseFromTextFormat(IamConfigFile, iamConfig);
} else if (NFs::Exists(DefaultIamConfigFile)) {
ParseFromTextFormat(DefaultIamConfigFile, iamConfig);
}

auto IamClientConfigPtr =
std::make_shared<NCloud::NIamClient::TIamClientConfig>(iamConfig);

IamClient = ClientFactories->IamClientFactory(
IamClientConfigPtr,
CreateLoggingService("console"),
Scheduler,
Timer);
IamClient->Start();

TString iamToken;
try {
auto tokenInfo = IamClient->GetTokenAsync().GetValue(WaitTimeout);
if (!HasError(tokenInfo)) {
iamToken = tokenInfo.GetResult().Token;
}
} catch (...) {
STORAGE_ERROR(CurrentExceptionMessage());
}

return iamToken;
}

void TCommand::InitClientConfig(TString IamTokenFromClient)
{
NProto::TClientAppConfig appConfig;
if (ConfigFile) {
Expand Down Expand Up @@ -627,6 +675,9 @@ void TCommand::InitClientConfig()
if (!iamToken) {
iamToken = GetIamTokenFromFile(IamTokenFile);
}
if (!iamToken) {
iamToken = std::move(IamTokenFromClient);
}
clientConfig.SetAuthToken(std::move(iamToken));
}

Expand Down Expand Up @@ -666,6 +717,10 @@ void TCommand::Start()

void TCommand::Stop()
{
if (IamClient) {
IamClient->Stop();
}

if (Scheduler) {
Scheduler->Stop();
}
Expand Down
12 changes: 11 additions & 1 deletion cloud/blockstore/apps/client/lib/command.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "bootstrap.h"

#include <cloud/blockstore/config/client.pb.h>
#include <cloud/blockstore/public/api/protos/mount.pb.h>

Expand All @@ -10,6 +12,7 @@
#include <cloud/blockstore/libs/service/public.h>
#include <cloud/blockstore/libs/throttling/throttler.h>
#include <cloud/storage/core/libs/common/error.h>
#include <cloud/storage/core/libs/iam/iface/client.h>

#include <library/cpp/actors/util/should_continue.h>
#include <library/cpp/getopt/small/last_getopt.h>
Expand All @@ -36,6 +39,7 @@ class TCommand
const ui64 BatchBlocksCount = 1024;

TString ConfigFile;
TString IamConfigFile;

TString Host;
ui32 InsecurePort = 0;
Expand All @@ -52,6 +56,8 @@ class TCommand
ILoggingServicePtr Logging;
IEncryptionClientFactoryPtr EncryptionClientFactory;

NCloud::NIamClient::IIamTokenClientPtr IamClient;

mutable TLog GrpcLog;
mutable TLog Log;

Expand Down Expand Up @@ -93,6 +99,8 @@ class TCommand

TProgramShouldContinue ShouldContinue;

std::shared_ptr<TClientFactories> ClientFactories;

static constexpr TDuration WaitTimeout = TDuration::MilliSeconds(100);

public:
Expand Down Expand Up @@ -120,6 +128,7 @@ class TCommand
IOutputStream& GetOutputStream();

void SetOutputStream(std::shared_ptr<IOutputStream> os);
void SetClientFactories(std::shared_ptr<TClientFactories> clientFactories);

static TString NormalizeCommand(TString command);

Expand Down Expand Up @@ -164,7 +173,8 @@ class TCommand

void Init();
void InitLWTrace();
void InitClientConfig();
TString InitIamTokenClient();
void InitClientConfig(TString IamTokenFromClient);

void Start();
void Stop();
Expand Down
21 changes: 21 additions & 0 deletions cloud/blockstore/apps/client/lib/command_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "bootstrap.h"
#include "factory.h"

#include <cloud/blockstore/libs/client/client.h>
Expand Down Expand Up @@ -26,6 +27,25 @@ static const ui64 DefaultBlocksCount = 4096;

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

std::shared_ptr<TClientFactories> MakeClientFactories()
{
auto clientFactories = std::make_shared<TClientFactories>();

clientFactories->IamClientFactory = [] (
NCloud::NIamClient::TIamClientConfigPtr config,
NCloud::ILoggingServicePtr logging,
NCloud::ISchedulerPtr scheduler,
NCloud::ITimerPtr timer)
{
Y_UNUSED(config);
Y_UNUSED(logging);
Y_UNUSED(scheduler);
Y_UNUSED(timer);
return NCloud::NIamClient::CreateIamTokenClientStub();
};
return clientFactories;
}

bool ExecuteRequest(
const char* command,
const TVector<TString>& argv,
Expand All @@ -43,6 +63,7 @@ bool ExecuteRequest(
Cerr << "Failed to find handler for command " << command << Endl;
return false;
}
handler->SetClientFactories(MakeClientFactories());

return handler->Run(args.size(), &args[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_link_libraries(cloud-blockstore-apps-client-lib-ut PUBLIC
library-cpp-cpuid_check
cpp-testing-unittest_main
apps-client-lib
libs-iam-iface
)
target_link_options(cloud-blockstore-apps-client-lib-ut PRIVATE
-Wl,-platform_version,macos,11.0,11.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_link_libraries(cloud-blockstore-apps-client-lib-ut PUBLIC
yutil
cpp-testing-unittest_main
apps-client-lib
libs-iam-iface
)
target_link_options(cloud-blockstore-apps-client-lib-ut PRIVATE
-ldl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ target_link_libraries(cloud-blockstore-apps-client-lib-ut PUBLIC
library-cpp-cpuid_check
cpp-testing-unittest_main
apps-client-lib
libs-iam-iface
)
target_link_options(cloud-blockstore-apps-client-lib-ut PRIVATE
-ldl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_link_libraries(cloud-blockstore-apps-client-lib-ut PUBLIC
library-cpp-cpuid_check
cpp-testing-unittest_main
apps-client-lib
libs-iam-iface
)
target_sources(cloud-blockstore-apps-client-lib-ut PRIVATE
${CMAKE_SOURCE_DIR}/cloud/blockstore/apps/client/lib/command_ut.cpp
Expand Down
1 change: 1 addition & 0 deletions cloud/blockstore/apps/client/lib/ut/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SRCS(

PEERDIR(
cloud/blockstore/apps/client/lib
cloud/storage/core/libs/iam/iface
)

END()
3 changes: 3 additions & 0 deletions cloud/blockstore/apps/client/lib/ya.make
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
LIBRARY()

OWNER(g:cloud-nbs)

SRCS(
alter_placement_group_membership.cpp
alter_volume.cpp
app.cpp
assign_volume.cpp
backup_volume.cpp
bootstrap.cpp
command.cpp
create_checkpoint.cpp
create_placement_group.cpp
Expand Down
Loading
Loading