Azure / azure-sdk-for-rust

This repository is for active development of the *unofficial* Azure SDK for Rust. This repository is *not* supported by the Azure SDK team.
MIT License
685 stars 232 forks source link

[docs] azure_identity docs seem to be wrong. #1588

Closed lcrownover closed 6 months ago

lcrownover commented 6 months ago

I'm trying to do something very simple: Use use the Azure CLI to print out an access token.

In trying to get something going, I'm following the official example on the azure_identity docs on crates.io, it shows:

let credential = DefaultAzureCredential::default();
let response = credential
    .get_token(&["https://management.azure.com/.default"])
    .await?;

credential is an instance of DefaultAzureCredential, which does not have a *public* .get_token() method. Using default(), the resulting object looks like:

DefaultAzureCredential { sources: [Environment(EnvironmentCredential { http_client: Client { accepts: Accepts, proxies: [Proxy(System({}), None)], referer: true, default_headers: {"accept": "*/*"} }, options: TokenCredentialOptions { authority_host: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("login.microsoftonline.com")), port: None, path: "/", query: None, fragment: None } }, cache: TokenCache(RwLock { value: {} }) }), ManagedIdentity(ImdsManagedIdentityCredential { http_client: Client { accepts: Accepts, proxies: [Proxy(System({}), None)], referer: true, default_headers: {"accept": "*/*"} }, object_id: None, client_id: None, msi_res_id: None, cache: TokenCache(RwLock { value: {} }) }), AzureCli(AzureCliCredential { cache: TokenCache(RwLock { value: {} }) })], cache: TokenCache(RwLock { value: {} }) }

So I then look at the README.md for the azure_identity package in this repository and it instead shows:

let credential = azure_identity::create_credential()?;

The only create_credential() method I see is in the token_credentials::specific_azure_credential file. This example doesn't work either.

So what is the correct way to get a token? There are plenty of get_token() and get_access_token() methods all over, but they're all private.

demoray commented 6 months ago

get_token is exposed via the TokenCredential trait, which the azure-identity credential providers implement.

Import azure_core::auth::TokenCredential and you'll have access to get_token.

lcrownover commented 6 months ago

That's interesting. The following code works:

    let credential = DefaultAzureCredential::default();
    let token = credential
        .get_token(&["https://management.azure.com/.default"])
        .await?;
    println!("token: {:?}", token.token.secret());

But after creating the credential, rust-analyzer doesn't seem to know about any of that functionality:

image

"get_token" listed in completions is just sourced from existing text in the buffer.

Same with VS Code:

image

I don't have buffer text completion configured in VS Code so there's no completions available at all.

I don't know much about how rust-analyzer works, but can you reproduce this?

demoray commented 6 months ago

I'm not familiar with using that portion of VS Code + rust-analyzer. With VS Code + rust-analyzer, I can confirm it is able to find the implementation using this example:

use azure_identity::DefaultAzureCredential;
use azure_core::auth::TokenCredential;

fn main() {
    let credential = DefaultAzureCredential::default();
    let _ = credential.get_token(&[".default"]);
}

image

As such, addressing issues in rust-analyzer+vscode is out of scope for the Azure SDK for Rust.

lcrownover commented 6 months ago

Fair enough, if your environment shows correct autocompletions then I suspect something is off on my end.

Thanks for the help!