Skip to content

Commit 948d293

Browse files
author
shuxu.li
committed
feat: Implement NoopAuthManager and integrate AuthManager into RestCatalog
1 parent cc804e8 commit 948d293

File tree

6 files changed

+86
-96
lines changed

6 files changed

+86
-96
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace {
3333
using AuthManagerRegistry =
3434
std::unordered_map<std::string, AuthManagerFactory, StringHash, StringEqual>;
3535

36-
/// \brief Known authentication types that are defined in the Iceberg spec.
3736
const std::unordered_set<std::string, StringHash, StringEqual>& KnownAuthTypes() {
3837
static const std::unordered_set<std::string, StringHash, StringEqual> kAuthTypes = {
3938
AuthProperties::kAuthTypeNone,
@@ -65,6 +64,12 @@ std::string InferAuthType(
6564
/// \brief Authentication manager that performs no authentication.
6665
class NoopAuthManager : public AuthManager {
6766
public:
67+
static Result<std::unique_ptr<AuthManager>> Make(
68+
[[maybe_unused]] std::string_view name,
69+
[[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) {
70+
return std::make_unique<NoopAuthManager>();
71+
}
72+
6873
Result<std::shared_ptr<AuthSession>> CatalogSession(
6974
[[maybe_unused]] HttpClient& client,
7075
[[maybe_unused]] const std::unordered_map<std::string, std::string>& properties)
@@ -73,18 +78,22 @@ class NoopAuthManager : public AuthManager {
7378
}
7479
};
7580

81+
template <typename T>
82+
AuthManagerFactory MakeAuthFactory() {
83+
return []([[maybe_unused]] std::string_view name,
84+
[[maybe_unused]] const std::unordered_map<std::string, std::string>& props)
85+
-> Result<std::unique_ptr<AuthManager>> { return T::Make(name, props); };
86+
}
87+
88+
AuthManagerRegistry CreateDefaultRegistry() {
89+
return {
90+
{AuthProperties::kAuthTypeNone, MakeAuthFactory<NoopAuthManager>()},
91+
};
92+
}
93+
7694
// Get the global registry of auth manager factories.
7795
AuthManagerRegistry& GetRegistry() {
78-
static AuthManagerRegistry registry = [] {
79-
AuthManagerRegistry r;
80-
r[AuthProperties::kAuthTypeNone] =
81-
[]([[maybe_unused]] std::string_view name,
82-
[[maybe_unused]] const std::unordered_map<std::string, std::string>& props)
83-
-> Result<std::unique_ptr<AuthManager>> {
84-
return std::make_unique<NoopAuthManager>();
85-
};
86-
return r;
87-
}();
96+
static AuthManagerRegistry registry = CreateDefaultRegistry();
8897
return registry;
8998
}
9099

src/iceberg/catalog/rest/http_client.cc

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cpr/cpr.h>
2323
#include <nlohmann/json.hpp>
2424

25+
#include "iceberg/catalog/rest/auth/auth_session.h"
2526
#include "iceberg/catalog/rest/constant.h"
2627
#include "iceberg/catalog/rest/error_handlers.h"
2728
#include "iceberg/catalog/rest/json_serde_internal.h"
@@ -146,11 +147,21 @@ HttpClient::HttpClient(std::unordered_map<std::string, std::string> default_head
146147

147148
HttpClient::~HttpClient() = default;
148149

150+
void HttpClient::SetAuthSession(auth::AuthSession* session) { session_ = session; }
151+
152+
Result<std::unordered_map<std::string, std::string>> HttpClient::AuthHeaders() {
153+
std::unordered_map<std::string, std::string> headers;
154+
if (session_) {
155+
ICEBERG_RETURN_UNEXPECTED(session_->Authenticate(headers));
156+
}
157+
return headers;
158+
}
159+
149160
Result<HttpResponse> HttpClient::Get(
150161
const std::string& path, const std::unordered_map<std::string, std::string>& params,
151-
const std::unordered_map<std::string, std::string>& headers,
152162
const ErrorHandler& error_handler) {
153-
auto final_headers = MergeHeaders(default_headers_, headers);
163+
ICEBERG_ASSIGN_OR_RAISE(auto auth_headers, AuthHeaders());
164+
auto final_headers = MergeHeaders(default_headers_, auth_headers);
154165
cpr::Response response =
155166
cpr::Get(cpr::Url{path}, GetParameters(params), final_headers, *connection_pool_);
156167

@@ -160,11 +171,10 @@ Result<HttpResponse> HttpClient::Get(
160171
return http_response;
161172
}
162173

163-
Result<HttpResponse> HttpClient::Post(
164-
const std::string& path, const std::string& body,
165-
const std::unordered_map<std::string, std::string>& headers,
166-
const ErrorHandler& error_handler) {
167-
auto final_headers = MergeHeaders(default_headers_, headers);
174+
Result<HttpResponse> HttpClient::Post(const std::string& path, const std::string& body,
175+
const ErrorHandler& error_handler) {
176+
ICEBERG_ASSIGN_OR_RAISE(auto auth_headers, AuthHeaders());
177+
auto final_headers = MergeHeaders(default_headers_, auth_headers);
168178
cpr::Response response =
169179
cpr::Post(cpr::Url{path}, cpr::Body{body}, final_headers, *connection_pool_);
170180

@@ -177,9 +187,9 @@ Result<HttpResponse> HttpClient::Post(
177187
Result<HttpResponse> HttpClient::PostForm(
178188
const std::string& path,
179189
const std::unordered_map<std::string, std::string>& form_data,
180-
const std::unordered_map<std::string, std::string>& headers,
181190
const ErrorHandler& error_handler) {
182-
auto final_headers = MergeHeaders(default_headers_, headers);
191+
ICEBERG_ASSIGN_OR_RAISE(auto auth_headers, AuthHeaders());
192+
auto final_headers = MergeHeaders(default_headers_, auth_headers);
183193
final_headers.insert_or_assign(kHeaderContentType, kMimeTypeFormUrlEncoded);
184194
std::vector<cpr::Pair> pair_list;
185195
pair_list.reserve(form_data.size());
@@ -196,10 +206,10 @@ Result<HttpResponse> HttpClient::PostForm(
196206
return http_response;
197207
}
198208

199-
Result<HttpResponse> HttpClient::Head(
200-
const std::string& path, const std::unordered_map<std::string, std::string>& headers,
201-
const ErrorHandler& error_handler) {
202-
auto final_headers = MergeHeaders(default_headers_, headers);
209+
Result<HttpResponse> HttpClient::Head(const std::string& path,
210+
const ErrorHandler& error_handler) {
211+
ICEBERG_ASSIGN_OR_RAISE(auto auth_headers, AuthHeaders());
212+
auto final_headers = MergeHeaders(default_headers_, auth_headers);
203213
cpr::Response response = cpr::Head(cpr::Url{path}, final_headers, *connection_pool_);
204214

205215
ICEBERG_RETURN_UNEXPECTED(HandleFailureResponse(response, error_handler));
@@ -210,9 +220,9 @@ Result<HttpResponse> HttpClient::Head(
210220

211221
Result<HttpResponse> HttpClient::Delete(
212222
const std::string& path, const std::unordered_map<std::string, std::string>& params,
213-
const std::unordered_map<std::string, std::string>& headers,
214223
const ErrorHandler& error_handler) {
215-
auto final_headers = MergeHeaders(default_headers_, headers);
224+
ICEBERG_ASSIGN_OR_RAISE(auto auth_headers, AuthHeaders());
225+
auto final_headers = MergeHeaders(default_headers_, auth_headers);
216226
cpr::Response response = cpr::Delete(cpr::Url{path}, GetParameters(params),
217227
final_headers, *connection_pool_);
218228

src/iceberg/catalog/rest/http_client.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,39 @@ class ICEBERG_REST_EXPORT HttpClient {
7878
HttpClient(HttpClient&&) = delete;
7979
HttpClient& operator=(HttpClient&&) = delete;
8080

81+
/// \brief Set the authentication session
82+
void SetAuthSession(auth::AuthSession* session);
83+
8184
/// \brief Sends a GET request.
8285
Result<HttpResponse> Get(const std::string& path,
8386
const std::unordered_map<std::string, std::string>& params,
84-
const std::unordered_map<std::string, std::string>& headers,
8587
const ErrorHandler& error_handler);
8688

8789
/// \brief Sends a POST request.
8890
Result<HttpResponse> Post(const std::string& path, const std::string& body,
89-
const std::unordered_map<std::string, std::string>& headers,
9091
const ErrorHandler& error_handler);
9192

9293
/// \brief Sends a POST request with form data.
9394
Result<HttpResponse> PostForm(
9495
const std::string& path,
9596
const std::unordered_map<std::string, std::string>& form_data,
96-
const std::unordered_map<std::string, std::string>& headers,
9797
const ErrorHandler& error_handler);
9898

9999
/// \brief Sends a HEAD request.
100-
Result<HttpResponse> Head(const std::string& path,
101-
const std::unordered_map<std::string, std::string>& headers,
102-
const ErrorHandler& error_handler);
100+
Result<HttpResponse> Head(const std::string& path, const ErrorHandler& error_handler);
103101

104102
/// \brief Sends a DELETE request.
105103
Result<HttpResponse> Delete(const std::string& path,
106104
const std::unordered_map<std::string, std::string>& params,
107-
const std::unordered_map<std::string, std::string>& headers,
108105
const ErrorHandler& error_handler);
109106

110107
private:
108+
/// \brief Get authentication headers.
109+
Result<std::unordered_map<std::string, std::string>> AuthHeaders();
110+
111111
std::unordered_map<std::string, std::string> default_headers_;
112112
std::unique_ptr<cpr::ConnectionPool> connection_pool_;
113+
auth::AuthSession* session_ = nullptr;
113114
};
114115

115116
} // namespace iceberg::rest

0 commit comments

Comments
 (0)