reqsign-azure-storage 2.0.2

Azure Storage signing implementation for reqsign.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::Credential;
#[cfg(not(target_arch = "wasm32"))]
use crate::provide_credential::{AzureCliCredentialProvider, ClientCertificateCredentialProvider};
use crate::provide_credential::{
    AzurePipelinesCredentialProvider, ClientSecretCredentialProvider, EnvCredentialProvider,
    ImdsCredentialProvider, WorkloadIdentityCredentialProvider,
};
use async_trait::async_trait;
use reqsign_core::{Context, ProvideCredential, ProvideCredentialChain, Result};

/// Default loader that tries multiple credential sources in order.
///
/// The default loader attempts to load credentials from the following sources in order:
/// 1. Environment variables (account key, SAS token)
/// 2. Azure CLI (local development)
/// 3. Client certificate (service principal with certificate)
/// 4. Client secret (service principal)
/// 5. Azure Pipelines (workload identity)
/// 6. Workload identity (federated credentials)
/// 7. IMDS (Azure VM managed identity)
#[derive(Debug)]
pub struct DefaultCredentialProvider {
    chain: ProvideCredentialChain<Credential>,
}

impl Default for DefaultCredentialProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl DefaultCredentialProvider {
    /// Create a builder to configure the default credential chain.
    pub fn builder() -> DefaultCredentialProviderBuilder {
        DefaultCredentialProviderBuilder::default()
    }

    /// Create a new default loader.
    pub fn new() -> Self {
        Self::builder().build()
    }

    /// Create with a custom credential chain.
    pub fn with_chain(chain: ProvideCredentialChain<Credential>) -> Self {
        Self { chain }
    }

    /// Add a credential provider to the front of the default chain.
    ///
    /// This allows adding a high-priority credential source that will be tried
    /// before all other providers in the default chain.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use reqsign_azure_storage::{DefaultCredentialProvider, StaticCredentialProvider};
    ///
    /// let provider = DefaultCredentialProvider::new()
    ///     .push_front(StaticCredentialProvider::new_shared_key("account_name", "account_key"));
    /// ```
    pub fn push_front(
        mut self,
        provider: impl ProvideCredential<Credential = Credential> + 'static,
    ) -> Self {
        self.chain = self.chain.push_front(provider);
        self
    }
}

/// Builder for `DefaultCredentialProvider`.
///
/// Use `configure_*` to adjust provider behavior and `disable_*(bool)` to
/// control participation in the chain. Then call `build()` to construct the
/// final provider that resolves credentials in the documented order.
#[derive(Default)]
pub struct DefaultCredentialProviderBuilder {
    env: Option<EnvCredentialProvider>,
    #[cfg(not(target_arch = "wasm32"))]
    azure_cli: Option<AzureCliCredentialProvider>,
    #[cfg(not(target_arch = "wasm32"))]
    client_certificate: Option<ClientCertificateCredentialProvider>,
    client_secret: Option<ClientSecretCredentialProvider>,
    azure_pipelines: Option<AzurePipelinesCredentialProvider>,
    workload_identity: Option<WorkloadIdentityCredentialProvider>,
    imds: Option<ImdsCredentialProvider>,
}

