Skip to main content

lance_namespace_impls/
credentials.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! Credential vending for cloud storage access.
5//!
6//! This module provides credential vending functionality that generates
7//! temporary, scoped credentials for accessing cloud storage. Similar to
8//! Apache Polaris's credential vending, it supports:
9//!
10//! - **AWS**: STS AssumeRole with scoped IAM policies (requires `credential-vendor-aws` feature)
11//! - **GCP**: OAuth2 tokens with access boundaries (requires `credential-vendor-gcp` feature)
12//! - **Azure**: SAS tokens with user delegation keys (requires `credential-vendor-azure` feature)
13//!
14//! The appropriate vendor is automatically selected based on the table location URI scheme:
15//! - `s3://` for AWS
16//! - `gs://` for GCP
17//! - `az://` for Azure
18//!
19//! ## Configuration via Properties
20//!
21//! Credential vendors are configured via properties with the `credential_vendor.` prefix.
22//!
23//! ### Properties format:
24//!
25//! ```text
26//! # Required to enable credential vending
27//! credential_vendor.enabled = "true"
28//!
29//! # Common properties (apply to all providers)
30//! credential_vendor.permission = "read"          # read, write, or admin (default: read)
31//!
32//! # AWS-specific properties (for s3:// locations)
33//! credential_vendor.aws_role_arn = "arn:aws:iam::123456789012:role/MyRole"  # required for AWS
34//! credential_vendor.aws_external_id = "my-external-id"
35//! credential_vendor.aws_region = "us-west-2"
36//! credential_vendor.aws_role_session_name = "my-session"
37//! credential_vendor.aws_duration_millis = "3600000"  # 1 hour (default, range: 15min-12hrs)
38//!
39//! # GCP-specific properties (for gs:// locations)
40//! # Note: GCP token duration cannot be configured; it's determined by the STS endpoint
41//! # To use a service account key file, set GOOGLE_APPLICATION_CREDENTIALS env var before starting
42//! credential_vendor.gcp_service_account = "my-sa@project.iam.gserviceaccount.com"
43//!
44//! # Azure-specific properties (for az:// locations)
45//! credential_vendor.azure_account_name = "mystorageaccount"  # required for Azure
46//! credential_vendor.azure_tenant_id = "my-tenant-id"
47//! credential_vendor.azure_duration_millis = "3600000"  # 1 hour (default, up to 7 days)
48//! ```
49//!
50//! ### Example using ConnectBuilder:
51//!
52//! ```ignore
53//! ConnectBuilder::new("dir")
54//!     .property("root", "s3://bucket/path")
55//!     .property("credential_vendor.enabled", "true")
56//!     .property("credential_vendor.aws_role_arn", "arn:aws:iam::123456789012:role/MyRole")
57//!     .property("credential_vendor.permission", "read")
58//!     .connect()
59//!     .await?;
60//! ```
61
62#[cfg(feature = "credential-vendor-aws")]
63pub mod aws;
64
65#[cfg(feature = "credential-vendor-azure")]
66pub mod azure;
67
68#[cfg(feature = "credential-vendor-gcp")]
69pub mod gcp;
70
71/// Credential caching module.
72/// Available when any credential vendor feature is enabled.
73#[cfg(any(
74    feature = "credential-vendor-aws",
75    feature = "credential-vendor-azure",
76    feature = "credential-vendor-gcp"
77))]
78pub mod cache;
79
80use std::collections::HashMap;
81use std::str::FromStr;
82
83use async_trait::async_trait;
84use lance_core::Result;
85use lance_io::object_store::uri_to_url;
86use lance_namespace::models::Identity;
87
88/// Default credential duration: 1 hour (3600000 milliseconds)
89pub const DEFAULT_CREDENTIAL_DURATION_MILLIS: u64 = 3600 * 1000;
90
91/// Redact a credential string for logging, showing first and last few characters.
92///
93/// This is useful for debugging while avoiding exposure of sensitive data.
94/// Format: `AKIAIOSF***MPLE` (first 8 + "***" + last 4)
95///
96/// Shows 8 characters at the start (useful since AWS keys always start with AKIA/ASIA)
97/// and 4 characters at the end. For short strings, shows only the first few with "***".
98///
99/// # Security Note
100///
101/// This function should only be used for identifiers and tokens, never for secrets
102/// like `aws_secret_access_key` which should never be logged even in redacted form.
103pub fn redact_credential(credential: &str) -> String {
104    const SHOW_START: usize = 8;
105    const SHOW_END: usize = 4;
106    const MIN_LENGTH_FOR_BOTH_ENDS: usize = SHOW_START + SHOW_END + 4; // Need at least 16 chars
107
108    if credential.is_empty() {
109        return "[empty]".to_string();
110    }
111
112    if credential.len() < MIN_LENGTH_FOR_BOTH_ENDS {
113        // For short credentials, just show beginning
114        let show = credential.len().min(SHOW_START);
115        format!("{}***", &credential[..show])
116    } else {
117        // Show first 8 and last 4 characters
118        format!(
119            "{}***{}",
120            &credential[..SHOW_START],
121            &credential[credential.len() - SHOW_END..]
122        )
123    }
124}
125
126/// Permission level for vended credentials.
127///
128/// This determines what access the vended credentials will have:
129/// - `Read`: Read-only access to all table content
130/// - `Write`: Full read and write access (no delete)
131/// - `Admin`: Full read, write, and delete access
132///
133/// Permission enforcement by cloud provider:
134/// - **AWS**: Permissions are enforced via scoped IAM policies attached to the AssumeRole request
135/// - **Azure**: Permissions are enforced via SAS token permissions
136/// - **GCP**: Permissions are enforced via Credential Access Boundaries (CAB) that downscope
137///   the OAuth2 token to specific GCS IAM roles
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
139pub enum VendedPermission {
140    /// Read-only access to all table content (metadata, indices, data files)
141    #[default]
142    Read,
143    /// Full read and write access (no delete)
144    /// This is intended ONLY for testing purposes to generate a write-only permission set.
145    /// Technically, any user with write permission could "delete" the file by
146    /// overwriting the file with empty content.
147    /// So this cannot really prevent malicious use cases.
148    Write,
149    /// Full read, write, and delete access
150    Admin,
151}
152
153impl VendedPermission {
154    /// Returns true if this permission allows writing
155    pub fn can_write(&self) -> bool {
156        matches!(self, Self::Write | Self::Admin)
157    }
158
159    /// Returns true if this permission allows deleting
160    pub fn can_delete(&self) -> bool {
161        matches!(self, Self::Admin)
162    }
163}
164
165impl FromStr for VendedPermission {
166    type Err = String;
167
168    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
169        match s.to_lowercase().as_str() {
170            "read" => Ok(Self::Read),
171            "write" => Ok(Self::Write),
172            "admin" => Ok(Self::Admin),
173            _ => Err(format!(
174                "Invalid permission '{}'. Must be one of: read, write, admin",
175                s
176            )),
177        }
178    }
179}
180
181impl std::fmt::Display for VendedPermission {
182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183        match self {
184            Self::Read => write!(f, "read"),
185            Self::Write => write!(f, "write"),
186            Self::Admin => write!(f, "admin"),
187        }
188    }
189}
190
191/// Property key prefix for credential vendor properties.
192/// Properties with this prefix are stripped when using `from_properties`.
193pub const PROPERTY_PREFIX: &str = "credential_vendor.";
194
195/// Common property key to explicitly enable credential vending (short form).
196pub const ENABLED: &str = "enabled";
197
198/// Common property key for permission level (short form).
199pub const PERMISSION: &str = "permission";
200
201/// Common property key to enable credential caching (short form).
202/// Default: true. Set to "false" to disable caching.
203pub const CACHE_ENABLED: &str = "cache_enabled";
204
205/// Common property key for API key salt (short form).
206/// Used to hash API keys before comparison: SHA256(api_key + ":" + salt)
207pub const API_KEY_SALT: &str = "api_key_salt";
208
209/// Property key prefix for API key hash to permission mappings (short form).
210/// Format: `api_key_hash.<sha256_hash> = "<permission>"`
211pub const API_KEY_HASH_PREFIX: &str = "api_key_hash.";
212
213/// AWS-specific property keys (short form, without prefix)
214#[cfg(feature = "credential-vendor-aws")]
215pub mod aws_props {
216    pub const ROLE_ARN: &str = "aws_role_arn";
217    pub const EXTERNAL_ID: &str = "aws_external_id";
218    pub const REGION: &str = "aws_region";
219    pub const ROLE_SESSION_NAME: &str = "aws_role_session_name";
220    /// AWS credential duration in milliseconds.
221    /// Default: 3600000 (1 hour). Range: 900000 (15 min) to 43200000 (12 hours).
222    pub const DURATION_MILLIS: &str = "aws_duration_millis";
223}
224
225/// GCP-specific property keys (short form, without prefix)
226#[cfg(feature = "credential-vendor-gcp")]
227pub mod gcp_props {
228    pub const SERVICE_ACCOUNT: &str = "gcp_service_account";
229
230    /// Workload Identity Provider resource name for OIDC token exchange.
231    /// Format: //iam.googleapis.com/projects/{project}/locations/global/workloadIdentityPools/{pool}/providers/{provider}
232    pub const WORKLOAD_IDENTITY_PROVIDER: &str = "gcp_workload_identity_provider";
233
234    /// Service account to impersonate after Workload Identity Federation (optional).
235    /// If not set, uses the federated identity directly.
236    pub const IMPERSONATION_SERVICE_ACCOUNT: &str = "gcp_impersonation_service_account";
237}
238
239/// Azure-specific property keys (short form, without prefix)
240#[cfg(feature = "credential-vendor-azure")]
241pub mod azure_props {
242    pub const TENANT_ID: &str = "azure_tenant_id";
243    /// Azure storage account name. Required for credential vending.
244    pub const ACCOUNT_NAME: &str = "azure_account_name";
245    /// Azure credential duration in milliseconds.
246    /// Default: 3600000 (1 hour). Azure SAS tokens can be valid up to 7 days.
247    pub const DURATION_MILLIS: &str = "azure_duration_millis";
248
249    /// Client ID of the Azure AD App Registration for Workload Identity Federation.
250    /// Required when using auth_token identity for OIDC token exchange.
251    pub const FEDERATED_CLIENT_ID: &str = "azure_federated_client_id";
252}
253
254/// Vended credentials with expiration information.
255#[derive(Clone)]
256pub struct VendedCredentials {
257    /// Storage options map containing credential keys.
258    /// - For AWS: `aws_access_key_id`, `aws_secret_access_key`, `aws_session_token`
259    /// - For GCP: `google_storage_token`
260    /// - For Azure: `azure_storage_sas_token`, `azure_storage_account_name`
261    pub storage_options: HashMap<String, String>,
262
263    /// Expiration time in milliseconds since Unix epoch.
264    pub expires_at_millis: u64,
265}
266
267impl std::fmt::Debug for VendedCredentials {
268    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
269        f.debug_struct("VendedCredentials")
270            .field(
271                "storage_options",
272                &format!("[{} keys redacted]", self.storage_options.len()),
273            )
274            .field("expires_at_millis", &self.expires_at_millis)
275            .finish()
276    }
277}
278
279impl VendedCredentials {
280    /// Create new vended credentials.
281    pub fn new(storage_options: HashMap<String, String>, expires_at_millis: u64) -> Self {
282        Self {
283            storage_options,
284            expires_at_millis,
285        }
286    }
287
288    /// Check if the credentials have expired.
289    pub fn is_expired(&self) -> bool {
290        let now_millis = std::time::SystemTime::now()
291            .duration_since(std::time::UNIX_EPOCH)
292            .expect("time went backwards")
293            .as_millis() as u64;
294        now_millis >= self.expires_at_millis
295    }
296}
297
298/// Trait for credential vendors that generate temporary credentials.
299///
300/// Each cloud provider has its own configuration passed via the vendor
301/// implementation. The permission level is configured at vendor creation time
302/// via [`VendedPermission`].
303#[async_trait]
304pub trait CredentialVendor: Send + Sync + std::fmt::Debug {
305    /// Vend credentials for accessing the specified table location.
306    ///
307    /// The permission level (read/write/admin) is determined by the vendor's
308    /// configuration, not per-request. When identity is provided, the vendor
309    /// may use different authentication flows:
310    ///
311    /// - `auth_token`: Use AssumeRoleWithWebIdentity (AWS validates the token)
312    /// - `api_key`: Validate against configured API key hashes and use AssumeRole
313    /// - `None`: Use static configuration with AssumeRole
314    ///
315    /// # Arguments
316    ///
317    /// * `table_location` - The table URI to vend credentials for
318    /// * `identity` - Optional identity from the request (api_key OR auth_token, mutually exclusive)
319    ///
320    /// # Returns
321    ///
322    /// Returns vended credentials with expiration information.
323    ///
324    /// # Errors
325    ///
326    /// Returns error if identity validation fails (no fallback to static config).
327    async fn vend_credentials(
328        &self,
329        table_location: &str,
330        identity: Option<&Identity>,
331    ) -> Result<VendedCredentials>;
332
333    /// Returns the cloud provider name (e.g., "aws", "gcp", "azure").
334    fn provider_name(&self) -> &'static str;
335
336    /// Returns the permission level configured for this vendor.
337    fn permission(&self) -> VendedPermission;
338}
339
340/// Detect the cloud provider from a URI scheme.
341///
342/// Supported schemes for credential vending:
343/// - AWS S3: `s3://`
344/// - GCP GCS: `gs://`
345/// - Azure Blob: `az://`
346///
347/// Returns "aws", "gcp", "azure", or "unknown".
348pub fn detect_provider_from_uri(uri: &str) -> &'static str {
349    let Ok(url) = uri_to_url(uri) else {
350        return "unknown";
351    };
352
353    match url.scheme() {
354        "s3" => "aws",
355        "gs" => "gcp",
356        "az" => "azure",
357        _ => "unknown",
358    }
359}
360
361/// Check if credential vending is enabled.
362///
363/// Returns true only if the `enabled` property is set to "true".
364/// This expects properties with short names (prefix already stripped).
365pub fn has_credential_vendor_config(properties: &HashMap<String, String>) -> bool {
366    properties
367        .get(ENABLED)
368        .map(|v| v.eq_ignore_ascii_case("true"))
369        .unwrap_or(false)
370}
371
372/// Create a credential vendor for the specified table location based on its URI scheme.
373///
374/// This function automatically detects the cloud provider from the table location
375/// and creates the appropriate credential vendor using the provided properties.
376///
377/// # Arguments
378///
379/// * `table_location` - The table URI to create a vendor for (e.g., "s3://bucket/path")
380/// * `properties` - Configuration properties for credential vendors
381///
382/// # Returns
383///
384/// Returns `Some(vendor)` if the provider is detected and configured, `None` if:
385/// - The provider cannot be detected from the URI (e.g., local file path)
386/// - The required feature is not enabled for the detected provider
387///
388/// # Errors
389///
390/// Returns an error if the provider is detected but required configuration is missing:
391/// - AWS: `credential_vendor.aws_role_arn` is required
392/// - Azure: `credential_vendor.azure_account_name` is required
393#[allow(unused_variables)]
394pub async fn create_credential_vendor_for_location(
395    table_location: &str,
396    properties: &HashMap<String, String>,
397) -> Result<Option<Box<dyn CredentialVendor>>> {
398    let provider = detect_provider_from_uri(table_location);
399
400    let vendor: Option<Box<dyn CredentialVendor>> = match provider {
401        #[cfg(feature = "credential-vendor-aws")]
402        "aws" => create_aws_vendor(properties).await?,
403
404        #[cfg(feature = "credential-vendor-gcp")]
405        "gcp" => create_gcp_vendor(properties).await?,
406
407        #[cfg(feature = "credential-vendor-azure")]
408        "azure" => create_azure_vendor(properties)?,
409
410        _ => None,
411    };
412
413    // Wrap with caching if enabled (default: true)
414    #[cfg(any(
415        feature = "credential-vendor-aws",
416        feature = "credential-vendor-azure",
417        feature = "credential-vendor-gcp"
418    ))]
419    if let Some(v) = vendor {
420        let cache_enabled = properties
421            .get(CACHE_ENABLED)
422            .map(|s| !s.eq_ignore_ascii_case("false"))
423            .unwrap_or(true);
424
425        if cache_enabled {
426            return Ok(Some(Box::new(cache::CachingCredentialVendor::new(v))));
427        } else {
428            return Ok(Some(v));
429        }
430    }
431
432    #[cfg(not(any(
433        feature = "credential-vendor-aws",
434        feature = "credential-vendor-azure",
435        feature = "credential-vendor-gcp"
436    )))]
437    let _ = vendor;
438
439    Ok(None)
440}
441
442/// Parse permission from properties, defaulting to Read
443#[allow(dead_code)]
444fn parse_permission(properties: &HashMap<String, String>) -> VendedPermission {
445    properties
446        .get(PERMISSION)
447        .and_then(|s| s.parse().ok())
448        .unwrap_or_default()
449}
450
451/// Parse duration from properties using a vendor-specific key, defaulting to DEFAULT_CREDENTIAL_DURATION_MILLIS
452#[allow(dead_code)]
453fn parse_duration_millis(properties: &HashMap<String, String>, key: &str) -> u64 {
454    properties
455        .get(key)
456        .and_then(|s| s.parse::<u64>().ok())
457        .unwrap_or(DEFAULT_CREDENTIAL_DURATION_MILLIS)
458}
459
460#[cfg(feature = "credential-vendor-aws")]
461async fn create_aws_vendor(
462    properties: &HashMap<String, String>,
463) -> Result<Option<Box<dyn CredentialVendor>>> {
464    use aws::{AwsCredentialVendor, AwsCredentialVendorConfig};
465    use lance_core::Error;
466
467    // AWS requires role_arn to be configured
468    let role_arn = properties
469        .get(aws_props::ROLE_ARN)
470        .ok_or_else(|| Error::InvalidInput {
471            source: "AWS credential vending requires 'credential_vendor.aws_role_arn' to be set"
472                .into(),
473            location: snafu::location!(),
474        })?;
475
476    let duration_millis = parse_duration_millis(properties, aws_props::DURATION_MILLIS);
477
478    let permission = parse_permission(properties);
479
480    let mut config = AwsCredentialVendorConfig::new(role_arn)
481        .with_duration_millis(duration_millis)
482        .with_permission(permission);
483
484    if let Some(external_id) = properties.get(aws_props::EXTERNAL_ID) {
485        config = config.with_external_id(external_id);
486    }
487    if let Some(region) = properties.get(aws_props::REGION) {
488        config = config.with_region(region);
489    }
490    if let Some(session_name) = properties.get(aws_props::ROLE_SESSION_NAME) {
491        config = config.with_role_session_name(session_name);
492    }
493
494    let vendor = AwsCredentialVendor::new(config).await?;
495    Ok(Some(Box::new(vendor)))
496}
497
498#[cfg(feature = "credential-vendor-gcp")]
499async fn create_gcp_vendor(
500    properties: &HashMap<String, String>,
501) -> Result<Option<Box<dyn CredentialVendor>>> {
502    use gcp::{GcpCredentialVendor, GcpCredentialVendorConfig};
503
504    let permission = parse_permission(properties);
505
506    let mut config = GcpCredentialVendorConfig::new().with_permission(permission);
507
508    if let Some(sa) = properties.get(gcp_props::SERVICE_ACCOUNT) {
509        config = config.with_service_account(sa);
510    }
511
512    let vendor = GcpCredentialVendor::new(config).await?;
513    Ok(Some(Box::new(vendor)))
514}
515
516#[cfg(feature = "credential-vendor-azure")]
517fn create_azure_vendor(
518    properties: &HashMap<String, String>,
519) -> Result<Option<Box<dyn CredentialVendor>>> {
520    use azure::{AzureCredentialVendor, AzureCredentialVendorConfig};
521    use lance_core::Error;
522
523    // Azure requires account_name to be configured
524    let account_name =
525        properties
526            .get(azure_props::ACCOUNT_NAME)
527            .ok_or_else(|| {
528                Error::InvalidInput {
529            source:
530                "Azure credential vending requires 'credential_vendor.azure_account_name' to be set"
531                    .into(),
532            location: snafu::location!(),
533        }
534            })?;
535
536    let duration_millis = parse_duration_millis(properties, azure_props::DURATION_MILLIS);
537    let permission = parse_permission(properties);
538
539    let mut config = AzureCredentialVendorConfig::new()
540        .with_account_name(account_name)
541        .with_duration_millis(duration_millis)
542        .with_permission(permission);
543
544    if let Some(tenant_id) = properties.get(azure_props::TENANT_ID) {
545        config = config.with_tenant_id(tenant_id);
546    }
547
548    let vendor = AzureCredentialVendor::new(config);
549    Ok(Some(Box::new(vendor)))
550}
551
552#[cfg(test)]
553mod tests {
554    use super::*;
555
556    #[test]
557    fn test_detect_provider_from_uri() {
558        // AWS (supported scheme: s3://)
559        assert_eq!(detect_provider_from_uri("s3://bucket/path"), "aws");
560        assert_eq!(detect_provider_from_uri("S3://bucket/path"), "aws");
561
562        // GCP (supported scheme: gs://)
563        assert_eq!(detect_provider_from_uri("gs://bucket/path"), "gcp");
564        assert_eq!(detect_provider_from_uri("GS://bucket/path"), "gcp");
565
566        // Azure (supported scheme: az://)
567        assert_eq!(detect_provider_from_uri("az://container/path"), "azure");
568
569        // Unknown (unsupported schemes)
570        assert_eq!(detect_provider_from_uri("/local/path"), "unknown");
571        assert_eq!(detect_provider_from_uri("file:///local/path"), "unknown");
572        assert_eq!(detect_provider_from_uri("memory://test"), "unknown");
573        // Hadoop-style schemes not supported by lance-io
574        assert_eq!(detect_provider_from_uri("s3a://bucket/path"), "unknown");
575        assert_eq!(
576            detect_provider_from_uri("abfss://container@account.dfs.core.windows.net/path"),
577            "unknown"
578        );
579        assert_eq!(
580            detect_provider_from_uri("wasbs://container@account.blob.core.windows.net/path"),
581            "unknown"
582        );
583    }
584
585    #[test]
586    fn test_vended_permission_from_str() {
587        // Valid values (case-insensitive)
588        assert_eq!(
589            "read".parse::<VendedPermission>().unwrap(),
590            VendedPermission::Read
591        );
592        assert_eq!(
593            "READ".parse::<VendedPermission>().unwrap(),
594            VendedPermission::Read
595        );
596        assert_eq!(
597            "write".parse::<VendedPermission>().unwrap(),
598            VendedPermission::Write
599        );
600        assert_eq!(
601            "WRITE".parse::<VendedPermission>().unwrap(),
602            VendedPermission::Write
603        );
604        assert_eq!(
605            "admin".parse::<VendedPermission>().unwrap(),
606            VendedPermission::Admin
607        );
608        assert_eq!(
609            "Admin".parse::<VendedPermission>().unwrap(),
610            VendedPermission::Admin
611        );
612
613        // Invalid values should return error
614        let err = "invalid".parse::<VendedPermission>().unwrap_err();
615        assert!(err.contains("Invalid permission"));
616        assert!(err.contains("invalid"));
617
618        let err = "".parse::<VendedPermission>().unwrap_err();
619        assert!(err.contains("Invalid permission"));
620
621        let err = "readwrite".parse::<VendedPermission>().unwrap_err();
622        assert!(err.contains("Invalid permission"));
623    }
624
625    #[test]
626    fn test_vended_permission_display() {
627        assert_eq!(VendedPermission::Read.to_string(), "read");
628        assert_eq!(VendedPermission::Write.to_string(), "write");
629        assert_eq!(VendedPermission::Admin.to_string(), "admin");
630    }
631
632    #[test]
633    fn test_parse_permission_with_invalid_values() {
634        // Invalid permission should default to Read
635        let mut props = HashMap::new();
636        props.insert(PERMISSION.to_string(), "invalid".to_string());
637        assert_eq!(parse_permission(&props), VendedPermission::Read);
638
639        // Empty permission should default to Read
640        props.insert(PERMISSION.to_string(), "".to_string());
641        assert_eq!(parse_permission(&props), VendedPermission::Read);
642
643        // Missing permission should default to Read
644        let empty_props: HashMap<String, String> = HashMap::new();
645        assert_eq!(parse_permission(&empty_props), VendedPermission::Read);
646    }
647
648    #[test]
649    fn test_parse_duration_millis_with_invalid_values() {
650        const TEST_KEY: &str = "test_duration_millis";
651
652        // Invalid duration should default to DEFAULT_CREDENTIAL_DURATION_MILLIS
653        let mut props = HashMap::new();
654        props.insert(TEST_KEY.to_string(), "not_a_number".to_string());
655        assert_eq!(
656            parse_duration_millis(&props, TEST_KEY),
657            DEFAULT_CREDENTIAL_DURATION_MILLIS
658        );
659
660        // Negative number (parsed as u64 fails)
661        props.insert(TEST_KEY.to_string(), "-1000".to_string());
662        assert_eq!(
663            parse_duration_millis(&props, TEST_KEY),
664            DEFAULT_CREDENTIAL_DURATION_MILLIS
665        );
666
667        // Empty string should default
668        props.insert(TEST_KEY.to_string(), "".to_string());
669        assert_eq!(
670            parse_duration_millis(&props, TEST_KEY),
671            DEFAULT_CREDENTIAL_DURATION_MILLIS
672        );
673
674        // Missing duration should default
675        let empty_props: HashMap<String, String> = HashMap::new();
676        assert_eq!(
677            parse_duration_millis(&empty_props, TEST_KEY),
678            DEFAULT_CREDENTIAL_DURATION_MILLIS
679        );
680
681        // Valid duration should work
682        props.insert(TEST_KEY.to_string(), "7200000".to_string());
683        assert_eq!(parse_duration_millis(&props, TEST_KEY), 7200000);
684    }
685
686    #[test]
687    fn test_has_credential_vendor_config() {
688        // enabled = true
689        let mut props = HashMap::new();
690        props.insert(ENABLED.to_string(), "true".to_string());
691        assert!(has_credential_vendor_config(&props));
692
693        // enabled = TRUE (case-insensitive)
694        props.insert(ENABLED.to_string(), "TRUE".to_string());
695        assert!(has_credential_vendor_config(&props));
696
697        // enabled = false
698        props.insert(ENABLED.to_string(), "false".to_string());
699        assert!(!has_credential_vendor_config(&props));
700
701        // enabled = invalid value
702        props.insert(ENABLED.to_string(), "yes".to_string());
703        assert!(!has_credential_vendor_config(&props));
704
705        // enabled missing
706        let empty_props: HashMap<String, String> = HashMap::new();
707        assert!(!has_credential_vendor_config(&empty_props));
708    }
709
710    #[test]
711    fn test_vended_credentials_debug_redacts_secrets() {
712        let mut storage_options = HashMap::new();
713        storage_options.insert(
714            "aws_access_key_id".to_string(),
715            "AKIAIOSFODNN7EXAMPLE".to_string(),
716        );
717        storage_options.insert(
718            "aws_secret_access_key".to_string(),
719            "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".to_string(),
720        );
721        storage_options.insert(
722            "aws_session_token".to_string(),
723            "FwoGZXIvYXdzE...".to_string(),
724        );
725
726        let creds = VendedCredentials::new(storage_options, 1234567890);
727        let debug_output = format!("{:?}", creds);
728
729        // Should NOT contain actual secrets
730        assert!(!debug_output.contains("AKIAIOSFODNN7EXAMPLE"));
731        assert!(!debug_output.contains("wJalrXUtnFEMI"));
732        assert!(!debug_output.contains("FwoGZXIvYXdzE"));
733
734        // Should contain redacted message
735        assert!(debug_output.contains("redacted"));
736        assert!(debug_output.contains("3 keys"));
737
738        // Should contain expiration time
739        assert!(debug_output.contains("1234567890"));
740    }
741
742    #[test]
743    fn test_vended_credentials_is_expired() {
744        // Create credentials that expired in the past
745        let past_millis = std::time::SystemTime::now()
746            .duration_since(std::time::UNIX_EPOCH)
747            .unwrap()
748            .as_millis() as u64
749            - 1000; // 1 second ago
750
751        let expired_creds = VendedCredentials::new(HashMap::new(), past_millis);
752        assert!(expired_creds.is_expired());
753
754        // Create credentials that expire in the future
755        let future_millis = std::time::SystemTime::now()
756            .duration_since(std::time::UNIX_EPOCH)
757            .unwrap()
758            .as_millis() as u64
759            + 3600000; // 1 hour from now
760
761        let valid_creds = VendedCredentials::new(HashMap::new(), future_millis);
762        assert!(!valid_creds.is_expired());
763    }
764
765    #[test]
766    fn test_redact_credential() {
767        // Long credential: shows first 8 and last 4
768        assert_eq!(redact_credential("AKIAIOSFODNN7EXAMPLE"), "AKIAIOSF***MPLE");
769
770        // Exactly 16 chars: shows first 8 and last 4
771        assert_eq!(redact_credential("1234567890123456"), "12345678***3456");
772
773        // Short credential (< 16 chars): shows only first few
774        assert_eq!(redact_credential("short1234567"), "short123***");
775        assert_eq!(redact_credential("short123"), "short123***");
776        assert_eq!(redact_credential("tiny"), "tiny***");
777        assert_eq!(redact_credential("ab"), "ab***");
778        assert_eq!(redact_credential("a"), "a***");
779
780        // Empty string
781        assert_eq!(redact_credential(""), "[empty]");
782
783        // Real-world examples
784        // AWS access key ID (20 chars) - shows AKIA + 4 more chars which helps identify the key
785        assert_eq!(redact_credential("AKIAIOSFODNN7EXAMPLE"), "AKIAIOSF***MPLE");
786
787        // GCP token (typically very long)
788        let long_token = "ya29.a0AfH6SMBx1234567890abcdefghijklmnopqrstuvwxyz";
789        assert_eq!(redact_credential(long_token), "ya29.a0A***wxyz");
790
791        // Azure SAS token
792        let sas_token = "sv=2021-06-08&ss=b&srt=sco&sp=rwdlacuiytfx&se=2024-12-31";
793        assert_eq!(redact_credential(sas_token), "sv=2021-***2-31");
794    }
795}