Skip to content
Merged
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
53 changes: 53 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ aws-credential-types = "1.2.10"
aws-sdk-s3 = "1.115.0"
aws-sigv4 = "1.6.3"
aws-smithy-runtime-api = "1.19.1"
base64 = "0.22.1"
bytes = "1.11.0"
clap = { version = "4.5.53", features = ["derive"] }
clap = { version = "4.5.53", features = ["derive", "env"] }
futures = "0.3.31"
http = "1.3.1"
iceberg = "0.11.0"
Expand All @@ -33,3 +34,6 @@ tokio = { version = "1.48.0", features = ["full"] }
tracing = "0.1.41"
tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
url = "2.5.7"

[dev-dependencies]
mockito = "1"
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ bergr rest https://glue.us-west-2.amazonaws.com/iceberg --warehouse 123456789012

Glue's REST endpoint needs `glue:GetCatalog` and `lakeformation:GetDataAccess` permissions, in addition to those the `glue` command needs.

#### Authentication

Choose how to authenticate with `--auth`, or set `BERGR_REST_AUTH`. Each type reads its settings, including secrets, from environment variables, so secrets never appear in shell history or `ps`.

| `--auth` | Reads |
|---|---|
| `auto` (default) | Behaves as `sigv4` for AWS Glue endpoints, otherwise `none` |
| `none` | nothing |
| `sigv4` | the standard AWS credential variables and profile |
| `bearer` | `BERGR_REST_BEARER_TOKEN` |
| `basic` | `BERGR_REST_BASIC_USER`, `BERGR_REST_BASIC_PASSWORD` |
| `oauth2` | `BERGR_REST_OAUTH2_CREDENTIAL` (`client_id:secret`); optionally `BERGR_REST_OAUTH2_SCOPE` and `BERGR_REST_OAUTH2_SERVER_URI` |

```bash
BERGR_REST_BEARER_TOKEN="$(cat ~/.config/my-catalog-token)" \
bergr rest --auth bearer https://catalog.example.com namespaces
```

