azure_identity/token_credentials/
virtual_machine_managed_identity_credential.rs

1use crate::{ImdsId, ImdsManagedIdentityCredential, TokenCredentialOptions};
2use azure_core::{
3    auth::{AccessToken, TokenCredential},
4    headers::HeaderName,
5    Url,
6};
7
8const ENDPOINT: &str = "http://169.254.169.254/metadata/identity/oauth2/token";
9const API_VERSION: &str = "2019-08-01";
10const SECRET_HEADER: HeaderName = HeaderName::from_static("x-identity-header");
11const SECRET_ENV: &str = "IDENTITY_HEADER";
12
13#[derive(Debug)]
14pub struct VirtualMachineManagedIdentityCredential {
15    credential: ImdsManagedIdentityCredential,
16}
17
18impl VirtualMachineManagedIdentityCredential {
19    pub fn new(options: impl Into<TokenCredentialOptions>) -> Self {
20        let endpoint = Url::parse(ENDPOINT).unwrap(); // valid url constant
21        Self {
22            credential: ImdsManagedIdentityCredential::new(
23                options,
24                endpoint,
25                API_VERSION,
26                SECRET_HEADER,
27                SECRET_ENV,
28                ImdsId::SystemAssigned,
29            ),
30        }
31    }
32}
33
34#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
35#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
36impl TokenCredential for VirtualMachineManagedIdentityCredential {
37    async fn get_token(&self, scopes: &[&str]) -> azure_core::Result<AccessToken> {
38        self.credential.get_token(scopes).await
39    }
40
41    async fn clear_cache(&self) -> azure_core::Result<()> {
42        self.credential.clear_cache().await
43    }
44}