Skip to content

Add initial AuthManager support #1512

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/catalog/rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ repository = { workspace = true }

[dependencies]
async-trait = { workspace = true }
base64 = { workspace = true }
chrono = { workspace = true }
http = { workspace = true }
iceberg = { workspace = true }
Expand All @@ -44,6 +45,7 @@ typed-builder = { workspace = true }
uuid = { workspace = true, features = ["v4"] }

[dev-dependencies]
base64 = { workspace = true }
ctor = { workspace = true }
iceberg_test_utils = { path = "../../test_utils", features = ["tests"] }
mockito = { workspace = true }
Expand Down
60 changes: 60 additions & 0 deletions crates/catalog/rest/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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.

use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose;

/// Trait for authentication managers that supply authorization headers.
#[async_trait]
pub trait AuthManager: Send + Sync + std::fmt::Debug {
/// Return the Authorization header value, or None if not applicable.
async fn auth_header(&self) -> Option<String>;
}

/// An `AuthManager` that performs no authentication.
#[derive(Debug, Default)]
pub struct NoopAuthManager;

#[async_trait]
impl AuthManager for NoopAuthManager {
async fn auth_header(&self) -> Option<String> {
None
}
}

/// An `AuthManager` for basic authentication.
#[derive(Debug)]
pub struct BasicAuthManager {
token: String,
}

impl BasicAuthManager {
/// Create a new `BasicAuthManager`.
pub fn new(username: &str, password: &str) -> Self {
let credentials = format!("{}:{}", username, password);
let token = general_purpose::STANDARD.encode(credentials.as_bytes());
BasicAuthManager { token }
}
}

#[async_trait]
impl AuthManager for BasicAuthManager {
async fn auth_header(&self) -> Option<String> {
Some(format!("Basic {}", self.token))
}
}
2 changes: 2 additions & 0 deletions crates/catalog/rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#![deny(missing_docs)]

/// Authentication for the REST catalog.
pub mod auth;
mod catalog;
mod client;
mod types;
Expand Down
38 changes: 38 additions & 0 deletions crates/catalog/rest/tests/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.

use base64::Engine;
use iceberg_catalog_rest::auth::{AuthManager, BasicAuthManager, NoopAuthManager};

#[tokio::test]
async fn test_noop_auth_manager() {
let auth_manager = NoopAuthManager;
let header = auth_manager.auth_header().await;
assert!(header.is_none());
}

#[tokio::test]
async fn test_basic_auth_manager() {
let username = "testuser";
let password = "testpassword";
let auth_manager = BasicAuthManager::new(username, password);
let header = auth_manager.auth_header().await;
assert!(header.is_some());
let expected_token = base64::engine::general_purpose::STANDARD
.encode(format!("{}:{}", username, password).as_bytes());
assert_eq!(header.unwrap(), format!("Basic {}", expected_token));
}
Loading