Uredi

Additional methods to authenticate to Azure resources from Rust apps

This article lists additional methods that apps can use to authenticate to Azure resources. These methods are less commonly used. When possible, use one of the methods outlined in the authenticating Rust apps to Azure using the Azure SDK overview.

Client certificate authentication

ClientCertificateCredential authenticates a service principal by using a certificate instead of a client secret. This method is more secure than client secrets because you can manage and rotate certificates more easily.

Note

ClientCertificateCredential requires the client_certificate feature flag. Add it to your Cargo.toml:

[dependencies]
azure_identity = { version = "0.32", features = ["client_certificate"] }

Example using ClientCertificateCredential

use azure_identity::ClientCertificateCredential;
use azure_core::credentials::SecretBytes;
use azure_security_keyvault_secrets::SecretClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tenant_id = std::env::var("AZURE_TENANT_ID")?;
    let client_id = std::env::var("AZURE_CLIENT_ID")?;
    let cert_path = std::env::var("AZURE_CLIENT_CERTIFICATE_PATH")?;

    // Read the certificate file
    let cert_bytes = std::fs::read(&cert_path)?;

    let credential = ClientCertificateCredential::new(
        tenant_id,
        client_id,
        SecretBytes::from(cert_bytes.as_slice()),
        None,
    )?;

    let client = SecretClient::new(
        "https://<your-key-vault-name>.vault.azure.net/",
        credential.clone(),
        None,
    )?;

    // Use the client to interact with Azure Key Vault
    // let secret = client.get("secret-name", None).await?;

    Ok(())
}

Workload identity authentication

WorkloadIdentityCredential supports Microsoft Entra Workload Identity on Kubernetes. This credential authenticates pods in Azure Kubernetes Service (AKS) clusters configured with workload identity.

When you configure it, the AKS cluster provides the necessary environment variables (AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_FEDERATED_TOKEN_FILE) that the credential uses automatically.

use azure_identity::WorkloadIdentityCredential;
use azure_security_keyvault_secrets::SecretClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // WorkloadIdentityCredential reads configuration from environment variables
    // set by the AKS workload identity webhook
    let credential = WorkloadIdentityCredential::new(None)?;

    let client = SecretClient::new(
        "https://<your-key-vault-name>.vault.azure.net/",
        credential.clone(),
        None,
    )?;

    // Use the client to interact with Azure Key Vault
    // let secret = client.get("secret-name", None).await?;

    Ok(())
}

For more information, see Use Microsoft Entra Workload Identity with AKS.

Azure Pipelines authentication

AzurePipelinesCredential authenticates an Azure Pipelines service connection. Use this credential in Azure DevOps pipeline tasks that need to access Azure resources.

use azure_identity::AzurePipelinesCredential;
use azure_core::credentials::Secret;
use azure_security_keyvault_secrets::SecretClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let system_access_token = std::env::var("SYSTEM_ACCESSTOKEN")?;

    let credential = AzurePipelinesCredential::new(
        String::from("<tenant-id>"),
        String::from("<client-id>"),
        "<service-connection-id>",
        Secret::new(system_access_token),
        None,
    )?;

    let client = SecretClient::new(
        "https://<your-key-vault-name>.vault.azure.net/",
        credential.clone(),
        None,
    )?;

    // Use the client in your pipeline task
    // let secret = client.get("secret-name", None).await?;

    Ok(())
}

Client assertion authentication (federated identity)

ClientAssertionCredential authenticates a service principal with a client assertion, which is useful for federated identity credential scenarios. For example, you can configure an application to trust tokens from a managed identity:

use azure_core::credentials::TokenCredential;
use azure_core::http::ClientMethodOptions;
use azure_identity::{ClientAssertion, ClientAssertionCredential, ManagedIdentityCredential};
use azure_security_keyvault_secrets::SecretClient;
use std::sync::Arc;

#[derive(Debug)]
struct AccessTokenAssertion {
    credential: Arc<dyn TokenCredential>,
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ClientAssertion for AccessTokenAssertion {
    async fn secret(&self, _: Option<ClientMethodOptions<'_>>) -> azure_core::Result<String> {
        Ok(self
            .credential
            .get_token(&[&"api://AzureADTokenExchange/.default"], None)
            .await?
            .token
            .secret()
            .to_string())
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let assertion = AccessTokenAssertion {
        credential: ManagedIdentityCredential::new(None)?,
    };

    let credential = ClientAssertionCredential::new(
        String::from("tenant ID"),
        String::from("client ID"),
        assertion,
        None,
    )?;

    let client = SecretClient::new(
        "https://<your-key-vault-name>.vault.azure.net/",
        credential.clone(),
        None,
    )?;

    // Use the client to interact with Azure Key Vault
    // let secret = client.get("secret-name", None).await?;

    Ok(())
}

For more information, see Configure an application to trust a managed identity.