Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clp-json): Add session token support for presigned URL authentication. #680

Merged
merged 5 commits into from
Jan 27, 2025
Merged
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
17 changes: 12 additions & 5 deletions components/core/src/clp/aws/AwsAuthenticationSigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,26 @@ auto
AwsAuthenticationSigner::get_canonical_query_string(string_view scope, string_view timestamp) const
-> string {
auto const uri = fmt::format("{}/{}", m_access_key_id, scope);
return fmt::format(
"{}={}&{}={}&{}={}&{}={}&{}={}",
auto canonical_query_string = fmt::format(
"{}={}&{}={}&{}={}&{}={}",
cXAmzAlgorithm,
cAws4HmacSha256,
cXAmzCredential,
encode_uri(uri, false),
cXAmzDate,
timestamp,
cXAmzExpires,
cDefaultExpireTime.count(),
cXAmzSignedHeaders,
cDefaultSignedHeaders
cDefaultExpireTime.count()
);
if (m_session_token.has_value()) {
canonical_query_string.append(fmt::format(
"&{}={}",
cXAmzSecurityToken,
encode_uri(m_session_token.value(), false)
));
}
canonical_query_string.append(fmt::format("&{}={}", cXAmzSignedHeaders, cDefaultSignedHeaders));
return canonical_query_string;
}

auto AwsAuthenticationSigner::get_signing_key(
Expand Down
11 changes: 9 additions & 2 deletions components/core/src/clp/aws/AwsAuthenticationSigner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CLP_AWS_AWSAUTHENTICATIONSIGNER_HPP

#include <chrono>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -69,9 +70,14 @@ class AwsAuthenticationSigner {
static constexpr std::string_view cHttpGetMethod{"GET"};

// Constructors
AwsAuthenticationSigner(std::string access_key_id, std::string secret_access_key)
AwsAuthenticationSigner(
std::string access_key_id,
std::string secret_access_key,
std::optional<std::string> session_token
)
: m_access_key_id{std::move(access_key_id)},
m_secret_access_key{std::move(secret_access_key)} {}
m_secret_access_key{std::move(secret_access_key)},
m_session_token{std::move(session_token)} {}

// Methods
/**
Expand Down Expand Up @@ -129,6 +135,7 @@ class AwsAuthenticationSigner {
// Variables
std::string m_access_key_id;
std::string m_secret_access_key;
std::optional<std::string> m_session_token;
};
} // namespace clp::aws

Expand Down
1 change: 1 addition & 0 deletions components/core/src/clp/aws/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ constexpr std::string_view cXAmzAlgorithm{"X-Amz-Algorithm"};
constexpr std::string_view cXAmzCredential{"X-Amz-Credential"};
constexpr std::string_view cXAmzDate{"X-Amz-Date"};
constexpr std::string_view cXAmzExpires{"X-Amz-Expires"};
constexpr std::string_view cXAmzSecurityToken{"X-Amz-Security-Token"};
constexpr std::string_view cXAmzSignature{"X-Amz-Signature"};
constexpr std::string_view cXAmzSignedHeaders{"X-Amz-SignedHeaders"};

Expand Down
6 changes: 3 additions & 3 deletions components/core/src/clp_s/CommandLineArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ CommandLineArguments::parse_arguments(int argc, char const** argv) {
->default_value(auth),
"Type of authentication required for network requests (s3 | none). Authentication"
" with s3 requires the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment"
" variables."
" variables, and optionally the AWS_SESSION_TOKEN environment variable."
);
// clang-format on

Expand Down Expand Up @@ -428,7 +428,7 @@ CommandLineArguments::parse_arguments(int argc, char const** argv) {
->default_value(auth),
"Type of authentication required for network requests (s3 | none). Authentication"
" with s3 requires the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment"
" variables."
" variables, and optionally the AWS_SESSION_TOKEN environment variable."
);
// clang-format on
extraction_options.add(decompression_options);
Expand Down Expand Up @@ -582,7 +582,7 @@ CommandLineArguments::parse_arguments(int argc, char const** argv) {
->default_value(auth),
"Type of authentication required for network requests (s3 | none). Authentication"
" with s3 requires the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment"
" variables."
" variables, and optionally the AWS_SESSION_TOKEN environment variable."
);
// clang-format on
search_options.add(match_options);
Expand Down
1 change: 1 addition & 0 deletions components/core/src/clp_s/InputConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace clp_s {
// Constants used for input configuration
constexpr char cAwsAccessKeyIdEnvVar[] = "AWS_ACCESS_KEY_ID";
constexpr char cAwsSecretAccessKeyEnvVar[] = "AWS_SECRET_ACCESS_KEY";
constexpr char cAwsSessionTokenEnvVar[] = "AWS_SESSION_TOKEN";

/**
* Enum class defining the source of a resource.
Expand Down
12 changes: 11 additions & 1 deletion components/core/src/clp_s/ReaderUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ReaderUtils.hpp"

#include <exception>
#include <string>
#include <string_view>

#include <spdlog/spdlog.h>
Expand Down Expand Up @@ -164,8 +165,17 @@ bool try_sign_url(std::string& url) {
);
return false;
}
std::optional<std::string> optional_aws_session_token{std::nullopt};
auto const aws_session_token = std::getenv(cAwsSessionTokenEnvVar);
if (nullptr != aws_session_token) {
optional_aws_session_token = std::string{aws_session_token};
}

clp::aws::AwsAuthenticationSigner signer{aws_access_key, aws_secret_access_key};
clp::aws::AwsAuthenticationSigner signer{
aws_access_key,
aws_secret_access_key,
optional_aws_session_token
};

try {
clp::aws::S3Url s3_url{url};
Expand Down
Loading