Skip to content

Commit 39f4164

Browse files
address review feedback
1 parent 655be23 commit 39f4164

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

src/iceberg/catalog/rest/auth/sigv4_auth_manager.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919

2020
#include "iceberg/catalog/rest/auth/sigv4_auth_manager.h"
2121

22-
#include <cstdlib>
2322
#include <sstream>
2423

2524
#include <aws/core/Aws.h>
2625
#include <aws/core/auth/AWSAuthSigner.h>
2726
#include <aws/core/auth/AWSCredentialsProvider.h>
2827
#include <aws/core/auth/AWSCredentialsProviderChain.h>
28+
#include <aws/core/client/ClientConfiguration.h>
2929
#include <aws/core/http/standard/StandardHttpRequest.h>
3030
#include <aws/core/utils/HashingUtils.h>
3131

3232
#include "iceberg/catalog/rest/auth/auth_manager_internal.h"
3333
#include "iceberg/catalog/rest/auth/auth_managers.h"
3434
#include "iceberg/catalog/rest/auth/auth_properties.h"
3535
#include "iceberg/catalog/rest/endpoint.h"
36-
#include "iceberg/util/checked_cast.h"
3736
#include "iceberg/util/macros.h"
3837
#include "iceberg/util/string_util.h"
3938

@@ -208,7 +207,9 @@ Result<std::shared_ptr<AuthSession>> SigV4AuthManager::CatalogSession(
208207
Result<std::shared_ptr<AuthSession>> SigV4AuthManager::ContextualSession(
209208
const std::unordered_map<std::string, std::string>& context,
210209
std::shared_ptr<AuthSession> parent) {
211-
auto sigv4_parent = internal::checked_pointer_cast<SigV4AuthSession>(std::move(parent));
210+
auto sigv4_parent = std::dynamic_pointer_cast<SigV4AuthSession>(std::move(parent));
211+
ICEBERG_PRECHECK(sigv4_parent != nullptr,
212+
"SigV4AuthManager parent must be a SigV4AuthSession");
212213

213214
ICEBERG_ASSIGN_OR_RAISE(auto delegate_session, delegate_->ContextualSession(
214215
context, sigv4_parent->delegate()));
@@ -221,7 +222,9 @@ Result<std::shared_ptr<AuthSession>> SigV4AuthManager::TableSession(
221222
const TableIdentifier& table,
222223
const std::unordered_map<std::string, std::string>& properties,
223224
std::shared_ptr<AuthSession> parent) {
224-
auto sigv4_parent = internal::checked_pointer_cast<SigV4AuthSession>(std::move(parent));
225+
auto sigv4_parent = std::dynamic_pointer_cast<SigV4AuthSession>(std::move(parent));
226+
ICEBERG_PRECHECK(sigv4_parent != nullptr,
227+
"SigV4AuthManager parent must be a SigV4AuthSession");
225228

226229
ICEBERG_ASSIGN_OR_RAISE(
227230
auto delegate_session,
@@ -233,6 +236,8 @@ Result<std::shared_ptr<AuthSession>> SigV4AuthManager::TableSession(
233236

234237
Status SigV4AuthManager::Close() { return delegate_->Close(); }
235238

239+
// TODO(sigv4): support loading a custom AWSCredentialsProvider via a class
240+
// name property, matching Java's AwsProperties.restCredentialsProvider().
236241
Result<std::shared_ptr<Aws::Auth::AWSCredentialsProvider>>
237242
SigV4AuthManager::MakeCredentialsProvider(
238243
const std::unordered_map<std::string, std::string>& properties) {
@@ -264,13 +269,10 @@ std::string SigV4AuthManager::ResolveSigningRegion(
264269
it != properties.end() && !it->second.empty()) {
265270
return it->second;
266271
}
267-
if (const char* env = std::getenv("AWS_REGION")) {
268-
return {env};
269-
}
270-
if (const char* env = std::getenv("AWS_DEFAULT_REGION")) {
271-
return {env};
272-
}
273-
return "us-east-1";
272+
// Delegates the full resolution chain (AWS_DEFAULT_REGION / AWS_REGION env,
273+
// ~/.aws/config profile, EC2/ECS IMDS, fallback us-east-1) to the AWS SDK.
274+
// Set AWS_EC2_METADATA_DISABLED=true to skip IMDS on non-EC2 hosts.
275+
return {Aws::Client::ClientConfiguration().region.c_str()};
274276
}
275277

276278
std::string SigV4AuthManager::ResolveSigningName(

src/iceberg/catalog/rest/http_client.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ cpr::Header ToCprHeader(const auth::HTTPRequest& request) {
8686
}
8787

8888
/// \brief Append URL-encoded query parameters to a URL, sorted by key.
89-
std::string AppendQueryString(
89+
Result<std::string> AppendQueryString(
9090
const std::string& base_url,
9191
const std::unordered_map<std::string, std::string>& params) {
9292
if (params.empty()) return base_url;
@@ -95,9 +95,9 @@ std::string AppendQueryString(
9595
bool first = true;
9696
for (const auto& [k, v] : sorted) {
9797
if (!first) url += "&";
98-
auto ek = EncodeString(k);
99-
auto ev = EncodeString(v);
100-
url += (ek ? *ek : k) + "=" + (ev ? *ev : v);
98+
ICEBERG_ASSIGN_OR_RAISE(auto ek, EncodeString(k));
99+
ICEBERG_ASSIGN_OR_RAISE(auto ev, EncodeString(v));
100+
url += ek + "=" + ev;
101101
first = false;
102102
}
103103
return url;
@@ -161,10 +161,11 @@ Result<HttpResponse> HttpClient::Get(
161161
const std::string& path, const std::unordered_map<std::string, std::string>& params,
162162
const std::unordered_map<std::string, std::string>& headers,
163163
const ErrorHandler& error_handler, auth::AuthSession& session) {
164+
ICEBERG_ASSIGN_OR_RAISE(auto url, AppendQueryString(path, params));
164165
ICEBERG_ASSIGN_OR_RAISE(
165166
auto authenticated,
166167
session.Authenticate({.method = HttpMethod::kGet,
167-
.url = AppendQueryString(path, params),
168+
.url = std::move(url),
168169
.headers = MergeHeaders(default_headers_, headers),
169170
.body = ""}));
170171
cpr::Response response = cpr::Get(cpr::Url{authenticated.url},
@@ -249,10 +250,11 @@ Result<HttpResponse> HttpClient::Delete(
249250
const std::string& path, const std::unordered_map<std::string, std::string>& params,
250251
const std::unordered_map<std::string, std::string>& headers,
251252
const ErrorHandler& error_handler, auth::AuthSession& session) {
253+
ICEBERG_ASSIGN_OR_RAISE(auto url, AppendQueryString(path, params));
252254
ICEBERG_ASSIGN_OR_RAISE(
253255
auto authenticated,
254256
session.Authenticate({.method = HttpMethod::kDelete,
255-
.url = AppendQueryString(path, params),
257+
.url = std::move(url),
256258
.headers = MergeHeaders(default_headers_, headers),
257259
.body = ""}));
258260
cpr::Response response = cpr::Delete(cpr::Url{authenticated.url},

0 commit comments

Comments
 (0)