Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding option to specify client_id for MSI #748

Merged
merged 9 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sdk/identity/src/token_credentials/default_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DefaultAzureCredentialBuilder {
}
if self.include_managed_identity_credential {
sources.push(DefaultAzureCredentialEnum::ManagedIdentity(
ImdsManagedIdentityCredential {},
ImdsManagedIdentityCredential::default(),
))
}
if self.include_cli_credential {
Expand Down Expand Up @@ -142,7 +142,9 @@ impl Default for DefaultAzureCredential {
DefaultAzureCredential {
sources: vec![
DefaultAzureCredentialEnum::Environment(EnvironmentCredential::default()),
DefaultAzureCredentialEnum::ManagedIdentity(ImdsManagedIdentityCredential {}),
DefaultAzureCredentialEnum::ManagedIdentity(
ImdsManagedIdentityCredential::default(),
),
DefaultAzureCredentialEnum::AzureCli(AzureCliCredential {}),
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,27 @@ const MSI_API_VERSION: &str = "2019-08-01";
/// This authentication type works in Azure VMs, App Service and Azure Functions applications, as well as the Azure Cloud Shell
///
/// Built up from docs at [https://docs.microsoft.com/azure/app-service/overview-managed-identity#using-the-rest-protocol](https://docs.microsoft.com/azure/app-service/overview-managed-identity#using-the-rest-protocol)
pub struct ImdsManagedIdentityCredential;
#[derive(Default)]
pub struct ImdsManagedIdentityCredential {
object_id: Option<String>,
client_id: Option<String>,
mi_res_id: Option<String>,
aj9411 marked this conversation as resolved.
Show resolved Hide resolved
}

impl ImdsManagedIdentityCredential {
/// Create a new ImdsManagedIdentityCredential with the given optional parameters. Only one of object_id, client_id and mi_res_id may be set.
pub fn new(
aj9411 marked this conversation as resolved.
Show resolved Hide resolved
object_id: Option<String>,
client_id: Option<String>,
mi_res_id: Option<String>,
aj9411 marked this conversation as resolved.
Show resolved Hide resolved
) -> Self {
Self {
object_id,
client_id,
mi_res_id,
aj9411 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[allow(missing_docs)]
#[non_exhaustive]
Expand All @@ -39,6 +59,8 @@ pub enum ManagedIdentityCredentialError {
IdentityUnavailableError,
#[error("The request failed due to a gateway error.")]
GatewayError,
#[error("Only one of object_id, client_id, and mi_res_id may be specified on a request to get a token.")]
aj9411 marked this conversation as resolved.
Show resolved Hide resolved
MoreThanOneIdParameterSpecified,
}

#[async_trait::async_trait]
Expand All @@ -49,7 +71,20 @@ impl TokenCredential for ImdsManagedIdentityCredential {
let msi_endpoint = std::env::var(MSI_ENDPOINT_ENV_KEY)
.unwrap_or_else(|_| "http://169.254.169.254/metadata/identity/oauth2/token".to_owned());

let query_items = vec![("api-version", MSI_API_VERSION), ("resource", resource)];
let mut query_items = vec![("api-version", MSI_API_VERSION), ("resource", resource)];

match (
self.object_id.as_ref(),
self.client_id.as_ref(),
self.mi_res_id.as_ref(),
) {
(Some(object_id), None, None) => query_items.push(("object_id", object_id)),
(None, Some(client_id), None) => query_items.push(("client_id", client_id)),
(None, None, Some(mi_res_id)) => query_items.push(("mi_res_id", mi_res_id)),
aj9411 marked this conversation as resolved.
Show resolved Hide resolved
_ => {
return Err(ManagedIdentityCredentialError::MoreThanOneIdParameterSpecified);
}
}

let msi_endpoint_url = Url::parse_with_params(&msi_endpoint, &query_items)
.map_err(ManagedIdentityCredentialError::MsiEndpointParseUrlError)?;
Expand Down