|
1 | | -# azure_identity |
| 1 | +# Azure Identity client library for Rust |
2 | 2 |
|
3 | | -Azure Identity crate for the unofficial Microsoft Azure SDK for Rust. This crate is part of a collection of crates: for more information please refer to <https://github.com/Azure/azure-sdk-for-rust>. |
| 3 | +The Azure Identity library provides [Microsoft Entra ID](https://learn.microsoft.com/entra/fundamentals/whatis) ([formerly Azure Active Directory](https://learn.microsoft.com/entra/fundamentals/new-name)) token authentication support across the Azure SDK. It provides a set of [`TokenCredential`][token_cred_ref] implementations that can be used to construct Azure SDK clients that support Microsoft Entra token authentication. |
4 | 4 |
|
5 | | -This crate provides several implementations of the [azure_core::auth::TokenCredential](https://docs.rs/azure_core/latest/azure_core/auth/trait.TokenCredential.html) trait. |
6 | | -It is recommended to start with `azure_identity::create_credential()?`, which will create an instance of `DefaultAzureCredential` by default. If you want to use a specific credential type, the `AZURE_CREDENTIAL_KIND` environment variable may be set to a value from `azure_credential_kinds`, such as `azurecli` or `virtualmachine`. |
| 5 | +[Source code] | [Package (crates.io)] | [API reference documentation] | [Microsoft Entra ID documentation] |
7 | 6 |
|
8 | | -```rust,no_run |
9 | | -use azure_core::credentials::TokenCredential; |
10 | | -use std::sync::Arc; |
| 7 | +## Getting started |
11 | 8 |
|
12 | | -#[tokio::main] |
13 | | -async fn main() -> Result<(), Box<dyn std::error::Error>> { |
14 | | - let subscription_id = |
15 | | - std::env::var("AZURE_SUBSCRIPTION_ID").expect("AZURE_SUBSCRIPTION_ID required"); |
| 9 | +### Install the package |
16 | 10 |
|
17 | | - let credential: Arc<dyn TokenCredential> = azure_identity::DefaultAzureCredential::new()?; |
| 11 | +Install the Azure Identity library for Rust with cargo: |
18 | 12 |
|
19 | | - // Let's enumerate the Azure storage accounts in the subscription using the REST API directly. |
20 | | - // This is just an example. It is easier to use the Azure SDK for Rust crates. |
21 | | - let url = url::Url::parse(&format!("https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.Storage/storageAccounts?api-version=2019-06-01"))?; |
| 13 | +```bash |
| 14 | +cargo add azure_identity |
| 15 | +``` |
22 | 16 |
|
23 | | - let access_token = credential |
24 | | - .get_token(&["https://management.azure.com/.default"]) |
25 | | - .await?; |
| 17 | +### Prerequisites |
26 | 18 |
|
27 | | - let response = reqwest::Client::new() |
28 | | - .get(url) |
29 | | - .header( |
30 | | - "Authorization", |
31 | | - format!("Bearer {}", access_token.token.secret()), |
32 | | - ) |
33 | | - .send() |
34 | | - .await? |
35 | | - .text() |
36 | | - .await?; |
| 19 | +* An [Azure subscription]. |
| 20 | +* The [Azure CLI] can also be useful for authenticating in a development environment, creating accounts, and managing account roles. |
37 | 21 |
|
38 | | - println!("{response}"); |
39 | | - Ok(()) |
40 | | -} |
41 | | -``` |
| 22 | +### Authenticate during local development |
| 23 | + |
| 24 | +When debugging and executing code locally, it's typical for developers to use their own accounts for authenticating calls to Azure services. The Azure Identity library supports authenticating through developer tools to simplify local development. |
| 25 | + |
| 26 | +#### Authenticate via the Azure CLI |
| 27 | + |
| 28 | +`DefaultAzureCredential` and `AzureCliCredential` can authenticate as the user signed in to the [Azure CLI]. To sign in to the Azure CLI, run `az login`. On a system with a default web browser, the Azure CLI launches the browser to authenticate a user. |
| 29 | + |
| 30 | +When no default browser is available, `az login` uses the device code authentication flow. This flow can also be selected manually by running `az login --use-device-code`. |
| 31 | + |
| 32 | +## Key concepts |
| 33 | + |
| 34 | +### Credentials |
| 35 | + |
| 36 | +A credential is a class that contains or can obtain the data needed for a service client to authenticate requests. Service clients across the Azure SDK accept a credential instance when they're constructed, and use that credential to authenticate requests. |
42 | 37 |
|
43 | | -## Design |
| 38 | +The Azure Identity library focuses on OAuth authentication with Microsoft Entra ID. It offers various credential classes capable of acquiring a Microsoft Entra access token. See the [Credential classes](#credential-classes "Credential classes") section for a list of this library's credential classes. |
44 | 39 |
|
45 | | -Each `TokenCredential` implementation provides a `new` constructor that returns an `azure_core::Result<Arc<Self>>`. The credential provider is contained within an `Arc` because these are designed to be reused by multiple clients for efficiency e.g.: |
| 40 | +### DefaultAzureCredential |
| 41 | + |
| 42 | +`DefaultAzureCredential` simplifies authentication while developing apps that deploy to Azure by combining credentials used in Azure hosting environments with credentials used in local development. |
| 43 | + |
| 44 | +#### Continuation policy |
| 45 | + |
| 46 | +`DefaultAzureCredential` attempts to authenticate with all developer credentials until one succeeds, regardless of any errors previous developer credentials experienced. For example, a developer credential may attempt to get a token and fail, so `DefaultAzureCredential` will continue to the next credential in the flow. Deployed service credentials stop the flow with a thrown exception if they're able to attempt token retrieval, but don't receive one. |
| 47 | + |
| 48 | +This allows for trying all of the developer credentials on your machine while having predictable deployed behavior. |
| 49 | + |
| 50 | +## Examples |
| 51 | + |
| 52 | +The following examples are provided: |
| 53 | +<!-- no toc --> |
| 54 | +* [Authenticate with DefaultAzureCredential](#authenticate-with-defaultazurecredential "Authenticate with DefaultAzureCredential") |
| 55 | + |
| 56 | +### Authenticate with `DefaultAzureCredential` |
| 57 | + |
| 58 | +More details on configuring your environment to use `DefaultAzureCredential` can be found in the class's [reference documentation][default_cred_ref]. |
| 59 | + |
| 60 | +This example demonstrates authenticating the `SecretClient` from the [azure_security_keyvault_secrets] crate using `DefaultAzureCredential`. |
46 | 61 |
|
47 | 62 | ```rust |
48 | | -use azure_core::credentials::TokenCredential; |
49 | 63 | use azure_identity::DefaultAzureCredential; |
50 | | -# use azure_core::{ClientOptions, Result}; |
51 | | -# use std::sync::Arc; |
52 | | -# struct StorageAccountClient; |
53 | | -# impl StorageAccountClient { |
54 | | -# fn new(_endpoint: &str, _credential: Arc<dyn TokenCredential>, _options: Option<ClientOptions>) -> Result<Arc<Self>> { |
55 | | -# Ok(Arc::new(StorageAccountClient)) |
56 | | -# } |
57 | | -# } |
58 | | -# struct SecretClient; |
59 | | -# impl SecretClient { |
60 | | -# fn new(_endpoint: &str, _credential: Arc<dyn TokenCredential>, _options: Option<ClientOptions>) -> Result<Arc<Self>> { |
61 | | -# Ok(Arc::new(SecretClient)) |
62 | | -# } |
63 | | -# } |
64 | | - |
65 | | -let credential = DefaultAzureCredential::new().unwrap(); |
66 | | -let storage_client = StorageAccountClient::new( |
67 | | - "https://myaccount.blob.storage.azure.net", |
68 | | - credential.clone(), |
69 | | - None, |
70 | | -); |
71 | | -let secret_client = SecretClient::new("https://myvault.keyvault.azure.net", |
72 | | - credential.clone(), |
73 | | - None, |
74 | | -); |
| 64 | +use azure_security_keyvault_secrets::SecretClient; |
| 65 | + |
| 66 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 67 | + let credential = DefaultAzureCredential::new()?; |
| 68 | + let client = SecretClient::new("https://your-key-vault-name.vault.azure.net/", credential.clone(), None)?; |
| 69 | + Ok(()) |
| 70 | +} |
75 | 71 | ``` |
76 | 72 |
|
77 | | -Credentials are cached in memory and refreshed as needed. Using the same credentials in multiple clients prevents authenticating and refreshing tokens numerous times for each client otherwise. |
| 73 | +## Credential classes |
| 74 | + |
| 75 | +### Credential chains |
| 76 | + |
| 77 | +|Credential|Usage |
| 78 | +|-|- |
| 79 | +|[`DefaultAzureCredential`][default_cred_ref]| Provides a simplified authentication experience to quickly start developing applications run in Azure. |
| 80 | + |
| 81 | +### Authenticate Azure-hosted applications |
| 82 | + |
| 83 | +|Credential|Usage |
| 84 | +|-|- |
| 85 | +|[`ImdsManagedIdentityCredential`][managed_id_cred_ref]| Authenticates the managed identity of an Azure resource. |
| 86 | +|[`WorkloadIdentityCredential`][workload_id_cred_ref]| Supports [Microsoft Entra Workload ID](https://learn.microsoft.com/azure/aks/workload-identity-overview) on Kubernetes. |
| 87 | + |
| 88 | +### Authenticate service principals |
| 89 | + |
| 90 | +|Credential|Usage|Reference |
| 91 | +|-|-|- |
| 92 | +|[`ClientCertificateCredential`][cert_cred_ref]| Authenticates a service principal using a certificate. | [Service principal authentication](https://learn.microsoft.com/entra/identity-platform/app-objects-and-service-principals) |
| 93 | + |
| 94 | +### Authenticate via development tools |
| 95 | + |
| 96 | +|Credential|Usage|Reference |
| 97 | +|-|-|- |
| 98 | +|[`AzureCliCredential`][cli_cred_ref]| Authenticates in a development environment with the Azure CLI. | [Azure CLI authentication](https://learn.microsoft.com/cli/azure/authenticate-azure-cli) |
| 99 | + |
| 100 | +## Next steps |
| 101 | + |
| 102 | +### Client library support |
| 103 | + |
| 104 | +Client and management libraries listed on the [Azure SDK release page](https://azure.github.io/azure-sdk/releases/latest/rust.html)that support Microsoft Entra authentication accept credentials from this library. You can learn more about using these libraries in their documentation, which is available at [Docs.rs](https://Docs.rs). |
| 105 | + |
| 106 | +### Provide feedback |
| 107 | + |
| 108 | +If you encounter bugs or have suggestions, [open an issue](https://github.com/Azure/azure-sdk-for-rust/issues). |
| 109 | + |
| 110 | +## Contributing |
| 111 | + |
| 112 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [https://cla.microsoft.com](https://cla.microsoft.com). |
| 113 | + |
| 114 | +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You'll only need to do this once across all repos using our CLA. |
| 115 | + |
| 116 | +This project has adopted the [Microsoft Open Source Code of Conduct ](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 117 | + |
| 118 | +<!-- LINKS --> |
| 119 | +[Azure CLI]: https://learn.microsoft.com/cli/azure |
| 120 | +[azure_security_keyvault_secrets]: https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/keyvault/azure_security_keyvault_secrets |
| 121 | +[Azure subscription]: https://azure.microsoft.com/free/ |
| 122 | +[cert_cred_ref]: https://docs.rs/azure_identity/latest/azure_identity/struct.ClientCertificateCredential.html |
| 123 | +[cli_cred_ref]: https://docs.rs/azure_identity/latest/azure_identity/struct.AzureauthCliCredential.html |
| 124 | +[default_cred_ref]: https://docs.rs/azure_identity/latest/azure_identity/struct.DefaultAzureCredential.html |
| 125 | +[Microsoft Entra ID documentation]: https://learn.microsoft.com/entra/identity/ |
| 126 | +[API reference documentation]: https://docs.rs/azure_identity/latest/azure_identity/ |
| 127 | +[managed_id_cred_ref]: https://docs.rs/azure_identity/latest/azure_identity/struct.ImdsManagedIdentityCredential.html |
| 128 | +[Package (crates.io)]: https://crates.io/crates/azure_identity |
| 129 | +[Source code]: https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/identity/azure_identity |
| 130 | +[token_cred_ref]: https://docs.rs/azure_core/latest/azure_core/struct.TokenCredential.html |
| 131 | +[workload_id_cred_ref]: https://docs.rs/azure_identity/latest/azure_identity/struct.WorkloadIdentityCredential.html |
0 commit comments