-
Notifications
You must be signed in to change notification settings - Fork 88
feat: add restcatalog authentication api #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
572d02a
904fdf0
83084ad
72df76d
54b4868
28e195a
37787fb
06a1a59
82b490a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| set(ICEBERG_REST_AUTH_SOURCES auth_session.cc auth_manager.cc auth_managers.cc) | ||
|
|
||
| # Expose sources to parent scope for inclusion in iceberg_rest library | ||
| set(ICEBERG_REST_AUTH_SOURCES | ||
| ${ICEBERG_REST_AUTH_SOURCES} | ||
| PARENT_SCOPE) | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/catalog/rest/auth/auth_manager.h" | ||
|
|
||
| namespace iceberg::rest::auth { | ||
|
|
||
| std::shared_ptr<AuthSession> AuthManager::InitSession( | ||
| HttpClient* init_client, | ||
| const std::unordered_map<std::string, std::string>& properties) { | ||
| // By default, use the catalog session for initialization | ||
| return CatalogSession(init_client, properties); | ||
| } | ||
|
|
||
| std::shared_ptr<AuthSession> AuthManager::TableSession( | ||
| [[maybe_unused]] const TableIdentifier& table, | ||
| [[maybe_unused]] const std::unordered_map<std::string, std::string>& properties, | ||
| std::shared_ptr<AuthSession> parent) { | ||
| // By default, return the parent session | ||
| return parent; | ||
| } | ||
|
|
||
| } // namespace iceberg::rest::auth |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| #include "iceberg/catalog/rest/auth/auth_session.h" | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #include "iceberg/catalog/rest/iceberg_rest_export.h" | ||
| #include "iceberg/table_identifier.h" | ||
wgtmac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// \file iceberg/catalog/rest/auth/auth_manager.h | ||
| /// \brief Authentication manager interface for REST catalog. | ||
|
|
||
| namespace iceberg::rest { | ||
| class HttpClient; | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } // namespace iceberg::rest | ||
|
|
||
| namespace iceberg::rest::auth { | ||
|
|
||
| /// \brief Manager for authentication sessions. | ||
| /// | ||
| /// This interface is used to create sessions for the catalog, tables/views, | ||
| /// and any other context that requires authentication. | ||
| /// | ||
| /// Managers are typically stateful and may require initialization and cleanup. | ||
| /// The manager is created by the catalog and is closed when the catalog is closed. | ||
| /// | ||
| /// This interface is modeled after Java Iceberg's AuthManager interface. | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| class ICEBERG_REST_EXPORT AuthManager { | ||
| public: | ||
| virtual ~AuthManager() = default; | ||
|
|
||
| /// \brief Return a temporary session for contacting the configuration endpoint. | ||
| /// | ||
| /// This session is used only during catalog initialization to fetch server | ||
| /// configuration. The returned session will be closed after the configuration | ||
| /// endpoint is contacted and should not be cached. | ||
| /// | ||
| /// The provided HTTP client is a short-lived client; it should only be used | ||
| /// to fetch initial credentials if required, and must be discarded after that. | ||
| /// | ||
| /// This method cannot return null. By default, it returns the catalog session. | ||
| /// | ||
| /// \param init_client A short-lived HTTP client for initialization. | ||
| /// \param properties Configuration properties. | ||
| /// \return A session for initialization, or the catalog session by default. | ||
| virtual std::shared_ptr<AuthSession> InitSession( | ||
| HttpClient* init_client, | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const std::unordered_map<std::string, std::string>& properties); | ||
|
|
||
| /// \brief Return a long-lived session for catalog operations. | ||
| /// | ||
| /// This session's lifetime is tied to the owning catalog. It serves as the | ||
| /// parent session for all other sessions (contextual and table-specific). | ||
| /// It is closed when the owning catalog is closed. | ||
| /// | ||
| /// The provided HTTP client is a long-lived, shared client. Implementors may | ||
| /// store it and reuse it for subsequent requests to the authorization server | ||
| /// (e.g., for renewing or refreshing credentials). It is not necessary to | ||
| /// close it when Close() is called. | ||
| /// | ||
| /// This method cannot return null. | ||
| /// | ||
| /// It is not required to cache the returned session internally, as the catalog | ||
| /// will keep it alive for the lifetime of the catalog. | ||
| /// | ||
| /// \param shared_client A long-lived, shared HTTP client. | ||
| /// \param properties Configuration properties (merged with server config). | ||
| /// \return A session for catalog operations. | ||
| virtual std::shared_ptr<AuthSession> CatalogSession( | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| HttpClient* shared_client, | ||
| const std::unordered_map<std::string, std::string>& properties) = 0; | ||
|
|
||
| /// \brief Return a session for a specific table or view. | ||
| /// | ||
| /// If the table or view requires a specific AuthSession (e.g., vended credentials), | ||
| /// this method should return a new AuthSession instance. Otherwise, it should | ||
| /// return the parent session. | ||
| /// | ||
| /// This method cannot return null. By default, it returns the parent session. | ||
| /// | ||
| /// Implementors should cache table sessions internally, as the catalog will not | ||
| /// cache them. Also, the owning catalog never closes table sessions; implementations | ||
| /// should manage their lifecycle and close them when they are no longer needed. | ||
| /// | ||
| /// \param table The table identifier. | ||
| /// \param properties Properties returned by the table/view endpoint. | ||
| /// \param parent The parent session (typically the catalog session). | ||
| /// \return A session for the table, or the parent session by default. | ||
| virtual std::shared_ptr<AuthSession> TableSession( | ||
| const TableIdentifier& table, | ||
| const std::unordered_map<std::string, std::string>& properties, | ||
| std::shared_ptr<AuthSession> parent); | ||
|
|
||
| /// \brief Close the manager and release any resources. | ||
| /// | ||
| /// This method is called when the owning catalog is closed. Implementations | ||
| /// should release any resources held by the manager, such as cached sessions | ||
| /// or background threads. | ||
| virtual void Close() {} | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| } // namespace iceberg::rest::auth | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/catalog/rest/auth/auth_managers.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cctype> | ||
|
|
||
| #include "iceberg/catalog/rest/auth/auth_properties.h" | ||
|
|
||
| namespace iceberg::rest::auth { | ||
|
|
||
| namespace { | ||
|
|
||
| /// \brief Convert a string to lowercase for case-insensitive comparison. | ||
| std::string ToLower(std::string_view str) { | ||
| std::string result(str); | ||
| std::ranges::transform(result, result.begin(), | ||
| [](unsigned char c) { return std::tolower(c); }); | ||
| return result; | ||
| } | ||
wgtmac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// \brief Infer the authentication type from properties. | ||
| /// | ||
| /// If no explicit auth type is set, this function tries to infer it from | ||
| /// other properties. If "credential" or "token" is present, it implies | ||
| /// OAuth2 authentication. Otherwise, defaults to no authentication. | ||
| /// | ||
| /// This behavior is consistent with Java Iceberg's AuthManagers. | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| std::string InferAuthType( | ||
wgtmac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const std::unordered_map<std::string, std::string>& properties) { | ||
| // Check for explicit auth type first | ||
| auto it = properties.find(std::string(AuthProperties::kAuthType)); | ||
| if (it != properties.end() && !it->second.empty()) { | ||
| return ToLower(it->second); | ||
| } | ||
|
|
||
| // Infer from OAuth2 properties (credential or token) | ||
| bool has_credential = | ||
| properties.contains(std::string(AuthProperties::kOAuth2Credential)); | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bool has_token = properties.contains(std::string(AuthProperties::kOAuth2Token)); | ||
| if (has_credential || has_token) { | ||
| return std::string(AuthProperties::kAuthTypeOAuth2); | ||
| } | ||
|
|
||
| // Default to no authentication | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return std::string(AuthProperties::kAuthTypeNone); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::unordered_map<std::string, AuthManagerFactory>& AuthManagers::GetRegistry() { | ||
| static std::unordered_map<std::string, AuthManagerFactory> registry; | ||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return registry; | ||
| } | ||
|
|
||
| void AuthManagers::Register(const std::string& auth_type, AuthManagerFactory factory) { | ||
| GetRegistry()[ToLower(auth_type)] = std::move(factory); | ||
| } | ||
|
|
||
| Result<std::unique_ptr<AuthManager>> AuthManagers::Load( | ||
| const std::string& name, | ||
| const std::unordered_map<std::string, std::string>& properties) { | ||
| std::string auth_type = InferAuthType(properties); | ||
|
|
||
| auto& registry = GetRegistry(); | ||
| auto it = registry.find(auth_type); | ||
| if (it == registry.end()) { | ||
| return NotImplemented("Authentication type '{}' is not supported", auth_type); | ||
lishuxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return it->second(name, properties); | ||
| } | ||
|
|
||
| } // namespace iceberg::rest::auth | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||
| /* | ||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||
| * or more contributor license agreements. See the NOTICE file | ||||||
| * distributed with this work for additional information | ||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||
| * to you under the Apache License, Version 2.0 (the | ||||||
| * "License"); you may not use this file except in compliance | ||||||
| * with the License. You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, | ||||||
| * software distributed under the License is distributed on an | ||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| * KIND, either express or implied. See the License for the | ||||||
| * specific language governing permissions and limitations | ||||||
| * under the License. | ||||||
| */ | ||||||
|
|
||||||
| #pragma once | ||||||
|
|
||||||
| #include <functional> | ||||||
| #include <memory> | ||||||
| #include <string> | ||||||
| #include <unordered_map> | ||||||
|
|
||||||
| #include "iceberg/catalog/rest/auth/auth_manager.h" | ||||||
| #include "iceberg/catalog/rest/iceberg_rest_export.h" | ||||||
| #include "iceberg/result.h" | ||||||
|
|
||||||
| /// \file iceberg/catalog/rest/auth/auth_managers.h | ||||||
| /// \brief Factory for creating authentication managers. | ||||||
|
|
||||||
| namespace iceberg::rest::auth { | ||||||
|
|
||||||
| /// \brief Factory function type for creating AuthManager instances. | ||||||
| /// | ||||||
| /// \param name The name of the manager (used for logging). | ||||||
| /// \param properties Configuration properties. | ||||||
| /// \return A unique pointer to the created AuthManager. | ||||||
| using AuthManagerFactory = std::function<std::unique_ptr<AuthManager>( | ||||||
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| const std::string& name, | ||||||
|
||||||
| const std::string& name, | |
| std::string_view name, |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// | |
| /// This class is modeled after Java Iceberg's AuthManagers class. |
lishuxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const std::string& name, | |
| std::string_view name, |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| static void Register(const std::string& auth_type, AuthManagerFactory factory); | |
| static void Register(std::string_view auth_type, AuthManagerFactory factory); |
Uh oh!
There was an error while loading. Please reload this page.