With `oauth2`, bergr exchanges the credential for a token at `<uri>/v1/oauth/tokens` (or `BERGR_REST_OAUTH2_SERVER_URI`, if set), asking for the `catalog` scope unless `BERGR_REST_OAUTH2_SCOPE` says otherwise. Some catalogs need a specific scope: [Apache Polaris](https://polaris.apache.org/), for example, wants `PRINCIPAL_ROLE:ALL`.

```bash
BERGR_REST_OAUTH2_CREDENTIAL="$(cat ~/.config/polaris-credential)" BERGR_REST_OAUTH2_SCOPE=PRINCIPAL_ROLE:ALL \
bergr rest --auth oauth2 http://localhost:8181/api/catalog --warehouse quickstart_catalog namespaces
```

`bearer` and `oauth2` correspond to the Iceberg REST catalog properties `token`, `credential`, `scope` and `oauth2-server-uri`. `sigv4` currently only supports AWS Glue endpoints (`glue.<region>.amazonaws.com`).

### Direct access via metadata file location

```bash
Expand Down
95 changes: 95 additions & 0 deletions src/basic_auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//! HTTP basic authentication for REST catalogs, which iceberg-catalog-rest doesn't provide

use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use http::HeaderValue;
use http::header::AUTHORIZATION;
use iceberg::sensitive::SensitiveString;
use iceberg::{Error, ErrorKind, Result};
use iceberg_catalog_rest::{AuthManager, AuthSession, HttpClient, HttpRequest};
use std::collections::HashMap;
use std::sync::Arc;

/// An [`AuthManager`] that adds an `Authorization: Basic …` header to every request.
#[derive(Debug, Clone)]
pub struct BasicAuthManager {
session: Arc<BasicAuthSession>,
}

impl BasicAuthManager {
pub fn new(user: &str, password: &SensitiveString) -> Result<Self> {
let credentials = STANDARD.encode(format!("{user}:{}", password.expose()));
let mut header = HeaderValue::try_from(format!("Basic {credentials}")).map_err(|e| {
Error::new(ErrorKind::DataInvalid, "invalid basic auth header").with_source(e)
})?;
header.set_sensitive(true);
Ok(Self {
session: Arc::new(BasicAuthSession { header }),
})
}
}

#[async_trait]
impl AuthManager for BasicAuthManager {
async fn init_session(
&self,
_client: &HttpClient,
_props: &HashMap<String, String>,
) -> Result<Box<dyn AuthSession>> {
Ok(Box::new(self.session.as_ref().clone()))
}

async fn catalog_session(
&self,
_client: &HttpClient,
_props: &HashMap<String, String>,
) -> Result<Arc<dyn AuthSession>> {
Ok(self.session.clone())
}
}

/// Holds the header value marked sensitive, so `Debug` output doesn't show it.
#[derive(Debug, Clone)]
struct BasicAuthSession {
header: HeaderValue,
}

#[async_trait]
impl AuthSession for BasicAuthSession {
async fn authenticate(&self, request: &mut HttpRequest) -> Result<()> {
request
.headers_mut()
.insert(AUTHORIZATION, self.header.clone());
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

fn manager() -> BasicAuthManager {
BasicAuthManager::new("alice", &"s3cret".to_string().into()).unwrap()
}

#[tokio::test]
async fn adds_basic_auth_header() {
let mut request = HttpRequest::new(
reqwest::Client::new()
.get("https://catalog.example.com/v1/config")
.build()
.unwrap(),
);
manager().session.authenticate(&mut request).await.unwrap();

// "alice:s3cret", base64-encoded
assert_eq!(request.headers()[AUTHORIZATION], "Basic YWxpY2U6czNjcmV0");
}

#[test]
fn debug_output_redacts_credentials() {
let debug = format!("{:?}", manager());
assert!(!debug.contains("YWxpY2U6czNjcmV0"), "{debug}");
}
}
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::rest_auth::AuthType;
use clap::{Parser, Subcommand};

/// bergr: A tool for inspecting Apache Iceberg tables
Expand Down Expand Up @@ -36,6 +37,11 @@ pub enum Commands {
#[arg(long)]
warehouse: Option<String>,

/// How to authenticate to the catalog. Settings, including secrets, are read from
/// environment variables, never from arguments.
#[arg(long, value_enum, env = "BERGR_REST_AUTH", default_value_t = AuthType::Auto)]
auth: AuthType,

#[command(subcommand)]
command: CatalogCommands,
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod aws;
pub mod basic_auth;
pub mod catalog_commands;
pub mod cli;
pub mod error;
pub mod file_existence;
pub mod rest;
pub mod rest_auth;
pub mod sigv4;
pub mod table_commands;
pub mod terminal_output;
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bergr::catalog_commands::handle_catalog_command;
use bergr::cli::{Cli, Commands};
use bergr::error::ExpectedError;
use bergr::rest::rest_catalog;
use bergr::sigv4::SigningScope;
use bergr::rest_auth::RestAuth;
use bergr::table_commands::{handle_table_command, load_table};
use bergr::terminal_output::TerminalOutput;
use clap::Parser;
Expand Down Expand Up @@ -77,11 +77,12 @@ async fn run(command: Commands) -> Result<()> {
Commands::Rest {
uri,
warehouse,
auth,
command,
} => {
let mut output = TerminalOutput::new();
match SigningScope::from_uri(&uri) {
Some(scope) => {
match RestAuth::resolve(auth, &uri, |name| std::env::var(name).ok())? {
RestAuth::SigV4(scope) => {
let aws_config = get_aws_config()
.await
.to_builder()
Expand All @@ -92,8 +93,8 @@ async fn run(command: Commands) -> Result<()> {
let s3 = aws_sdk_s3::Client::new(&aws_config);
handle_catalog_command(&catalog, command, &mut output, Some(&s3)).await?;
}
None => {
let catalog = rest_catalog(&uri, warehouse.as_deref()).await?;
RestAuth::Http(auth) => {
let catalog = rest_catalog(&uri, warehouse.as_deref(), &auth).await?;
handle_catalog_command(&catalog, command, &mut output, None).await?;
}
}
Expand Down
Loading
Loading