-
Notifications
You must be signed in to change notification settings - Fork 525
feat(rest): introduce AuthManager/AuthSession and migrate OAuth2 #2838
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
Open
plusplusjiajia
wants to merge
2
commits into
apache:main
Choose a base branch
from
plusplusjiajia:feat/rest-auth-manager-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // 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. | ||
|
|
||
| //! Pluggable authentication for the REST catalog, mirroring Iceberg Java's | ||
| //! `AuthManager`/`AuthSession` API. | ||
|
|
||
| mod oauth2; | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::fmt::Debug; | ||
| use std::sync::Arc; | ||
|
|
||
| use async_trait::async_trait; | ||
| use http::{HeaderMap, Method}; | ||
| use iceberg::Result; | ||
| pub use oauth2::OAuth2Manager; | ||
| use reqwest::Request; | ||
|
|
||
| /// `rest.auth.type` value disabling authentication. | ||
| pub const AUTH_TYPE_NONE: &str = "none"; | ||
| /// `rest.auth.type` value selecting OAuth2 token authentication. | ||
| pub const AUTH_TYPE_OAUTH2: &str = "oauth2"; | ||
|
|
||
| /// Creates the [`AuthSession`]s used to authenticate REST catalog requests. | ||
| /// | ||
| /// A manager is created once per catalog, either from the `rest.auth.type` | ||
| /// property or injected through `RestCatalogBuilder::with_auth_manager`, and | ||
| /// lives for the lifetime of the catalog. | ||
| #[async_trait] | ||
| pub trait AuthManager: Debug + Send + Sync { | ||
| /// Session used for the initial `/v1/config` handshake, built from the | ||
| /// user-supplied configuration. | ||
| async fn init_session(&self) -> Result<Arc<dyn AuthSession>>; | ||
|
|
||
| /// Session used for all subsequent catalog requests, given the properties | ||
| /// merged from the user configuration and the server's config response. | ||
| /// | ||
| /// Implementations may carry state (e.g. a cached token) over from the | ||
| /// init session. | ||
| async fn catalog_session( | ||
| &self, | ||
| props: &HashMap<String, String>, | ||
| ) -> Result<Arc<dyn AuthSession>>; | ||
| } | ||
|
|
||
| /// An outgoing REST request being authenticated by an [`AuthSession`]. | ||
| /// | ||
| /// Wraps the request so authentication implementations depend only on the | ||
| /// stable `http` crate and standard types, not on the concrete HTTP client the | ||
| /// REST catalog uses internally. | ||
| pub struct AuthRequest<'a> { | ||
| inner: &'a mut Request, | ||
| } | ||
|
|
||
| impl<'a> AuthRequest<'a> { | ||
| pub(crate) fn new(inner: &'a mut Request) -> Self { | ||
| Self { inner } | ||
| } | ||
|
|
||
| /// The request method. | ||
| pub fn method(&self) -> &Method { | ||
| self.inner.method() | ||
| } | ||
|
|
||
| /// The request URL, as a string (scheme, host, path and query). | ||
| pub fn url_str(&self) -> &str { | ||
| self.inner.url().as_str() | ||
| } | ||
|
|
||
| /// The request headers. | ||
| pub fn headers(&self) -> &HeaderMap { | ||
| self.inner.headers() | ||
| } | ||
|
|
||
| /// The mutable request headers, e.g. to add an `Authorization` header. | ||
| pub fn headers_mut(&mut self) -> &mut HeaderMap { | ||
| self.inner.headers_mut() | ||
| } | ||
|
|
||
| /// The in-memory request body, or `None` for an empty or streaming body. | ||
| pub fn body(&self) -> Option<&[u8]> { | ||
| self.inner.body().and_then(|body| body.as_bytes()) | ||
| } | ||
| } | ||
|
|
||
| /// Authenticates outgoing REST catalog requests. | ||
| #[async_trait] | ||
| pub trait AuthSession: Debug + Send + Sync { | ||
| /// Applies authentication to the request (adds headers, signs, ...). | ||
| async fn authenticate(&self, request: &mut AuthRequest<'_>) -> Result<()>; | ||
|
|
||
| /// Drops any cached credentials so the next request re-authenticates. | ||
| /// | ||
| /// Backs the existing [`RestCatalog::invalidate_token`] API; not part of | ||
| /// the intended extension surface, and the default no-op is usually fine. | ||
| /// | ||
| /// [`RestCatalog::invalidate_token`]: crate::RestCatalog::invalidate_token | ||
| async fn invalidate(&self) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Proactively refreshes cached credentials (e.g. re-exchanges an OAuth2 | ||
| /// client credential for a new token), leaving them intact on failure. | ||
| /// | ||
| /// Like [`Self::invalidate`], backs [`RestCatalog::regenerate_token`]. | ||
| /// | ||
| /// [`RestCatalog::regenerate_token`]: crate::RestCatalog::regenerate_token | ||
| async fn refresh(&self) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// [`AuthManager`] that performs no authentication. | ||
| #[derive(Debug)] | ||
| pub struct NoopAuthManager; | ||
|
|
||
| /// [`AuthSession`] that performs no authentication. | ||
| #[derive(Debug)] | ||
| struct NoopSession; | ||
|
|
||
| #[async_trait] | ||
| impl AuthManager for NoopAuthManager { | ||
| async fn init_session(&self) -> Result<Arc<dyn AuthSession>> { | ||
| Ok(Arc::new(NoopSession)) | ||
| } | ||
|
|
||
| async fn catalog_session( | ||
| &self, | ||
| _props: &HashMap<String, String>, | ||
| ) -> Result<Arc<dyn AuthSession>> { | ||
| Ok(Arc::new(NoopSession)) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl AuthSession for NoopSession { | ||
| async fn authenticate(&self, _request: &mut AuthRequest<'_>) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
I wonder whether we should call out in the comments that these methods are only exposed for backwards-compatibility and that they aren't the intended main interface to work with going forward.
To give implementers of custom AuthManagers some guidance.
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.
@DerGut Good call — documented : both back the existin
invalidate_token/regenerate_tokenAPIs, not the intended extension surface.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.
Thank you!