From 48e9b42b3c033c040fa120f556e2d05f3f533b4d Mon Sep 17 00:00:00 2001 From: Mike Williams Date: Wed, 11 Nov 2026 15:39:15 +1100 Subject: [PATCH] fix: report missing AWS credentials as an error, not a panic; lint against unwrap() get_aws_config() panicked when no AWS credentials could be found, printing a Debug dump and exiting 101. It now returns an ExpectedError naming each provider that was tried and why it failed, and exits 1. Enable clippy::unwrap_used (allowed in tests), so CI rejects new unwrap()s in production code, and replace the four in main.rs's tracing setup. --- Cargo.toml | 3 +++ clippy.toml | 2 ++ src/aws.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++------- src/main.rs | 31 +++++++++++++---------- 4 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 clippy.toml diff --git a/Cargo.toml b/Cargo.toml index c37550b..c6ab8e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,3 +37,6 @@ url = "2.5.7" [dev-dependencies] mockito = "1" + +[lints.clippy] +unwrap_used = "warn" diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..d4f7c47 --- /dev/null +++ b/clippy.toml @@ -0,0 +1,2 @@ +# Lints configured in Cargo.toml [lints.clippy] +allow-unwrap-in-tests = true diff --git a/src/aws.rs b/src/aws.rs index 3e2659a..b27fbac 100644 --- a/src/aws.rs +++ b/src/aws.rs @@ -1,5 +1,6 @@ //! AWS integration utilities for credential loading +use crate::error::ExpectedError; use crate::rest::rest_catalog_props; use crate::sigv4::{SigV4AuthManager, SigningScope}; use anyhow::{Context as _, Result}; @@ -78,21 +79,32 @@ fn s3_storage_factory(aws_config: &aws_config::SdkConfig) -> Arc aws_config::SdkConfig { +pub async fn get_aws_config() -> Result { let config = aws_config::defaults(BehaviorVersion::latest()) .credentials_provider(build_credentials_provider()) .load() .await; - // Try to resolve credentials to detect configuration errors early - if let Some(creds_provider) = config.credentials_provider() { - creds_provider - .provide_credentials() - .await - .expect("AWS credentials not configured"); - } + check_credentials(&config).await?; + Ok(config) +} - config +/// Resolve credentials now, so a missing or broken configuration fails early with a clear error. +pub async fn check_credentials(aws_config: &aws_config::SdkConfig) -> Result<()> { + let Some(provider) = aws_config.credentials_provider() else { + return Ok(()); + }; + provider + .provide_credentials() + .await + .map(|_| ()) + .map_err(|err| { + ExpectedError::UserInput(format!( + "AWS credentials not configured: {:#}", + anyhow::Error::new(err) + )) + .into() + }) } pub fn s3_file_io(aws_config: &aws_config::SdkConfig) -> FileIO { @@ -188,6 +200,49 @@ mod tests { .await } + #[derive(Debug)] + struct NoCredentials; + + impl ProvideCredentials for NoCredentials { + fn provide_credentials<'a>( + &'a self, + ) -> aws_credential_types::provider::future::ProvideCredentials<'a> + where + Self: 'a, + { + aws_credential_types::provider::future::ProvideCredentials::ready(Err( + aws_credential_types::provider::error::CredentialsError::not_loaded( + "no credentials here", + ), + )) + } + } + + #[tokio::test] + async fn check_credentials_reports_missing_credentials_as_expected_error() { + let aws_config = aws_config::defaults(BehaviorVersion::latest()) + .credentials_provider(NoCredentials) + .load() + .await; + + let err = check_credentials(&aws_config).await.unwrap_err(); + + let expected = err + .downcast_ref::() + .expect("should be an ExpectedError"); + assert!( + expected.to_string().contains("AWS credentials"), + "unexpected message: {expected}" + ); + } + + #[tokio::test] + async fn check_credentials_accepts_working_credentials() { + let aws_config = test_aws_config().await; + + assert!(check_credentials(&aws_config).await.is_ok()); + } + #[tokio::test] async fn test_s3_file_io_sets_region() { let aws_config = test_aws_config().await; diff --git a/src/main.rs b/src/main.rs index 00b43cf..531d092 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,23 +12,28 @@ use clap::Parser; use iceberg::io::FileIO; use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt}; +/// Logged at debug level with `--debug`: bergr itself, plus the AWS SDK's Glue, S3 and HTTP operations. +const DEBUG_TARGETS: [&str; 4] = ["bergr", "aws_sdk_glue", "aws_sdk_s3", "aws_smithy_runtime"]; #[tokio::main] async fn main() { let cli = Cli::parse(); if cli.debug { + let filter = DEBUG_TARGETS.iter().fold( + EnvFilter::builder() + .with_default_directive(tracing::Level::INFO.into()) + .from_env_lossy(), + |filter, target| { + filter.add_directive( + format!("{target}=debug") + .parse() + .expect("debug targets are valid directives"), + ) + }, + ); tracing_subscriber::registry() .with(fmt::layer().with_writer(std::io::stderr)) - .with( - EnvFilter::builder() - .with_default_directive(tracing::Level::INFO.into()) - .from_env_lossy() - .add_directive("bergr=debug".parse().unwrap()) - // AWS SDK tracing for Glue, S3, and HTTP operations - .add_directive("aws_sdk_glue=debug".parse().unwrap()) - .add_directive("aws_sdk_s3=debug".parse().unwrap()) - .add_directive("aws_smithy_runtime=debug".parse().unwrap()), - ) + .with(filter) .init(); } @@ -55,7 +60,7 @@ async fn run(command: Commands) -> Result<()> { Commands::From { location, command } => { let is_s3 = location.starts_with("s3://") || location.starts_with("s3a://"); let (file_io, s3) = if is_s3 { - let aws_config = get_aws_config().await; + let aws_config = get_aws_config().await?; ( s3_file_io(&aws_config), Some(aws_sdk_s3::Client::new(&aws_config)), @@ -68,7 +73,7 @@ async fn run(command: Commands) -> Result<()> { handle_table_command(&table, command, &mut output, s3.as_ref()).await?; } Commands::Glue { command } => { - let aws_config = get_aws_config().await; + let aws_config = get_aws_config().await?; let catalog = glue_catalog(&aws_config).await?; let s3 = aws_sdk_s3::Client::new(&aws_config); let mut output = TerminalOutput::new(); @@ -85,7 +90,7 @@ async fn run(command: Commands) -> Result<()> { match RestAuth::resolve(auth, &uri, |name| std::env::var(name).ok())? { RestAuth::SigV4(scope) => { let aws_config = get_aws_config() - .await + .await? .to_builder() .region(Region::new(scope.region.clone())) .build();