|
| 1 | +use super::{CredentialSigner, VendedCredentials}; |
| 2 | +use async_trait::async_trait; |
| 3 | +use chrono::{DateTime, Utc, Duration}; |
| 4 | +use std::collections::HashMap; |
| 5 | +use anyhow::{Result, anyhow}; |
| 6 | + |
| 7 | +#[cfg(feature = "azure-oauth")] |
| 8 | +use azure_core::auth::TokenCredential; |
| 9 | +#[cfg(feature = "azure-oauth")] |
| 10 | +use azure_identity::ClientSecretCredential; |
| 11 | + |
| 12 | +/// Azure ADLS Gen2 credential signer that generates SAS tokens |
| 13 | +pub struct AzureSasSigner { |
| 14 | + pub account_name: String, |
| 15 | + pub account_key: Option<String>, |
| 16 | + pub tenant_id: Option<String>, |
| 17 | + pub client_id: Option<String>, |
| 18 | + pub client_secret: Option<String>, |
| 19 | + pub container: String, |
| 20 | +} |
| 21 | + |
| 22 | +impl AzureSasSigner { |
| 23 | + pub fn new( |
| 24 | + account_name: String, |
| 25 | + account_key: Option<String>, |
| 26 | + tenant_id: Option<String>, |
| 27 | + client_id: Option<String>, |
| 28 | + client_secret: Option<String>, |
| 29 | + container: String, |
| 30 | + ) -> Self { |
| 31 | + Self { |
| 32 | + account_name, |
| 33 | + account_key, |
| 34 | + tenant_id, |
| 35 | + client_id, |
| 36 | + client_secret, |
| 37 | + container, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[async_trait] |
| 43 | +impl CredentialSigner for AzureSasSigner { |
| 44 | + async fn generate_credentials( |
| 45 | + &self, |
| 46 | + _resource_path: &str, |
| 47 | + _permissions: &[String], |
| 48 | + duration: Duration, |
| 49 | + ) -> Result<VendedCredentials> { |
| 50 | + #[cfg(feature = "azure-oauth")] |
| 51 | + { |
| 52 | + tracing::info!("🔑 Generating Azure credentials"); |
| 53 | + |
| 54 | + let expires_at = Utc::now() + duration; |
| 55 | + |
| 56 | + // Try OAuth2 if credentials are available |
| 57 | + if let (Some(tenant_id), Some(client_id), Some(client_secret)) = |
| 58 | + (&self.tenant_id, &self.client_id, &self.client_secret) { |
| 59 | + |
| 60 | + let authority_host = azure_core::Url::parse("https://login.microsoftonline.com") |
| 61 | + .map_err(|e| anyhow!("Failed to parse authority host: {}", e))?; |
| 62 | + |
| 63 | + let credential = ClientSecretCredential::new( |
| 64 | + azure_core::new_http_client(), |
| 65 | + authority_host, |
| 66 | + tenant_id.clone(), |
| 67 | + client_id.clone(), |
| 68 | + client_secret.clone(), |
| 69 | + ); |
| 70 | + |
| 71 | + let token = credential |
| 72 | + .get_token(&["https://storage.azure.com/.default"]) |
| 73 | + .await |
| 74 | + .map_err(|e| anyhow!("Azure token acquisition failed: {}", e))?; |
| 75 | + |
| 76 | + let mut config = HashMap::new(); |
| 77 | + config.insert("credential-type".to_string(), "azure-oauth".to_string()); |
| 78 | + config.insert("azure-oauth-token".to_string(), token.token.secret().to_string()); |
| 79 | + config.insert("azure-account-name".to_string(), self.account_name.clone()); |
| 80 | + config.insert("azure-container".to_string(), self.container.clone()); |
| 81 | + |
| 82 | + tracing::info!("✅ Successfully generated Azure OAuth2 token"); |
| 83 | + |
| 84 | + return Ok(VendedCredentials { |
| 85 | + prefix: format!("abfss://{}@{}.dfs.core.windows.net/", |
| 86 | + self.container, self.account_name), |
| 87 | + config, |
| 88 | + expires_at: Some(expires_at), |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + // Fallback to account key if available |
| 93 | + if let Some(account_key) = &self.account_key { |
| 94 | + let mut config = HashMap::new(); |
| 95 | + config.insert("credential-type".to_string(), "azure-key".to_string()); |
| 96 | + config.insert("azure-account-name".to_string(), self.account_name.clone()); |
| 97 | + config.insert("azure-account-key".to_string(), account_key.clone()); |
| 98 | + config.insert("azure-container".to_string(), self.container.clone()); |
| 99 | + |
| 100 | + tracing::info!("✅ Using Azure account key credentials"); |
| 101 | + |
| 102 | + return Ok(VendedCredentials { |
| 103 | + prefix: format!("abfss://{}@{}.dfs.core.windows.net/", |
| 104 | + self.container, self.account_name), |
| 105 | + config, |
| 106 | + expires_at: None, // Account keys don't expire |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + Err(anyhow!("Azure credentials not configured properly")) |
| 111 | + } |
| 112 | + |
| 113 | + #[cfg(not(feature = "azure-oauth"))] |
| 114 | + { |
| 115 | + tracing::warn!("Azure OAuth feature not enabled, returning placeholder credentials"); |
| 116 | + let mut config = HashMap::new(); |
| 117 | + config.insert("credential-type".to_string(), "azure-sas".to_string()); |
| 118 | + config.insert("azure-sas-token".to_string(), "PLACEHOLDER_SAS_TOKEN".to_string()); |
| 119 | + config.insert("azure-account-name".to_string(), self.account_name.clone()); |
| 120 | + config.insert("azure-container".to_string(), self.container.clone()); |
| 121 | + |
| 122 | + Ok(VendedCredentials { |
| 123 | + prefix: format!("abfss://{}@{}.dfs.core.windows.net/", |
| 124 | + self.container, self.account_name), |
| 125 | + config, |
| 126 | + expires_at: Some(Utc::now() + duration), |
| 127 | + }) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fn storage_type(&self) -> &str { |
| 132 | + "azure" |
| 133 | + } |
| 134 | +} |
0 commit comments