lishuxu commented on code in PR #1132:
URL: https://github.com/apache/iceberg-cpp/pull/1132#discussion_r4720350004
##########
src/iceberg/test/rest_catalog_unit_test.cc:
##########
@@ -669,6 +670,202 @@ 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<OAuth2RefreshModeCase> {};
+
+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<std::string, std::string> expected_form;
+ 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"}};
+ }
+ EXPECT_CALL(*mock_client_, PostForm("https://auth.example/token/", _, _, _,
_))
+ .WillOnce([&](const std::string&, const auto& form, const auto& headers,
+ const ErrorHandler&, auth::AuthSession&) ->
Result<HttpResponse> {
+ 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"})");
+ });
+
+ test::MockExecutor executor;
+ std::promise<ExecutorTask> 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<HttpResponse> {
+ 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},
Review Comment:
Reduced the matrix to Default, ServerDefaultDisables,
ClientOverridesServerDefault, and ServerEnables.
##########
src/iceberg/test/auth_manager_test.cc:
##########
@@ -736,6 +751,765 @@ 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<std::string> {
+ 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<HttpResponse> {
+ 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<std::string, std::string> 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<std::string, std::string> 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));
+ // 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()));
+ }
+ 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<ExecutorTask> queued_;
+ std::shared_ptr<RefreshHttpClient> client_ =
std::make_shared<RefreshHttpClient>();
+ std::unordered_map<std::string, std::string> 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<AuthManager> manager_;
+ std::shared_ptr<AuthSession> session_;
+};
+
+TEST_P(OAuth2ManagerRefreshTest, CatalogRefreshUsesConfiguredGrant) {
Review Comment:
Removed CatalogRefreshUsesConfiguredGrant. The child-session tests still
cover grant inheritance and recovery with the child’s own credential.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]