impl DefaultCredentialProviderBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn configure_env<F>(mut self, f: F) -> Self
    where
        F: FnOnce(EnvCredentialProvider) -> EnvCredentialProvider,
    {
        let p = self.env.take().unwrap_or_default();
        self.env = Some(f(p));
        self
    }

    pub fn disable_env(mut self, disable: bool) -> Self {
        if disable {
            self.env = None;
        } else if self.env.is_none() {
            self.env = Some(EnvCredentialProvider::new());
        }
        self
    }

    /// Configure the Azure CLI credential provider.
    ///
    /// This is typically used for local development where Azure CLI is
    /// available and authenticated. You can tweak behavior (like alternative
    /// token acquisition strategies) via the provided closure. Only available
    /// on non-wasm32 targets.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn configure_azure_cli<F>(mut self, f: F) -> Self
    where
        F: FnOnce(AzureCliCredentialProvider) -> AzureCliCredentialProvider,
    {
        let p = self.azure_cli.take().unwrap_or_default();
        self.azure_cli = Some(f(p));
        self
    }

    /// Disable (true) or ensure enabled (false) the Azure CLI provider.
    ///
    /// Useful in CI or constrained environments where Azure CLI is not
    /// present, or to explicitly opt out. Only available on non-wasm32 targets.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn disable_azure_cli(mut self, disable: bool) -> Self {
        if disable {
            self.azure_cli = None;
        } else if self.azure_cli.is_none() {
            self.azure_cli = Some(AzureCliCredentialProvider::new());
        }
        self
    }

    /// Configure the client certificate credential provider.
    ///
    /// Customize certificate-based SPN authentication (tenant/client/cert
    /// parameters). Only available on non-wasm32 targets.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn configure_client_certificate<F>(mut self, f: F) -> Self
    where
        F: FnOnce(ClientCertificateCredentialProvider) -> ClientCertificateCredentialProvider,
    {
        let p = self.client_certificate.take().unwrap_or_default();
        self.client_certificate = Some(f(p));
        self
    }

    /// Disable (true) or ensure enabled (false) the client certificate provider.
    ///
    /// Use to explicitly remove certificate-based auth from the chain or to
    /// ensure participation. Only available on non-wasm32 targets.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn disable_client_certificate(mut self, disable: bool) -> Self {
        if disable {
            self.client_certificate = None;
        } else if self.client_certificate.is_none() {
            self.client_certificate = Some(ClientCertificateCredentialProvider::new());
        }
        self
    }

    /// Configure the client secret credential provider.
    ///
    /// Customize tenant/client parameters used to exchange a client secret
    /// for an access token suitable for Azure Storage.
    pub fn configure_client_secret<F>(mut self, f: F) -> Self
    where
        F: FnOnce(ClientSecretCredentialProvider) -> ClientSecretCredentialProvider,
    {
        let p = self.client_secret.take().unwrap_or_default();
        self.client_secret = Some(f(p));
        self
    }

    /// Disable (true) or ensure enabled (false) the client secret provider.
    pub fn disable_client_secret(mut self, disable: bool) -> Self {
        if disable {
            self.client_secret = None;
        } else if self.client_secret.is_none() {
            self.client_secret = Some(ClientSecretCredentialProvider::new());
        }
        self
    }

    /// Configure the Azure Pipelines workload identity provider.
    ///
    /// Allows customizing how OIDC tokens from Azure Pipelines are exchanged
    /// for Azure AD access tokens.
    pub fn configure_azure_pipelines<F>(mut self, f: F) -> Self
    where
        F: FnOnce(AzurePipelinesCredentialProvider) -> AzurePipelinesCredentialProvider,
    {
        let p = self.azure_pipelines.take().unwrap_or_default();
        self.azure_pipelines = Some(f(p));
        self
    }

    /// Disable (true) or ensure enabled (false) the Azure Pipelines provider.
    pub fn disable_azure_pipelines(mut self, disable: bool) -> Self {
        if disable {
            self.azure_pipelines = None;
        } else if self.azure_pipelines.is_none() {
            self.azure_pipelines = Some(AzurePipelinesCredentialProvider::new());
        }
        self
    }

    /// Configure the Kubernetes workload identity provider.
    ///
    /// Allows customizing tenant or other parameters used to exchange
    /// federated tokens for Azure AD access tokens.
    pub fn configure_workload_identity<F>(mut self, f: F) -> Self
    where
        F: FnOnce(WorkloadIdentityCredentialProvider) -> WorkloadIdentityCredentialProvider,
    {
        let p = self.workload_identity.take().unwrap_or_default();
        self.workload_identity = Some(f(p));
        self
    }

    /// Disable (true) or ensure enabled (false) the workload identity provider.
    pub fn disable_workload_identity(mut self, disable: bool) -> Self {
        if disable {
            self.workload_identity = None;
        } else if self.workload_identity.is_none() {
            self.workload_identity = Some(WorkloadIdentityCredentialProvider::new());
        }
        self
    }

    /// Configure the Azure IMDS provider.
    ///
    /// Allows setting an alternate metadata endpoint or other parameters used
    /// to obtain managed identity tokens on Azure VMs.
    pub fn configure_imds<F>(mut self, f: F) -> Self
    where
        F: FnOnce(ImdsCredentialProvider) -> ImdsCredentialProvider,
    {
        let p = self.imds.take().unwrap_or_default();
        self.imds = Some(f(p));
        self
    }

    /// Disable (true) or ensure enabled (false) the IMDS provider.
    pub fn disable_imds(mut self, disable: bool) -> Self {
        if disable {
            self.imds = None;
        } else if self.imds.is_none() {
            self.imds = Some(ImdsCredentialProvider::new());
        }
        self
    }

    /// Build the `DefaultCredentialProvider` with the configured options.
    pub fn build(self) -> DefaultCredentialProvider {
        let mut chain = ProvideCredentialChain::new();

        if let Some(p) = self.env {
            chain = chain.push(p);
        } else {
            chain = chain.push(EnvCredentialProvider::new());
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            if let Some(p) = self.azure_cli {
                chain = chain.push(p);
            } else {
                chain = chain.push(AzureCliCredentialProvider::new());
            }

            if let Some(p) = self.client_certificate {
                chain = chain.push(p);
            } else {
                chain = chain.push(ClientCertificateCredentialProvider::new());
            }
        }

        if let Some(p) = self.client_secret {
            chain = chain.push(p);
        } else {
            chain = chain.push(ClientSecretCredentialProvider::new());
        }

        if let Some(p) = self.azure_pipelines {
            chain = chain.push(p);
        } else {
            chain = chain.push(AzurePipelinesCredentialProvider::new());
        }

        if let Some(p) = self.workload_identity {
            chain = chain.push(p);
        } else {
            chain = chain.push(WorkloadIdentityCredentialProvider::new());
        }

        if let Some(p) = self.imds {
            chain = chain.push(p);
        } else {
            chain = chain.push(ImdsCredentialProvider::new());
        }

        DefaultCredentialProvider::with_chain(chain)
    }
}

