Skip to content

Commit

Permalink
Remove grpc dependency leak through iam headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gazizonoki committed Nov 13, 2024
1 parent 07e05c9 commit 982180d
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 202 deletions.
17 changes: 16 additions & 1 deletion include/ydb-cpp-sdk/client/iam/iam.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#pragma once

#include "common/iam.h"
#include "types.h"
namespace NYdb {

/// Acquire an IAM token using a local metadata service on a virtual machine.
TCredentialsProviderFactoryPtr CreateIamCredentialsProviderFactory(const TIamHost& params = {});

/// Acquire an IAM token using a JSON Web Token (JWT) file name.
TCredentialsProviderFactoryPtr CreateIamJwtFileCredentialsProviderFactory(const TIamJwtFilename& params);

/// Acquire an IAM token using JSON Web Token (JWT) contents.
TCredentialsProviderFactoryPtr CreateIamJwtParamsCredentialsProviderFactory(const TIamJwtContent& param);

// Acquire an IAM token using a user OAuth token.
TCredentialsProviderFactoryPtr CreateIamOAuthCredentialsProviderFactory(const TIamOAuth& params);

}
53 changes: 53 additions & 0 deletions include/ydb-cpp-sdk/client/iam/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <ydb-cpp-sdk/client/types/credentials/credentials.h>
#include <ydb-cpp-sdk/library/jwt/jwt.h>

#include <util/datetime/base.h>

#include <fstream>
#include <string>

namespace NYdb {

namespace NIam {

constexpr std::string_view DEFAULT_ENDPOINT = "iam.api.cloud.yandex.net";
constexpr bool DEFAULT_ENABLE_SSL = true;

constexpr std::string_view DEFAULT_HOST = "169.254.169.254";
constexpr uint32_t DEFAULT_PORT = 80;

constexpr TDuration DEFAULT_REFRESH_PERIOD = TDuration::Hours(1);
constexpr TDuration DEFAULT_REQUEST_TIMEOUT = TDuration::Seconds(10);

}

struct TIamHost {
std::string Host = std::string(NIam::DEFAULT_HOST);
uint32_t Port = NIam::DEFAULT_PORT;
TDuration RefreshPeriod = NIam::DEFAULT_REFRESH_PERIOD;
};

struct TIamEndpoint {
std::string Endpoint = std::string(NIam::DEFAULT_ENDPOINT);
TDuration RefreshPeriod = NIam::DEFAULT_REFRESH_PERIOD;
TDuration RequestTimeout = NIam::DEFAULT_REQUEST_TIMEOUT;
bool EnableSsl = NIam::DEFAULT_ENABLE_SSL;
};

struct TIamJwtFilename : TIamEndpoint { std::string JwtFilename; };

struct TIamJwtContent : TIamEndpoint { std::string JwtContent; };

struct TIamJwtParams : TIamEndpoint { TJwtParams JwtParams; };

struct TIamOAuth : TIamEndpoint { std::string OAuthToken; };


inline TJwtParams ReadJwtKeyFile(const std::string& filename) {
std::ifstream input(filename, std::ios::in);
return ParseJwtParams({std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()});
}

}
2 changes: 1 addition & 1 deletion include/ydb-cpp-sdk/client/iam_private/iam.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <ydb-cpp-sdk/client/iam/common/iam.h>
#include <ydb-cpp-sdk/client/iam/types.h>

