๐Ÿ“ฆ heaths / dotazure-rs

๐Ÿ“„ secrets.rs ยท 36 lines
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// Copyright 2025 Heath Stewart.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

use azure_identity::AzureDeveloperCliCredential;
use azure_security_keyvault_secrets::SecretClient;
use dotazure::error::{ErrorKind, ResultExt};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    const ENV_VAR_NAME: &str = "AZURE_KEYVAULT_URL";

    if dotazure::load()? {
        eprintln!("loaded environment variables");
    }

    let endpoint = env::var(ENV_VAR_NAME)
        .with_context_fn(ErrorKind::NotFound, || format!("{ENV_VAR_NAME} not set"))?;
    let credential = AzureDeveloperCliCredential::new(None)?;
    let client = SecretClient::new(&endpoint, credential.clone(), None)?;

    let secret = client
        .get_secret("my-secret", "", None)
        .await?
        .into_body()
        .await?;
    let secret = secret
        .value
        .as_ref()
        .map_or_else(|| "(none)", |v| v.as_str());

    println!("{}", secret);

    Ok(())
}