Skip to content

Commit 6eb0091

Browse files
authored
fix: using proxy options for generating auth token (#1402)
1 parent b1d47fd commit 6eb0091

3 files changed

Lines changed: 36 additions & 30 deletions

File tree

google/cloud/odbc/bq_client_interface/odbc_authentication.cc

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ auto const kSelfSignedJwtEnvVar =
3535
"GOOGLE_CLOUD_CPP_EXPERIMENTAL_DISABLE_SELF_SIGNED_JWT";
3636

3737
StatusRecordOr<std::shared_ptr<Credentials>> CreateServiceCredentials(
38-
std::string const& credentials_file_path) {
38+
std::string const& credentials_file_path, Options const& options) {
3939
if (credentials_file_path.empty()) {
4040
LOG(ERROR)
4141
<< "CreateServiceCredentials:: The path to the file can't be empty";
@@ -73,19 +73,19 @@ StatusRecordOr<std::shared_ptr<Credentials>> CreateServiceCredentials(
7373
credentials_file_path};
7474
}
7575

76-
return ::google::cloud::MakeServiceAccountCredentials(contents);
76+
return ::google::cloud::MakeServiceAccountCredentials(contents, options);
7777
}
7878

7979
StatusRecordOr<std::shared_ptr<Credentials>>
80-
CreateApplicationDefaultCredentials() {
80+
CreateApplicationDefaultCredentials(Options const& options) {
8181
// C++ client library in google-cloud-cpp first checks
8282
// GOOGLE_APPLICATION_CREDENTIALS env var and use it if it's present. Then it
8383
// looks for a 'default' location of the file with credentials.
84-
return ::google::cloud::MakeGoogleDefaultCredentials();
84+
return ::google::cloud::MakeGoogleDefaultCredentials(options);
8585
}
8686

8787
StatusRecordOr<std::shared_ptr<Credentials>> CreateExternalAuthCredentialsJSON(
88-
std::string const& credentials_file_path) {
88+
std::string const& credentials_file_path, Options const& options) {
8989
if (credentials_file_path.empty()) {
9090
LOG(ERROR) << "CreateExternalAuthCredentialsJSON:: The path to the "
9191
"external auth JSON file can't be empty";
@@ -117,7 +117,7 @@ StatusRecordOr<std::shared_ptr<Credentials>> CreateExternalAuthCredentialsJSON(
117117
credentials_file_path};
118118
}
119119

120-
return ::google::cloud::MakeExternalAccountCredentials(contents);
120+
return ::google::cloud::MakeExternalAccountCredentials(contents, options);
121121
}
122122

123123
StatusRecordOr<nlohmann::json> CreateJsonCredsObject(
@@ -140,7 +140,8 @@ StatusRecordOr<nlohmann::json> CreateJsonCredsObject(
140140
}
141141

142142
StatusRecordOr<std::shared_ptr<Credentials>>
143-
CreateExternalAccountAuthenticationBYOID(Oauth const& oauth) {
143+
CreateExternalAccountAuthenticationBYOID(Oauth const& oauth,
144+
Options const& options) {
144145
if (!IsBYOIDPropsSet(oauth)) {
145146
LOG(ERROR)
146147
<< "CreateExternalAccountAuthenticationBYOID:: Unable to create "
@@ -158,23 +159,25 @@ CreateExternalAccountAuthenticationBYOID(Oauth const& oauth) {
158159
<< json_creds.GetStatusRecord().message;
159160
return json_creds.GetStatusRecord();
160161
}
161-
return ::google::cloud::MakeExternalAccountCredentials((*json_creds).dump());
162+
return ::google::cloud::MakeExternalAccountCredentials((*json_creds).dump(),
163+
options);
162164
}
163165

164166
StatusRecordOr<std::shared_ptr<Credentials>> CreateCredentials(
165-
Oauth const& oauth) {
167+
Oauth const& oauth, Options const& options) {
166168
switch (oauth.auth_mechanism) {
167169
case OauthMechanism::kServiceAndUserAccount:
168-
return CreateServiceCredentials(oauth.credentials_file_path);
170+
return CreateServiceCredentials(oauth.credentials_file_path, options);
169171
case OauthMechanism::kApplicationDefault:
170-
return CreateApplicationDefaultCredentials();
172+
return CreateApplicationDefaultCredentials(options);
171173
case OauthMechanism::kExternalUser: {
172174
if (!IsBYOIDPropsSet(oauth)) {
173175
// Call creation of external auth via JSON file
174-
return CreateExternalAuthCredentialsJSON(oauth.credentials_file_path);
176+
return CreateExternalAuthCredentialsJSON(oauth.credentials_file_path,
177+
options);
175178
}
176179
// Call creation of external auth via BYOID properties.
177-
return CreateExternalAccountAuthenticationBYOID(oauth);
180+
return CreateExternalAccountAuthenticationBYOID(oauth, options);
178181
}
179182
}
180183
LOG(ERROR) << "CreateCredentials:: OauthMechanism enum is invalid";

google/cloud/odbc/bq_client_interface/odbc_authentication.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ inline bool IsBYOIDPropsSet(Oauth const& oauth) {
9595

9696
/// Creates an object of UnifiedCredentials depending on the input arguments.
9797
odbc_internal::StatusRecordOr<std::shared_ptr<Credentials>> CreateCredentials(
98-
Oauth const& oauth);
98+
Oauth const& oauth,
99+
::google::cloud::Options const& options = ::google::cloud::Options{});
99100

100101
/// Creates OAuth2 access_token
101102
odbc_internal::StatusRecordOr<AccessToken> GetOAuth2Token(

google/cloud/odbc/bq_client_interface/odbc_bq_client.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,10 @@ google::cloud::ProxyConfig CreateProxyConfig(std::string hostname,
7979

8080
StatusRecordOr<std::shared_ptr<ODBCBQClient>> ODBCBQClient::CreateBQClient(
8181
Oauth const& oauth) {
82-
StatusRecordOr<std::shared_ptr<Credentials>> credentials =
83-
CreateCredentials(oauth);
84-
if (!credentials) {
85-
LOG(ERROR) << "CreateBQClient::CreateCredentials:: "
86-
<< credentials.GetStatusRecord().message;
87-
return credentials.GetStatusRecord();
88-
}
89-
90-
Options options =
91-
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
92-
*credentials);
93-
94-
std::string pem_file = oauth.ssl_credentials.pem_root_certs;
95-
if (!pem_file.empty()) {
96-
options.set<google::cloud::CARootsFilePathOption>(pem_file);
97-
}
82+
// 1. Initialize Options and set Proxy/SSL settings FIRST
83+
google::cloud::Options options;
9884

85+
// Set Proxy
9986
options.set<google::cloud::ProxyOption>(
10087
ProxyConfig()
10188
.set_hostname(oauth.proxy_options.hostname)
@@ -104,9 +91,24 @@ StatusRecordOr<std::shared_ptr<ODBCBQClient>> ODBCBQClient::CreateBQClient(
10491
.set_password(oauth.proxy_options.password)
10592
.set_scheme("http"));
10693

94+
std::string pem_file = oauth.ssl_credentials.pem_root_certs;
95+
if (!pem_file.empty()) {
96+
options.set<google::cloud::CARootsFilePathOption>(pem_file);
97+
}
98+
10799
options.set<google::cloud::UserAgentProductsOption>(
108100
{"Google-Bigquery-ODBC/" + std::string(DRIVER_VERSION)});
109101

102+
StatusRecordOr<std::shared_ptr<Credentials>> credentials =
103+
CreateCredentials(oauth, options);
104+
if (!credentials) {
105+
LOG(ERROR) << "CreateBQClient::CreateCredentials:: "
106+
<< credentials.GetStatusRecord().message;
107+
return credentials.GetStatusRecord();
108+
}
109+
110+
options.set<google::cloud::UnifiedCredentialsOption>(*credentials);
111+
110112
if (oauth.tpc.enable_tpc && oauth.tpc.universe_domain != "googleapis.com") {
111113
options.set<google::cloud::internal::UniverseDomainOption>(
112114
oauth.tpc.universe_domain);

0 commit comments

Comments
 (0)