namespace NYdb {

Expand Down
2 changes: 1 addition & 1 deletion src/client/helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ target_link_libraries(client-helpers
PUBLIC
yutil
client-ydb_types-credentials-oauth2
client-iam-common
client-iam
client-ydb_types-credentials
)

Expand Down
2 changes: 1 addition & 1 deletion src/client/helpers/helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <ydb-cpp-sdk/client/helpers/helpers.h>

#include <ydb-cpp-sdk/client/iam/common/iam.h>
#include <ydb-cpp-sdk/client/iam/iam.h>
#include <ydb-cpp-sdk/client/resources/ydb_ca.h>
#include <ydb-cpp-sdk/client/types/credentials/oauth2_token_exchange/from_file.h>

Expand Down
18 changes: 12 additions & 6 deletions src/client/iam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ add_subdirectory(common)

_ydb_sdk_add_library(client-iam)

target_link_libraries(client-iam PUBLIC
yutil
api-client-yc_public
client-iam-common
target_link_libraries(client-iam
PUBLIC
client-ydb_types-credentials
http-simple
json
library-jwt
yutil
PRIVATE
api-client-yc_public
client-iam-common
)

_ydb_sdk_install_targets(TARGETS client-iam)

target_sources(client-iam PRIVATE
iam.cpp
)

_ydb_sdk_make_client_component(Iam client-iam)
23 changes: 9 additions & 14 deletions src/client/iam/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
_ydb_sdk_add_library(client-iam-common)
_ydb_sdk_add_library(client-iam-common INTERFACE)

target_link_libraries(client-iam-common PUBLIC
yutil
grpc-client
http-simple
json
library-jwt
client-ydb_types-credentials
api-client-yc_public
target_link_libraries(client-iam-common
INTERFACE
client-ydb_types-credentials
grpc-client
library-jwt
threading-future
yutil
)

target_sources(client-iam-common PRIVATE
iam.cpp
)

_ydb_sdk_make_client_component(Iam client-iam-common)
_ydb_sdk_install_targets(client-iam-common)
114 changes: 0 additions & 114 deletions src/client/iam/common/iam.cpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,69 +1,16 @@
#pragma once

#include <ydb-cpp-sdk/client/iam/types.h>
#include <ydb-cpp-sdk/client/types/credentials/credentials.h>

#include <ydb-cpp-sdk/library/grpc/client/grpc_client_low.h>
#include <library/cpp/threading/future/future.h>

#include <ydb-cpp-sdk/library/jwt/jwt.h>
#include <util/datetime/base.h>
#include <library/cpp/threading/future/future.h>

#include <util/system/spinlock.h>
#include <util/string/builder.h>

#include <fstream>
#include <util/system/spinlock.h>

namespace NYdb {

namespace NIam {
constexpr std::string_view DEFAULT_ENDPOINT = "iam.api.cloud.yandex.net";
constexpr bool DEFAULT_ENABLE_SSL = true;

constexpr std::string_view DEFAULT_HOST = "169.254.169.254";
constexpr uint32_t DEFAULT_PORT = 80;

constexpr TDuration DEFAULT_REFRESH_PERIOD = TDuration::Hours(1);
constexpr TDuration DEFAULT_REQUEST_TIMEOUT = TDuration::Seconds(10);
}

struct TIamHost {
std::string Host = std::string(NIam::DEFAULT_HOST);
uint32_t Port = NIam::DEFAULT_PORT;
TDuration RefreshPeriod = NIam::DEFAULT_REFRESH_PERIOD;
};

struct TIamEndpoint {
std::string Endpoint = std::string(NIam::DEFAULT_ENDPOINT);
TDuration RefreshPeriod = NIam::DEFAULT_REFRESH_PERIOD;
TDuration RequestTimeout = NIam::DEFAULT_REQUEST_TIMEOUT;
bool EnableSsl = NIam::DEFAULT_ENABLE_SSL;
};

struct TIamJwtFilename : TIamEndpoint { std::string JwtFilename; };

struct TIamJwtContent : TIamEndpoint { std::string JwtContent; };

struct TIamJwtParams : TIamEndpoint { TJwtParams JwtParams; };

inline TJwtParams ReadJwtKeyFile(const std::string& filename) {
std::ifstream input(filename, std::ios::in);
return ParseJwtParams({std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()});
}

struct TIamOAuth : TIamEndpoint { std::string OAuthToken; };

/// Acquire an IAM token using a local metadata service on a virtual machine.
TCredentialsProviderFactoryPtr CreateIamCredentialsProviderFactory(const TIamHost& params = {});

/// Acquire an IAM token using a JSON Web Token (JWT) file name.
TCredentialsProviderFactoryPtr CreateIamJwtFileCredentialsProviderFactory(const TIamJwtFilename& params);

/// Acquire an IAM token using JSON Web Token (JWT) contents.
TCredentialsProviderFactoryPtr CreateIamJwtParamsCredentialsProviderFactory(const TIamJwtContent& param);

// Acquire an IAM token using a user OAuth token.
TCredentialsProviderFactoryPtr CreateIamOAuthCredentialsProviderFactory(const TIamOAuth& params);

constexpr TDuration BACKOFF_START = TDuration::MilliSeconds(50);
constexpr TDuration BACKOFF_MAX = TDuration::Seconds(10);

Expand Down
Loading

0 comments on commit 982180d

Please sign in to comment.