From a20e2c120e25aa663bd37693afef26e732bf32b4 Mon Sep 17 00:00:00 2001 From: Li Jiajia Date: Wed, 25 Nov 2026 22:47:36 -0800 Subject: [PATCH] feat(storage/opendal): read vended s3.* properties in the native OSS backend REST catalogs vend S3-style credentials (`s3.*`) for OSS tables. When no `oss.*` property is set, the native OSS backend now reads `s3.endpoint`, `s3.access-key-id`, `s3.secret-access-key` and `s3.session-token`. The two sets are never mixed, so credentials only reach the endpoint they were configured with. Co-Authored-By: Claude Opus 5.5 --- crates/storage/opendal/src/oss.rs | 130 +++++++++++++++++++++++++++--- 1 file changed, 120 insertions(+), 10 deletions(-) diff --git a/crates/storage/opendal/src/oss.rs b/crates/storage/opendal/src/oss.rs index a1cf44549c..13f75ed3b5 100644 --- a/crates/storage/opendal/src/oss.rs +++ b/crates/storage/opendal/src/oss.rs @@ -17,7 +17,10 @@ use std::collections::HashMap; -use iceberg::io::{OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET, OSS_ENDPOINT}; +use iceberg::io::{ + OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET, OSS_ENDPOINT, S3_ACCESS_KEY_ID, S3_ENDPOINT, + S3_SECRET_ACCESS_KEY, S3_SESSION_TOKEN, +}; use iceberg::{Error, ErrorKind, Result}; use opendal::services::OssConfig; use opendal::{Configurator, Operator}; @@ -26,17 +29,25 @@ use url::Url; use crate::utils::from_opendal_error; /// Parse iceberg props to oss config. +/// +/// Without any `oss.*` property, reads the `s3.*` ones, which REST catalogs +/// vend for OSS. The two are never mixed, so credentials only reach the +/// endpoint they were configured with. pub(crate) fn oss_config_parse(mut m: HashMap) -> Result { let mut cfg: OssConfig = OssConfig::default(); - if let Some(endpoint) = m.remove(OSS_ENDPOINT) { - cfg.endpoint = Some(endpoint); - }; - if let Some(access_key_id) = m.remove(OSS_ACCESS_KEY_ID) { - cfg.access_key_id = Some(access_key_id); - }; - if let Some(access_key_secret) = m.remove(OSS_ACCESS_KEY_SECRET) { - cfg.access_key_secret = Some(access_key_secret); - }; + let has_oss_props = [OSS_ENDPOINT, OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET] + .iter() + .any(|key| m.contains_key(*key)); + if has_oss_props { + cfg.endpoint = m.remove(OSS_ENDPOINT); + cfg.access_key_id = m.remove(OSS_ACCESS_KEY_ID); + cfg.access_key_secret = m.remove(OSS_ACCESS_KEY_SECRET); + } else { + cfg.endpoint = m.remove(S3_ENDPOINT); + cfg.access_key_id = m.remove(S3_ACCESS_KEY_ID); + cfg.access_key_secret = m.remove(S3_SECRET_ACCESS_KEY); + cfg.security_token = m.remove(S3_SESSION_TOKEN); + } Ok(cfg) } @@ -55,3 +66,102 @@ pub(crate) fn oss_config_build(cfg: &OssConfig, path: &str) -> Result Operator::new(builder).map_err(from_opendal_error) } + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use super::*; + + const VENDED_S3_PROPS: [(&str, &str); 4] = [ + (S3_ENDPOINT, "https://oss-cn-hangzhou.aliyuncs.com"), + (S3_ACCESS_KEY_ID, "STS.key"), + (S3_SECRET_ACCESS_KEY, "secret"), + (S3_SESSION_TOKEN, "token"), + ]; + + fn props(pairs: &[(&str, &str)]) -> HashMap { + pairs + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect() + } + + #[test] + fn test_oss_config_parse_reads_s3_props() { + let mut expected = OssConfig::default(); + expected.endpoint = Some("https://oss-cn-hangzhou.aliyuncs.com".to_string()); + expected.access_key_id = Some("STS.key".to_string()); + expected.access_key_secret = Some("secret".to_string()); + expected.security_token = Some("token".to_string()); + + assert_eq!(oss_config_parse(props(&VENDED_S3_PROPS)).unwrap(), expected); + } + + #[test] + fn test_oss_config_parse_ignores_s3_props_with_any_oss_prop() { + for key in [OSS_ENDPOINT, OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET] { + let oss_only = props(&[(key, "value")]); + let mut with_s3 = props(&VENDED_S3_PROPS); + with_s3.extend(oss_only.clone()); + + assert_eq!( + oss_config_parse(with_s3).unwrap(), + oss_config_parse(oss_only).unwrap(), + "{key}" + ); + } + } + + #[test] + fn test_oss_config_parse_reads_oss_props() { + let mut expected = OssConfig::default(); + expected.endpoint = Some("https://oss-cn-beijing.aliyuncs.com".to_string()); + expected.access_key_id = Some("key".to_string()); + expected.access_key_secret = Some("secret".to_string()); + + let config = oss_config_parse(props(&[ + (OSS_ENDPOINT, "https://oss-cn-beijing.aliyuncs.com"), + (OSS_ACCESS_KEY_ID, "key"), + (OSS_ACCESS_KEY_SECRET, "secret"), + ])) + .unwrap(); + + assert_eq!(config, expected); + } + + #[test] + fn test_oss_config_build_rejects_s3_credentials_without_an_endpoint() { + let config = oss_config_parse(props(&VENDED_S3_PROPS[1..])).unwrap(); + + let err = oss_config_build(&config, "oss://bucket/key").unwrap_err(); + + assert!(err.to_string().contains("endpoint is empty"), "{err}"); + } + + #[tokio::test] + async fn test_s3_session_credentials_sign_native_oss_requests() { + let config = oss_config_parse(props(&VENDED_S3_PROPS)).unwrap(); + let operator = oss_config_build(&config, "oss://bucket/key").unwrap(); + + let request = operator + .presign_read("key", Duration::from_secs(60)) + .await + .unwrap(); + + let url = Url::parse(&request.uri().to_string()).unwrap(); + let query: HashMap<_, _> = url.query_pairs().into_owned().collect(); + assert_eq!( + ( + url.host_str(), + query.get("OSSAccessKeyId").map(String::as_str), + query.get("security-token").map(String::as_str), + ), + ( + Some("bucket.oss-cn-hangzhou.aliyuncs.com"), + Some("STS.key"), + Some("token"), + ) + ); + } +}