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 3 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
11 changes: 10 additions & 1 deletion components/core/src/clp/aws/AwsAuthenticationSigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,16 @@ auto
AwsAuthenticationSigner::get_canonical_query_string(string_view scope, string_view timestamp) const
-> string {
auto const uri = fmt::format("{}/{}", m_access_key_id, scope);
std::string session_token_parameter;
if (m_session_token.has_value()) {
session_token_parameter = fmt::format(
"&{}={}",
cAmzSecurityToken,
encode_uri(m_session_token.value(), false)
);
}
return fmt::format(
"{}={}&{}={}&{}={}&{}={}&{}={}",
"{}={}&{}={}&{}={}&{}={}{}&{}={}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will be the behavior is m_session_token.has_value() == false?

I assume session_token_parameter will have an empty string as value hence the extra {} will evaluate to nothing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally feel it might be better to append session_token_parameter explicitly, since te number of {}={} already make the original fmt::format pretty hard to decipher (I had to count by myself when making the change), but ulitimately it's up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will refactor to append session token explicitly.

cXAmzAlgorithm,
cAws4HmacSha256,
cXAmzCredential,
Expand All @@ -258,6 +266,7 @@ AwsAuthenticationSigner::get_canonical_query_string(string_view scope, string_vi
timestamp,
cXAmzExpires,
cDefaultExpireTime.count(),
session_token_parameter,
cXAmzSignedHeaders,
cDefaultSignedHeaders
);
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 cAmzSecurityToken{"X-Amz-Security-Token"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing an "X"?

Suggested change
constexpr std::string_view cAmzSecurityToken{"X-Amz-Security-Token"};
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
14 changes: 13 additions & 1 deletion components/core/src/clp_s/ReaderUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "ReaderUtils.hpp"

#include <exception>
#include <string>
#include <string_view>
#include <unordered_map>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't see this used in the file. Is it intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope -- leftover from when I was going to pass the token as an http header. Will delete.


#include <spdlog/spdlog.h>

#include "../clp/aws/AwsAuthenticationSigner.hpp"
#include "../clp/CurlDownloadHandler.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a leftover. Will delete.

#include "../clp/FileReader.hpp"
#include "../clp/NetworkReader.hpp"
#include "../clp/ReaderInterface.hpp"
Expand Down Expand Up @@ -164,8 +167,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