From f5059bd2a40905447c15c148e859d554a9c57dcf Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Sun, 15 Nov 2026 20:57:13 +0800 Subject: [PATCH 01/10] feat(rest): support OAuth2 token exchange refresh and recovery --- src/iceberg/catalog/rest/auth/auth_manager.cc | 11 +- .../catalog/rest/auth/auth_properties.h | 4 +- src/iceberg/catalog/rest/auth/auth_session.cc | 16 +- src/iceberg/catalog/rest/auth/auth_session.h | 1 + .../catalog/rest/auth/auth_session_internal.h | 129 ++- src/iceberg/test/auth_manager_test.cc | 787 +++++++++++++++++- src/iceberg/test/http_client_test.cc | 250 +++++- src/iceberg/test/rest_catalog_unit_test.cc | 185 ++++ 8 files changed, 1315 insertions(+), 68 deletions(-) diff --git a/src/iceberg/catalog/rest/auth/auth_manager.cc b/src/iceberg/catalog/rest/auth/auth_manager.cc index 0fad64eca1..d0dd92753a 100644 --- a/src/iceberg/catalog/rest/auth/auth_manager.cc +++ b/src/iceberg/catalog/rest/auth/auth_manager.cc @@ -309,6 +309,7 @@ class OAuth2Manager : public AuthManager { // Initialize catalog-level configuration keep_refreshed_ = config.keep_refreshed(); + exchange_enabled_ = config.exchange_enabled(); ICEBERG_ASSIGN_OR_RAISE(session_timeout_, ParseSessionTimeout(config)); // Create session cache @@ -420,11 +421,10 @@ class OAuth2Manager : public AuthManager { std::nullopt) const { ICEBERG_PRECHECK(refresh_client_ != nullptr, "OAuth2 catalog session must be initialized before child sessions"); - return internal::MakeOAuth2Session(token_response, config.oauth2_server_uri(), - config.client_id(), config.client_secret(), - config.scope(), keep_refreshed, - config.optional_oauth_params(), refresh_client_, - token_request_started_at, refresh_executor_); + return internal::MakeOAuth2Session( + token_response, config.oauth2_server_uri(), config.credential(), config.scope(), + keep_refreshed, config.optional_oauth_params(), refresh_client_, + token_request_started_at, refresh_executor_, exchange_enabled_); } Result ParentInfo(const AuthSession& parent) const { @@ -530,6 +530,7 @@ class OAuth2Manager : public AuthManager { // Catalog-level configuration initialized in CatalogSession bool keep_refreshed_ = true; + bool exchange_enabled_ = true; std::chrono::milliseconds session_timeout_{3'600'000}; std::shared_ptr session_cache_; std::once_flag no_refresh_executor_warning_; diff --git a/src/iceberg/catalog/rest/auth/auth_properties.h b/src/iceberg/catalog/rest/auth/auth_properties.h index 0cf9be5e44..d65f2fc6c9 100644 --- a/src/iceberg/catalog/rest/auth/auth_properties.h +++ b/src/iceberg/catalog/rest/auth/auth_properties.h @@ -79,8 +79,8 @@ class ICEBERG_REST_EXPORT AuthProperties : public ConfigBase { "v1/oauth/tokens"}; inline static Entry kKeepRefreshed{"token-refresh-enabled", true}; - /// Whether to use token exchange for refreshing OAuth2 sessions. - /// NOTE: Not yet implemented; the value is ignored. + /// Whether to use token exchange when refreshing credential-based OAuth2 sessions. + /// When false, refresh uses client credentials. inline static Entry kExchangeEnabled{"token-exchange-enabled", true}; inline static Entry kAudience{"audience", ""}; diff --git a/src/iceberg/catalog/rest/auth/auth_session.cc b/src/iceberg/catalog/rest/auth/auth_session.cc index bd74b5bb60..dd8f0d70c8 100644 --- a/src/iceberg/catalog/rest/auth/auth_session.cc +++ b/src/iceberg/catalog/rest/auth/auth_session.cc @@ -64,28 +64,30 @@ Result> AuthSession::MakeOAuth2( const std::unordered_map& optional_oauth_params, std::shared_ptr client, Executor* auth_executor) { return internal::MakeOAuth2Session( - initial_token, token_endpoint, client_id, client_secret, scope, keep_refreshed, - optional_oauth_params, std::move(client), std::nullopt, auth_executor); + initial_token, token_endpoint, + client_id.empty() ? client_secret : client_id + ":" + client_secret, scope, + keep_refreshed, optional_oauth_params, std::move(client), std::nullopt, + auth_executor, + /*exchange_enabled=*/false); } Result> internal::MakeOAuth2Session( const OAuthTokenResponse& initial_token, const std::string& token_endpoint, - const std::string& client_id, const std::string& client_secret, - const std::string& scope, bool keep_refreshed, + const std::string& credential, const std::string& scope, bool keep_refreshed, const std::unordered_map& optional_oauth_params, std::shared_ptr client, std::optional token_request_started_at, - Executor* refresh_executor) { + Executor* refresh_executor, bool exchange_enabled) { ICEBERG_PRECHECK(!keep_refreshed || refresh_executor != nullptr, "OAuth2 token refresh is enabled but no auth executor was provided; " "pass an executor or set {}=false", AuthProperties::kKeepRefreshed.key()); internal::OAuth2Session::Config config{ .token_endpoint = token_endpoint, - .client_id = client_id, - .client_secret = client_secret, + .credential = credential, .scope = scope, .optional_oauth_params = optional_oauth_params, + .exchange_enabled = exchange_enabled, .refresh_executor = keep_refreshed ? refresh_executor : nullptr, }; ICEBERG_ASSIGN_OR_RAISE(auto session, internal::OAuth2Session::Make( diff --git a/src/iceberg/catalog/rest/auth/auth_session.h b/src/iceberg/catalog/rest/auth/auth_session.h index 393418e2a4..b5dddcebd6 100644 --- a/src/iceberg/catalog/rest/auth/auth_session.h +++ b/src/iceberg/catalog/rest/auth/auth_session.h @@ -88,6 +88,7 @@ class ICEBERG_REST_EXPORT AuthSession { /// \brief Create an OAuth2 session with automatic token refresh. /// /// Refreshes expiring tokens in the background when keep_refreshed is true. + /// This factory uses the client-credentials grant for refresh. /// Authenticate() uses the cached token without refreshing it. /// /// \param initial_token The initial token response from FetchToken(). diff --git a/src/iceberg/catalog/rest/auth/auth_session_internal.h b/src/iceberg/catalog/rest/auth/auth_session_internal.h index a4454aafc9..a912a264db 100644 --- a/src/iceberg/catalog/rest/auth/auth_session_internal.h +++ b/src/iceberg/catalog/rest/auth/auth_session_internal.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include "iceberg/catalog/rest/auth/oauth2_util.h" #include "iceberg/catalog/rest/http_client.h" #include "iceberg/logging/log_macros.h" +#include "iceberg/util/base64.h" #include "iceberg/util/macros.h" #include "iceberg/util/retry_util.h" #include "iceberg/util/scheduled_executor_internal.h" @@ -65,12 +67,16 @@ class OAuth2Session final : public AuthSession, public: struct Config { std::string token_endpoint; - std::string client_id; - std::string client_secret; + std::string credential; std::string scope; std::unordered_map optional_oauth_params; + /// The legacy public factory keeps client-credentials refresh. + bool exchange_enabled = false; /// Borrowed executor for background refresh. Null disables refresh. Executor* refresh_executor = nullptr; + /// Internal clock override for deterministic expiration tests. + std::function now = + std::chrono::steady_clock::now; }; static Result> Make( @@ -98,7 +104,7 @@ class OAuth2Session final : public AuthSession, return OAuth2SessionInfo{ .token = token_, .issued_token_type = issued_token_type_, - .credential = Credential(config_), + .credential = config_.credential, .scope = config_.scope, .oauth2_server_uri = config_.token_endpoint, .optional_oauth_params = config_.optional_oauth_params, @@ -135,8 +141,8 @@ class OAuth2Session final : public AuthSession, /// \brief Whether this session will keep refreshing its token. /// - /// False if refresh is disabled, was stopped by StopRefreshing() or Close(), or - /// gave up after repeated failures. + /// False if expiration is unknown, refresh is disabled, was stopped by + /// StopRefreshing() or Close(), or gave up after repeated failures. bool IsRefreshable() const { std::lock_guard lock(refresh_mutex_); return scheduler_.has_value() && !closed_ && !refresh_stopped_; @@ -169,15 +175,10 @@ class OAuth2Session final : public AuthSession, return {}; } - static std::string Credential(const Config& config) { - return config.client_id.empty() ? config.client_secret - : config.client_id + ":" + config.client_secret; - } - static Result MakeRefreshProperties(const Config& config) { std::unordered_map properties = config.optional_oauth_params; - properties[AuthProperties::kCredential.key()] = Credential(config); + properties[AuthProperties::kCredential.key()] = config.credential; properties[AuthProperties::kScope.key()] = config.scope; properties[AuthProperties::kOAuth2ServerUri.key()] = config.token_endpoint; return AuthProperties::FromProperties(properties); @@ -215,32 +216,31 @@ class OAuth2Session final : public AuthSession, }; void UpdateTokenState(const OAuthTokenResponse& token_response, - std::optional - token_request_started_at = std::nullopt) { + std::optional expiration) { token_ = token_response.access_token; issued_token_type_ = token_response.issued_token_type.empty() ? AuthProperties::kAccessTokenType : token_response.issued_token_type; headers_ = OAuth2Util::AuthHeaders(token_); - expires_at_ = std::chrono::steady_clock::time_point{}; - auto request_started_at = - token_request_started_at.value_or(std::chrono::steady_clock::now()); - if (auto expiration = TokenExpirationTime(token_response, request_started_at); - expiration.has_value()) { - expires_at_ = *expiration; - } + expires_at_ = expiration.value_or(std::chrono::steady_clock::time_point{}); } void SetInitialToken( const OAuthTokenResponse& token_response, std::optional token_request_started_at) { - UpdateTokenState(token_response, token_request_started_at); + auto request_started_at = token_request_started_at.value_or(config_.now()); + UpdateTokenState( + token_response, + TokenExpirationTime(token_response, request_started_at, + std::chrono::system_clock::now(), config_.now())); if (!scheduler_) { return; } // Refresh an already-expired initial token immediately. if (auto delay = CalculateRefreshDelay()) { ScheduleRefresh(*delay); + } else { + MarkRefreshStopped(); } } @@ -254,21 +254,60 @@ class OAuth2Session final : public AuthSession, auto result = RetryRunner>({.num_retries = 5}).Run([this] { return RefreshToken(); }); + if (!result.has_value()) { + // One final credential-backed attempt, within the same close guard. + result = RefreshToken(/*recover=*/true); + } if (!result.has_value()) { ICEBERG_LOG_WARN("Failed to refresh OAuth2 token: {}", result.error().message); MarkRefreshStopped(); return std::nullopt; } + if (!result->has_value()) { + MarkRefreshStopped(); + } return *result; } - // Record that no further refresh will be scheduled after a failure. + // Record that no further refresh will be scheduled. void MarkRefreshStopped() { std::lock_guard lock(refresh_mutex_); refresh_stopped_ = true; } - Result> RefreshToken() { + class ClientCredentialsSession final : public AuthSession { + public: + Result Authenticate(HttpRequest request) override { + // HttpClient merges its defaults before invoking the session. + request.headers.erase("Authorization"); + return request; + } + }; + + Result FetchRefreshToken(const std::string& token, + const std::string& issued_token_type, + bool recover) { + if (!config_.exchange_enabled) { + ClientCredentialsSession session; + return OAuth2Util::FetchToken(*client_, session, refresh_properties_); + } + auto empty_session = AuthSession::MakeDefault({}); + auto headers = + recover + ? std::unordered_map{{"Authorization", + "Basic " + + Base64::Encode( + config_.credential)}} + : OAuth2Util::AuthHeaders(token); + // Explicit request headers override any default Authorization on the client. + // Keep the current token as subject, without a parent actor. + return OAuth2Util::ExchangeToken(*client_, *empty_session, headers, token, + issued_token_type, std::nullopt, std::nullopt, + config_.scope, config_.token_endpoint, + config_.optional_oauth_params); + } + + Result> RefreshToken(bool recover = false) { { std::lock_guard lock(refresh_mutex_); if (closed_) { @@ -276,21 +315,34 @@ class OAuth2Session final : public AuthSession, } } try { - auto request_started_at = std::chrono::steady_clock::now(); - auto empty_session = AuthSession::MakeDefault({}); - // TODO(lishuxu): Honor token-exchange-enabled when refreshing tokens. - ICEBERG_ASSIGN_OR_RAISE( - auto response, - OAuth2Util::FetchToken(*client_, *empty_session, refresh_properties_)); + std::string token; + std::string issued_token_type; { - std::unique_lock lock(mutex_); - UpdateTokenState(response, request_started_at); + std::shared_lock lock(mutex_); + token = token_; + issued_token_type = issued_token_type_; + recover = recover || (expires_at_ != std::chrono::steady_clock::time_point{} && + expires_at_ <= config_.now()); } - auto delay = CalculateRefreshDelay(); - if (delay.has_value() && *delay <= std::chrono::milliseconds::zero()) { - return TokenExpired("Refreshed OAuth2 token is already expired"); + if ((!config_.exchange_enabled || recover) && config_.credential.empty()) { + ICEBERG_LOG_WARN("Stopping OAuth2 token refresh: no credential available"); + return std::nullopt; + } + auto request_started_at = config_.now(); + ICEBERG_ASSIGN_OR_RAISE(auto response, + FetchRefreshToken(token, issued_token_type, recover)); + auto expiration = TokenExpirationTime( + response, request_started_at, std::chrono::system_clock::now(), config_.now()); + { + std::unique_lock lock(mutex_); + // Keep the current token available for requests and exchange retries until + // the replacement has passed expiration validation. + if (expiration.has_value() && *expiration <= config_.now()) { + return TokenExpired("Refreshed OAuth2 token is already expired"); + } + UpdateTokenState(response, expiration); } - return delay; + return CalculateRefreshDelay(); } catch (const std::exception& e) { return UnknownError("OAuth2 token refresh threw: {}", e.what()); } catch (...) { @@ -338,7 +390,7 @@ class OAuth2Session final : public AuthSession, if (expires_at_ == std::chrono::steady_clock::time_point{}) { return std::nullopt; } - auto now = std::chrono::steady_clock::now(); + auto now = config_.now(); if (expires_at_ <= now) { return std::chrono::milliseconds::zero(); } @@ -369,11 +421,10 @@ class OAuth2Session final : public AuthSession, /// Returns InvalidArgument if keep_refreshed is true without an executor. Result> MakeOAuth2Session( const OAuthTokenResponse& initial_token, const std::string& token_endpoint, - const std::string& client_id, const std::string& client_secret, - const std::string& scope, bool keep_refreshed, + const std::string& credential, const std::string& scope, bool keep_refreshed, const std::unordered_map& optional_oauth_params, std::shared_ptr client, std::optional token_request_started_at, - Executor* refresh_executor); + Executor* refresh_executor, bool exchange_enabled); } // namespace iceberg::rest::auth::internal diff --git a/src/iceberg/test/auth_manager_test.cc b/src/iceberg/test/auth_manager_test.cc index 076df633ed..5d4746aad8 100644 --- a/src/iceberg/test/auth_manager_test.cc +++ b/src/iceberg/test/auth_manager_test.cc @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -682,10 +683,22 @@ namespace { class RefreshHttpClient : public HttpClient { public: - Result PostForm(const std::string&, - const std::unordered_map&, - const std::unordered_map&, - const ErrorHandler&, AuthSession&) override { + struct Request { + std::string path; + std::unordered_map form; + HttpHeaders headers; + }; + + Result PostForm( + const std::string& path, const std::unordered_map& form, + const std::unordered_map& headers, const ErrorHandler&, + AuthSession& session) override { + ICEBERG_ASSIGN_OR_RAISE( + auto authenticated, + session.Authenticate({.method = HttpMethod::kPost, + .url = path, + .headers = {headers.begin(), headers.end()}})); + recorded_requests.push_back({path, form, std::move(authenticated.headers)}); ++requests; return response(); } @@ -693,6 +706,7 @@ class RefreshHttpClient : public HttpClient { return IOError("refresh failed"); }; std::atomic requests{0}; + std::vector recorded_requests; }; using test::MockExecutor; @@ -736,6 +750,760 @@ TEST(OAuth2SessionTest, RefreshesAndReschedulesThroughPublicFactory) { ICEBERG_UNWRAP_OR_FAIL(auto request, session->Authenticate({})); EXPECT_EQ(request.headers.at("Authorization"), "Bearer second"); EXPECT_EQ(client->requests, 2); + for (const auto& request : client->recorded_requests) { + EXPECT_EQ(request.form.at("grant_type"), "client_credentials"); + EXPECT_FALSE(request.headers.contains("Authorization")); + } +} + +class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { + protected: + void SetUp() override { + if (!GetParam().empty()) { + properties_[AuthProperties::kExchangeEnabled.key()] = GetParam(); + } + ICEBERG_UNWRAP_OR_FAIL(manager_, AuthManagers::Load("test-catalog", properties_)); + manager_->SetAuthRefreshExecutor(&executor_); + client_->response = [this]() -> Result { + return HttpResponse( + 200, client_->requests == 1 + ? R"({"access_token":"initial","token_type":"bearer","expires_in":1})" + : R"({"access_token":"fresh","token_type":"bearer"})"); + }; + } + + void TearDown() override { + if (session_) { + EXPECT_THAT(session_->Close(), IsOk()); + } + if (manager_) { + EXPECT_THAT(manager_->Close(), IsOk()); + } + } + + void ExpectRefresh() { + EXPECT_CALL(executor_, Submit).WillOnce([this](ExecutorTask task) -> Status { + queued_.set_value(std::move(task)); + return {}; + }); + } + + void RunRefresh() { + auto task = queued_.get_future(); + ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); + std::move(task.get())(); + } + + void CheckRequests(const std::string& client_id, const std::string& client_secret, + const std::string& parent_token = "") { + ASSERT_EQ(client_->recorded_requests.size(), 2); + std::unordered_map credential_form{ + {"grant_type", "client_credentials"}, {"client_id", client_id}, + {"client_secret", client_secret}, {"scope", "catalog"}, + {"audience", "catalog-service"}, {"resource", "https://catalog.example"}, + }; + const auto& initial = client_->recorded_requests[0]; + EXPECT_THAT(initial.form, ::testing::ContainerEq(credential_form)); + if (parent_token.empty()) { + EXPECT_TRUE(initial.headers.empty()); + } else { + EXPECT_EQ(initial.headers.at("Authorization"), "Bearer " + parent_token); + } + + const auto& refresh = client_->recorded_requests[1]; + if (GetParam() == "false") { + EXPECT_THAT(refresh.form, ::testing::ContainerEq(credential_form)); + EXPECT_TRUE(refresh.headers.empty()); + } else { + const std::unordered_map exchange_form{ + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", "initial"}, + {"subject_token_type", AuthProperties::kAccessTokenType}, + {"scope", "catalog"}, + {"audience", "catalog-service"}, + {"resource", "https://catalog.example"}, + }; + EXPECT_THAT(refresh.form, ::testing::ContainerEq(exchange_form)); + EXPECT_THAT(refresh.headers, ::testing::ElementsAre(::testing::Pair( + "Authorization", "Bearer initial"))); + } + for (const auto& request : client_->recorded_requests) { + EXPECT_EQ(request.path, properties_.at(AuthProperties::kOAuth2ServerUri.key())); + } + ICEBERG_UNWRAP_OR_FAIL(auto request, session_->Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer fresh"); + EXPECT_EQ(session_->OAuth2Info()->credential, client_id + ":" + client_secret); + } + + MockExecutor executor_; + std::promise queued_; + std::shared_ptr client_ = std::make_shared(); + std::unordered_map properties_{ + {AuthProperties::kAuthType, "oauth2"}, + {AuthProperties::kCredential.key(), "catalog:catalog-secret"}, + {AuthProperties::kOAuth2ServerUri.key(), "https://auth.example/token"}, + {AuthProperties::kScope.key(), "catalog"}, + {AuthProperties::kAudience.key(), "catalog-service"}, + {AuthProperties::kResource.key(), "https://catalog.example"}, + }; + std::unique_ptr manager_; + std::shared_ptr session_; +}; + +TEST_P(OAuth2ManagerRefreshTest, CatalogRefreshUsesConfiguredGrant) { + ExpectRefresh(); + ICEBERG_UNWRAP_OR_FAIL(session_, manager_->CatalogSession(client_, properties_)); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + CheckRequests("catalog", "catalog-secret"); +} + +TEST_P(OAuth2ManagerRefreshTest, CredentialContextInheritsCatalogRefreshGrant) { + ExpectRefresh(); + properties_[AuthProperties::kToken.key()] = "parent-token"; + ICEBERG_UNWRAP_OR_FAIL(auto parent, manager_->CatalogSession(client_, properties_)); + SessionContext context{ + .session_id = "tenant-a", + .credentials = {{AuthProperties::kCredential.key(), "child:child-secret"}}, + }; + ICEBERG_UNWRAP_OR_FAIL(session_, manager_->ContextualSession(context, parent)); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + CheckRequests("child", "child-secret", "parent-token"); + ICEBERG_UNWRAP_OR_FAIL(auto cached, manager_->ContextualSession(context, parent)); + EXPECT_EQ(cached, session_); +} + +TEST_P(OAuth2ManagerRefreshTest, ExpiredCredentialContextRecoversWithOwnRawCredential) { + ExpectRefresh(); + properties_[AuthProperties::kToken.key()] = "parent-token"; + ICEBERG_UNWRAP_OR_FAIL(auto parent, manager_->CatalogSession(client_, properties_)); + client_->response = [this]() -> Result { + return HttpResponse( + 200, + client_->requests == 1 + ? R"({"access_token":"child-expired","token_type":"bearer","issued_token_type":"urn:ietf:params:oauth:token-type:jwt","expires_in":0})" + : R"({"access_token":"child-fresh","token_type":"bearer"})"); + }; + SessionContext context{ + .session_id = "tenant-recovery", + .credentials = {{AuthProperties::kCredential.key(), ":child-secret"}}, + }; + ICEBERG_UNWRAP_OR_FAIL(session_, manager_->ContextualSession(context, parent)); + EXPECT_EQ(session_->OAuth2Info()->credential, ":child-secret"); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + ASSERT_EQ(client_->recorded_requests.size(), 2); + const auto& initial = client_->recorded_requests[0]; + const std::unordered_map credential_form{ + {"grant_type", "client_credentials"}, + {"client_secret", "child-secret"}, + {"scope", "catalog"}, + {"audience", "catalog-service"}, + {"resource", "https://catalog.example"}, + }; + EXPECT_THAT(initial.form, ::testing::ContainerEq(credential_form)); + EXPECT_EQ(initial.headers.at("Authorization"), "Bearer parent-token"); + const auto& recovery = client_->recorded_requests[1]; + if (GetParam() == "false") { + EXPECT_THAT(recovery.form, ::testing::ContainerEq(credential_form)); + EXPECT_TRUE(recovery.headers.empty()); + } else { + const std::unordered_map exchange_form{ + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", "child-expired"}, + {"subject_token_type", AuthProperties::kJwtTokenType}, + {"scope", "catalog"}, + {"audience", "catalog-service"}, + {"resource", "https://catalog.example"}, + }; + EXPECT_THAT(recovery.form, ::testing::ContainerEq(exchange_form)); + EXPECT_THAT(recovery.headers, ::testing::ElementsAre(::testing::Pair( + "Authorization", "Basic OmNoaWxkLXNlY3JldA=="))); + } + EXPECT_EQ(recovery.path, properties_.at(AuthProperties::kOAuth2ServerUri.key())); + EXPECT_EQ(session_->OAuth2Info()->credential, ":child-secret"); + ICEBERG_UNWRAP_OR_FAIL(auto request, session_->Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer child-fresh"); + ICEBERG_UNWRAP_OR_FAIL(auto parent_request, parent->Authenticate({})); + EXPECT_EQ(parent_request.headers.at("Authorization"), "Bearer parent-token"); + ICEBERG_UNWRAP_OR_FAIL(auto cached, manager_->ContextualSession(context, parent)); + EXPECT_EQ(cached, session_); + EXPECT_EQ(client_->requests, 2); +} + +TEST_P(OAuth2ManagerRefreshTest, RefreshDisabledDoesNotScheduleCatalogOrContext) { + EXPECT_CALL(executor_, Submit).Times(0); + properties_[AuthProperties::kKeepRefreshed.key()] = "false"; + client_->response = []() -> Result { + return HttpResponse( + 200, R"({"access_token":"initial","token_type":"bearer","expires_in":0})"); + }; + ICEBERG_UNWRAP_OR_FAIL(session_, manager_->CatalogSession(client_, properties_)); + ICEBERG_UNWRAP_OR_FAIL( + auto child, + manager_->ContextualSession( + {.session_id = "tenant-a", + .credentials = {{AuthProperties::kCredential.key(), "child:child-secret"}}}, + session_)); + ASSERT_TRUE(test::WaitForSharedTimer()); + EXPECT_EQ(client_->requests, 2); + ICEBERG_UNWRAP_OR_FAIL(auto request, child->Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer initial"); +} + +INSTANTIATE_TEST_SUITE_P(ExchangeModes, OAuth2ManagerRefreshTest, + ::testing::Values(std::string(""), std::string("true"), + std::string("false")), + [](const ::testing::TestParamInfo& info) { + return info.param.empty() ? "Default" + : info.param == "true" ? "Enabled" + : "Disabled"; + }); + +class OAuth2ExchangeRefreshTest : public ::testing::Test { + protected: + void SetUp() override { + config_.now = [this] { return now_; }; + } + void ExpectRefreshes(int count) { + if (count == 0) { + EXPECT_CALL(executor_, Submit).Times(0); + return; + } + EXPECT_CALL(executor_, Submit) + .Times(count) + .WillRepeatedly([this](ExecutorTask task) -> Status { + queued_.at(submitted_++).set_value(std::move(task)); + return {}; + }); + } + + Status Start() { + ICEBERG_ASSIGN_OR_RAISE( + session_, internal::OAuth2Session::Make(token_, config_, client_, std::nullopt)); + return {}; + } + + void RunRefresh(size_t index = 0) { + auto task = queued_.at(index).get_future(); + ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); + std::move(task.get())(); + } + + MockExecutor executor_; + std::array, 2> queued_; + int submitted_ = 0; + std::shared_ptr client_ = std::make_shared(); + std::chrono::steady_clock::time_point now_ = std::chrono::steady_clock::now(); + internal::OAuth2Session::Config config_{ + .token_endpoint = "https://auth.example/token", + .scope = "catalog", + .optional_oauth_params = {{"audience", "catalog-service"}, + {"resource", "https://catalog.example"}}, + .exchange_enabled = true, + .refresh_executor = &executor_, + }; + OAuthTokenResponse token_{ + .access_token = "initial", + .token_type = "bearer", + .expires_in_secs = 1, + .issued_token_type = AuthProperties::kAccessTokenType, + }; + // Destroy the session before its borrowed executor and task promises. + std::shared_ptr session_; +}; + +TEST_F(OAuth2ExchangeRefreshTest, ExchangesCurrentTokenAndTypeOnEachRefresh) { + ExpectRefreshes(2); + // Even when a credential is available, exchange must keep the current subject. + config_.credential = "client:secret"; + client_->response = [&]() -> Result { + if (client_->requests == 1) { + return HttpResponse( + 200, nlohmann::json({{"access_token", "first"}, + {"token_type", "bearer"}, + {"issued_token_type", AuthProperties::kJwtTokenType}, + {"expires_in", 1}}) + .dump()); + } + return HttpResponse(200, R"({"access_token":"second","token_type":"bearer"})"); + }; + ASSERT_THAT(Start(), IsOk()); + + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + ICEBERG_UNWRAP_OR_FAIL(auto first_request, session_->Authenticate({})); + EXPECT_EQ(first_request.headers.at("Authorization"), "Bearer first"); + ASSERT_TRUE(session_->ExpiresAt().has_value()); + EXPECT_TRUE(session_->IsRefreshable()); + + ASSERT_NO_FATAL_FAILURE(RunRefresh(1)); + ASSERT_EQ(client_->recorded_requests.size(), 2); + for (size_t i = 0; i < client_->recorded_requests.size(); ++i) { + const auto& request = client_->recorded_requests[i]; + const std::string subject = i == 0 ? "initial" : "first"; + const std::unordered_map expected_form{ + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", subject}, + {"subject_token_type", + i == 0 ? AuthProperties::kAccessTokenType : AuthProperties::kJwtTokenType}, + {"scope", "catalog"}, + {"audience", "catalog-service"}, + {"resource", "https://catalog.example"}, + }; + EXPECT_EQ(request.path, config_.token_endpoint); + EXPECT_THAT(request.form, ::testing::ContainerEq(expected_form)); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer " + subject); + } + ICEBERG_UNWRAP_OR_FAIL(auto second_request, session_->Authenticate({})); + EXPECT_EQ(second_request.headers.at("Authorization"), "Bearer second"); + EXPECT_EQ(session_->OAuth2Info()->issued_token_type, AuthProperties::kAccessTokenType); + EXPECT_FALSE(session_->ExpiresAt().has_value()); + EXPECT_FALSE(session_->IsRefreshable()); + EXPECT_THAT(session_->Close(), IsOk()); +} + +TEST_F(OAuth2ExchangeRefreshTest, RetriesExchangeWithoutChangingIdentity) { + ExpectRefreshes(1); + client_->response = [&]() -> Result { + if (client_->requests == 1) { + throw std::runtime_error("HTTP library failure"); + } + if (client_->requests == 2) { + return IOError("temporarily unavailable"); + } + return HttpResponse(200, R"({"access_token":"fresh","token_type":"bearer"})"); + }; + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + EXPECT_EQ(client_->requests, 3); + EXPECT_THAT(fake_retry.observed_base_delays_ms(), ::testing::ElementsAre(100, 200)); + for (const auto& request : client_->recorded_requests) { + EXPECT_EQ(request.form.at("subject_token"), "initial"); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer initial"); + EXPECT_FALSE(request.form.contains("client_secret")); + } + EXPECT_EQ(session_->OAuth2Info()->token, "fresh"); + EXPECT_THAT(session_->Close(), IsOk()); +} + +class OAuth2ExpiredExchangeRefreshTest : public OAuth2ExchangeRefreshTest, + public ::testing::WithParamInterface { + protected: + HttpResponse ExpiredResponse() const { + return HttpResponse( + 200, nlohmann::json( + {{"access_token", GetParam() ? MakeJwt(R"({"exp":0})") : "expired"}, + {"token_type", "bearer"}, + {"issued_token_type", GetParam() ? AuthProperties::kJwtTokenType + : AuthProperties::kAccessTokenType}, + {"expires_in", GetParam() ? 3600 : 0}}) + .dump()); + } + + void ExpectInitialState( + std::optional expiration) { + EXPECT_EQ(session_->OAuth2Info()->token, token_.access_token); + EXPECT_EQ(session_->OAuth2Info()->issued_token_type, token_.issued_token_type); + EXPECT_EQ(session_->ExpiresAt(), expiration); + ICEBERG_UNWRAP_OR_FAIL(auto request, session_->Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer " + token_.access_token); + } + + void ExpectInitialExchangeRequests(bool final_recovery = false) { + for (size_t i = 0; i < client_->recorded_requests.size(); ++i) { + const auto& request = client_->recorded_requests[i]; + EXPECT_EQ(request.form.at("subject_token"), token_.access_token); + EXPECT_EQ(request.form.at("subject_token_type"), token_.issued_token_type); + EXPECT_EQ(request.headers.at("Authorization"), + final_recovery && i == 6 ? "Basic Y2xpZW50OnNlY3JldA==" + : "Bearer " + token_.access_token); + } + } +}; + +TEST_P(OAuth2ExpiredExchangeRefreshTest, RetriesWithPreviousTokenUntilSuccess) { + ExpectRefreshes(1); + ASSERT_THAT(Start(), IsOk()); + auto initial_expiration = session_->ExpiresAt(); + client_->response = [&]() -> Result { + if (client_->requests == 1) { + return ExpiredResponse(); + } + ExpectInitialState(initial_expiration); + const auto& request = client_->recorded_requests.back(); + if (request.form.at("subject_token") != token_.access_token || + request.headers.at("Authorization") != "Bearer " + token_.access_token) { + return IOError("authorization server rejects the expired token"); + } + return HttpResponse(200, R"({"access_token":"fresh","token_type":"bearer"})"); + }; + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + EXPECT_EQ(client_->requests, 2); + EXPECT_THAT(fake_retry.observed_base_delays_ms(), ::testing::ElementsAre(100)); + ExpectInitialExchangeRequests(); + ICEBERG_UNWRAP_OR_FAIL(auto request, session_->Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer fresh"); + EXPECT_EQ(session_->OAuth2Info()->issued_token_type, AuthProperties::kAccessTokenType); + EXPECT_FALSE(session_->ExpiresAt().has_value()); + EXPECT_FALSE(session_->IsRefreshable()); + EXPECT_THAT(session_->Close(), IsOk()); +} + +TEST_P(OAuth2ExpiredExchangeRefreshTest, KeepsPreviousTokenAfterRetriesExhausted) { + ExpectRefreshes(1); + config_.credential = "client:secret"; + ASSERT_THAT(Start(), IsOk()); + auto initial_expiration = session_->ExpiresAt(); + client_->response = [&]() -> Result { return ExpiredResponse(); }; + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + EXPECT_EQ(client_->requests, 7); + ExpectInitialExchangeRequests(true); + ExpectInitialState(initial_expiration); + EXPECT_FALSE(session_->IsRefreshable()); + EXPECT_THAT(session_->Close(), IsOk()); +} + +INSTANTIATE_TEST_SUITE_P(ExpirationSources, OAuth2ExpiredExchangeRefreshTest, + ::testing::Bool(), + [](const ::testing::TestParamInfo& info) { + return info.param ? "Jwt" : "ExpiresIn"; + }); + +TEST_F(OAuth2ExchangeRefreshTest, StopsAfterExchangeRetryBudget) { + ExpectRefreshes(1); + config_.credential = "client:secret"; + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + EXPECT_EQ(client_->requests, 7); + for (size_t i = 0; i < client_->recorded_requests.size(); ++i) { + const auto& request = client_->recorded_requests[i]; + EXPECT_EQ(request.headers.at("Authorization"), + i == 6 ? "Basic Y2xpZW50OnNlY3JldA==" : "Bearer initial"); + EXPECT_EQ(request.form.at("grant_type"), + "urn:ietf:params:oauth:grant-type:token-exchange"); + EXPECT_EQ(request.form.at("subject_token"), "initial"); + EXPECT_FALSE(request.form.contains("client_secret")); + } + EXPECT_EQ(session_->OAuth2Info()->token, "initial"); + EXPECT_FALSE(session_->IsRefreshable()); + EXPECT_THAT(session_->Close(), IsOk()); +} + +class OAuth2ExpiredCredentialTest + : public OAuth2ExchangeRefreshTest, + public ::testing::WithParamInterface> {}; + +TEST_P(OAuth2ExpiredCredentialTest, UsesOriginalCredentialForExpiredToken) { + ExpectRefreshes(1); + token_.expires_in_secs = 0; + config_.credential = GetParam().first; + client_->response = []() -> Result { + return HttpResponse(200, R"({"access_token":"fresh","token_type":"bearer"})"); + }; + ASSERT_THAT(Start(), IsOk()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + ASSERT_EQ(client_->recorded_requests.size(), 1); + const auto& request = client_->recorded_requests.front(); + EXPECT_EQ(request.headers.at("Authorization"), "Basic " + GetParam().second); + EXPECT_EQ(request.form.at("subject_token"), "initial"); + EXPECT_EQ(request.form.at("subject_token_type"), AuthProperties::kAccessTokenType); + EXPECT_FALSE(request.form.contains("actor_token")); + EXPECT_FALSE(request.form.contains("client_secret")); + EXPECT_EQ(session_->OAuth2Info()->credential, GetParam().first); + EXPECT_EQ(session_->OAuth2Info()->token, "fresh"); + EXPECT_FALSE(session_->IsRefreshable()); +} + +INSTANTIATE_TEST_SUITE_P( + CredentialForms, OAuth2ExpiredCredentialTest, + ::testing::Values(std::pair{"id:secret", "aWQ6c2VjcmV0"}, + std::pair{"secret", "c2VjcmV0"}, + std::pair{":secret", "OnNlY3JldA=="}, + std::pair{"id:secret:tail", + "aWQ6c2VjcmV0OnRhaWw="})); + +TEST_F(OAuth2ExchangeRefreshTest, SwitchesToBasicWhenTokenExpiresDuringRetries) { + ExpectRefreshes(1); + config_.credential = "client:secret"; + client_->response = [&]() -> Result { + if (client_->requests == 1) { + now_ += std::chrono::seconds(2); + return IOError("Bearer refresh failed"); + } + return HttpResponse(200, R"({"access_token":"fresh","token_type":"bearer"})"); + }; + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + ASSERT_EQ(client_->recorded_requests.size(), 2); + EXPECT_EQ(client_->recorded_requests[0].headers.at("Authorization"), "Bearer initial"); + EXPECT_EQ(client_->recorded_requests[1].headers.at("Authorization"), + "Basic Y2xpZW50OnNlY3JldA=="); + for (const auto& request : client_->recorded_requests) { + EXPECT_EQ(request.form.at("subject_token"), "initial"); + EXPECT_EQ(request.form.at("subject_token_type"), AuthProperties::kAccessTokenType); + } + EXPECT_EQ(session_->OAuth2Info()->token, "fresh"); +} + +TEST_F(OAuth2ExchangeRefreshTest, FinalRecoveryUsesOwnRequestTimeAndReschedules) { + ExpectRefreshes(2); + config_.credential = "client:secret"; + client_->response = [&]() -> Result { + if (client_->requests <= 6) { + if (client_->requests == 6) { + now_ += std::chrono::milliseconds(250); + } + return IOError("Bearer refresh failed"); + } + if (client_->requests == 7) { + return HttpResponse( + 200, R"({"access_token":"recovered","token_type":"bearer","expires_in":1})"); + } + return HttpResponse(200, R"({"access_token":"next","token_type":"bearer"})"); + }; + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + ASSERT_EQ(client_->recorded_requests.size(), 7); + for (size_t i = 0; i < 7; ++i) { + const auto& request = client_->recorded_requests[i]; + EXPECT_EQ(request.headers.at("Authorization"), + i == 6 ? "Basic Y2xpZW50OnNlY3JldA==" : "Bearer initial"); + EXPECT_EQ(request.form.at("subject_token"), "initial"); + EXPECT_FALSE(request.form.contains("actor_token")); + } + EXPECT_THAT(fake_retry.observed_base_delays_ms(), + ::testing::ElementsAre(100, 200, 400, 800, 1600)); + EXPECT_EQ(session_->ExpiresAt(), now_ + std::chrono::seconds(1)); + EXPECT_EQ(session_->OAuth2Info()->token, "recovered"); + EXPECT_TRUE(session_->IsRefreshable()); + ASSERT_NO_FATAL_FAILURE(RunRefresh(1)); + EXPECT_EQ(client_->recorded_requests.back().headers.at("Authorization"), + "Bearer recovered"); + EXPECT_EQ(client_->recorded_requests.back().form.at("subject_token"), "recovered"); + EXPECT_EQ(session_->OAuth2Info()->token, "next"); + EXPECT_FALSE(session_->IsRefreshable()); +} + +TEST_F(OAuth2ExchangeRefreshTest, ExpiredJwtSharesSevenAttemptBudget) { + ExpectRefreshes(1); + config_.credential = "client:secret"; + token_.access_token = MakeJwt(R"({"exp":0})"); + token_.issued_token_type = AuthProperties::kJwtTokenType; + token_.expires_in_secs = 3600; + ASSERT_THAT(Start(), IsOk()); + const auto expiration = session_->ExpiresAt(); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + ASSERT_EQ(client_->recorded_requests.size(), 7); + for (const auto& request : client_->recorded_requests) { + EXPECT_EQ(request.headers.at("Authorization"), "Basic Y2xpZW50OnNlY3JldA=="); + EXPECT_EQ(request.form.at("subject_token"), token_.access_token); + EXPECT_EQ(request.form.at("subject_token_type"), AuthProperties::kJwtTokenType); + } + EXPECT_EQ(session_->OAuth2Info()->token, token_.access_token); + EXPECT_EQ(session_->ExpiresAt(), expiration); + EXPECT_FALSE(session_->IsRefreshable()); +} + +class OAuth2MissingCredentialTest : public OAuth2ExchangeRefreshTest, + public ::testing::WithParamInterface {}; + +TEST_P(OAuth2MissingCredentialTest, DoesNotSendUnrecoverableRequest) { + ExpectRefreshes(1); + config_.exchange_enabled = GetParam(); + token_.expires_in_secs = 0; + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + EXPECT_EQ(client_->requests, 0); + EXPECT_EQ(session_->OAuth2Info()->token, "initial"); + EXPECT_FALSE(session_->IsRefreshable()); +} + +INSTANTIATE_TEST_SUITE_P(GrantModes, OAuth2MissingCredentialTest, ::testing::Bool()); + +TEST_F(OAuth2ExchangeRefreshTest, MissingCredentialSkipsFinalRecovery) { + ExpectRefreshes(1); + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + ASSERT_EQ(client_->recorded_requests.size(), 6); + for (const auto& request : client_->recorded_requests) { + EXPECT_EQ(request.headers.at("Authorization"), "Bearer initial"); + } + EXPECT_EQ(session_->OAuth2Info()->token, "initial"); + EXPECT_FALSE(session_->IsRefreshable()); +} + +TEST_F(OAuth2ExchangeRefreshTest, MissingCredentialStopsWhenTokenExpiresDuringRetries) { + ExpectRefreshes(1); + client_->response = [&]() -> Result { + now_ += std::chrono::seconds(2); + return IOError("Bearer refresh failed"); + }; + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + EXPECT_EQ(client_->requests, 1); + EXPECT_EQ(client_->recorded_requests.front().headers.at("Authorization"), + "Bearer initial"); + EXPECT_EQ(session_->OAuth2Info()->token, "initial"); + EXPECT_FALSE(session_->IsRefreshable()); +} + +TEST_F(OAuth2ExchangeRefreshTest, StopBeforeFinalRecoverySkipsRequest) { + ExpectRefreshes(1); + config_.credential = "client:secret"; + client_->response = [&]() -> Result { + if (client_->requests == 6) { + session_->StopRefreshing(); + } + return IOError("refresh failed"); + }; + ASSERT_THAT(Start(), IsOk()); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + + EXPECT_EQ(client_->requests, 6); + EXPECT_EQ(session_->OAuth2Info()->token, "initial"); + EXPECT_FALSE(session_->IsRefreshable()); +} + +TEST_F(OAuth2ExchangeRefreshTest, CloseWaitsForFinalRecovery) { + ExpectRefreshes(1); + config_.credential = "client:secret"; + std::promise entered; + std::promise release; + auto entered_future = entered.get_future(); + auto release_future = release.get_future(); + client_->response = [&]() -> Result { + if (client_->requests == 7) { + entered.set_value(); + release_future.wait(); + return HttpResponse( + 200, R"({"access_token":"recovered","token_type":"bearer","expires_in":1})"); + } + return IOError("refresh failed"); + }; + ASSERT_THAT(Start(), IsOk()); + auto task = queued_[0].get_future(); + ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); + auto refresh = std::async(std::launch::async, [&] { + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + std::move(task.get())(); + }); + auto entered_status = entered_future.wait_for(std::chrono::seconds(5)); + if (entered_status != std::future_status::ready) { + release.set_value(); + } + ASSERT_EQ(entered_status, std::future_status::ready); + auto closing = std::async(std::launch::async, [&] { return session_->Close(); }); + EXPECT_EQ(closing.wait_for(std::chrono::milliseconds(30)), std::future_status::timeout); + release.set_value(); + refresh.get(); + EXPECT_THAT(closing.get(), IsOk()); + + EXPECT_EQ(client_->requests, 7); + EXPECT_EQ(session_->OAuth2Info()->token, "recovered"); + EXPECT_FALSE(session_->IsRefreshable()); +} + +TEST_F(OAuth2ExchangeRefreshTest, CloseSkipsQueuedExchange) { + ExpectRefreshes(1); + ASSERT_THAT(Start(), IsOk()); + auto task = queued_[0].get_future(); + ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); + EXPECT_THAT(session_->Close(), IsOk()); + std::move(task.get())(); + EXPECT_EQ(client_->requests, 0); +} + +TEST_F(OAuth2ExchangeRefreshTest, StopDuringExchangePreventsRescheduling) { + ExpectRefreshes(1); + client_->response = [&]() -> Result { + session_->StopRefreshing(); + return HttpResponse( + 200, R"({"access_token":"fresh","token_type":"bearer","expires_in":1})"); + }; + ASSERT_THAT(Start(), IsOk()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + EXPECT_EQ(session_->OAuth2Info()->token, "fresh"); + EXPECT_FALSE(session_->IsRefreshable()); + ASSERT_TRUE(test::WaitForSharedTimer(std::chrono::seconds(1))); + EXPECT_THAT(session_->Close(), IsOk()); +} + +TEST_F(OAuth2ExchangeRefreshTest, CloseWaitsForExchangeAndStopsRetries) { + ExpectRefreshes(1); + std::promise entered; + std::promise release; + auto entered_future = entered.get_future(); + auto release_future = release.get_future(); + client_->response = [&]() -> Result { + entered.set_value(); + release_future.wait(); + return IOError("exchange failed during Close"); + }; + ASSERT_THAT(Start(), IsOk()); + auto task = queued_[0].get_future(); + ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); + auto refresh = std::async(std::launch::async, [&] { std::move(task.get())(); }); + auto entered_status = entered_future.wait_for(std::chrono::seconds(5)); + if (entered_status != std::future_status::ready) { + release.set_value(); + } + ASSERT_EQ(entered_status, std::future_status::ready); + + auto closing = std::async(std::launch::async, [&] { return session_->Close(); }); + EXPECT_EQ(closing.wait_for(std::chrono::milliseconds(30)), std::future_status::timeout); + release.set_value(); + refresh.get(); + EXPECT_THAT(closing.get(), IsOk()); + EXPECT_EQ(client_->requests, 1); + EXPECT_FALSE(session_->IsRefreshable()); +} + +TEST_F(OAuth2ExchangeRefreshTest, DoesNotScheduleWithoutExpirationOrExecutor) { + ExpectRefreshes(0); + token_.expires_in_secs = std::nullopt; + ASSERT_THAT(Start(), IsOk()); + EXPECT_FALSE(session_->IsRefreshable()); + EXPECT_THAT(session_->Close(), IsOk()); + + token_.expires_in_secs = 0; + config_.refresh_executor = nullptr; + ASSERT_THAT(Start(), IsOk()); + EXPECT_FALSE(session_->IsRefreshable()); + ASSERT_TRUE(test::WaitForSharedTimer()); + EXPECT_EQ(client_->requests, 0); } TEST(OAuth2SessionTest, AlreadyExpiredRefreshedTokenBacksOff) { @@ -863,7 +1631,13 @@ TEST(OAuth2SessionTest, StopsAfterHttpRetryBudget) { test::FakeRetryEnvironment fake_retry; ScopedRetryTestHooks hooks(fake_retry.hooks()); std::move(task.get())(); - EXPECT_EQ(client->requests, 6); + EXPECT_EQ(client->requests, 7); + for (const auto& request : client->recorded_requests) { + EXPECT_EQ(request.form.at("grant_type"), "client_credentials"); + EXPECT_EQ(request.form.at("client_id"), "client"); + EXPECT_EQ(request.form.at("client_secret"), "secret"); + EXPECT_FALSE(request.headers.contains("Authorization")); + } EXPECT_THAT(fake_retry.observed_base_delays_ms(), ::testing::ElementsAre(100, 200, 400, 800, 1600)); // No further refresh is scheduled, so the session no longer counts as refreshable. @@ -885,8 +1659,7 @@ TEST(OAuth2SessionTest, StopRefreshingKeepsSessionUsable) { auto session, internal::OAuth2Session::Make(token_response, {.token_endpoint = "http://localhost/oauth/tokens", - .client_id = "client_id", - .client_secret = "client_secret", + .credential = "client_id:client_secret", .scope = "catalog", .refresh_executor = &executor}, std::make_shared(), std::nullopt)); diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index 6cc653131e..dd5f2fca02 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -26,13 +26,18 @@ #include #include #include +#include +#include +#include #include #include #include +#include #include #include #include +#include #ifndef _WIN32 # include @@ -45,8 +50,11 @@ #endif #include "iceberg/catalog/rest/auth/auth_session.h" +#include "iceberg/catalog/rest/auth/auth_session_internal.h" #include "iceberg/catalog/rest/error_handlers.h" +#include "iceberg/test/executor.h" #include "iceberg/test/matchers.h" +#include "iceberg/util/url_encoder.h" namespace iceberg::rest { @@ -55,10 +63,16 @@ namespace iceberg::rest { namespace { /// \brief Minimal HTTP/1.1 server on 127.0.0.1 that answers every request with -/// a fixed response and closes the connection. +/// a response from the handler and closes the connection. class LoopbackHttpServer { public: - explicit LoopbackHttpServer(std::string response) : response_(std::move(response)) {} + using Handler = std::function; + + explicit LoopbackHttpServer(Handler handler) : handler_(std::move(handler)) {} + + explicit LoopbackHttpServer(std::string response) + : LoopbackHttpServer( + [response = std::move(response)](const std::string&) { return response; }) {} ~LoopbackHttpServer() { stop_ = true; @@ -116,16 +130,16 @@ class LoopbackHttpServer { if (conn < 0) { continue; } - ReadRequest(conn); + auto request = ReadRequest(conn); ++request_count_; - SendAll(conn, response_); + SendAll(conn, handler_(request)); close(conn); } } /// \brief Consume the request headers and any Content-Length body so the /// client does not see a connection reset before it reads the response. - static void ReadRequest(int conn) { + static std::string ReadRequest(int conn) { timeval timeout{.tv_sec = 10, .tv_usec = 0}; setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); setsockopt(conn, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); @@ -137,7 +151,7 @@ class LoopbackHttpServer { while (true) { ssize_t n = recv(conn, buf.data(), buf.size(), 0); if (n <= 0) { - return; + return request; } request.append(buf.data(), static_cast(n)); if (header_end == std::string::npos) { @@ -148,7 +162,7 @@ class LoopbackHttpServer { content_length = ParseContentLength(request.substr(0, header_end)); } if (request.size() >= header_end + 4 + content_length) { - return; + return request; } } } @@ -183,7 +197,7 @@ class LoopbackHttpServer { } } - std::string response_; + Handler handler_; int listen_fd_ = -1; int port_ = 0; std::atomic stop_{false}; @@ -249,8 +263,228 @@ void ServeOnce(int listener, std::string_view response, bool end) { ::close(connection); } +HttpHeaders RequestHeaders(const std::string& request) { + HttpHeaders headers; + const auto header_end = request.find("\r\n\r\n"); + if (header_end == std::string::npos) return headers; + auto start = request.find("\r\n") + 2; + while (start < header_end) { + auto end = request.find("\r\n", start); + auto separator = request.find(':', start); + if (separator < end) { + auto value_start = request.find_first_not_of(" \t", separator + 1); + headers.emplace(request.substr(start, separator - start), + request.substr(value_start, end - value_start)); + } + start = end + 2; + } + return headers; +} + +std::unordered_map RequestForm(const std::string& request) { + std::unordered_map form; + auto header_end = request.find("\r\n\r\n"); + if (header_end == std::string::npos) return form; + auto body = request.substr(header_end + 4); + size_t start = 0; + while (start < body.size()) { + auto end = body.find('&', start); + auto field = body.substr(start, end == std::string::npos ? end : end - start); + auto separator = field.find('='); + if (separator != std::string::npos) { + form.emplace(UrlEncoder::Decode(field.substr(0, separator)), + UrlEncoder::Decode(field.substr(separator + 1))); + } + if (end == std::string::npos) break; + start = end + 1; + } + return form; +} + +std::string JsonResponse(int status, const nlohmann::json& json) { + auto body = json.dump(); + return std::format( + "HTTP/1.1 {} Response\r\nContent-Type: application/json\r\n" + "Content-Length: {}\r\nConnection: close\r\n\r\n{}", + status, body.size(), body); +} + } // namespace +TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { + using auth::AuthProperties; + using auth::internal::OAuth2Session; + const std::array tokens = {"Initial+/=", "First+/=", "Second+/="}; + const std::array types = {AuthProperties::kAccessTokenType, + AuthProperties::kJwtTokenType, + AuthProperties::kAccessTokenType}; + // The server alone owns generation; the client must follow the returned token/type. + size_t generation = 0; + LoopbackHttpServer server([&](const std::string& request) { + const auto headers = RequestHeaders(request); + const bool authenticated = + headers.contains("Authorization") && + headers.at("Authorization") == "Bearer " + tokens.at(generation); + if (request.starts_with("GET /protected ")) { + return authenticated ? JsonResponse(200, {{"ok", true}}) + : JsonResponse(401, {{"error", + {{"code", 401}, + {"type", "NotAuthorizedException"}, + {"message", "stale token"}}}}); + } + + const auto form = RequestForm(request); + const std::unordered_map expected_form{ + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", tokens.at(generation)}, + {"subject_token_type", types.at(generation)}, + {"scope", "catalog"}, + }; + if (!request.starts_with("POST /v1/oauth/tokens ") || !authenticated || + (headers.find("Content-Type") == headers.end() || + headers.at("Content-Type") != "application/x-www-form-urlencoded") || + form != expected_form || generation >= 2) { + return JsonResponse(400, {{"error", "invalid_request"}}); + } + ++generation; + nlohmann::json response{{"access_token", tokens[generation]}, + {"token_type", "bearer"}, + {"issued_token_type", types[generation]}}; + if (generation == 1) response["expires_in"] = 1; + return JsonResponse(200, response); + }); + ASSERT_TRUE(server.Start()); + + std::array, 2> completed; + test::ThreadExecutor workers; + test::MockExecutor executor; + size_t submitted = 0; + EXPECT_CALL(executor, Submit).Times(2).WillRepeatedly([&](ExecutorTask task) -> Status { + auto& done = completed.at(submitted++); + return workers.Submit(ExecutorTask([task = std::move(task), &done]() mutable { + std::move(task)(); + done.set_value(); + })); + }); + auto client = std::make_shared( + std::unordered_map{ + {"authorization", "Bearer stale-default"}}, + std::chrono::seconds(2), std::chrono::seconds(2)); + ICEBERG_UNWRAP_OR_FAIL( + auto session, OAuth2Session::Make({.access_token = tokens[0], + .token_type = "bearer", + .expires_in_secs = 1, + .issued_token_type = types[0]}, + {.token_endpoint = server.url("/v1/oauth/tokens"), + .scope = "catalog", + .exchange_enabled = true, + .refresh_executor = &executor}, + client, std::nullopt)); + for (auto& done : completed) { + ASSERT_EQ(done.get_future().wait_for(std::chrono::seconds(10)), + std::future_status::ready); + } + EXPECT_FALSE(session->IsRefreshable()); + + // A real resource request accepts the refreshed session and rejects the old token. + HttpClient resource_client({}, std::chrono::seconds(2), std::chrono::seconds(2)); + const auto& handler = *DefaultErrorHandler::Instance(); + EXPECT_THAT(resource_client.Get(server.url("/protected"), {}, {}, handler, *session), + IsOk()); + auto old_session = + auth::AuthSession::MakeDefault({{"Authorization", "Bearer " + tokens[0]}}); + EXPECT_THAT( + resource_client.Get(server.url("/protected"), {}, {}, handler, *old_session), + IsError(ErrorKind::kNotAuthorized)); + EXPECT_THAT(session->Close(), IsOk()); + EXPECT_EQ(server.request_count(), 4); +} + +class OAuth2HttpRecoveryTest : public ::testing::TestWithParam {}; + +TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenAndAuthenticatesResourceOverHttp) { + using auth::AuthProperties; + using auth::internal::OAuth2Session; + const bool exchange_enabled = GetParam(); + const std::string initial_token = "Expired+/="; + const std::string refreshed_token = "Recovered+/="; + LoopbackHttpServer server([&](const std::string& request) { + const auto headers = RequestHeaders(request); + if (request.starts_with("GET /protected ")) { + if (headers.contains("Authorization") && + headers.at("Authorization") == "Bearer " + refreshed_token) { + return JsonResponse(200, {{"ok", true}}); + } + return JsonResponse(401, {{"error", + {{"code", 401}, + {"type", "NotAuthorizedException"}, + {"message", "wrong token"}}}}); + } + std::unordered_map expected_form{{"scope", "catalog"}}; + bool valid_auth; + if (exchange_enabled) { + expected_form.insert({ + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", initial_token}, + {"subject_token_type", AuthProperties::kAccessTokenType}, + }); + valid_auth = headers.contains("Authorization") && + headers.at("Authorization") == "Basic OlNlQ3JFdDpUYWls"; + } else { + expected_form.insert( + {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); + valid_auth = !headers.contains("Authorization"); + } + if (!request.starts_with("POST /v1/oauth/tokens ") || !valid_auth || + RequestForm(request) != expected_form || !headers.contains("X-Trace") || + headers.at("X-Trace") != "KeepThisValue" || !headers.contains("Content-Type") || + headers.at("Content-Type") != "application/x-www-form-urlencoded") { + return JsonResponse(400, {{"error", "invalid_request"}}); + } + return JsonResponse(200, + {{"access_token", refreshed_token}, {"token_type", "bearer"}}); + }); + ASSERT_TRUE(server.Start()); + + test::MockExecutor executor; + std::promise queued; + auto ready = queued.get_future(); + EXPECT_CALL(executor, Submit).WillOnce([&](ExecutorTask task) -> Status { + queued.set_value(std::move(task)); + return {}; + }); + auto client = std::make_shared( + std::unordered_map{ + {"authorization", "Bearer StaleDefault"}, {"X-Trace", "KeepThisValue"}}, + std::chrono::seconds(2), std::chrono::seconds(2)); + ICEBERG_UNWRAP_OR_FAIL( + auto session, + OAuth2Session::Make( + {.access_token = initial_token, .token_type = "bearer", .expires_in_secs = 0}, + {.token_endpoint = server.url("/v1/oauth/tokens"), + .credential = ":SeCrEt:Tail", + .scope = "catalog", + .exchange_enabled = exchange_enabled, + .refresh_executor = &executor}, + client, std::nullopt)); + ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); + std::move(ready.get())(); + EXPECT_EQ(session->OAuth2Info()->token, refreshed_token); + EXPECT_FALSE(session->IsRefreshable()); + + HttpClient resource_client({}, std::chrono::seconds(2), std::chrono::seconds(2)); + EXPECT_THAT(resource_client.Get(server.url("/protected"), {}, {}, + *DefaultErrorHandler::Instance(), *session), + IsOk()); + EXPECT_THAT(session->Close(), IsOk()); + EXPECT_EQ(server.request_count(), 2); +} + +INSTANTIATE_TEST_SUITE_P(ExchangeModes, OAuth2HttpRecoveryTest, ::testing::Bool(), + [](const ::testing::TestParamInfo& info) { + return info.param ? "Exchange" : "ClientCredentials"; + }); + TEST(HttpClientTest, GetFailsWhenItsTransferFails) { // A 200 arriving first does not make a truncated response a success. constexpr std::string_view kTruncated = diff --git a/src/iceberg/test/rest_catalog_unit_test.cc b/src/iceberg/test/rest_catalog_unit_test.cc index 1b162763b4..640530128d 100644 --- a/src/iceberg/test/rest_catalog_unit_test.cc +++ b/src/iceberg/test/rest_catalog_unit_test.cc @@ -54,6 +54,7 @@ #include "iceberg/table_update.h" #include "iceberg/test/executor.h" #include "iceberg/test/matchers.h" +#include "iceberg/test/retry.h" namespace iceberg::rest { @@ -669,6 +670,190 @@ class RestCatalogOAuth2UriTest : public RestCatalogLoadTableTest { int factory_calls_ = 0; }; +struct OAuth2RefreshModeCase { + std::string name; + std::string client; + std::string server_default; + std::string server_override; + bool exchange_enabled; +}; + +class RestCatalogOAuth2RefreshTest + : public RestCatalogOAuth2UriTest, + public ::testing::WithParamInterface {}; + +TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRequests) { + const auto& mode = GetParam(); + properties_["token-refresh-enabled"] = "true"; + if (!mode.client.empty()) { + properties_["token-exchange-enabled"] = mode.client; + } + CatalogConfig server_config; + if (!mode.server_default.empty()) { + server_config.defaults["token-exchange-enabled"] = mode.server_default; + } + if (!mode.server_override.empty()) { + server_config.overrides["token-exchange-enabled"] = mode.server_override; + } + ExpectInit(server_config, + R"({"access_token":"initial","token_type":"bearer","expires_in":1})"); + + std::unordered_map expected_form; + std::unordered_map expected_headers; + if (mode.exchange_enabled) { + expected_form = { + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", "initial"}, + {"subject_token_type", auth::AuthProperties::kAccessTokenType}, + {"scope", "catalog"}, + }; + expected_headers = {{"Authorization", "Bearer initial"}}; + } else { + expected_form = {{"grant_type", "client_credentials"}, + {"client_id", "client"}, + {"client_secret", "secret"}, + {"scope", "catalog"}}; + } + EXPECT_CALL(*mock_client_, PostForm("https://auth.example/token/", _, _, _, _)) + .WillOnce([&](const std::string&, const auto& form, const auto& headers, + const ErrorHandler&, auth::AuthSession&) -> Result { + EXPECT_THAT(form, ::testing::ContainerEq(expected_form)); + EXPECT_THAT(headers, ::testing::ContainerEq(expected_headers)); + return HttpResponse(200, R"({"access_token":"refreshed","token_type":"bearer"})"); + }); + + test::MockExecutor executor; + std::promise queued; + auto ready = queued.get_future(); + EXPECT_CALL(executor, Submit).WillOnce([&](ExecutorTask task) -> Status { + queued.set_value(std::move(task)); + return {}; + }); + ICEBERG_UNWRAP_OR_FAIL(auto root, MakeOAuth2Catalog(&executor)); + ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); + std::move(ready.get())(); + + EXPECT_CALL(*mock_client_, Get("https://catalog.example/api/v1/namespaces", _, _, _, _)) + .WillOnce([](const std::string&, const auto&, const auto&, const ErrorHandler&, + auth::AuthSession& session) -> Result { + ICEBERG_ASSIGN_OR_RAISE(auto request, session.Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer refreshed"); + return HttpResponse(200, R"({"namespaces":[]})"); + }); + ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog()); + EXPECT_THAT(catalog->ListNamespaces({}), IsOk()); + EXPECT_EQ(factory_calls_, 2); +} + +INSTANTIATE_TEST_SUITE_P( + ExchangeModes, RestCatalogOAuth2RefreshTest, + ::testing::Values(OAuth2RefreshModeCase{"Default", "", "", "", true}, + OAuth2RefreshModeCase{"Enabled", "true", "", "", true}, + OAuth2RefreshModeCase{"Disabled", "false", "", "", false}, + OAuth2RefreshModeCase{"ServerDisables", "true", "", "false", false}, + OAuth2RefreshModeCase{"ServerEnables", "false", "", "true", true}, + OAuth2RefreshModeCase{"ServerDefaultDisables", "", "false", "", + false}, + OAuth2RefreshModeCase{"ClientOverridesServerDefault", "false", + "true", "", false}), + [](const ::testing::TestParamInfo& info) { + return info.param.name; + }); + +struct OAuth2RecoveryCase { + std::string name; + bool expired; + bool exchange_enabled; +}; + +class RestCatalogOAuth2RecoveryTest + : public RestCatalogOAuth2UriTest, + public ::testing::WithParamInterface {}; + +TEST_P(RestCatalogOAuth2RecoveryTest, RecoversAndUpdatesBusinessRequests) { + const auto& mode = GetParam(); + properties_["token-refresh-enabled"] = "true"; + properties_["token-exchange-enabled"] = mode.exchange_enabled ? "true" : "false"; + properties_["credential"] = ":SeCrEt:Tail"; + ExpectInit({}, nlohmann::json({{"access_token", "Initial"}, + {"token_type", "bearer"}, + {"expires_in", mode.expired ? 0 : 1}}) + .dump()); + + const int expected_requests = mode.expired ? 1 : 7; + int requests = 0; + EXPECT_CALL(*mock_client_, PostForm("https://auth.example/token/", _, _, _, _)) + .Times(expected_requests) + .WillRepeatedly([&](const std::string&, const auto& form, const auto& headers, + const ErrorHandler&, + auth::AuthSession& session) -> Result { + ++requests; + HttpRequest request{.headers = {{"authorization", "Bearer StaleDefault"}, + {"X-Trace", "KeepThisValue"}}}; + for (const auto& [key, value] : headers) { + request.headers.insert_or_assign(key, value); + } + ICEBERG_ASSIGN_OR_RAISE(auto authenticated, + session.Authenticate(std::move(request))); + std::unordered_map expected_form{{"scope", "catalog"}}; + if (mode.exchange_enabled) { + expected_form.insert({ + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", "Initial"}, + {"subject_token_type", auth::AuthProperties::kAccessTokenType}, + }); + EXPECT_EQ(authenticated.headers.at("Authorization"), + mode.expired || requests == 7 ? "Basic OlNlQ3JFdDpUYWls" + : "Bearer Initial"); + } else { + expected_form.insert( + {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); + EXPECT_FALSE(authenticated.headers.contains("Authorization")); + } + EXPECT_THAT(form, ::testing::ContainerEq(expected_form)); + EXPECT_EQ(authenticated.headers.at("X-Trace"), "KeepThisValue"); + if (requests < expected_requests) { + return IOError("ordinary refresh failed"); + } + return HttpResponse(200, R"({"access_token":"Recovered","token_type":"bearer"})"); + }); + + test::MockExecutor executor; + std::promise queued; + auto ready = queued.get_future(); + EXPECT_CALL(executor, Submit).WillOnce([&](ExecutorTask task) -> Status { + queued.set_value(std::move(task)); + return {}; + }); + ICEBERG_UNWRAP_OR_FAIL(auto root, MakeOAuth2Catalog(&executor)); + ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); + test::FakeRetryEnvironment retry_environment; + ScopedRetryTestHooks retry_hooks(retry_environment.hooks()); + std::move(ready.get())(); + EXPECT_EQ(requests, expected_requests); + + EXPECT_CALL(*mock_client_, Get("https://catalog.example/api/v1/namespaces", _, _, _, _)) + .WillOnce([](const std::string&, const auto&, const auto&, const ErrorHandler&, + auth::AuthSession& session) -> Result { + ICEBERG_ASSIGN_OR_RAISE(auto request, session.Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer Recovered"); + return HttpResponse(200, R"({"namespaces":[]})"); + }); + ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog()); + EXPECT_THAT(catalog->ListNamespaces({}), IsOk()); + EXPECT_EQ(factory_calls_, 2); +} + +INSTANTIATE_TEST_SUITE_P( + RecoveryModes, RestCatalogOAuth2RecoveryTest, + ::testing::Values(OAuth2RecoveryCase{"ExpiredExchange", true, true}, + OAuth2RecoveryCase{"FinalExchange", false, true}, + OAuth2RecoveryCase{"ExpiredClientCredentials", true, false}, + OAuth2RecoveryCase{"FinalClientCredentials", false, false}), + [](const ::testing::TestParamInfo& info) { + return info.param.name; + }); + TEST_F(RestCatalogOAuth2UriTest, TrustsServerOverridesByDefault) { ExpectInit({.overrides = {{"uri", "HTTPS://other-catalog.example/base/"}, {"oauth2-server-uri", "oauth/token///"}, From fa50779b960c0454962f893201ab65e8f1df3c67 Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Mon, 16 Nov 2026 11:47:27 +0800 Subject: [PATCH 02/10] test(rest): stabilize OAuth2 refresh tests and fix initializer lint --- src/iceberg/test/auth_manager_test.cc | 12 ++++++++--- src/iceberg/test/http_client_test.cc | 21 +++++++++++-------- src/iceberg/test/rest_catalog_unit_test.cc | 24 ++++++++++++++++------ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/iceberg/test/auth_manager_test.cc b/src/iceberg/test/auth_manager_test.cc index 5d4746aad8..1d9751541a 100644 --- a/src/iceberg/test/auth_manager_test.cc +++ b/src/iceberg/test/auth_manager_test.cc @@ -698,7 +698,8 @@ class RefreshHttpClient : public HttpClient { session.Authenticate({.method = HttpMethod::kPost, .url = path, .headers = {headers.begin(), headers.end()}})); - recorded_requests.push_back({path, form, std::move(authenticated.headers)}); + recorded_requests.push_back( + {.path = path, .form = form, .headers = std::move(authenticated.headers)}); ++requests; return response(); } @@ -824,8 +825,13 @@ class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { {"resource", "https://catalog.example"}, }; EXPECT_THAT(refresh.form, ::testing::ContainerEq(exchange_form)); - EXPECT_THAT(refresh.headers, ::testing::ElementsAre(::testing::Pair( - "Authorization", "Bearer initial"))); + // A delayed worker may correctly recover with the session's own credential. + EXPECT_THAT(refresh.headers, + ::testing::ElementsAre(::testing::Pair( + "Authorization", + ::testing::AnyOf( + "Bearer initial", + "Basic " + Base64::Encode(client_id + ":" + client_secret))))); } for (const auto& request : client_->recorded_requests) { EXPECT_EQ(request.path, properties_.at(AuthProperties::kOAuth2ServerUri.key())); diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index dd5f2fca02..09e6a494f7 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -371,15 +371,18 @@ TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { {"authorization", "Bearer stale-default"}}, std::chrono::seconds(2), std::chrono::seconds(2)); ICEBERG_UNWRAP_OR_FAIL( - auto session, OAuth2Session::Make({.access_token = tokens[0], - .token_type = "bearer", - .expires_in_secs = 1, - .issued_token_type = types[0]}, - {.token_endpoint = server.url("/v1/oauth/tokens"), - .scope = "catalog", - .exchange_enabled = true, - .refresh_executor = &executor}, - client, std::nullopt)); + auto session, OAuth2Session::Make( + {.access_token = tokens[0], + .token_type = "bearer", + .expires_in_secs = 1, + .issued_token_type = types[0]}, + {.token_endpoint = server.url("/v1/oauth/tokens"), + .scope = "catalog", + .exchange_enabled = true, + .refresh_executor = &executor, + // Worker latency must not expire the subject. + .now = [now = std::chrono::steady_clock::now()] { return now; }}, + client, std::nullopt)); for (auto& done : completed) { ASSERT_EQ(done.get_future().wait_for(std::chrono::seconds(10)), std::future_status::ready); diff --git a/src/iceberg/test/rest_catalog_unit_test.cc b/src/iceberg/test/rest_catalog_unit_test.cc index 640530128d..698e7938e1 100644 --- a/src/iceberg/test/rest_catalog_unit_test.cc +++ b/src/iceberg/test/rest_catalog_unit_test.cc @@ -699,7 +699,6 @@ TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRe R"({"access_token":"initial","token_type":"bearer","expires_in":1})"); std::unordered_map expected_form; - std::unordered_map expected_headers; if (mode.exchange_enabled) { expected_form = { {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, @@ -707,7 +706,6 @@ TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRe {"subject_token_type", auth::AuthProperties::kAccessTokenType}, {"scope", "catalog"}, }; - expected_headers = {{"Authorization", "Bearer initial"}}; } else { expected_form = {{"grant_type", "client_credentials"}, {"client_id", "client"}, @@ -718,7 +716,16 @@ TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRe .WillOnce([&](const std::string&, const auto& form, const auto& headers, const ErrorHandler&, auth::AuthSession&) -> Result { EXPECT_THAT(form, ::testing::ContainerEq(expected_form)); - EXPECT_THAT(headers, ::testing::ContainerEq(expected_headers)); + if (mode.exchange_enabled) { + // Config selection is unchanged if the queued refresh starts after expiry. + EXPECT_THAT( + headers, + ::testing::ElementsAre(::testing::Pair( + "Authorization", + ::testing::AnyOf("Bearer initial", "Basic Y2xpZW50OnNlY3JldA==")))); + } else { + EXPECT_TRUE(headers.empty()); + } return HttpResponse(200, R"({"access_token":"refreshed","token_type":"bearer"})"); }); @@ -802,9 +809,14 @@ TEST_P(RestCatalogOAuth2RecoveryTest, RecoversAndUpdatesBusinessRequests) { {"subject_token", "Initial"}, {"subject_token_type", auth::AuthProperties::kAccessTokenType}, }); - EXPECT_EQ(authenticated.headers.at("Authorization"), - mode.expired || requests == 7 ? "Basic OlNlQ3JFdDpUYWls" - : "Bearer Initial"); + if (mode.expired || requests == 7) { + EXPECT_EQ(authenticated.headers.at("Authorization"), + "Basic OlNlQ3JFdDpUYWls"); + } else { + // Earlier attempts can also use Basic if the queued token has expired. + EXPECT_THAT(authenticated.headers.at("Authorization"), + ::testing::AnyOf("Bearer Initial", "Basic OlNlQ3JFdDpUYWls")); + } } else { expected_form.insert( {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); From 84a7dca8d160fb6def414c24dc4f26503093ff49 Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Mon, 16 Nov 2026 19:40:32 +0800 Subject: [PATCH 03/10] fix(rest): preserve default auth on client credentials refresh --- src/iceberg/catalog/rest/auth/auth_session.h | 1 + .../catalog/rest/auth/auth_session_internal.h | 14 +----- src/iceberg/test/http_client_test.cc | 44 ++++++++++++++++++- src/iceberg/test/rest_catalog_unit_test.cc | 2 +- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/iceberg/catalog/rest/auth/auth_session.h b/src/iceberg/catalog/rest/auth/auth_session.h index b5dddcebd6..3b7fb35209 100644 --- a/src/iceberg/catalog/rest/auth/auth_session.h +++ b/src/iceberg/catalog/rest/auth/auth_session.h @@ -89,6 +89,7 @@ class ICEBERG_REST_EXPORT AuthSession { /// /// Refreshes expiring tokens in the background when keep_refreshed is true. /// This factory uses the client-credentials grant for refresh. + /// Refresh requests preserve the client's default Authorization header. /// Authenticate() uses the cached token without refreshing it. /// /// \param initial_token The initial token response from FetchToken(). diff --git a/src/iceberg/catalog/rest/auth/auth_session_internal.h b/src/iceberg/catalog/rest/auth/auth_session_internal.h index a912a264db..4dcc0cb235 100644 --- a/src/iceberg/catalog/rest/auth/auth_session_internal.h +++ b/src/iceberg/catalog/rest/auth/auth_session_internal.h @@ -275,23 +275,13 @@ class OAuth2Session final : public AuthSession, refresh_stopped_ = true; } - class ClientCredentialsSession final : public AuthSession { - public: - Result Authenticate(HttpRequest request) override { - // HttpClient merges its defaults before invoking the session. - request.headers.erase("Authorization"); - return request; - } - }; - Result FetchRefreshToken(const std::string& token, const std::string& issued_token_type, bool recover) { + auto empty_session = AuthSession::MakeDefault({}); if (!config_.exchange_enabled) { - ClientCredentialsSession session; - return OAuth2Util::FetchToken(*client_, session, refresh_properties_); + return OAuth2Util::FetchToken(*client_, *empty_session, refresh_properties_); } - auto empty_session = AuthSession::MakeDefault({}); auto headers = recover ? std::unordered_map{{"Authorization", diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index 09e6a494f7..bbe4021c51 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -403,6 +403,47 @@ TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { EXPECT_EQ(server.request_count(), 4); } +TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { + constexpr std::string_view kAuthorization = "Basic Z2F0ZXdheTpzZWNyZXQ="; + LoopbackHttpServer server([&](const std::string& request) { + EXPECT_TRUE(request.starts_with("POST /v1/oauth/tokens ")); + auto headers = RequestHeaders(request); + EXPECT_EQ(headers["Authorization"], kAuthorization); + const std::unordered_map expected_form{ + {"grant_type", "client_credentials"}, + {"client_id", "client"}, + {"client_secret", "secret"}, + {"scope", "catalog"}, + }; + EXPECT_EQ(RequestForm(request), expected_form); + return JsonResponse(200, {{"access_token", "refreshed"}, {"token_type", "bearer"}}); + }); + ASSERT_TRUE(server.Start()); + + test::MockExecutor executor; + std::promise queued; + auto ready = queued.get_future(); + EXPECT_CALL(executor, Submit).WillOnce([&](ExecutorTask task) -> Status { + queued.set_value(std::move(task)); + return {}; + }); + auto client = std::make_shared( + std::unordered_map{ + {"authorization", std::string(kAuthorization)}}, + std::chrono::seconds(2), std::chrono::seconds(2)); + ICEBERG_UNWRAP_OR_FAIL( + auto session, + auth::AuthSession::MakeOAuth2( + {.access_token = "initial", .token_type = "bearer", .expires_in_secs = 0}, + server.url("/v1/oauth/tokens"), "client", "secret", "catalog", + /*keep_refreshed=*/true, {}, client, &executor)); + ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); + std::move(ready.get())(); + + EXPECT_EQ(session->OAuth2Info()->token, "refreshed"); + EXPECT_EQ(server.request_count(), 1); +} + class OAuth2HttpRecoveryTest : public ::testing::TestWithParam {}; TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenAndAuthenticatesResourceOverHttp) { @@ -436,7 +477,8 @@ TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenAndAuthenticatesResourceOverH } else { expected_form.insert( {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); - valid_auth = !headers.contains("Authorization"); + valid_auth = headers.contains("Authorization") && + headers.at("Authorization") == "Bearer StaleDefault"; } if (!request.starts_with("POST /v1/oauth/tokens ") || !valid_auth || RequestForm(request) != expected_form || !headers.contains("X-Trace") || diff --git a/src/iceberg/test/rest_catalog_unit_test.cc b/src/iceberg/test/rest_catalog_unit_test.cc index 698e7938e1..c7922174d4 100644 --- a/src/iceberg/test/rest_catalog_unit_test.cc +++ b/src/iceberg/test/rest_catalog_unit_test.cc @@ -820,7 +820,7 @@ TEST_P(RestCatalogOAuth2RecoveryTest, RecoversAndUpdatesBusinessRequests) { } else { expected_form.insert( {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); - EXPECT_FALSE(authenticated.headers.contains("Authorization")); + EXPECT_EQ(authenticated.headers.at("Authorization"), "Bearer StaleDefault"); } EXPECT_THAT(form, ::testing::ContainerEq(expected_form)); EXPECT_EQ(authenticated.headers.at("X-Trace"), "KeepThisValue"); From a963f6206e4c1e25fbcc233d3a2b8ffaea51711c Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Fri, 27 Nov 2026 14:27:42 +0800 Subject: [PATCH 04/10] fix(rest): correct OAuth2 refresh authentication and refine recovery tests --- src/iceberg/catalog/rest/auth/auth_session.h | 3 +- .../catalog/rest/auth/auth_session_internal.h | 20 +-- src/iceberg/catalog/rest/http_client.cc | 10 ++ src/iceberg/catalog/rest/http_client.h | 3 + src/iceberg/test/auth_manager_test.cc | 117 ++++++++++-------- src/iceberg/test/http_client_test.cc | 7 +- src/iceberg/test/rest_catalog_unit_test.cc | 114 ++++++----------- src/iceberg/test/url_encoder_test.cc | 5 + src/iceberg/util/url_encoder.cc | 28 +++-- src/iceberg/util/url_encoder.h | 4 + 10 files changed, 162 insertions(+), 149 deletions(-) diff --git a/src/iceberg/catalog/rest/auth/auth_session.h b/src/iceberg/catalog/rest/auth/auth_session.h index 3b7fb35209..123401e161 100644 --- a/src/iceberg/catalog/rest/auth/auth_session.h +++ b/src/iceberg/catalog/rest/auth/auth_session.h @@ -89,7 +89,8 @@ class ICEBERG_REST_EXPORT AuthSession { /// /// Refreshes expiring tokens in the background when keep_refreshed is true. /// This factory uses the client-credentials grant for refresh. - /// Refresh requests preserve the client's default Authorization header. + /// Refresh requests preserve the client's default Authorization header, even when + /// both client_id and client_secret are empty. /// Authenticate() uses the cached token without refreshing it. /// /// \param initial_token The initial token response from FetchToken(). diff --git a/src/iceberg/catalog/rest/auth/auth_session_internal.h b/src/iceberg/catalog/rest/auth/auth_session_internal.h index 4dcc0cb235..40bfaa800c 100644 --- a/src/iceberg/catalog/rest/auth/auth_session_internal.h +++ b/src/iceberg/catalog/rest/auth/auth_session_internal.h @@ -41,6 +41,7 @@ #include "iceberg/util/macros.h" #include "iceberg/util/retry_util.h" #include "iceberg/util/scheduled_executor_internal.h" +#include "iceberg/util/url_encoder.h" namespace iceberg::rest::auth::internal { @@ -275,6 +276,14 @@ class OAuth2Session final : public AuthSession, refresh_stopped_ = true; } + std::unordered_map BasicAuthHeaders() const { + // RFC 6749 section 2.3.1 encodes each field before adding the Basic separator. + auto credential = + UrlEncoder::EncodeFormComponent(refresh_properties_.client_id()) + ":" + + UrlEncoder::EncodeFormComponent(refresh_properties_.client_secret()); + return {{"Authorization", "Basic " + Base64::Encode(credential)}}; + } + Result FetchRefreshToken(const std::string& token, const std::string& issued_token_type, bool recover) { @@ -282,13 +291,7 @@ class OAuth2Session final : public AuthSession, if (!config_.exchange_enabled) { return OAuth2Util::FetchToken(*client_, *empty_session, refresh_properties_); } - auto headers = - recover - ? std::unordered_map{{"Authorization", - "Basic " + - Base64::Encode( - config_.credential)}} - : OAuth2Util::AuthHeaders(token); + auto headers = recover ? BasicAuthHeaders() : OAuth2Util::AuthHeaders(token); // Explicit request headers override any default Authorization on the client. // Keep the current token as subject, without a parent actor. return OAuth2Util::ExchangeToken(*client_, *empty_session, headers, token, @@ -314,7 +317,8 @@ class OAuth2Session final : public AuthSession, recover = recover || (expires_at_ != std::chrono::steady_clock::time_point{} && expires_at_ <= config_.now()); } - if ((!config_.exchange_enabled || recover) && config_.credential.empty()) { + if (config_.credential.empty() && + (config_.exchange_enabled ? recover : !client_->HasDefaultAuthorization())) { ICEBERG_LOG_WARN("Stopping OAuth2 token refresh: no credential available"); return std::nullopt; } diff --git a/src/iceberg/catalog/rest/http_client.cc b/src/iceberg/catalog/rest/http_client.cc index 019f78209d..cd06b4a65a 100644 --- a/src/iceberg/catalog/rest/http_client.cc +++ b/src/iceberg/catalog/rest/http_client.cc @@ -29,6 +29,7 @@ #include "iceberg/catalog/rest/rest_util.h" #include "iceberg/result.h" #include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" namespace iceberg::rest { @@ -207,6 +208,15 @@ HttpClient::HttpClient(std::unordered_map default_head HttpClient::~HttpClient() = default; +bool HttpClient::HasDefaultAuthorization() const { + for (const auto& [name, value] : default_headers_) { + if (StringUtils::EqualsIgnoreCase(name, "Authorization")) { + return value.find_first_not_of(" \t") != std::string::npos; + } + } + return false; +} + Result HttpClient::RequestUri(const std::string& path) const { // Match Java HTTPRequest.check, then requestUri. URI syntax validation remains // the transport's responsibility rather than emulating java.net.URI. diff --git a/src/iceberg/catalog/rest/http_client.h b/src/iceberg/catalog/rest/http_client.h index 9a24593878..6c43ae4d29 100644 --- a/src/iceberg/catalog/rest/http_client.h +++ b/src/iceberg/catalog/rest/http_client.h @@ -84,6 +84,9 @@ class ICEBERG_REST_EXPORT HttpClient { HttpClient(HttpClient&&) = delete; HttpClient& operator=(HttpClient&&) = delete; + /// \brief Whether this client has a nonempty default Authorization header. + bool HasDefaultAuthorization() const; + /// \brief Sends a GET request. virtual Result Get( const std::string& path, const std::unordered_map& params, diff --git a/src/iceberg/test/auth_manager_test.cc b/src/iceberg/test/auth_manager_test.cc index 1d9751541a..f33793fdd1 100644 --- a/src/iceberg/test/auth_manager_test.cc +++ b/src/iceberg/test/auth_manager_test.cc @@ -683,6 +683,8 @@ namespace { class RefreshHttpClient : public HttpClient { public: + using HttpClient::HttpClient; + struct Request { std::string path; std::unordered_map form; @@ -1210,10 +1212,11 @@ class OAuth2ExpiredCredentialTest : public OAuth2ExchangeRefreshTest, public ::testing::WithParamInterface> {}; -TEST_P(OAuth2ExpiredCredentialTest, UsesOriginalCredentialForExpiredToken) { +TEST_P(OAuth2ExpiredCredentialTest, EncodesClientCredentialsForBasicAuth) { ExpectRefreshes(1); token_.expires_in_secs = 0; - config_.credential = GetParam().first; + const auto& [credential, expected_user_password] = GetParam(); + config_.credential = credential; client_->response = []() -> Result { return HttpResponse(200, R"({"access_token":"fresh","token_type":"bearer"})"); }; @@ -1222,23 +1225,27 @@ TEST_P(OAuth2ExpiredCredentialTest, UsesOriginalCredentialForExpiredToken) { ASSERT_EQ(client_->recorded_requests.size(), 1); const auto& request = client_->recorded_requests.front(); - EXPECT_EQ(request.headers.at("Authorization"), "Basic " + GetParam().second); + EXPECT_EQ(request.headers.at("Authorization"), + "Basic " + Base64::Encode(expected_user_password)); EXPECT_EQ(request.form.at("subject_token"), "initial"); EXPECT_EQ(request.form.at("subject_token_type"), AuthProperties::kAccessTokenType); EXPECT_FALSE(request.form.contains("actor_token")); EXPECT_FALSE(request.form.contains("client_secret")); - EXPECT_EQ(session_->OAuth2Info()->credential, GetParam().first); + EXPECT_EQ(session_->OAuth2Info()->credential, credential); EXPECT_EQ(session_->OAuth2Info()->token, "fresh"); EXPECT_FALSE(session_->IsRefreshable()); } INSTANTIATE_TEST_SUITE_P( CredentialForms, OAuth2ExpiredCredentialTest, - ::testing::Values(std::pair{"id:secret", "aWQ6c2VjcmV0"}, - std::pair{"secret", "c2VjcmV0"}, - std::pair{":secret", "OnNlY3JldA=="}, - std::pair{"id:secret:tail", - "aWQ6c2VjcmV0OnRhaWw="})); + ::testing::Values( + std::pair{"id:secret", "id:secret"}, + std::pair{"secret", ":secret"}, + std::pair{":secret", ":secret"}, + std::pair{"id:", "id:"}, + std::pair{"id:secret:tail", "id:secret%3Atail"}, + std::pair{"a+b:s+e cret", "a%2Bb:s%2Be+cret"}, + std::pair{"*~:%caf\xC3\xA9", "*%7E:%25caf%C3%A9"})); TEST_F(OAuth2ExchangeRefreshTest, SwitchesToBasicWhenTokenExpiresDuringRetries) { ExpectRefreshes(1); @@ -1350,8 +1357,25 @@ TEST_P(OAuth2MissingCredentialTest, DoesNotSendUnrecoverableRequest) { INSTANTIATE_TEST_SUITE_P(GrantModes, OAuth2MissingCredentialTest, ::testing::Bool()); +TEST_F(OAuth2ExchangeRefreshTest, PublicFactorySkipsRefreshWithEmptyAuthorization) { + ExpectRefreshes(1); + auto client = std::make_shared( + std::unordered_map{{"Authorization", ""}}); + ICEBERG_UNWRAP_OR_FAIL( + auto session, + AuthSession::MakeOAuth2(ExpiredInitialToken(), config_.token_endpoint, "", "", + "catalog", true, {}, client, &executor_)); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + ASSERT_NO_FATAL_FAILURE(RunRefresh()); + EXPECT_EQ(client->requests, 0); + EXPECT_EQ(session->OAuth2Info()->token, "initial"); +} + TEST_F(OAuth2ExchangeRefreshTest, MissingCredentialSkipsFinalRecovery) { ExpectRefreshes(1); + client_ = std::make_shared( + std::unordered_map{{"Authorization", "Bearer shared"}}); ASSERT_THAT(Start(), IsOk()); test::FakeRetryEnvironment fake_retry; ScopedRetryTestHooks hooks(fake_retry.hooks()); @@ -1442,16 +1466,6 @@ TEST_F(OAuth2ExchangeRefreshTest, CloseWaitsForFinalRecovery) { EXPECT_FALSE(session_->IsRefreshable()); } -TEST_F(OAuth2ExchangeRefreshTest, CloseSkipsQueuedExchange) { - ExpectRefreshes(1); - ASSERT_THAT(Start(), IsOk()); - auto task = queued_[0].get_future(); - ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); - EXPECT_THAT(session_->Close(), IsOk()); - std::move(task.get())(); - EXPECT_EQ(client_->requests, 0); -} - TEST_F(OAuth2ExchangeRefreshTest, StopDuringExchangePreventsRescheduling) { ExpectRefreshes(1); client_->response = [&]() -> Result { @@ -1467,36 +1481,6 @@ TEST_F(OAuth2ExchangeRefreshTest, StopDuringExchangePreventsRescheduling) { EXPECT_THAT(session_->Close(), IsOk()); } -TEST_F(OAuth2ExchangeRefreshTest, CloseWaitsForExchangeAndStopsRetries) { - ExpectRefreshes(1); - std::promise entered; - std::promise release; - auto entered_future = entered.get_future(); - auto release_future = release.get_future(); - client_->response = [&]() -> Result { - entered.set_value(); - release_future.wait(); - return IOError("exchange failed during Close"); - }; - ASSERT_THAT(Start(), IsOk()); - auto task = queued_[0].get_future(); - ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); - auto refresh = std::async(std::launch::async, [&] { std::move(task.get())(); }); - auto entered_status = entered_future.wait_for(std::chrono::seconds(5)); - if (entered_status != std::future_status::ready) { - release.set_value(); - } - ASSERT_EQ(entered_status, std::future_status::ready); - - auto closing = std::async(std::launch::async, [&] { return session_->Close(); }); - EXPECT_EQ(closing.wait_for(std::chrono::milliseconds(30)), std::future_status::timeout); - release.set_value(); - refresh.get(); - EXPECT_THAT(closing.get(), IsOk()); - EXPECT_EQ(client_->requests, 1); - EXPECT_FALSE(session_->IsRefreshable()); -} - TEST_F(OAuth2ExchangeRefreshTest, DoesNotScheduleWithoutExpirationOrExecutor) { ExpectRefreshes(0); token_.expires_in_secs = std::nullopt; @@ -1653,6 +1637,41 @@ TEST(OAuth2SessionTest, StopsAfterHttpRetryBudget) { ASSERT_THAT(session->Close(), IsOk()); } +TEST(OAuth2SessionTest, FinalClientCredentialsRecoveryUpdatesSession) { + MockExecutor executor; + std::promise queued; + auto task = queued.get_future(); + EXPECT_CALL(executor, Submit).WillOnce([&](ExecutorTask work) -> Status { + queued.set_value(std::move(work)); + return {}; + }); + auto client = std::make_shared(); + client->response = [&]() -> Result { + if (client->requests == 7) { + return HttpResponse(200, R"({"access_token":"recovered","token_type":"bearer"})"); + } + return IOError("ordinary refresh failed"); + }; + ICEBERG_UNWRAP_OR_FAIL( + auto session, + AuthSession::MakeOAuth2( + {.access_token = "initial", .token_type = "bearer", .expires_in_secs = 1}, + "https://auth.example/token", "client", "secret", "catalog", + /*keep_refreshed=*/true, {}, client, &executor)); + ASSERT_EQ(task.wait_for(std::chrono::seconds(5)), std::future_status::ready); + test::FakeRetryEnvironment fake_retry; + ScopedRetryTestHooks hooks(fake_retry.hooks()); + std::move(task.get())(); + + ASSERT_EQ(client->recorded_requests.size(), 7); + const auto& recovery = client->recorded_requests.back(); + EXPECT_EQ(recovery.form.at("grant_type"), "client_credentials"); + EXPECT_EQ(recovery.form.at("client_id"), "client"); + EXPECT_EQ(recovery.form.at("client_secret"), "secret"); + ICEBERG_UNWRAP_OR_FAIL(auto request, session->Authenticate({})); + EXPECT_EQ(request.headers.at("Authorization"), "Bearer recovered"); +} + TEST(OAuth2SessionTest, StopRefreshingKeepsSessionUsable) { MockExecutor executor; EXPECT_CALL(executor, Submit).Times(0); diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index bbe4021c51..0c5d9e1452 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -411,8 +411,7 @@ TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { EXPECT_EQ(headers["Authorization"], kAuthorization); const std::unordered_map expected_form{ {"grant_type", "client_credentials"}, - {"client_id", "client"}, - {"client_secret", "secret"}, + {"client_secret", ""}, {"scope", "catalog"}, }; EXPECT_EQ(RequestForm(request), expected_form); @@ -435,7 +434,7 @@ TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { auto session, auth::AuthSession::MakeOAuth2( {.access_token = "initial", .token_type = "bearer", .expires_in_secs = 0}, - server.url("/v1/oauth/tokens"), "client", "secret", "catalog", + server.url("/v1/oauth/tokens"), "", "", "catalog", /*keep_refreshed=*/true, {}, client, &executor)); ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); std::move(ready.get())(); @@ -473,7 +472,7 @@ TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenAndAuthenticatesResourceOverH {"subject_token_type", AuthProperties::kAccessTokenType}, }); valid_auth = headers.contains("Authorization") && - headers.at("Authorization") == "Basic OlNlQ3JFdDpUYWls"; + headers.at("Authorization") == "Basic OlNlQ3JFdCUzQVRhaWw="; } else { expected_form.insert( {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); diff --git a/src/iceberg/test/rest_catalog_unit_test.cc b/src/iceberg/test/rest_catalog_unit_test.cc index c7922174d4..7f820b1f4c 100644 --- a/src/iceberg/test/rest_catalog_unit_test.cc +++ b/src/iceberg/test/rest_catalog_unit_test.cc @@ -754,81 +754,48 @@ TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRe INSTANTIATE_TEST_SUITE_P( ExchangeModes, RestCatalogOAuth2RefreshTest, - ::testing::Values(OAuth2RefreshModeCase{"Default", "", "", "", true}, - OAuth2RefreshModeCase{"Enabled", "true", "", "", true}, - OAuth2RefreshModeCase{"Disabled", "false", "", "", false}, - OAuth2RefreshModeCase{"ServerDisables", "true", "", "false", false}, - OAuth2RefreshModeCase{"ServerEnables", "false", "", "true", true}, - OAuth2RefreshModeCase{"ServerDefaultDisables", "", "false", "", - false}, - OAuth2RefreshModeCase{"ClientOverridesServerDefault", "false", - "true", "", false}), + ::testing::Values( + OAuth2RefreshModeCase{"Default", "", "", "", true}, + OAuth2RefreshModeCase{"ServerDefaultDisables", "", "false", "", false}, + OAuth2RefreshModeCase{"ClientOverridesServerDefault", "false", "true", "", false}, + OAuth2RefreshModeCase{"ServerEnables", "false", "", "true", true}), [](const ::testing::TestParamInfo& info) { return info.param.name; }); -struct OAuth2RecoveryCase { - std::string name; - bool expired; - bool exchange_enabled; -}; - -class RestCatalogOAuth2RecoveryTest - : public RestCatalogOAuth2UriTest, - public ::testing::WithParamInterface {}; - -TEST_P(RestCatalogOAuth2RecoveryTest, RecoversAndUpdatesBusinessRequests) { - const auto& mode = GetParam(); +TEST_F(RestCatalogOAuth2UriTest, FinalExchangeUpdatesBusinessRequests) { properties_["token-refresh-enabled"] = "true"; - properties_["token-exchange-enabled"] = mode.exchange_enabled ? "true" : "false"; + properties_["token-exchange-enabled"] = "true"; properties_["credential"] = ":SeCrEt:Tail"; - ExpectInit({}, nlohmann::json({{"access_token", "Initial"}, - {"token_type", "bearer"}, - {"expires_in", mode.expired ? 0 : 1}}) - .dump()); + ExpectInit({}, R"({"access_token":"Initial","token_type":"bearer","expires_in":1})"); - const int expected_requests = mode.expired ? 1 : 7; - int requests = 0; - EXPECT_CALL(*mock_client_, PostForm("https://auth.example/token/", _, _, _, _)) - .Times(expected_requests) - .WillRepeatedly([&](const std::string&, const auto& form, const auto& headers, - const ErrorHandler&, - auth::AuthSession& session) -> Result { - ++requests; - HttpRequest request{.headers = {{"authorization", "Bearer StaleDefault"}, - {"X-Trace", "KeepThisValue"}}}; - for (const auto& [key, value] : headers) { - request.headers.insert_or_assign(key, value); - } - ICEBERG_ASSIGN_OR_RAISE(auto authenticated, - session.Authenticate(std::move(request))); - std::unordered_map expected_form{{"scope", "catalog"}}; - if (mode.exchange_enabled) { - expected_form.insert({ - {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, - {"subject_token", "Initial"}, - {"subject_token_type", auth::AuthProperties::kAccessTokenType}, - }); - if (mode.expired || requests == 7) { - EXPECT_EQ(authenticated.headers.at("Authorization"), - "Basic OlNlQ3JFdDpUYWls"); - } else { - // Earlier attempts can also use Basic if the queued token has expired. - EXPECT_THAT(authenticated.headers.at("Authorization"), - ::testing::AnyOf("Bearer Initial", "Basic OlNlQ3JFdDpUYWls")); - } - } else { - expected_form.insert( - {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); - EXPECT_EQ(authenticated.headers.at("Authorization"), "Bearer StaleDefault"); - } - EXPECT_THAT(form, ::testing::ContainerEq(expected_form)); - EXPECT_EQ(authenticated.headers.at("X-Trace"), "KeepThisValue"); - if (requests < expected_requests) { - return IOError("ordinary refresh failed"); - } - return HttpResponse(200, R"({"access_token":"Recovered","token_type":"bearer"})"); - }); + const std::unordered_map exchange_form{ + {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, + {"subject_token", "Initial"}, + {"subject_token_type", auth::AuthProperties::kAccessTokenType}, + {"scope", "catalog"}, + }; + const std::string basic_auth = "Basic OlNlQ3JFdCUzQVRhaWw="; + ::testing::Sequence attempts; + // A delayed refresh may already need Basic authentication before the final attempt. + EXPECT_CALL( + *mock_client_, + PostForm("https://auth.example/token/", exchange_form, + ::testing::ElementsAre(::testing::Pair( + "Authorization", ::testing::AnyOf("Bearer Initial", basic_auth))), + _, _)) + .Times(6) + .InSequence(attempts) + .WillRepeatedly( + []() -> Result { return IOError("ordinary refresh failed"); }); + EXPECT_CALL( + *mock_client_, + PostForm("https://auth.example/token/", exchange_form, + ::testing::ElementsAre(::testing::Pair("Authorization", basic_auth)), _, + _)) + .InSequence(attempts) + .WillOnce(Return( + HttpResponse(200, R"({"access_token":"Recovered","token_type":"bearer"})"))); test::MockExecutor executor; std::promise queued; @@ -842,7 +809,6 @@ TEST_P(RestCatalogOAuth2RecoveryTest, RecoversAndUpdatesBusinessRequests) { test::FakeRetryEnvironment retry_environment; ScopedRetryTestHooks retry_hooks(retry_environment.hooks()); std::move(ready.get())(); - EXPECT_EQ(requests, expected_requests); EXPECT_CALL(*mock_client_, Get("https://catalog.example/api/v1/namespaces", _, _, _, _)) .WillOnce([](const std::string&, const auto&, const auto&, const ErrorHandler&, @@ -856,16 +822,6 @@ TEST_P(RestCatalogOAuth2RecoveryTest, RecoversAndUpdatesBusinessRequests) { EXPECT_EQ(factory_calls_, 2); } -INSTANTIATE_TEST_SUITE_P( - RecoveryModes, RestCatalogOAuth2RecoveryTest, - ::testing::Values(OAuth2RecoveryCase{"ExpiredExchange", true, true}, - OAuth2RecoveryCase{"FinalExchange", false, true}, - OAuth2RecoveryCase{"ExpiredClientCredentials", true, false}, - OAuth2RecoveryCase{"FinalClientCredentials", false, false}), - [](const ::testing::TestParamInfo& info) { - return info.param.name; - }); - TEST_F(RestCatalogOAuth2UriTest, TrustsServerOverridesByDefault) { ExpectInit({.overrides = {{"uri", "HTTPS://other-catalog.example/base/"}, {"oauth2-server-uri", "oauth/token///"}, diff --git a/src/iceberg/test/url_encoder_test.cc b/src/iceberg/test/url_encoder_test.cc index 59f75a8e4f..c197df608e 100644 --- a/src/iceberg/test/url_encoder_test.cc +++ b/src/iceberg/test/url_encoder_test.cc @@ -44,6 +44,11 @@ TEST(UrlEncoderTest, Encode) { EXPECT_THAT(UrlEncoder::Encode(""), ::testing::Eq("")); } +TEST(UrlEncoderTest, EncodeFormComponent) { + EXPECT_EQ(UrlEncoder::Encode("a b+*~"), "a%20b%2B%2A~"); + EXPECT_EQ(UrlEncoder::EncodeFormComponent("a b+*~"), "a+b%2B*%7E"); +} + TEST(UrlEncoderTest, Decode) { // Decode percent-encoded strings EXPECT_THAT(UrlEncoder::Decode("hello%20world"), ::testing::Eq("hello world")); diff --git a/src/iceberg/util/url_encoder.cc b/src/iceberg/util/url_encoder.cc index 39247753cc..7cc9e0c14d 100644 --- a/src/iceberg/util/url_encoder.cc +++ b/src/iceberg/util/url_encoder.cc @@ -20,15 +20,17 @@ #include "iceberg/util/url_encoder.h" #include -#include namespace iceberg { namespace { -bool IsUnreserved(unsigned char c) { +enum class Encoding { kUrl, kForm }; + +bool IsUnreserved(unsigned char c, Encoding encoding) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || - c == '-' || c == '.' || c == '_' || c == '~'; + c == '-' || c == '.' || c == '_' || + c == (encoding == Encoding::kForm ? '*' : '~'); } // Helper: convert hex char to int (0–15), returns -1 if invalid @@ -39,17 +41,17 @@ constexpr int8_t FromHex(char c) { return -1; } -} // namespace - -std::string UrlEncoder::Encode(std::string_view str_to_encode) { - static const char* kHexChars = "0123456789ABCDEF"; +std::string EncodeComponent(std::string_view str_to_encode, Encoding encoding) { + static constexpr char kHexChars[] = "0123456789ABCDEF"; std::string result; result.reserve(str_to_encode.size() * 3 / 2 /* Heuristic reservation */); for (char c : str_to_encode) { auto b = static_cast(c); - if (IsUnreserved(b)) { + if (IsUnreserved(b, encoding)) { result += c; + } else if (encoding == Encoding::kForm && c == ' ') { + result += '+'; } else { result += '%'; result += kHexChars[b >> 4]; @@ -60,6 +62,16 @@ std::string UrlEncoder::Encode(std::string_view str_to_encode) { return result; } +} // namespace + +std::string UrlEncoder::Encode(std::string_view str_to_encode) { + return EncodeComponent(str_to_encode, Encoding::kUrl); +} + +std::string UrlEncoder::EncodeFormComponent(std::string_view value) { + return EncodeComponent(value, Encoding::kForm); +} + std::string UrlEncoder::Decode(std::string_view str_to_decode) { std::string result; result.reserve(str_to_decode.size()); diff --git a/src/iceberg/util/url_encoder.h b/src/iceberg/util/url_encoder.h index c6b8dd7077..82dd321f89 100644 --- a/src/iceberg/util/url_encoder.h +++ b/src/iceberg/util/url_encoder.h @@ -42,6 +42,10 @@ class ICEBERG_EXPORT UrlEncoder { /// \return The URL-encoded string. static std::string Encode(std::string_view str_to_encode); + /// \brief Encode a field name or value for application/x-www-form-urlencoded. + /// Spaces become '+', and literal '+' characters become "%2B". + static std::string EncodeFormComponent(std::string_view value); + /// \brief URL-decode a string. /// /// \details Decodes percent-encoded characters (e.g., "%20" -> space). From 8748b7c7ab91f35d6e1ce078415c759bdaf617eb Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Fri, 27 Nov 2026 14:30:21 +0800 Subject: [PATCH 05/10] fix(rest): form-encode OAuth requests and deduplicate refresh tests --- src/iceberg/catalog/rest/http_client.cc | 16 +++++++++------- src/iceberg/test/auth_manager_test.cc | 19 ++++--------------- src/iceberg/test/http_client_test.cc | 15 +++++++++++++-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/iceberg/catalog/rest/http_client.cc b/src/iceberg/catalog/rest/http_client.cc index cd06b4a65a..26e4b931a5 100644 --- a/src/iceberg/catalog/rest/http_client.cc +++ b/src/iceberg/catalog/rest/http_client.cc @@ -30,6 +30,7 @@ #include "iceberg/result.h" #include "iceberg/util/macros.h" #include "iceberg/util/string_util.h" +#include "iceberg/util/url_encoder.h" namespace iceberg::rest { @@ -283,14 +284,15 @@ Result HttpClient::PostForm( ICEBERG_ASSIGN_OR_RAISE(auto request_uri, RequestUri(path)); std::unordered_map form_headers(headers); form_headers.insert_or_assign(kHeaderContentType, kMimeTypeFormUrlEncoded); - std::vector pair_list; - pair_list.reserve(form_data.size()); - for (const auto& [key, val] : form_data) { - pair_list.emplace_back(key, val); + std::string encoded_body; + for (const auto& [key, value] : form_data) { + if (!encoded_body.empty()) { + encoded_body += '&'; + } + encoded_body += UrlEncoder::EncodeFormComponent(key); + encoded_body += '='; + encoded_body += UrlEncoder::EncodeFormComponent(value); } - // Sign the exact bytes cpr will put on the wire. - std::string encoded_body = - cpr::Payload(pair_list.begin(), pair_list.end()).GetContent(); ICEBERG_ASSIGN_OR_RAISE( auto authenticated, AuthenticateRequest(session, HttpMethod::kPost, std::move(request_uri), diff --git a/src/iceberg/test/auth_manager_test.cc b/src/iceberg/test/auth_manager_test.cc index f33793fdd1..4a203ce732 100644 --- a/src/iceberg/test/auth_manager_test.cc +++ b/src/iceberg/test/auth_manager_test.cc @@ -797,8 +797,8 @@ class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { std::move(task.get())(); } - void CheckRequests(const std::string& client_id, const std::string& client_secret, - const std::string& parent_token = "") { + void CheckChildRequests(const std::string& client_id, const std::string& client_secret, + const std::string& parent_token) { ASSERT_EQ(client_->recorded_requests.size(), 2); std::unordered_map credential_form{ {"grant_type", "client_credentials"}, {"client_id", client_id}, @@ -807,11 +807,7 @@ class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { }; const auto& initial = client_->recorded_requests[0]; EXPECT_THAT(initial.form, ::testing::ContainerEq(credential_form)); - if (parent_token.empty()) { - EXPECT_TRUE(initial.headers.empty()); - } else { - EXPECT_EQ(initial.headers.at("Authorization"), "Bearer " + parent_token); - } + EXPECT_EQ(initial.headers.at("Authorization"), "Bearer " + parent_token); const auto& refresh = client_->recorded_requests[1]; if (GetParam() == "false") { @@ -858,13 +854,6 @@ class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { std::shared_ptr session_; }; -TEST_P(OAuth2ManagerRefreshTest, CatalogRefreshUsesConfiguredGrant) { - ExpectRefresh(); - ICEBERG_UNWRAP_OR_FAIL(session_, manager_->CatalogSession(client_, properties_)); - ASSERT_NO_FATAL_FAILURE(RunRefresh()); - CheckRequests("catalog", "catalog-secret"); -} - TEST_P(OAuth2ManagerRefreshTest, CredentialContextInheritsCatalogRefreshGrant) { ExpectRefresh(); properties_[AuthProperties::kToken.key()] = "parent-token"; @@ -875,7 +864,7 @@ TEST_P(OAuth2ManagerRefreshTest, CredentialContextInheritsCatalogRefreshGrant) { }; ICEBERG_UNWRAP_OR_FAIL(session_, manager_->ContextualSession(context, parent)); ASSERT_NO_FATAL_FAILURE(RunRefresh()); - CheckRequests("child", "child-secret", "parent-token"); + CheckChildRequests("child", "child-secret", "parent-token"); ICEBERG_UNWRAP_OR_FAIL(auto cached, manager_->ContextualSession(context, parent)); EXPECT_EQ(cached, session_); } diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index 0c5d9e1452..cb973cc8ad 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -19,6 +19,7 @@ #include "iceberg/catalog/rest/http_client.h" +#include #include #include #include @@ -286,6 +287,7 @@ std::unordered_map RequestForm(const std::string& requ auto header_end = request.find("\r\n\r\n"); if (header_end == std::string::npos) return form; auto body = request.substr(header_end + 4); + std::replace(body.begin(), body.end(), '+', ' '); size_t start = 0; while (start < body.size()) { auto end = body.find('&', start); @@ -338,7 +340,7 @@ TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, {"subject_token", tokens.at(generation)}, {"subject_token_type", types.at(generation)}, - {"scope", "catalog"}, + {"scope", "catalog reports+read"}, }; if (!request.starts_with("POST /v1/oauth/tokens ") || !authenticated || (headers.find("Content-Type") == headers.end() || @@ -377,7 +379,7 @@ TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { .expires_in_secs = 1, .issued_token_type = types[0]}, {.token_endpoint = server.url("/v1/oauth/tokens"), - .scope = "catalog", + .scope = "catalog reports+read", .exchange_enabled = true, .refresh_executor = &executor, // Worker latency must not expire the subject. @@ -605,6 +607,15 @@ class CapturingAuthSession final : public auth::AuthSession { HttpRequest request_; }; +TEST(HttpClientTest, PostFormEncodesFieldNamesAndValuesBeforeAuthentication) { + HttpClient client; + CapturingAuthSession session; + auto result = client.PostForm("https://auth.example/token", {{"a b+c", "x y+z&="}}, {}, + *DefaultErrorHandler::Instance(), session); + EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument)); + EXPECT_EQ(session.request().body, "a+b%2Bc=x+y%2Bz%26%3D"); +} + TEST(HttpClientTest, BuildsRequestUriLikeJavaHttpRequest) { HttpClient client({}, {}, {}, std::string("https://catalog.example/api/")); CapturingAuthSession session; From 8c08fe16acd75e4242e22d822dfd7c1ecad942ec Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Fri, 27 Nov 2026 14:33:28 +0800 Subject: [PATCH 06/10] test(rest): verify the client refresh endpoint with both OAuth2 grants --- src/iceberg/test/rest_catalog_unit_test.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/iceberg/test/rest_catalog_unit_test.cc b/src/iceberg/test/rest_catalog_unit_test.cc index 5139d83ae9..925a32d3c5 100644 --- a/src/iceberg/test/rest_catalog_unit_test.cc +++ b/src/iceberg/test/rest_catalog_unit_test.cc @@ -800,7 +800,8 @@ TEST_F(RestCatalogOAuth2UriTest, FinalExchangeUpdatesBusinessRequests) { properties_["token-refresh-enabled"] = "true"; properties_["token-exchange-enabled"] = "true"; properties_["credential"] = ":SeCrEt:Tail"; - ExpectInit({}, R"({"access_token":"Initial","token_type":"bearer","expires_in":1})"); + ExpectInit({.overrides = {{"oauth2-server-uri", "https://server-auth.example/token"}}}, + R"({"access_token":"Initial","token_type":"bearer","expires_in":1})"); const std::unordered_map exchange_form{ {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, @@ -887,6 +888,7 @@ TEST_F(RestCatalogOAuth2UriTest, RefreshUsesTheClientEndpointByDefault) { return {}; }); properties_["token-refresh-enabled"] = "true"; + properties_["token-exchange-enabled"] = "false"; ExpectInit({.overrides = {{"uri", "https://other-catalog.example"}, {"oauth2-server-uri", "https://server-auth.example/token"}, {"rest.auth.oauth2.trust-server-uri", "true"}}}, From e306446f8847b0ef8adfd6b70a6d4fcf621cd979 Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Fri, 27 Nov 2026 16:01:30 +0800 Subject: [PATCH 07/10] fix(rest): delegate client credentials refresh to the HTTP client --- .../catalog/rest/auth/auth_session_internal.h | 3 +- src/iceberg/catalog/rest/http_client.cc | 10 ----- src/iceberg/catalog/rest/http_client.h | 3 -- src/iceberg/test/auth_manager_test.cc | 27 +----------- src/iceberg/test/http_client_test.cc | 43 +++++++++++++++---- 5 files changed, 37 insertions(+), 49 deletions(-) diff --git a/src/iceberg/catalog/rest/auth/auth_session_internal.h b/src/iceberg/catalog/rest/auth/auth_session_internal.h index 40bfaa800c..ec33e4b953 100644 --- a/src/iceberg/catalog/rest/auth/auth_session_internal.h +++ b/src/iceberg/catalog/rest/auth/auth_session_internal.h @@ -317,8 +317,7 @@ class OAuth2Session final : public AuthSession, recover = recover || (expires_at_ != std::chrono::steady_clock::time_point{} && expires_at_ <= config_.now()); } - if (config_.credential.empty() && - (config_.exchange_enabled ? recover : !client_->HasDefaultAuthorization())) { + if (config_.exchange_enabled && recover && config_.credential.empty()) { ICEBERG_LOG_WARN("Stopping OAuth2 token refresh: no credential available"); return std::nullopt; } diff --git a/src/iceberg/catalog/rest/http_client.cc b/src/iceberg/catalog/rest/http_client.cc index 4388a48715..7f25f17742 100644 --- a/src/iceberg/catalog/rest/http_client.cc +++ b/src/iceberg/catalog/rest/http_client.cc @@ -32,7 +32,6 @@ #include "iceberg/catalog/rest/rest_util.h" #include "iceberg/result.h" #include "iceberg/util/macros.h" -#include "iceberg/util/string_util.h" #include "iceberg/util/url_encoder.h" namespace iceberg::rest { @@ -262,15 +261,6 @@ HttpClient::HttpClient(std::unordered_map default_head HttpClient::~HttpClient() = default; -bool HttpClient::HasDefaultAuthorization() const { - for (const auto& [name, value] : default_headers_) { - if (StringUtils::EqualsIgnoreCase(name, "Authorization")) { - return value.find_first_not_of(" \t") != std::string::npos; - } - } - return false; -} - Result HttpClient::RequestUri(const std::string& path) const { // Match Java HTTPRequest.check, then requestUri. URI syntax validation remains // the transport's responsibility rather than emulating java.net.URI. diff --git a/src/iceberg/catalog/rest/http_client.h b/src/iceberg/catalog/rest/http_client.h index 9b2b1918fd..bc7e60a2a0 100644 --- a/src/iceberg/catalog/rest/http_client.h +++ b/src/iceberg/catalog/rest/http_client.h @@ -79,9 +79,6 @@ class ICEBERG_REST_EXPORT HttpClient { HttpClient(HttpClient&&) = delete; HttpClient& operator=(HttpClient&&) = delete; - /// \brief Whether this client has a nonempty default Authorization header. - bool HasDefaultAuthorization() const; - /// \brief Sends a GET request. virtual Result Get( const std::string& path, const std::unordered_map& params, diff --git a/src/iceberg/test/auth_manager_test.cc b/src/iceberg/test/auth_manager_test.cc index 4a203ce732..2a0ad0df51 100644 --- a/src/iceberg/test/auth_manager_test.cc +++ b/src/iceberg/test/auth_manager_test.cc @@ -683,8 +683,6 @@ namespace { class RefreshHttpClient : public HttpClient { public: - using HttpClient::HttpClient; - struct Request { std::string path; std::unordered_map form; @@ -1327,12 +1325,8 @@ TEST_F(OAuth2ExchangeRefreshTest, ExpiredJwtSharesSevenAttemptBudget) { EXPECT_FALSE(session_->IsRefreshable()); } -class OAuth2MissingCredentialTest : public OAuth2ExchangeRefreshTest, - public ::testing::WithParamInterface {}; - -TEST_P(OAuth2MissingCredentialTest, DoesNotSendUnrecoverableRequest) { +TEST_F(OAuth2ExchangeRefreshTest, MissingCredentialSkipsExpiredTokenRecovery) { ExpectRefreshes(1); - config_.exchange_enabled = GetParam(); token_.expires_in_secs = 0; ASSERT_THAT(Start(), IsOk()); test::FakeRetryEnvironment fake_retry; @@ -1344,27 +1338,8 @@ TEST_P(OAuth2MissingCredentialTest, DoesNotSendUnrecoverableRequest) { EXPECT_FALSE(session_->IsRefreshable()); } -INSTANTIATE_TEST_SUITE_P(GrantModes, OAuth2MissingCredentialTest, ::testing::Bool()); - -TEST_F(OAuth2ExchangeRefreshTest, PublicFactorySkipsRefreshWithEmptyAuthorization) { - ExpectRefreshes(1); - auto client = std::make_shared( - std::unordered_map{{"Authorization", ""}}); - ICEBERG_UNWRAP_OR_FAIL( - auto session, - AuthSession::MakeOAuth2(ExpiredInitialToken(), config_.token_endpoint, "", "", - "catalog", true, {}, client, &executor_)); - test::FakeRetryEnvironment fake_retry; - ScopedRetryTestHooks hooks(fake_retry.hooks()); - ASSERT_NO_FATAL_FAILURE(RunRefresh()); - EXPECT_EQ(client->requests, 0); - EXPECT_EQ(session->OAuth2Info()->token, "initial"); -} - TEST_F(OAuth2ExchangeRefreshTest, MissingCredentialSkipsFinalRecovery) { ExpectRefreshes(1); - client_ = std::make_shared( - std::unordered_map{{"Authorization", "Bearer shared"}}); ASSERT_THAT(Start(), IsOk()); test::FakeRetryEnvironment fake_retry; ScopedRetryTestHooks hooks(fake_retry.hooks()); diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index 02009503b8..37f011a946 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -405,12 +405,30 @@ TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { EXPECT_EQ(server.request_count(), 4); } -TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { - constexpr std::string_view kAuthorization = "Basic Z2F0ZXdheTpzZWNyZXQ="; - LoopbackHttpServer server([&](const std::string& request) { +namespace { + +constexpr char kGatewayAuthorization[] = "Basic Z2F0ZXdheTpzZWNyZXQ="; + +class GatewayHttpClient : public HttpClient { + public: + GatewayHttpClient() + : HttpClient({}, std::chrono::seconds(2), std::chrono::seconds(2)) {} + + Result PostForm( + const std::string& path, const std::unordered_map& form, + const std::unordered_map& headers, + const ErrorHandler& error_handler, auth::AuthSession& session) override { + auto gateway_headers = headers; + gateway_headers["Authorization"] = kGatewayAuthorization; + return HttpClient::PostForm(path, form, gateway_headers, error_handler, session); + } +}; + +void CheckPublicOAuth2FactoryRefresh(const std::shared_ptr& client) { + LoopbackHttpServer server([](const std::string& request) { EXPECT_TRUE(request.starts_with("POST /v1/oauth/tokens ")); auto headers = RequestHeaders(request); - EXPECT_EQ(headers["Authorization"], kAuthorization); + EXPECT_EQ(headers["Authorization"], kGatewayAuthorization); const std::unordered_map expected_form{ {"grant_type", "client_credentials"}, {"client_secret", ""}, @@ -428,10 +446,6 @@ TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { queued.set_value(std::move(task)); return {}; }); - auto client = std::make_shared( - std::unordered_map{ - {"authorization", std::string(kAuthorization)}}, - std::chrono::seconds(2), std::chrono::seconds(2)); ICEBERG_UNWRAP_OR_FAIL( auto session, auth::AuthSession::MakeOAuth2( @@ -445,6 +459,19 @@ TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { EXPECT_EQ(server.request_count(), 1); } +} // namespace + +TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { + CheckPublicOAuth2FactoryRefresh(std::make_shared( + std::unordered_map{ + {"authorization", kGatewayAuthorization}}, + std::chrono::seconds(2), std::chrono::seconds(2))); +} + +TEST(HttpClientTest, PublicOAuth2FactoryRefreshesWithAuthorizationAddedByPostForm) { + CheckPublicOAuth2FactoryRefresh(std::make_shared()); +} + class OAuth2HttpRecoveryTest : public ::testing::TestWithParam {}; TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenAndAuthenticatesResourceOverHttp) { From 81f5f53145b8568f4e5bbfc23a9effc190380031 Mon Sep 17 00:00:00 2001 From: "shuxu.li" Date: Fri, 27 Nov 2026 17:08:38 +0800 Subject: [PATCH 08/10] fix(rest): address clang-tidy warnings in form encoding and tests --- src/iceberg/test/http_client_test.cc | 6 +++--- src/iceberg/util/url_encoder.cc | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index 37f011a946..ffcea577a2 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -287,7 +287,7 @@ std::unordered_map RequestForm(const std::string& requ auto header_end = request.find("\r\n\r\n"); if (header_end == std::string::npos) return form; auto body = request.substr(header_end + 4); - std::replace(body.begin(), body.end(), '+', ' '); + std::ranges::replace(body, '+', ' '); size_t start = 0; while (start < body.size()) { auto end = body.find('&', start); @@ -407,7 +407,7 @@ TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { namespace { -constexpr char kGatewayAuthorization[] = "Basic Z2F0ZXdheTpzZWNyZXQ="; +constexpr std::string_view kGatewayAuthorization = "Basic Z2F0ZXdheTpzZWNyZXQ="; class GatewayHttpClient : public HttpClient { public: @@ -464,7 +464,7 @@ void CheckPublicOAuth2FactoryRefresh(const std::shared_ptr& client) TEST(HttpClientTest, PublicOAuth2FactoryPreservesAuthorizationOnRefresh) { CheckPublicOAuth2FactoryRefresh(std::make_shared( std::unordered_map{ - {"authorization", kGatewayAuthorization}}, + {"authorization", std::string(kGatewayAuthorization)}}, std::chrono::seconds(2), std::chrono::seconds(2))); } diff --git a/src/iceberg/util/url_encoder.cc b/src/iceberg/util/url_encoder.cc index 7cc9e0c14d..68574db85e 100644 --- a/src/iceberg/util/url_encoder.cc +++ b/src/iceberg/util/url_encoder.cc @@ -42,7 +42,7 @@ constexpr int8_t FromHex(char c) { } std::string EncodeComponent(std::string_view str_to_encode, Encoding encoding) { - static constexpr char kHexChars[] = "0123456789ABCDEF"; + static constexpr std::string_view kHexChars = "0123456789ABCDEF"; std::string result; result.reserve(str_to_encode.size() * 3 / 2 /* Heuristic reservation */); From 04a01722822c254809e7cb66d414d54a54e23623 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Tue, 1 Dec 2026 23:39:40 +0800 Subject: [PATCH 09/10] fix(rest): reject non-bearer tokens during OAuth2 exchange refresh Preserve the current session token when an exchange returns a token that cannot authenticate catalog requests. Cover retry and recovery for this case, verify rescheduling from JWT expiry, and trim duplicate refresh tests. --- .../catalog/rest/auth/auth_session_internal.h | 19 +- src/iceberg/test/auth_manager_test.cc | 177 +++++++++--------- src/iceberg/test/http_client_test.cc | 139 +++++--------- src/iceberg/test/rest_catalog_unit_test.cc | 56 ++---- 4 files changed, 166 insertions(+), 225 deletions(-) diff --git a/src/iceberg/catalog/rest/auth/auth_session_internal.h b/src/iceberg/catalog/rest/auth/auth_session_internal.h index ec33e4b953..9a00ce03d1 100644 --- a/src/iceberg/catalog/rest/auth/auth_session_internal.h +++ b/src/iceberg/catalog/rest/auth/auth_session_internal.h @@ -41,6 +41,7 @@ #include "iceberg/util/macros.h" #include "iceberg/util/retry_util.h" #include "iceberg/util/scheduled_executor_internal.h" +#include "iceberg/util/string_util.h" #include "iceberg/util/url_encoder.h" namespace iceberg::rest::auth::internal { @@ -294,10 +295,18 @@ class OAuth2Session final : public AuthSession, auto headers = recover ? BasicAuthHeaders() : OAuth2Util::AuthHeaders(token); // Explicit request headers override any default Authorization on the client. // Keep the current token as subject, without a parent actor. - return OAuth2Util::ExchangeToken(*client_, *empty_session, headers, token, - issued_token_type, std::nullopt, std::nullopt, - config_.scope, config_.token_endpoint, - config_.optional_oauth_params); + ICEBERG_ASSIGN_OR_RAISE( + auto response, OAuth2Util::ExchangeToken( + *client_, *empty_session, headers, token, issued_token_type, + std::nullopt, std::nullopt, config_.scope, + config_.token_endpoint, config_.optional_oauth_params)); + // RFC 8693 allows N_A in an exchange response, but this session authenticates + // resource requests with Bearer. + if (!StringUtils::EqualsIgnoreCase(response.token_type, "bearer")) { + return ValidationFailed("Cannot use OAuth2 token type '{}' as a bearer token", + response.token_type); + } + return response; } Result> RefreshToken(bool recover = false) { @@ -403,7 +412,7 @@ class OAuth2Session final : public AuthSession, AuthProperties refresh_properties_; std::shared_ptr client_; bool closed_ = false; - // Set when refresh gives up after repeated failures; guarded by refresh_mutex_. + // The refresh loop will not schedule another attempt. Guarded by refresh_mutex_. bool refresh_stopped_ = false; mutable std::mutex refresh_mutex_; std::condition_variable refresh_cv_; diff --git a/src/iceberg/test/auth_manager_test.cc b/src/iceberg/test/auth_manager_test.cc index 2a0ad0df51..f6c8ad2041 100644 --- a/src/iceberg/test/auth_manager_test.cc +++ b/src/iceberg/test/auth_manager_test.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -757,12 +758,9 @@ TEST(OAuth2SessionTest, RefreshesAndReschedulesThroughPublicFactory) { } } -class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { +class OAuth2ManagerTest : public ::testing::Test { protected: void SetUp() override { - if (!GetParam().empty()) { - properties_[AuthProperties::kExchangeEnabled.key()] = GetParam(); - } ICEBERG_UNWRAP_OR_FAIL(manager_, AuthManagers::Load("test-catalog", properties_)); manager_->SetAuthRefreshExecutor(&executor_); client_->response = [this]() -> Result { @@ -795,6 +793,30 @@ class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { std::move(task.get())(); } + MockExecutor executor_; + std::promise queued_; + std::shared_ptr client_ = std::make_shared(); + std::unordered_map properties_{ + {AuthProperties::kAuthType, "oauth2"}, + {AuthProperties::kCredential.key(), "catalog:catalog-secret"}, + {AuthProperties::kOAuth2ServerUri.key(), "https://auth.example/token"}, + {AuthProperties::kScope.key(), "catalog"}, + {AuthProperties::kAudience.key(), "catalog-service"}, + {AuthProperties::kResource.key(), "https://catalog.example"}, + }; + std::unique_ptr manager_; + std::shared_ptr session_; +}; + +class OAuth2ManagerRefreshTest : public OAuth2ManagerTest, + public ::testing::WithParamInterface { + protected: + void SetUp() override { + properties_[AuthProperties::kExchangeEnabled.key()] = GetParam() ? "true" : "false"; + properties_[AuthProperties::kToken.key()] = "parent-token"; + OAuth2ManagerTest::SetUp(); + } + void CheckChildRequests(const std::string& client_id, const std::string& client_secret, const std::string& parent_token) { ASSERT_EQ(client_->recorded_requests.size(), 2); @@ -808,7 +830,7 @@ class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { EXPECT_EQ(initial.headers.at("Authorization"), "Bearer " + parent_token); const auto& refresh = client_->recorded_requests[1]; - if (GetParam() == "false") { + if (!GetParam()) { EXPECT_THAT(refresh.form, ::testing::ContainerEq(credential_form)); EXPECT_TRUE(refresh.headers.empty()); } else { @@ -836,25 +858,10 @@ class OAuth2ManagerRefreshTest : public ::testing::TestWithParam { EXPECT_EQ(request.headers.at("Authorization"), "Bearer fresh"); EXPECT_EQ(session_->OAuth2Info()->credential, client_id + ":" + client_secret); } - - MockExecutor executor_; - std::promise queued_; - std::shared_ptr client_ = std::make_shared(); - std::unordered_map properties_{ - {AuthProperties::kAuthType, "oauth2"}, - {AuthProperties::kCredential.key(), "catalog:catalog-secret"}, - {AuthProperties::kOAuth2ServerUri.key(), "https://auth.example/token"}, - {AuthProperties::kScope.key(), "catalog"}, - {AuthProperties::kAudience.key(), "catalog-service"}, - {AuthProperties::kResource.key(), "https://catalog.example"}, - }; - std::unique_ptr manager_; - std::shared_ptr session_; }; TEST_P(OAuth2ManagerRefreshTest, CredentialContextInheritsCatalogRefreshGrant) { ExpectRefresh(); - properties_[AuthProperties::kToken.key()] = "parent-token"; ICEBERG_UNWRAP_OR_FAIL(auto parent, manager_->CatalogSession(client_, properties_)); SessionContext context{ .session_id = "tenant-a", @@ -869,7 +876,6 @@ TEST_P(OAuth2ManagerRefreshTest, CredentialContextInheritsCatalogRefreshGrant) { TEST_P(OAuth2ManagerRefreshTest, ExpiredCredentialContextRecoversWithOwnRawCredential) { ExpectRefresh(); - properties_[AuthProperties::kToken.key()] = "parent-token"; ICEBERG_UNWRAP_OR_FAIL(auto parent, manager_->CatalogSession(client_, properties_)); client_->response = [this]() -> Result { return HttpResponse( @@ -898,7 +904,7 @@ TEST_P(OAuth2ManagerRefreshTest, ExpiredCredentialContextRecoversWithOwnRawCrede EXPECT_THAT(initial.form, ::testing::ContainerEq(credential_form)); EXPECT_EQ(initial.headers.at("Authorization"), "Bearer parent-token"); const auto& recovery = client_->recorded_requests[1]; - if (GetParam() == "false") { + if (!GetParam()) { EXPECT_THAT(recovery.form, ::testing::ContainerEq(credential_form)); EXPECT_TRUE(recovery.headers.empty()); } else { @@ -925,7 +931,7 @@ TEST_P(OAuth2ManagerRefreshTest, ExpiredCredentialContextRecoversWithOwnRawCrede EXPECT_EQ(client_->requests, 2); } -TEST_P(OAuth2ManagerRefreshTest, RefreshDisabledDoesNotScheduleCatalogOrContext) { +TEST_F(OAuth2ManagerTest, RefreshDisabledDoesNotScheduleCatalogOrContext) { EXPECT_CALL(executor_, Submit).Times(0); properties_[AuthProperties::kKeepRefreshed.key()] = "false"; client_->response = []() -> Result { @@ -945,13 +951,9 @@ TEST_P(OAuth2ManagerRefreshTest, RefreshDisabledDoesNotScheduleCatalogOrContext) EXPECT_EQ(request.headers.at("Authorization"), "Bearer initial"); } -INSTANTIATE_TEST_SUITE_P(ExchangeModes, OAuth2ManagerRefreshTest, - ::testing::Values(std::string(""), std::string("true"), - std::string("false")), - [](const ::testing::TestParamInfo& info) { - return info.param.empty() ? "Default" - : info.param == "true" ? "Enabled" - : "Disabled"; +INSTANTIATE_TEST_SUITE_P(ExchangeModes, OAuth2ManagerRefreshTest, ::testing::Bool(), + [](const ::testing::TestParamInfo& info) { + return info.param ? "Enabled" : "Disabled"; }); class OAuth2ExchangeRefreshTest : public ::testing::Test { @@ -1007,17 +1009,22 @@ class OAuth2ExchangeRefreshTest : public ::testing::Test { std::shared_ptr session_; }; -TEST_F(OAuth2ExchangeRefreshTest, ExchangesCurrentTokenAndTypeOnEachRefresh) { +TEST_F(OAuth2ExchangeRefreshTest, ReschedulesFromJwtExpiryAndExchangesTheIssuedToken) { ExpectRefreshes(2); // Even when a credential is available, exchange must keep the current subject. config_.credential = "client:secret"; + std::string issued_jwt; client_->response = [&]() -> Result { if (client_->requests == 1) { + // Omit expires_in so that only the JWT expiration can trigger the next refresh. + const auto expires_at = std::chrono::floor( + std::chrono::system_clock::now() + std::chrono::seconds(2)); + issued_jwt = MakeJwt( + nlohmann::json({{"exp", expires_at.time_since_epoch().count()}}).dump()); return HttpResponse( - 200, nlohmann::json({{"access_token", "first"}, - {"token_type", "bearer"}, - {"issued_token_type", AuthProperties::kJwtTokenType}, - {"expires_in", 1}}) + 200, nlohmann::json({{"access_token", issued_jwt}, + {"token_type", "Bearer"}, + {"issued_token_type", AuthProperties::kJwtTokenType}}) .dump()); } return HttpResponse(200, R"({"access_token":"second","token_type":"bearer"})"); @@ -1026,7 +1033,7 @@ TEST_F(OAuth2ExchangeRefreshTest, ExchangesCurrentTokenAndTypeOnEachRefresh) { ASSERT_NO_FATAL_FAILURE(RunRefresh()); ICEBERG_UNWRAP_OR_FAIL(auto first_request, session_->Authenticate({})); - EXPECT_EQ(first_request.headers.at("Authorization"), "Bearer first"); + EXPECT_EQ(first_request.headers.at("Authorization"), "Bearer " + issued_jwt); ASSERT_TRUE(session_->ExpiresAt().has_value()); EXPECT_TRUE(session_->IsRefreshable()); @@ -1034,7 +1041,7 @@ TEST_F(OAuth2ExchangeRefreshTest, ExchangesCurrentTokenAndTypeOnEachRefresh) { ASSERT_EQ(client_->recorded_requests.size(), 2); for (size_t i = 0; i < client_->recorded_requests.size(); ++i) { const auto& request = client_->recorded_requests[i]; - const std::string subject = i == 0 ? "initial" : "first"; + const auto& subject = i == 0 ? token_.access_token : issued_jwt; const std::unordered_map expected_form{ {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, {"subject_token", subject}, @@ -1083,18 +1090,17 @@ TEST_F(OAuth2ExchangeRefreshTest, RetriesExchangeWithoutChangingIdentity) { EXPECT_THAT(session_->Close(), IsOk()); } -class OAuth2ExpiredExchangeRefreshTest : public OAuth2ExchangeRefreshTest, - public ::testing::WithParamInterface { +struct RejectedExchangeResponse { + std::string name; + OAuthTokenResponse token; +}; + +class OAuth2RejectedExchangeRefreshTest + : public OAuth2ExchangeRefreshTest, + public ::testing::WithParamInterface { protected: - HttpResponse ExpiredResponse() const { - return HttpResponse( - 200, nlohmann::json( - {{"access_token", GetParam() ? MakeJwt(R"({"exp":0})") : "expired"}, - {"token_type", "bearer"}, - {"issued_token_type", GetParam() ? AuthProperties::kJwtTokenType - : AuthProperties::kAccessTokenType}, - {"expires_in", GetParam() ? 3600 : 0}}) - .dump()); + HttpResponse RejectedResponse() const { + return HttpResponse(200, ToJson(GetParam().token).dump()); } void ExpectInitialState( @@ -1118,20 +1124,15 @@ class OAuth2ExpiredExchangeRefreshTest : public OAuth2ExchangeRefreshTest, } }; -TEST_P(OAuth2ExpiredExchangeRefreshTest, RetriesWithPreviousTokenUntilSuccess) { +TEST_P(OAuth2RejectedExchangeRefreshTest, RetriesWithPreviousTokenUntilSuccess) { ExpectRefreshes(1); ASSERT_THAT(Start(), IsOk()); auto initial_expiration = session_->ExpiresAt(); client_->response = [&]() -> Result { if (client_->requests == 1) { - return ExpiredResponse(); + return RejectedResponse(); } ExpectInitialState(initial_expiration); - const auto& request = client_->recorded_requests.back(); - if (request.form.at("subject_token") != token_.access_token || - request.headers.at("Authorization") != "Bearer " + token_.access_token) { - return IOError("authorization server rejects the expired token"); - } return HttpResponse(200, R"({"access_token":"fresh","token_type":"bearer"})"); }; test::FakeRetryEnvironment fake_retry; @@ -1149,12 +1150,12 @@ TEST_P(OAuth2ExpiredExchangeRefreshTest, RetriesWithPreviousTokenUntilSuccess) { EXPECT_THAT(session_->Close(), IsOk()); } -TEST_P(OAuth2ExpiredExchangeRefreshTest, KeepsPreviousTokenAfterRetriesExhausted) { +TEST_P(OAuth2RejectedExchangeRefreshTest, KeepsPreviousTokenAfterRetriesExhausted) { ExpectRefreshes(1); config_.credential = "client:secret"; ASSERT_THAT(Start(), IsOk()); auto initial_expiration = session_->ExpiresAt(); - client_->response = [&]() -> Result { return ExpiredResponse(); }; + client_->response = [&]() -> Result { return RejectedResponse(); }; test::FakeRetryEnvironment fake_retry; ScopedRetryTestHooks hooks(fake_retry.hooks()); ASSERT_NO_FATAL_FAILURE(RunRefresh()); @@ -1166,34 +1167,27 @@ TEST_P(OAuth2ExpiredExchangeRefreshTest, KeepsPreviousTokenAfterRetriesExhausted EXPECT_THAT(session_->Close(), IsOk()); } -INSTANTIATE_TEST_SUITE_P(ExpirationSources, OAuth2ExpiredExchangeRefreshTest, - ::testing::Bool(), - [](const ::testing::TestParamInfo& info) { - return info.param ? "Jwt" : "ExpiresIn"; - }); - -TEST_F(OAuth2ExchangeRefreshTest, StopsAfterExchangeRetryBudget) { - ExpectRefreshes(1); - config_.credential = "client:secret"; - ASSERT_THAT(Start(), IsOk()); - test::FakeRetryEnvironment fake_retry; - ScopedRetryTestHooks hooks(fake_retry.hooks()); - ASSERT_NO_FATAL_FAILURE(RunRefresh()); - - EXPECT_EQ(client_->requests, 7); - for (size_t i = 0; i < client_->recorded_requests.size(); ++i) { - const auto& request = client_->recorded_requests[i]; - EXPECT_EQ(request.headers.at("Authorization"), - i == 6 ? "Basic Y2xpZW50OnNlY3JldA==" : "Bearer initial"); - EXPECT_EQ(request.form.at("grant_type"), - "urn:ietf:params:oauth:grant-type:token-exchange"); - EXPECT_EQ(request.form.at("subject_token"), "initial"); - EXPECT_FALSE(request.form.contains("client_secret")); - } - EXPECT_EQ(session_->OAuth2Info()->token, "initial"); - EXPECT_FALSE(session_->IsRefreshable()); - EXPECT_THAT(session_->Close(), IsOk()); -} +INSTANTIATE_TEST_SUITE_P( + Responses, OAuth2RejectedExchangeRefreshTest, + ::testing::Values( + RejectedExchangeResponse{"ExpiredJwt", + {.access_token = MakeJwt(R"({"exp":0})"), + .token_type = "bearer", + .expires_in_secs = 3600, + .issued_token_type = AuthProperties::kJwtTokenType}}, + RejectedExchangeResponse{"ZeroLifetime", + {.access_token = "expired", + .token_type = "bearer", + .expires_in_secs = 0, + .issued_token_type = AuthProperties::kAccessTokenType}}, + RejectedExchangeResponse{"NonBearerJwt", + {.access_token = MakeJwt(R"({"sub":"user"})"), + .token_type = "N_A", + .expires_in_secs = 3600, + .issued_token_type = AuthProperties::kJwtTokenType}}), + [](const ::testing::TestParamInfo& info) { + return info.param.name; + }); class OAuth2ExpiredCredentialTest : public OAuth2ExchangeRefreshTest, @@ -1286,8 +1280,11 @@ TEST_F(OAuth2ExchangeRefreshTest, FinalRecoveryUsesOwnRequestTimeAndReschedules) const auto& request = client_->recorded_requests[i]; EXPECT_EQ(request.headers.at("Authorization"), i == 6 ? "Basic Y2xpZW50OnNlY3JldA==" : "Bearer initial"); + EXPECT_EQ(request.form.at("grant_type"), + "urn:ietf:params:oauth:grant-type:token-exchange"); EXPECT_EQ(request.form.at("subject_token"), "initial"); EXPECT_FALSE(request.form.contains("actor_token")); + EXPECT_FALSE(request.form.contains("client_secret")); } EXPECT_THAT(fake_retry.observed_base_delays_ms(), ::testing::ElementsAre(100, 200, 400, 800, 1600)); @@ -1420,9 +1417,17 @@ TEST_F(OAuth2ExchangeRefreshTest, CloseWaitsForFinalRecovery) { } ASSERT_EQ(entered_status, std::future_status::ready); auto closing = std::async(std::launch::async, [&] { return session_->Close(); }); - EXPECT_EQ(closing.wait_for(std::chrono::milliseconds(30)), std::future_status::timeout); + // Close disables refresh before waiting for the outstanding request. + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); + while (session_->IsRefreshable() && std::chrono::steady_clock::now() < deadline) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + const bool close_started = !session_->IsRefreshable(); + const auto closing_status = closing.wait_for(std::chrono::milliseconds(30)); release.set_value(); refresh.get(); + EXPECT_TRUE(close_started); + EXPECT_EQ(closing_status, std::future_status::timeout); EXPECT_THAT(closing.get(), IsOk()); EXPECT_EQ(client_->requests, 7); diff --git a/src/iceberg/test/http_client_test.cc b/src/iceberg/test/http_client_test.cc index ffcea577a2..d5464a9ebc 100644 --- a/src/iceberg/test/http_client_test.cc +++ b/src/iceberg/test/http_client_test.cc @@ -313,96 +313,73 @@ std::string JsonResponse(int status, const nlohmann::json& json) { } // namespace -TEST(HttpClientTest, OAuth2SessionRefreshesTokensOverHttp) { +TEST(HttpClientTest, ExchangesTokenAndAuthenticatesResourceOverHttp) { using auth::AuthProperties; using auth::internal::OAuth2Session; - const std::array tokens = {"Initial+/=", "First+/=", "Second+/="}; - const std::array types = {AuthProperties::kAccessTokenType, - AuthProperties::kJwtTokenType, - AuthProperties::kAccessTokenType}; - // The server alone owns generation; the client must follow the returned token/type. - size_t generation = 0; + const std::string initial_token = "Initial+/="; + const std::string refreshed_token = "Refreshed+/="; LoopbackHttpServer server([&](const std::string& request) { - const auto headers = RequestHeaders(request); - const bool authenticated = - headers.contains("Authorization") && - headers.at("Authorization") == "Bearer " + tokens.at(generation); + auto headers = RequestHeaders(request); if (request.starts_with("GET /protected ")) { - return authenticated ? JsonResponse(200, {{"ok", true}}) - : JsonResponse(401, {{"error", - {{"code", 401}, - {"type", "NotAuthorizedException"}, - {"message", "stale token"}}}}); + if (headers["Authorization"] == "Bearer " + refreshed_token) { + return JsonResponse(200, {{"ok", true}}); + } + return JsonResponse(401, {{"error", + {{"code", 401}, + {"type", "NotAuthorizedException"}, + {"message", "stale token"}}}}); } - const auto form = RequestForm(request); + EXPECT_TRUE(request.starts_with("POST /v1/oauth/tokens ")); + EXPECT_EQ(headers["Authorization"], "Bearer " + initial_token); + EXPECT_EQ(headers["Content-Type"], "application/x-www-form-urlencoded"); const std::unordered_map expected_form{ {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, - {"subject_token", tokens.at(generation)}, - {"subject_token_type", types.at(generation)}, + {"subject_token", initial_token}, + {"subject_token_type", AuthProperties::kAccessTokenType}, {"scope", "catalog reports+read"}, }; - if (!request.starts_with("POST /v1/oauth/tokens ") || !authenticated || - (headers.find("Content-Type") == headers.end() || - headers.at("Content-Type") != "application/x-www-form-urlencoded") || - form != expected_form || generation >= 2) { - return JsonResponse(400, {{"error", "invalid_request"}}); - } - ++generation; - nlohmann::json response{{"access_token", tokens[generation]}, - {"token_type", "bearer"}, - {"issued_token_type", types[generation]}}; - if (generation == 1) response["expires_in"] = 1; - return JsonResponse(200, response); + EXPECT_EQ(RequestForm(request), expected_form); + return JsonResponse(200, + {{"access_token", refreshed_token}, {"token_type", "bearer"}}); }); ASSERT_TRUE(server.Start()); - std::array, 2> completed; - test::ThreadExecutor workers; test::MockExecutor executor; - size_t submitted = 0; - EXPECT_CALL(executor, Submit).Times(2).WillRepeatedly([&](ExecutorTask task) -> Status { - auto& done = completed.at(submitted++); - return workers.Submit(ExecutorTask([task = std::move(task), &done]() mutable { - std::move(task)(); - done.set_value(); - })); + std::promise queued; + auto ready = queued.get_future(); + EXPECT_CALL(executor, Submit).WillOnce([&](ExecutorTask task) -> Status { + queued.set_value(std::move(task)); + return {}; }); auto client = std::make_shared( std::unordered_map{ {"authorization", "Bearer stale-default"}}, std::chrono::seconds(2), std::chrono::seconds(2)); ICEBERG_UNWRAP_OR_FAIL( - auto session, OAuth2Session::Make( - {.access_token = tokens[0], - .token_type = "bearer", - .expires_in_secs = 1, - .issued_token_type = types[0]}, - {.token_endpoint = server.url("/v1/oauth/tokens"), - .scope = "catalog reports+read", - .exchange_enabled = true, - .refresh_executor = &executor, - // Worker latency must not expire the subject. - .now = [now = std::chrono::steady_clock::now()] { return now; }}, - client, std::nullopt)); - for (auto& done : completed) { - ASSERT_EQ(done.get_future().wait_for(std::chrono::seconds(10)), - std::future_status::ready); - } - EXPECT_FALSE(session->IsRefreshable()); + auto session, + OAuth2Session::Make( + {.access_token = initial_token, .token_type = "bearer", .expires_in_secs = 1}, + {.token_endpoint = server.url("/v1/oauth/tokens"), + .scope = "catalog reports+read", + .exchange_enabled = true, + .refresh_executor = &executor, + // Keep the subject valid until the test runs the queued refresh. + .now = [now = std::chrono::steady_clock::now()] { return now; }}, + client, std::nullopt)); + ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); + std::move(ready.get())(); - // A real resource request accepts the refreshed session and rejects the old token. HttpClient resource_client({}, std::chrono::seconds(2), std::chrono::seconds(2)); const auto& handler = *DefaultErrorHandler::Instance(); EXPECT_THAT(resource_client.Get(server.url("/protected"), {}, {}, handler, *session), IsOk()); auto old_session = - auth::AuthSession::MakeDefault({{"Authorization", "Bearer " + tokens[0]}}); + auth::AuthSession::MakeDefault({{"Authorization", "Bearer " + initial_token}}); EXPECT_THAT( resource_client.Get(server.url("/protected"), {}, {}, handler, *old_session), IsError(ErrorKind::kNotAuthorized)); - EXPECT_THAT(session->Close(), IsOk()); - EXPECT_EQ(server.request_count(), 4); + EXPECT_EQ(server.request_count(), 3); } namespace { @@ -474,46 +451,31 @@ TEST(HttpClientTest, PublicOAuth2FactoryRefreshesWithAuthorizationAddedByPostFor class OAuth2HttpRecoveryTest : public ::testing::TestWithParam {}; -TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenAndAuthenticatesResourceOverHttp) { +TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenOverHttp) { using auth::AuthProperties; using auth::internal::OAuth2Session; const bool exchange_enabled = GetParam(); const std::string initial_token = "Expired+/="; const std::string refreshed_token = "Recovered+/="; LoopbackHttpServer server([&](const std::string& request) { - const auto headers = RequestHeaders(request); - if (request.starts_with("GET /protected ")) { - if (headers.contains("Authorization") && - headers.at("Authorization") == "Bearer " + refreshed_token) { - return JsonResponse(200, {{"ok", true}}); - } - return JsonResponse(401, {{"error", - {{"code", 401}, - {"type", "NotAuthorizedException"}, - {"message", "wrong token"}}}}); - } + auto headers = RequestHeaders(request); + EXPECT_TRUE(request.starts_with("POST /v1/oauth/tokens ")); + EXPECT_EQ(headers["X-Trace"], "KeepThisValue"); + EXPECT_EQ(headers["Content-Type"], "application/x-www-form-urlencoded"); std::unordered_map expected_form{{"scope", "catalog"}}; - bool valid_auth; if (exchange_enabled) { + EXPECT_EQ(headers["Authorization"], "Basic OlNlQ3JFdCUzQVRhaWw="); expected_form.insert({ {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, {"subject_token", initial_token}, {"subject_token_type", AuthProperties::kAccessTokenType}, }); - valid_auth = headers.contains("Authorization") && - headers.at("Authorization") == "Basic OlNlQ3JFdCUzQVRhaWw="; } else { + EXPECT_EQ(headers["Authorization"], "Bearer StaleDefault"); expected_form.insert( {{"grant_type", "client_credentials"}, {"client_secret", "SeCrEt:Tail"}}); - valid_auth = headers.contains("Authorization") && - headers.at("Authorization") == "Bearer StaleDefault"; - } - if (!request.starts_with("POST /v1/oauth/tokens ") || !valid_auth || - RequestForm(request) != expected_form || !headers.contains("X-Trace") || - headers.at("X-Trace") != "KeepThisValue" || !headers.contains("Content-Type") || - headers.at("Content-Type") != "application/x-www-form-urlencoded") { - return JsonResponse(400, {{"error", "invalid_request"}}); } + EXPECT_EQ(RequestForm(request), expected_form); return JsonResponse(200, {{"access_token", refreshed_token}, {"token_type", "bearer"}}); }); @@ -543,14 +505,7 @@ TEST_P(OAuth2HttpRecoveryTest, RecoversExpiredTokenAndAuthenticatesResourceOverH ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); std::move(ready.get())(); EXPECT_EQ(session->OAuth2Info()->token, refreshed_token); - EXPECT_FALSE(session->IsRefreshable()); - - HttpClient resource_client({}, std::chrono::seconds(2), std::chrono::seconds(2)); - EXPECT_THAT(resource_client.Get(server.url("/protected"), {}, {}, - *DefaultErrorHandler::Instance(), *session), - IsOk()); - EXPECT_THAT(session->Close(), IsOk()); - EXPECT_EQ(server.request_count(), 2); + EXPECT_EQ(server.request_count(), 1); } INSTANTIATE_TEST_SUITE_P(ExchangeModes, OAuth2HttpRecoveryTest, ::testing::Bool(), diff --git a/src/iceberg/test/rest_catalog_unit_test.cc b/src/iceberg/test/rest_catalog_unit_test.cc index 925a32d3c5..00b6f93a88 100644 --- a/src/iceberg/test/rest_catalog_unit_test.cc +++ b/src/iceberg/test/rest_catalog_unit_test.cc @@ -715,7 +715,7 @@ class RestCatalogOAuth2RefreshTest : public RestCatalogOAuth2UriTest, public ::testing::WithParamInterface {}; -TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRequests) { +TEST_P(RestCatalogOAuth2RefreshTest, MergedConfigSelectsRefreshGrant) { const auto& mode = GetParam(); properties_["token-refresh-enabled"] = "true"; if (!mode.client.empty()) { @@ -729,38 +729,20 @@ TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRe server_config.overrides["token-exchange-enabled"] = mode.server_override; } ExpectInit(server_config, - R"({"access_token":"initial","token_type":"bearer","expires_in":1})"); + R"({"access_token":"initial","token_type":"bearer","expires_in":0})"); - std::unordered_map expected_form; + const char* grant_type = mode.exchange_enabled + ? "urn:ietf:params:oauth:grant-type:token-exchange" + : "client_credentials"; + std::unordered_map expected_headers; if (mode.exchange_enabled) { - expected_form = { - {"grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"}, - {"subject_token", "initial"}, - {"subject_token_type", auth::AuthProperties::kAccessTokenType}, - {"scope", "catalog"}, - }; - } else { - expected_form = {{"grant_type", "client_credentials"}, - {"client_id", "client"}, - {"client_secret", "secret"}, - {"scope", "catalog"}}; + expected_headers.emplace("Authorization", "Basic Y2xpZW50OnNlY3JldA=="); } - EXPECT_CALL(*mock_client_, PostForm("https://auth.example/token/", _, _, _, _)) - .WillOnce([&](const std::string&, const auto& form, const auto& headers, - const ErrorHandler&, auth::AuthSession&) -> Result { - EXPECT_THAT(form, ::testing::ContainerEq(expected_form)); - if (mode.exchange_enabled) { - // Config selection is unchanged if the queued refresh starts after expiry. - EXPECT_THAT( - headers, - ::testing::ElementsAre(::testing::Pair( - "Authorization", - ::testing::AnyOf("Bearer initial", "Basic Y2xpZW50OnNlY3JldA==")))); - } else { - EXPECT_TRUE(headers.empty()); - } - return HttpResponse(200, R"({"access_token":"refreshed","token_type":"bearer"})"); - }); + EXPECT_CALL(*mock_client_, + PostForm(_, ::testing::Contains(::testing::Pair("grant_type", grant_type)), + expected_headers, _, _)) + .WillOnce(Return( + HttpResponse(200, R"({"access_token":"refreshed","token_type":"bearer"})"))); test::MockExecutor executor; std::promise queued; @@ -769,20 +751,10 @@ TEST_P(RestCatalogOAuth2RefreshTest, RefreshUsesMergedConfigAndUpdatesBusinessRe queued.set_value(std::move(task)); return {}; }); - ICEBERG_UNWRAP_OR_FAIL(auto root, MakeOAuth2Catalog(&executor)); + auto catalog = MakeOAuth2Catalog(&executor); + ASSERT_THAT(catalog, IsOk()); ASSERT_EQ(ready.wait_for(std::chrono::seconds(5)), std::future_status::ready); std::move(ready.get())(); - - EXPECT_CALL(*mock_client_, Get("https://catalog.example/api/v1/namespaces", _, _, _, _)) - .WillOnce([](const std::string&, const auto&, const auto&, const ErrorHandler&, - auth::AuthSession& session) -> Result { - ICEBERG_ASSIGN_OR_RAISE(auto request, session.Authenticate({})); - EXPECT_EQ(request.headers.at("Authorization"), "Bearer refreshed"); - return HttpResponse(200, R"({"namespaces":[]})"); - }); - ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog()); - EXPECT_THAT(catalog->ListNamespaces({}), IsOk()); - EXPECT_EQ(factory_calls_, 2); } INSTANTIATE_TEST_SUITE_P( From 28c6aff7e52a3f82eee7104ee9abd2ab2b987dce Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Wed, 2 Dec 2026 00:20:13 +0800 Subject: [PATCH 10/10] test(rest): use braced initialization for the OAuth2 test response --- src/iceberg/test/auth_manager_test.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/iceberg/test/auth_manager_test.cc b/src/iceberg/test/auth_manager_test.cc index f6c8ad2041..a6cb8432fe 100644 --- a/src/iceberg/test/auth_manager_test.cc +++ b/src/iceberg/test/auth_manager_test.cc @@ -1099,9 +1099,7 @@ class OAuth2RejectedExchangeRefreshTest : public OAuth2ExchangeRefreshTest, public ::testing::WithParamInterface { protected: - HttpResponse RejectedResponse() const { - return HttpResponse(200, ToJson(GetParam().token).dump()); - } + HttpResponse RejectedResponse() const { return {200, ToJson(GetParam().token).dump()}; } void ExpectInitialState( std::optional expiration) {