Skip to content

Commit

Permalink
[Identity] Aggregate errors for get_token (#481)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Gendein <[email protected]>
  • Loading branch information
JoshGendein and Joshua Gendein authored Nov 6, 2021
1 parent bcbd8a0 commit 1e52c00
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions sdk/identity/src/token_credentials/default_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::{
AzureCliCredential, EnvironmentCredential, ManagedIdentityCredential, TokenCredential,
};
use azure_core::TokenResponse;
use log::debug;

#[derive(Debug, Default)]
/// Provides a mechanism of selectively disabling credentials used for a `DefaultAzureCredential` instance
Expand Down Expand Up @@ -67,8 +66,11 @@ pub enum DefaultAzureCredentialError {
EnvironmentCredentialError(#[from] super::EnvironmentCredentialError),
#[error("Error getting managed identity credential: {0}")]
ManagedIdentityCredentialError(#[from] super::ManagedIdentityCredentialError),
#[error("End of default list")]
EndOfDefaultList,
#[error(
"Multiple errors were encountered while attempting to authenticate:\n{}",
format_aggregate_error(.0)
)]
CredentialUnavailable(Vec<DefaultAzureCredentialError>),
}

/// Types of TokenCredential supported by DefaultAzureCredential
Expand Down Expand Up @@ -134,16 +136,16 @@ impl TokenCredential for DefaultAzureCredential {
type Error = DefaultAzureCredentialError;
/// Try to fetch a token using each of the credential sources until one succeeds
async fn get_token(&self, resource: &str) -> Result<TokenResponse, Self::Error> {
let mut errors = Vec::new();
for source in &self.sources {
let token_res = source.get_token(resource).await;

if let Ok(token) = token_res {
return Ok(token);
} else {
debug!("Failed to get credentials: {:?}", token_res.err().unwrap());
match token_res {
Ok(token) => return Ok(token),
Err(error) => errors.push(error),
}
}
Err(DefaultAzureCredentialError::EndOfDefaultList)
Err(DefaultAzureCredentialError::CredentialUnavailable(errors))
}
}

Expand All @@ -158,3 +160,11 @@ impl azure_core::TokenCredential for DefaultAzureCredential {
.map_err(|error| azure_core::Error::GetTokenError(Box::new(error)))
}
}

fn format_aggregate_error(errors: &[DefaultAzureCredentialError]) -> String {
errors
.iter()
.map(|error| error.to_string())
.collect::<Vec<String>>()
.join("\n")
}

0 comments on commit 1e52c00

Please sign in to comment.