
Workload Identity credential authentication
Source:R/credential-workload-identity.R
WorkloadIdentityCredential.RdAuthenticates using Azure Workload Identity by reading a federated token from a file and exchanging it for an Azure AD access token. This is commonly used in Kubernetes environments (AKS) where a service account token is mounted into the pod.
Details
The credential implements the OAuth 2.0 client credentials flow with a JWT
bearer assertion (client_assertion). It reads the federated identity token
from a file on each call to get_token() so that token rotation by the
runtime (e.g., Kubernetes) is automatically picked up.
The following environment variables are used when parameters are not provided:
AZURE_CLIENT_ID: Client (application) ID of the Azure AD applicationAZURE_TENANT_ID: Azure AD tenant IDAZURE_FEDERATED_TOKEN_FILE: Path to the file containing the federated token
Methods
Inherited methods
Method new()
Create a new Workload Identity credential
Usage
WorkloadIdentityCredential$new(
scope = NULL,
tenant_id = Sys.getenv(environment_variables$azure_tenant_id, unset = NA_character_),
client_id = Sys.getenv(environment_variables$azure_client_id, unset = NA_character_),
token_file_path = default_federated_token_file()
)Arguments
scopeA character string specifying the OAuth2 scope. Defaults to the Azure Resource Manager scope.
tenant_idA character string specifying the Azure AD tenant ID. Defaults to the
AZURE_TENANT_IDenvironment variable.client_idA character string specifying the client (application) ID. Defaults to the
AZURE_CLIENT_IDenvironment variable.token_file_pathA character string specifying the path to the file containing the federated identity token. Defaults to the
AZURE_FEDERATED_TOKEN_FILEenvironment variable.
Method get_token()
Get an access token by exchanging the federated token
Details
Returns a valid in-object cached token immediately if one exists. Otherwise reads the federated token from the file and exchanges it for a new access token so that token rotation performed by the runtime is automatically reflected.
Returns
An httr2::oauth_token() object containing the access token
Examples
if (FALSE) { # \dontrun{
# Create credential using environment variables
# (requires AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_FEDERATED_TOKEN_FILE)
cred <- WorkloadIdentityCredential$new(
scope = "https://management.azure.com/.default"
)
# Or supply parameters directly
cred <- WorkloadIdentityCredential$new(
tenant_id = "your-tenant-id",
client_id = "your-client-id",
token_file_path = "/var/run/secrets/azure/tokens/azure-identity-token",
scope = "https://management.azure.com/.default"
)
# Get an access token
token <- cred$get_token()
# Use with httr2 request
req <- httr2::request("https://management.azure.com/subscriptions")
resp <- httr2::req_perform(cred$req_auth(req))
} # }