Commit 181c2c8f authored by Dzmitry Poloyko (EPAM)'s avatar Dzmitry Poloyko (EPAM) Committed by Jørgen Lind
Browse files

GONRG-397 Pass Coogle Cloud credentials via options

parent 77a52683
......@@ -88,14 +88,48 @@ PyGlobal::initModule(py::module& m)
AzurePresignedOpenOptions_.def_readwrite("baseUrl" , &AzurePresignedOpenOptions::baseUrl, OPENVDS_DOCSTRING(AzurePresignedOpenOptions_baseUrl));
AzurePresignedOpenOptions_.def_readwrite("urlSuffix" , &AzurePresignedOpenOptions::urlSuffix, OPENVDS_DOCSTRING(AzurePresignedOpenOptions_urlSuffix));
// GoogleCredentialsToken
py::class_<GoogleCredentialsToken, std::unique_ptr<GoogleCredentialsToken>>
GoogleCredentialsToken_(m,"GoogleCredentialsToken", OPENVDS_DOCSTRING(GoogleCredentialsToken));
GoogleCredentialsToken_.def(py::init<const std::string & >(), py::arg("token"), OPENVDS_DOCSTRING(GoogleCredentialsToken_GoogleCredentialsToken));
GoogleCredentialsToken_.def(py::init<std::string && >(), py::arg("token"), OPENVDS_DOCSTRING(GoogleCredentialsToken_GoogleCredentialsToken_2));
// GoogleCredentialsPath
py::class_<GoogleCredentialsPath, std::unique_ptr<GoogleCredentialsPath>>
GoogleCredentialsPath_(m,"GoogleCredentialsPath", OPENVDS_DOCSTRING(GoogleCredentialsPath));
GoogleCredentialsPath_.def(py::init<const std::string & >(), py::arg("path"), OPENVDS_DOCSTRING(GoogleCredentialsPath_GoogleCredentialsPath));
GoogleCredentialsPath_.def(py::init<std::string && >(), py::arg("path"), OPENVDS_DOCSTRING(GoogleCredentialsPath_GoogleCredentialsPath_2));
// GoogleCredentialsJson
py::class_<GoogleCredentialsJson, std::unique_ptr<GoogleCredentialsJson>>
GoogleCredentialsJson_(m,"GoogleCredentialsJson", OPENVDS_DOCSTRING(GoogleCredentialsJson));
GoogleCredentialsJson_.def(py::init<const std::string & >(), py::arg("json"), OPENVDS_DOCSTRING(GoogleCredentialsJson_GoogleCredentialsJson));
GoogleCredentialsJson_.def(py::init<std::string && >(), py::arg("json"), OPENVDS_DOCSTRING(GoogleCredentialsJson_GoogleCredentialsJson_2));
// GoogleOpenOptions
py::class_<GoogleOpenOptions, OpenOptions, std::unique_ptr<GoogleOpenOptions>>
GoogleOpenOptions_(m,"GoogleOpenOptions", OPENVDS_DOCSTRING(GoogleOpenOptions));
GoogleOpenOptions_.def(py::init< >(), OPENVDS_DOCSTRING(GoogleOpenOptions_GoogleOpenOptions));
GoogleOpenOptions_.def(py::init<const std::string &, const std::string &>(), py::arg("bucket"), py::arg("pathPrefix"), OPENVDS_DOCSTRING(GoogleOpenOptions_GoogleOpenOptions_2));
GoogleOpenOptions_.def(py::init<const std::string &, const std::string &, const native::GoogleCredentialsToken &>(), py::arg("bucket"), py::arg("pathPrefix"), py::arg("credentials"), OPENVDS_DOCSTRING(GoogleOpenOptions_GoogleOpenOptions_3));
GoogleOpenOptions_.def(py::init<const std::string &, const std::string &, const native::GoogleCredentialsPath &>(), py::arg("bucket"), py::arg("pathPrefix"), py::arg("credentials"), OPENVDS_DOCSTRING(GoogleOpenOptions_GoogleOpenOptions_4));
GoogleOpenOptions_.def(py::init<const std::string &, const std::string &, const native::GoogleCredentialsJson &>(), py::arg("bucket"), py::arg("pathPrefix"), py::arg("credentials"), OPENVDS_DOCSTRING(GoogleOpenOptions_GoogleOpenOptions_5));
GoogleOpenOptions_.def_readwrite("credentialsType" , &GoogleOpenOptions::credentialsType, OPENVDS_DOCSTRING(GoogleOpenOptions_credentialsType));
GoogleOpenOptions_.def_readwrite("bucket" , &GoogleOpenOptions::bucket , OPENVDS_DOCSTRING(GoogleOpenOptions_bucket));
GoogleOpenOptions_.def_readwrite("pathPrefix" , &GoogleOpenOptions::pathPrefix , OPENVDS_DOCSTRING(GoogleOpenOptions_pathPrefix));
GoogleOpenOptions_.def_readwrite("credentials" , &GoogleOpenOptions::credentials, OPENVDS_DOCSTRING(GoogleOpenOptions_credentials));
py::enum_<GoogleOpenOptions::CredentialsType>
GoogleOpenOptions_CredentialsType_(GoogleOpenOptions_,"CredentialsType", OPENVDS_DOCSTRING(GoogleOpenOptions_CredentialsType));
GoogleOpenOptions_CredentialsType_.value("Default" , GoogleOpenOptions::CredentialsType::Default, OPENVDS_DOCSTRING(GoogleOpenOptions_CredentialsType_Default));
GoogleOpenOptions_CredentialsType_.value("AccessToken" , GoogleOpenOptions::CredentialsType::AccessToken, OPENVDS_DOCSTRING(GoogleOpenOptions_CredentialsType_AccessToken));
GoogleOpenOptions_CredentialsType_.value("JsonPath" , GoogleOpenOptions::CredentialsType::JsonPath, OPENVDS_DOCSTRING(GoogleOpenOptions_CredentialsType_JsonPath));
GoogleOpenOptions_CredentialsType_.value("Json" , GoogleOpenOptions::CredentialsType::Json, OPENVDS_DOCSTRING(GoogleOpenOptions_CredentialsType_Json));
// HttpOpenOptions
py::class_<HttpOpenOptions, OpenOptions, std::unique_ptr<HttpOpenOptions>>
......
......@@ -577,8 +577,69 @@ Returns:
--------
Number of chunks downloaded.)doc";
static const char *__doc_OpenVDS_GoogleCredentialsJson =
R"doc(Credentials for opening a VDS in Google Cloud Storage by the string
containing json with credentials Using OAuth)doc";
static const char *__doc_OpenVDS_GoogleCredentialsJson_GoogleCredentialsJson =
R"doc(GoogleCredentialsPath constructor
Parameters:
-----------
json :
The string containing json with credentials)doc";
static const char *__doc_OpenVDS_GoogleCredentialsJson_GoogleCredentialsJson_2 = R"doc()doc";
static const char *__doc_OpenVDS_GoogleCredentialsJson_json = R"doc()doc";
static const char *__doc_OpenVDS_GoogleCredentialsPath =
R"doc(Credentials for opening a VDS in Google Cloud Storage by path to the
service account json file Using OAuth)doc";
static const char *__doc_OpenVDS_GoogleCredentialsPath_GoogleCredentialsPath =
R"doc(GoogleCredentialsPath constructor
Parameters:
-----------
path :
The path to the service account json file)doc";
static const char *__doc_OpenVDS_GoogleCredentialsPath_GoogleCredentialsPath_2 = R"doc()doc";
static const char *__doc_OpenVDS_GoogleCredentialsPath_path = R"doc()doc";
static const char *__doc_OpenVDS_GoogleCredentialsToken =
R"doc(Credentials for opening a VDS in Google Cloud Storage by using the
string containing an access token Using OAuth)doc";
static const char *__doc_OpenVDS_GoogleCredentialsToken_GoogleCredentialsToken =
R"doc(GoogleCredentialsPath constructor
Parameters:
-----------
token :
The string containing an access token)doc";
static const char *__doc_OpenVDS_GoogleCredentialsToken_GoogleCredentialsToken_2 = R"doc()doc";
static const char *__doc_OpenVDS_GoogleCredentialsToken_token = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions = R"doc(Options for opening a VDS in Google Cloud Storage)doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_CredentialsType = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_CredentialsType_AccessToken = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_CredentialsType_Default = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_CredentialsType_Json = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_CredentialsType_JsonPath = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_GoogleOpenOptions = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_GoogleOpenOptions_2 =
......@@ -591,10 +652,23 @@ bucket :
The bucket of the VDS
pathPrefix :
The prefix of the VDS)doc";
The prefix of the VDS
credentials :
Google Cloud Storage access credentials)doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_GoogleOpenOptions_3 = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_GoogleOpenOptions_4 = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_GoogleOpenOptions_5 = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_bucket = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_credentials = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_credentialsType = R"doc()doc";
static const char *__doc_OpenVDS_GoogleOpenOptions_pathPrefix = R"doc()doc";
static const char *__doc_OpenVDS_HttpOpenOptions =
......
......@@ -26,6 +26,22 @@ namespace OpenVDS
{
static const std::string GOOGLEAPIS = "https://storage.googleapis.com";
class OAuthTokenCredentials : public google::cloud::storage::v1::oauth2::Credentials
{
std::string token;
public:
explicit OAuthTokenCredentials(const std::string& token) : token(token) {}
explicit OAuthTokenCredentials(std::string&& token) : token(std::move(token)) {}
google::cloud::v1::StatusOr<std::string> AuthorizationHeader() override;
};
google::cloud::v1::StatusOr<std::string> OAuthTokenCredentials::AuthorizationHeader()
{
return token;
}
IOManagerGoogle::IOManagerGoogle(const GoogleOpenOptions& openOptions, Error &error)
: IOManager(OpenOptions::GoogleStorage)
, m_curlHandler(error)
......@@ -39,14 +55,49 @@ namespace OpenVDS
return;
}
auto credentials = google::cloud::storage::v1::oauth2::GoogleDefaultCredentials();
if (!credentials) {
error.code = -2;
error.string = "Google Cloud Storage Config error. Unable to get Google Default Credentials.";
return;
switch (openOptions.credentialsType) {
case GoogleOpenOptions::CredentialsType::Default:
{
auto credentials = google::cloud::storage::v1::oauth2::GoogleDefaultCredentials();
if (!credentials) {
error.code = -2;
error.string = "Google Cloud Storage Config error. Unable to get Google Default Credentials.";
return;
}
m_credentials = *credentials;
}
break;
case GoogleOpenOptions::CredentialsType::AccessToken:
if (openOptions.credentials.empty()) {
error.code = -2;
error.string = "Google Cloud Storage Config error. Authorization Token is empty";
return;
}
m_credentials = std::make_shared<OAuthTokenCredentials>(openOptions.credentials);
break;
case GoogleOpenOptions::CredentialsType::JsonPath:
{
auto credentials = google::cloud::storage::v1::oauth2::CreateServiceAccountCredentialsFromJsonFilePath(openOptions.credentials);
if (!credentials) {
error.code = -2;
error.string = "Google Cloud Storage Config error. Unable to create service account credentials fromJson file path.";
return;
}
m_credentials = *credentials;
}
break;
case GoogleOpenOptions::CredentialsType::Json:
{
auto credentials = google::cloud::storage::v1::oauth2::CreateServiceAccountCredentialsFromJsonContents(openOptions.credentials);
if (!credentials) {
error.code = -2;
error.string = "Google Cloud Storage Config error. Unable to create service account credentials from json contents";
return;
}
m_credentials = *credentials;
}
break;
}
m_credentials = *credentials;
}
static std::string downloadUrl(const std::string& googleapi, const std::string& bucket, const std::string& pathPrefix, const std::string& objectName)
......
......@@ -164,13 +164,90 @@ struct AzurePresignedOpenOptions : OpenOptions
AzurePresignedOpenOptions(const std::string &baseUrl, const std::string &urlSuffix) : OpenOptions(AzurePresigned), baseUrl(baseUrl), urlSuffix(urlSuffix) {}
};
/// <summary>
/// Credentials for opening a VDS in Google Cloud Storage
/// by using the string containing an access token
/// Using OAuth
/// </summary>
class GoogleCredentialsToken
{
friend struct GoogleOpenOptions;
std::string token;
public:
/// <summary>
/// GoogleCredentialsPath constructor
/// </summary>
/// <param name="token">
/// The string containing an access token
/// </param>
explicit GoogleCredentialsToken(std::string const& token) : token(token) {}
explicit GoogleCredentialsToken(std::string&& token) noexcept : token(std::move(token)) {}
};
/// <summary>
/// Credentials for opening a VDS in Google Cloud Storage
/// by path to the service account json file
/// Using OAuth
/// </summary>
class GoogleCredentialsPath
{
friend struct GoogleOpenOptions;
std::string path;
public:
/// <summary>
/// GoogleCredentialsPath constructor
/// </summary>
/// <param name="path">
/// The path to the service account json file
/// </param>
explicit GoogleCredentialsPath(std::string const& path) : path(path) {}
explicit GoogleCredentialsPath(std::string&& path) noexcept : path(std::move(path)) {}
};
/// <summary>
/// Credentials for opening a VDS in Google Cloud Storage
/// by the string containing json with credentials
/// Using OAuth
/// </summary>
class GoogleCredentialsJson
{
friend struct GoogleOpenOptions;
std::string json;
public:
/// <summary>
/// GoogleCredentialsPath constructor
/// </summary>
/// <param name="json">
/// The string containing json with credentials
/// </param>
explicit GoogleCredentialsJson(std::string const& json) : json(json) {}
explicit GoogleCredentialsJson(std::string&& json) noexcept : json(std::move(json)) {}
};
/// <summary>
/// Options for opening a VDS in Google Cloud Storage
/// </summary>
struct GoogleOpenOptions : OpenOptions
{
enum class CredentialsType
{
Default = 0,
AccessToken,
JsonPath,
Json
};
CredentialsType credentialsType = CredentialsType::Default;
std::string bucket;
std::string pathPrefix;
std::string credentials;
GoogleOpenOptions() : OpenOptions(GoogleStorage) {}
/// <summary>
......@@ -182,7 +259,35 @@ struct GoogleOpenOptions : OpenOptions
/// <param name="pathPrefix">
/// The prefix of the VDS
/// </param>
GoogleOpenOptions(std::string const & bucket, std::string const & pathPrefix) : OpenOptions(GoogleStorage), bucket(bucket), pathPrefix(pathPrefix) {}
/// <param name="credentials">
/// Google Cloud Storage access credentials
/// </param>
GoogleOpenOptions(std::string const& bucket, std::string const& pathPrefix)
: OpenOptions(GoogleStorage)
, bucket(bucket)
, pathPrefix(pathPrefix)
{}
GoogleOpenOptions(std::string const& bucket, std::string const& pathPrefix, GoogleCredentialsToken const& credentials)
: OpenOptions(GoogleStorage)
, credentialsType(CredentialsType::AccessToken)
, bucket(bucket)
, pathPrefix(pathPrefix)
, credentials(credentials.token)
{}
GoogleOpenOptions(std::string const& bucket, std::string const& pathPrefix, GoogleCredentialsPath const& credentials)
: OpenOptions(GoogleStorage)
, credentialsType(CredentialsType::JsonPath)
, bucket(bucket)
, pathPrefix(pathPrefix)
, credentials(credentials.path)
{}
GoogleOpenOptions(std::string const& bucket, std::string const& pathPrefix, GoogleCredentialsJson const& credentials)
: OpenOptions(GoogleStorage)
, credentialsType(CredentialsType::Json)
, bucket(bucket)
, pathPrefix(pathPrefix)
, credentials(credentials.json)
{}
};
/// <summary>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment