Skip to content

Commit 5970eb0

Browse files
UgnineSirdisGazizonoki
authored andcommitted
Moved commit "OAuth2 token exchange in ydb cli" from ydb repo
1 parent 9b511f4 commit 5970eb0

File tree

11 files changed

+1120
-139
lines changed

11 files changed

+1120
-139
lines changed

include/ydb-cpp-sdk/client/helpers/helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace NYdb {
99
//! YDB_ANONYMOUS_CREDENTIALS="1" — uses anonymous access (used for test installation),
1010
//! YDB_METADATA_CREDENTIALS="1" — uses metadata service,
1111
//! YDB_ACCESS_TOKEN_CREDENTIALS=<access-token> — access token (for example, IAM-token).
12+
//! YDB_OAUTH2_KEY_FILE=<path-to-file> - OAuth 2.0 RFC8693 token exchange credentials parameters json file
1213
//! If grpcs protocol is given in endpoint (or protocol is empty), enables SSL and uses
1314
//! certificate from resourses and user cert from env variable "YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"
1415
TDriverConfig CreateFromEnvironment(const std::string& connectionString = "");
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include <ydb-cpp-sdk/client/types/credentials/credentials.h>
4+
5+
#include <string>
6+
#include <vector>
7+
8+
namespace NYdb {
9+
10+
// Lists supported algorithms for creation of OAuth 2.0 token exchange provider via config file
11+
std::vector<std::string> GetSupportedOauth2TokenExchangeJwtAlgorithms();
12+
13+
// Creates OAuth 2.0 token exchange credentials provider factory that exchanges token using standard protocol
14+
// https://www.rfc-editor.org/rfc/rfc8693
15+
//
16+
// Config file must be a valid json file
17+
//
18+
// Fields of json file
19+
// grant-type: [string] Grant type option (default: see TOauth2TokenExchangeParams)
20+
// res: [string] Resource option (optional)
21+
// aud: [string | list of strings] Audience option for token exchange request (optional)
22+
// scope: [string | list of strings] Scope option (optional)
23+
// requested-token-type: [string] Requested token type option (default: see TOauth2TokenExchangeParams)
24+
// subject-credentials: [creds_json] Subject credentials options (optional)
25+
// actor-credentials: [creds_json] Actor credentials options (optional)
26+
// token-endpoint: [string] Token endpoint. Can be overritten with tokenEndpoint param (if it is not empty)
27+
//
28+
// Fields of creds_json (JWT):
29+
// type: [string] Token source type. Set JWT
30+
// alg: [string] Algorithm for JWT signature. Supported algorithms can be listed with GetSupportedOauth2TokenExchangeJwtAlgorithms()
31+
// private-key: [string] (Private) key in PEM format for JWT signature
32+
// kid: [string] Key id JWT standard claim (optional)
33+
// iss: [string] Issuer JWT standard claim (optional)
34+
// sub: [string] Subject JWT standard claim (optional)
35+
// aud: [string | list of strings] Audience JWT standard claim (optional)
36+
// jti: [string] JWT ID JWT standard claim (optional)
37+
// ttl: [string] Token TTL (default: see TJwtTokenSourceParams)
38+
//
39+
// Fields of creds_json (FIXED):
40+
// type: [string] Token source type. Set FIXED
41+
// token: [string] Token value
42+
// token-type: [string] Token type value. It will become subject_token_type/actor_token_type parameter in token exchange request (https://www.rfc-editor.org/rfc/rfc8693)
43+
//
44+
std::shared_ptr<ICredentialsProviderFactory> CreateOauth2TokenExchangeFileCredentialsProviderFactory(const std::string& configFilePath, const std::string& tokenEndpoint = {});
45+
46+
} // namespace NYdb

src/client/helpers/CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
_ydb_sdk_add_library(client-helpers)
22

3-
target_link_libraries(client-helpers PUBLIC
4-
yutil
5-
client-iam-common
6-
client-ydb_types-credentials
7-
yql-public-issue-protos
3+
target_link_libraries(client-helpers
4+
PUBLIC
5+
yutil
6+
client-ydb_types-credentials-oauth2
7+
client-iam-common
8+
client-ydb_types-credentials
9+
yql-public-issue-protos
810
)
911

10-
target_sources(client-helpers PRIVATE
11-
helpers.cpp
12+
target_sources(client-helpers
13+
PRIVATE
14+
helpers.cpp
1215
)
1316

1417
_ydb_sdk_make_client_component(Helpers client-helpers)

src/client/helpers/helpers.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
#include <ydb-cpp-sdk/client/iam/common/iam.h>
44
#include <ydb-cpp-sdk/client/resources/ydb_ca.h>
5+
#include <ydb-cpp-sdk/client/types/credentials/oauth2_token_exchange/from_file.h>
6+
57
#include <src/client/impl/ydb_internal/common/parser.h>
68
#include <src/client/impl/ydb_internal/common/getenv.h>
9+
710
#include <util/stream/file.h>
811

912
namespace NYdb {
@@ -37,7 +40,7 @@ TDriverConfig CreateFromEnvironment(const std::string& connectionString) {
3740
}
3841

3942
bool useMetadataCredentials = GetStrFromEnv("YDB_METADATA_CREDENTIALS", "0") == "1";
40-
if (useMetadataCredentials){
43+
if (useMetadataCredentials) {
4144
auto factory = CreateIamCredentialsProviderFactory();
4245
try {
4346
factory->CreateProvider();
@@ -49,11 +52,18 @@ TDriverConfig CreateFromEnvironment(const std::string& connectionString) {
4952
}
5053

5154
std::string accessToken = GetStrFromEnv("YDB_ACCESS_TOKEN_CREDENTIALS", "");
52-
if (accessToken != ""){
55+
if (accessToken != "") {
5356
driverConfig.SetAuthToken(accessToken);
5457
return driverConfig;
5558
}
5659

60+
std::string oauth2KeyFile = GetStrFromEnv("YDB_OAUTH2_KEY_FILE", "");
61+
if (!saKeyFile.empty()) {
62+
driverConfig.SetCredentialsProviderFactory(
63+
CreateOauth2TokenExchangeFileCredentialsProviderFactory(oauth2KeyFile));
64+
return driverConfig;
65+
}
66+
5767
ythrow yexception() << "Unable to create driver config from environment";
5868
}
5969

@@ -65,4 +75,3 @@ TDriverConfig CreateFromSaKeyFile(const std::string& saKeyFile, const std::strin
6575
}
6676

6777
} // namespace NYdb
68-
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
_ydb_sdk_add_library(client-ydb_types-credentials-oauth2)
22

3-
target_link_libraries(client-ydb_types-credentials-oauth2 PUBLIC
4-
yutil
5-
jwt-cpp::jwt-cpp
6-
cgiparam
7-
http-misc
8-
http-simple
9-
json
10-
retry
11-
uri
12-
client-ydb_types-credentials
13-
client-ydb_types
3+
target_link_libraries(client-ydb_types-credentials-oauth2
4+
PUBLIC
5+
yutil
6+
jwt-cpp::jwt-cpp
7+
cgiparam
8+
http-misc
9+
http-simple
10+
json
11+
retry
12+
uri
13+
client-ydb_types-credentials
14+
client-ydb_types
1415
)
1516

16-
target_compile_definitions(client-ydb_types-credentials-oauth2 PUBLIC
17-
YDB_SDK_USE_NEW_JWT
17+
target_compile_definitions(client-ydb_types-credentials-oauth2
18+
PUBLIC
19+
YDB_SDK_USE_NEW_JWT
1820
)
1921

20-
target_sources(client-ydb_types-credentials-oauth2 PRIVATE
21-
credentials.cpp
22-
jwt_token_source.cpp
22+
target_sources(client-ydb_types-credentials-oauth2
23+
PRIVATE
24+
credentials.cpp
25+
from_file.cpp
26+
jwt_token_source.cpp
2327
)
2428

2529
_ydb_sdk_install_targets(TARGETS client-ydb_types-credentials-oauth2)

src/client/types/credentials/oauth2_token_exchange/credentials.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,16 @@ struct TPrivateOauth2TokenExchangeParams: public TOauth2TokenExchangeParams {
141141

142142
private:
143143
void ParseTokenEndpoint() {
144+
if (TokenEndpoint_.empty()) {
145+
throw std::invalid_argument(INV_ARG "token endpoint not set");
146+
}
144147
NUri::TUri url;
145148
NUri::TUri::TState::EParsed parseStatus = url.Parse(TokenEndpoint_, NUri::TFeature::FeaturesAll);
146149
if (parseStatus != NUri::TUri::TState::EParsed::ParsedOK) {
147150
throw std::invalid_argument(INV_ARG "failed to parse url");
148151
}
149152
if (url.IsNull(NUri::TUri::FieldScheme)) {
150-
throw std::invalid_argument(INV_ARG "token url without scheme");
153+
throw std::invalid_argument(TStringBuilder() << INV_ARG "token url without scheme: " << TokenEndpoint_);
151154
}
152155
TokenHost_ = TStringBuilder() << url.GetField(NUri::TUri::FieldScheme) << "://" << url.GetHost();
153156

0 commit comments

Comments
 (0)