Skip to content
Open
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
29 changes: 16 additions & 13 deletions google/cloud/odbc/bq_client_interface/odbc_authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ StatusRecordOr<nlohmann::json> CreateJsonCredsObject(
}

StatusRecordOr<std::shared_ptr<Credentials>>
CreateExternalAccountAuthenticationBYOID(Oauth const& oauth,
CreateExternalAccountAuthenticationBYOID(ConnProps const& conn_props,
Options const& options) {
if (!IsBYOIDPropsSet(oauth)) {
if (!IsBYOIDPropsSet(conn_props)) {
LOG(ERROR)
<< "CreateExternalAccountAuthenticationBYOID:: Unable to create "
"external auth credentials: Required BYOID Properties are not set ";
Expand All @@ -182,8 +182,9 @@ CreateExternalAccountAuthenticationBYOID(Oauth const& oauth,
"BYOID Properties are not set "};
}
StatusRecordOr<nlohmann::json> json_creds = CreateJsonCredsObject(
oauth.byoid_aud_url, oauth.byoid_creds_src, oauth.byoid_pool_user_project,
oauth.byoid_subj_token_type, oauth.byoid_token_url);
conn_props.byoid_aud_url, conn_props.byoid_creds_src,
conn_props.byoid_pool_user_project, conn_props.byoid_subj_token_type,
conn_props.byoid_token_url);
if (!json_creds) {
LOG(ERROR)
<< "CreateExternalAccountAuthenticationBYOID::CreateJsonCredsObject:: "
Expand All @@ -195,29 +196,31 @@ CreateExternalAccountAuthenticationBYOID(Oauth const& oauth,
}

StatusRecordOr<std::shared_ptr<Credentials>> CreateCredentials(
Oauth const& oauth, Options const& options) {
switch (oauth.auth_mechanism) {
ConnProps const& conn_props, Options const& options) {
switch (conn_props.auth_mechanism) {
case OauthMechanism::kServiceAccount:
return CreateServiceCredentials(oauth.credentials_file_path, options);
return CreateServiceCredentials(conn_props.credentials_file_path,
options);
case OauthMechanism::kUserAccount:
return CreateUserCredentials(oauth.credentials_file_path, options);
return CreateUserCredentials(conn_props.credentials_file_path, options);
case OauthMechanism::kApplicationDefault:
return CreateApplicationDefaultCredentials(options);
case OauthMechanism::kExternalUser: {
if (oauth.credentials_file_path.empty() && !IsBYOIDPropsSet(oauth)) {
if (conn_props.credentials_file_path.empty() &&
!IsBYOIDPropsSet(conn_props)) {
LOG(ERROR) << "CreateCredentials:: The path to the external auth JSON "
"file can't be empty";
return StatusRecord{
SQLStates::k_HY000(),
"The path to the external auth JSON file can't be empty"};
}
if (!oauth.credentials_file_path.empty()) {
if (!conn_props.credentials_file_path.empty()) {
// Call creation of external auth via JSON file
return CreateExternalAuthCredentialsJSON(oauth.credentials_file_path,
options);
return CreateExternalAuthCredentialsJSON(
conn_props.credentials_file_path, options);
}
// Call creation of external auth via BYOID properties.
return CreateExternalAccountAuthenticationBYOID(oauth, options);
return CreateExternalAccountAuthenticationBYOID(conn_props, options);
}
}
LOG(ERROR) << "CreateCredentials:: OauthMechanism enum is invalid";
Expand Down
10 changes: 6 additions & 4 deletions google/cloud/odbc/bq_client_interface/odbc_authentication.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct GCD {
std::string universe_domain;
};

struct Oauth {
struct ConnProps {
OauthMechanism auth_mechanism;
std::string credentials_file_path;
/////////////////////////////////////////////////////////////////
Expand All @@ -84,6 +84,7 @@ struct Oauth {
// The URI used to generate authentication tokens. Defaults to
// https://sts.googleapis.com/v1/token.
std::string byoid_token_url;
std::string quota_project_id;
SslCredentials ssl_credentials;
ProxyOptions proxy_options;
std::string kms_key_name;
Expand All @@ -92,13 +93,14 @@ struct Oauth {
};

// Returns true if all required BYOID properties are set.
inline bool IsBYOIDPropsSet(Oauth const& oauth) {
return (!oauth.byoid_aud_url.empty() && !oauth.byoid_creds_src.empty());
inline bool IsBYOIDPropsSet(ConnProps const& conn_props) {
return (!conn_props.byoid_aud_url.empty() &&
!conn_props.byoid_creds_src.empty());
}

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

/// Creates OAuth2 access_token
Expand Down
31 changes: 19 additions & 12 deletions google/cloud/odbc/bq_client_interface/odbc_bq_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "google/cloud/odbc/bq_client_interface/tables.h"
#include "google/cloud/odbc/internal/status_record_or.h"
#include "google/cloud/odbc/internal/version.h"
#include "google/cloud/common_options.h"
#include "google/cloud/completion_queue.h"
#include "google/cloud/credentials.h"
#include "google/cloud/grpc_options.h"
Expand Down Expand Up @@ -154,13 +155,14 @@ google::cloud::ProxyConfig CreateProxyConfig(std::string hostname,
} // namespace

StatusRecordOr<std::shared_ptr<ODBCBQClient>> ODBCBQClient::CreateBQClient(
Oauth const& oauth) {
ConnProps const& conn_props) {
// 1. Initialize Options and set Proxy/SSL settings FIRST
google::cloud::Options options;

std::string pem_file = oauth.ssl_credentials.pem_root_certs;
std::string pem_file = conn_props.ssl_credentials.pem_root_certs;
#ifdef _WIN32
bool use_system_trust_store = oauth.ssl_credentials.use_system_trust_store;
bool use_system_trust_store =
conn_props.ssl_credentials.use_system_trust_store;
std::string pem_path;
if (use_system_trust_store == true) {
auto pem_path_or = ExportWindowsSystemCertsToPem();
Expand All @@ -184,17 +186,17 @@ StatusRecordOr<std::shared_ptr<ODBCBQClient>> ODBCBQClient::CreateBQClient(
// Set Proxy
options.set<google::cloud::ProxyOption>(
ProxyConfig()
.set_hostname(oauth.proxy_options.hostname)
.set_port(oauth.proxy_options.port)
.set_username(oauth.proxy_options.username)
.set_password(oauth.proxy_options.password)
.set_hostname(conn_props.proxy_options.hostname)
.set_port(conn_props.proxy_options.port)
.set_username(conn_props.proxy_options.username)
.set_password(conn_props.proxy_options.password)
.set_scheme("http"));

options.set<google::cloud::UserAgentProductsOption>(
{"Google-Bigquery-ODBC/" + std::string(DRIVER_VERSION)});

StatusRecordOr<std::shared_ptr<Credentials>> credentials =
CreateCredentials(oauth, options);
CreateCredentials(conn_props, options);
if (!credentials.Ok()) {
LOG(ERROR) << "CreateBQClient::CreateCredentials:: "
<< credentials.GetStatusRecord().message;
Expand All @@ -210,17 +212,22 @@ StatusRecordOr<std::shared_ptr<ODBCBQClient>> ODBCBQClient::CreateBQClient(

options.set<google::cloud::UnifiedCredentialsOption>(*credentials);

if (oauth.gcd.enable_gcd && oauth.gcd.universe_domain != "googleapis.com") {
if (!conn_props.quota_project_id.empty()) {
options.set<google::cloud::UserProjectOption>(conn_props.quota_project_id);
}

if (conn_props.gcd.enable_gcd &&
conn_props.gcd.universe_domain != "googleapis.com") {
options.set<google::cloud::internal::UniverseDomainOption>(
oauth.gcd.universe_domain);
conn_props.gcd.universe_domain);
}

// Handle Private Service Connect URIs
std::string bigquery_endpoint;
std::string readapi_endpoint;

if (!oauth.psc.empty()) {
std::stringstream ss(oauth.psc);
if (!conn_props.psc.empty()) {
std::stringstream ss(conn_props.psc);
std::string token;
while (std::getline(ss, token, ',')) {
auto pos = token.find('=');
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/odbc/bq_client_interface/odbc_bq_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ odbc_internal::StatusRecordOr<std::string> ExportWindowsSystemCertsToPem();
class ODBCBQClient {
public:
static odbc_internal::StatusRecordOr<std::shared_ptr<ODBCBQClient>>
CreateBQClient(Oauth const& oauth);
CreateBQClient(ConnProps const& conn_props);
~ODBCBQClient() = default;

ODBCBQClient(ODBCBQClient const&) = default;
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/odbc/bq_driver/internal/driver_form.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#pragma comment(lib, "Comctl32.lib") // Link with Comctl32.lib

namespace google::cloud::odbc_bq_driver_internal {
using google::cloud::odbc_bigquery_client_interface::Oauth;
using google::cloud::odbc_bigquery_client_interface::ConnProps;
using google::cloud::odbc_bigquery_client_interface::OauthMechanism;
using google::cloud::odbc_bigquery_client_interface::ODBCBQClient;
using google::cloud::odbc_bq_driver::SQLDriverConnectInternal;
Expand Down Expand Up @@ -765,12 +765,12 @@ void EvaluateFields(HWND hwnd) {
}

void PopulateDropdown(HWND h_dataset_box, std::string text,
std::string key_file, std::string oauth,
std::string key_file, std::string conn_props,
std::string catalog) {
SendMessage(h_dataset_box, CB_RESETCONTENT, 0, 0);

StatusRecordOr<std::string> status_record =
DriverForm::GetCatalogAndDataset(text, key_file, oauth, catalog);
DriverForm::GetCatalogAndDataset(text, key_file, conn_props, catalog);

if (!status_record.Ok()) {
MessageBox(h_dataset_box, status_record.GetStatusRecord().message.c_str(),
Expand Down
13 changes: 7 additions & 6 deletions google/cloud/odbc/bq_driver/internal/odbc_conn_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void ConnectionHandle::SetUp(Section& dsn_section,
dsn_.description = dsn_section["DESCRIPTION"];
dsn_.driver = dsn_section["DRIVER"];
dsn_.catalog = dsn_section["CATALOG"];
dsn_.quota_project_id = dsn_section["QUOTAPROJECTID"];
dsn_.default_dataset = dsn_section["DEFAULTDATASET"];
std::string filter_tables = dsn_section["FILTERTABLESONDEFAULTDATASET"];
if (!filter_tables.empty()) {
Expand Down Expand Up @@ -276,16 +277,16 @@ ConnectionHandle& ConnectionHandle::operator=(

StatusRecord ConnectionHandle::ValidateExternalUser(
Authentication const& auth) {
if (auth.oauth.auth_mechanism == OauthMechanism::kExternalUser) {
if (!auth.oauth.credentials_file_path.empty()) {
if (auth.conn_props.auth_mechanism == OauthMechanism::kExternalUser) {
if (!auth.conn_props.credentials_file_path.empty()) {
// KeyFilePath takes precedence.
return StatusRecord::Ok();
}

// Validate BYOID properties.
return ValidateBYOIDProperties(auth.oauth.byoid_aud_url,
auth.oauth.byoid_creds_src,
auth.oauth.byoid_subj_token_type);
return ValidateBYOIDProperties(auth.conn_props.byoid_aud_url,
auth.conn_props.byoid_creds_src,
auth.conn_props.byoid_subj_token_type);
}
return StatusRecord::Ok();
}
Expand All @@ -299,7 +300,7 @@ StatusRecord ConnectionHandle::Connect(Authentication& auth) {
return validation_status;
}
StatusRecordOr<std::shared_ptr<ODBCBQClient>> response =
ODBCBQClient::CreateBQClient(auth.oauth);
ODBCBQClient::CreateBQClient(auth.conn_props);
if (!response) {
LOG(ERROR) << "ConnectionHandle::Connect::CreateBQClient:: "
<< response.GetStatusRecord().message;
Expand Down
5 changes: 3 additions & 2 deletions google/cloud/odbc/bq_driver/internal/odbc_conn_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace google::cloud::odbc_bq_driver_internal {

using google::cloud::odbc_bigquery_client_interface::Oauth;
using google::cloud::odbc_bigquery_client_interface::ConnProps;
using google::cloud::odbc_bigquery_client_interface::OauthMechanism;
using google::cloud::odbc_bigquery_client_interface::ODBCBQClient;

Expand All @@ -35,7 +35,7 @@ inline std::uint32_t const kDefaultMaxRetries = 6;

// Details of authentication provided in the odbc.ini/Windows Registry
struct Authentication {
Oauth oauth;
ConnProps conn_props;
// TODO(jsrinnn): Remove this if it is not being used.
std::string email;
// TODO(jsrinnn): Remove this if it is not being used.
Expand All @@ -48,6 +48,7 @@ struct Dsn {
std::string description;
std::string driver;
std::string catalog;
std::string quota_project_id;
std::string default_dataset;
std::string dsn_name;
std::string key_file_path;
Expand Down
Loading
Loading