plusplusjiajia commented on code in PR #2838:
URL: https://github.com/apache/iceberg-rust/pull/2838#discussion_r3760159919
##########
crates/catalog/rest/src/client.rs:
##########
@@ -250,17 +107,20 @@ impl HttpClient {
.headers(self.extra_headers.clone())
}
- /// Executes the given `Request` and returns a `Response`.
- pub async fn execute(&self, mut request: Request) -> Result<Response> {
- request.headers_mut().extend(self.extra_headers.clone());
- Ok(self.client.execute(request).await?)
- }
-
// Queries the Iceberg REST catalog after authentication with the given
`Request` and
// returns a `Response`.
- pub async fn query_catalog(&self, mut request: Request) ->
Result<Response> {
- self.authenticate(&mut request).await?;
- self.execute(request).await
+ pub async fn query_catalog(
+ &self,
+ mut request: Request,
Review Comment:
@CTTY Done. I also took the "use it in most places" part: the catalog layer
builds HttpRequest everywhere now, so reqwest::Request only appears inside
HttpClient.
##########
crates/catalog/rest/src/client.rs:
##########
@@ -18,55 +18,43 @@
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
-use http::StatusCode;
use iceberg::{Error, ErrorKind, Result};
use reqwest::header::HeaderMap;
use reqwest::{Client, IntoUrl, Method, Request, RequestBuilder, Response};
use serde::de::DeserializeOwned;
-use tokio::sync::Mutex;
use crate::RestCatalogConfig;
-use crate::types::{ErrorResponse, TokenResponse};
+use crate::auth::{AuthSession, HttpRequest};
pub(crate) struct HttpClient {
client: Client,
- /// The token to be used for authentication.
- ///
- /// It's possible to fetch the token from the server while needed.
- token: Mutex<Option<String>>,
- /// The token endpoint to be used for authentication.
- token_endpoint: String,
- /// The credential to be used for authentication.
- credential: Option<(Option<String>, String)>,
/// Extra headers to be added to each request.
extra_headers: HeaderMap,
- /// Extra oauth parameters to be added to each authentication request.
- extra_oauth_params: HashMap<String, String>,
/// Whether to disable header redaction in error logs (defaults to false
for security).
disable_header_redaction: bool,
Review Comment:
@CTTY Yes — I'd moved the session out to RestContext along with the manager,
one step too far. It's back on HttpClient as an Arc<dyn AuthSession> that
authenticates everything the client sends; only the manager and its resolution
live on RestCatalog.
##########
crates/catalog/rest/src/auth/mod.rs:
##########
@@ -0,0 +1,313 @@
+// 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.
+ ///
+ /// Returns a [`Box`]: an init session is used once and released, unlike
+ /// the shared [`AuthManager::catalog_session`].
+ async fn init_session(&self) -> Result<Box<dyn AuthSession>>;
Review Comment:
@DerGut Agreed, and done — the trait takes HttpClient, and post_form returns
status + bytes, so no reqwest types in it.
--
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]