#[async_trait]
impl ProvideCredential for DefaultCredentialProvider {
    type Credential = Credential;

    async fn provide_credential(&self, ctx: &Context) -> Result<Option<Self::Credential>> {
        self.chain.provide_credential(ctx).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use reqsign_core::StaticEnv;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_config_loader_priority() {
        let env = StaticEnv {
            home_dir: None,
            envs: HashMap::from([
                (
                    "AZBLOB_ACCOUNT_NAME".to_string(),
                    "test_account".to_string(),
                ),
                ("AZBLOB_ACCOUNT_KEY".to_string(), "dGVzdF9rZXk=".to_string()),
            ]),
        };

        // Create a mock context - in real usage Context would be created with proper FileRead and HttpSend
        let ctx = reqsign_core::Context::new()
            .with_file_read(MockFileRead)
            .with_http_send(MockHttpSend)
            .with_env(env);

        let loader = DefaultCredentialProvider::new();

        let cred = loader.provide_credential(&ctx).await.unwrap().unwrap();
        match cred {
            crate::Credential::SharedKey {
                account_name,
                account_key,
            } => {
                assert_eq!(account_name, "test_account");
                assert_eq!(account_key, "dGVzdF9rZXk=");
            }
            _ => panic!("Expected SharedKey credential"),
        }
    }

    #[tokio::test]
    async fn test_sas_token_priority() {
        let env = StaticEnv {
            home_dir: None,
            envs: HashMap::from([(
                "AZURE_STORAGE_SAS_TOKEN".to_string(),
                "sv=2021-01-01&ss=b&srt=c&sp=rwdlaciytfx".to_string(),
            )]),
        };

        let ctx = reqsign_core::Context::new()
            .with_file_read(MockFileRead)
            .with_http_send(MockHttpSend)
            .with_env(env);

        let loader = DefaultCredentialProvider::new();

        let cred = loader.provide_credential(&ctx).await.unwrap().unwrap();
        match cred {
            crate::Credential::SasToken { token } => {
                assert_eq!(token, "sv=2021-01-01&ss=b&srt=c&sp=rwdlaciytfx");
            }
            _ => panic!("Expected SasToken credential"),
        }
    }

    // Mock implementations for testing
    #[derive(Debug)]
    struct MockFileRead;

    #[async_trait]
    impl reqsign_core::FileRead for MockFileRead {
        async fn file_read(&self, _path: &str) -> Result<Vec<u8>> {
            Ok(Vec::new())
        }
    }

    #[derive(Debug)]
    struct MockHttpSend;

    #[async_trait]
    impl reqsign_core::HttpSend for MockHttpSend {
        async fn http_send(
            &self,
            _req: http::Request<bytes::Bytes>,
        ) -> Result<http::Response<bytes::Bytes>> {
            Ok(http::Response::new(bytes::Bytes::new()))
        }
    }
}