Supporting WorkloadIdentity auth type - #519
Conversation
There was a problem hiding this comment.
🟡 Not ready to approve
The new workload identity token-file assertion logic should defensively strip/validate file contents and provide clearer read errors, and the new config alias should be excluded from provider_settings per existing conventions.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
This PR adds Workload Identity authentication support to the Microsoft 365 Agents SDK for Python by introducing a new workload_identity auth type, extending auth configuration to accept a federated token file path, and updating the MSAL auth flow to use that token as a client assertion.
Changes:
- Added
workload_identitytoAuthTypes. - Extended
AgentAuthConfigurationwithFEDERATED_TOKEN_FILE/federated_token_fileinitialization support. - Updated MSAL client creation to read a federated token file and use it for
client_assertionwhenAUTH_TYPEisworkload_identity.
File summaries
| File | Description |
|---|---|
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/auth_types.py | Adds the new workload_identity auth type enum value. |
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/agent_auth_configuration.py | Adds configuration support for a federated token file path used by workload identity. |
| libraries/microsoft-agents-authentication-msal/microsoft_agents/authentication/msal/msal_auth.py | Implements workload identity MSAL client_assertion creation by reading the federated token file. |
Review details
Suppressed comments (1)
libraries/microsoft-agents-authentication-msal/microsoft_agents/authentication/msal/msal_auth.py:290
- The federated token file is read verbatim and may include trailing newlines/whitespace; that would change the JWT string and can cause assertion auth to fail. Also, if the file can’t be read, the current error will be an unhelpful
FileNotFoundError/OSError. Consider stripping the token and raising a clearerValueErrorthat includes the configured path.
if not self._msal_configuration.FEDERATED_TOKEN_FILE:
raise ValueError(
"FEDERATED_TOKEN_FILE must be set in configuration."
)
- Files reviewed: 3/3 changed files
- Comments generated: 3
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
WorkloadIdentity auth type
There was a problem hiding this comment.
🟡 Not ready to approve
The workload identity token file should be read defensively (at least stripping whitespace) and the new configuration field should be reflected in the configuration docstring for discoverability.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Review details
Suppressed comments (2)
libraries/microsoft-agents-authentication-msal/microsoft_agents/authentication/msal/msal_auth.py:296
- The workload identity client_assertion reads the token file verbatim, so any trailing newline/whitespace in the projected file will be included in the assertion string. That can result in an invalid JWT being sent to Entra; it’s safer to read as text and strip surrounding whitespace before returning it.
federated_token_file = self._msal_configuration.FEDERATED_TOKEN_FILE
def get_assertion() -> str:
with open(federated_token_file, "rb") as f:
return f.read().decode("utf-8")
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/agent_auth_configuration.py:101
- AgentAuthConfiguration’s docstring lists the supported configuration fields, but the newly introduced FEDERATED_TOKEN_FILE field isn’t documented there. This makes the configuration surface harder to discover for consumers.
AZURE_REGION: str | None
IDPM_RESOURCE: str | None
ANONYMOUS_ALLOWED: bool = False
VALIDATE_ISSUER: bool = False
FEDERATED_TOKEN_FILE: str | None
- Files reviewed: 5/5 changed files
- Comments generated: 0 new
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
There was a problem hiding this comment.
🟡 Not ready to approve
There are functional issues in auth-type normalization and token-file assertion parsing that can prevent Workload Identity from working when configured via env-style keys.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Review details
Suppressed comments (2)
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/agent_auth_configuration.py:138
- AUTH_TYPE is assigned directly from kwargs["AUTHTYPE"], which is typically a string (e.g. "WorkloadIdentity"). That leaves AUTH_TYPE as a plain str, so comparisons like
AUTH_TYPE == AuthTypes.workload_identitywill fail and the Workload Identity branch in MSAL auth won’t execute when configured via env-style keys.
federated_token_file: str | None = None,
**kwargs: Any,
):
self.AUTH_TYPE = auth_type or kwargs.get("AUTHTYPE", AuthTypes.client_secret)
libraries/microsoft-agents-authentication-msal/microsoft_agents/authentication/msal/msal_auth.py:295
- The projected federated token file commonly ends with a trailing newline. Returning the raw file contents (including whitespace) can break client assertion parsing. Read as text and strip surrounding whitespace/newlines before returning the assertion.
federated_token_file = self._msal_configuration.FEDERATED_TOKEN_FILE
def get_assertion() -> str:
with open(federated_token_file, "rb") as f:
return f.read().decode("utf-8")
- Files reviewed: 6/6 changed files
- Comments generated: 0 new
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
…into users/robrandao/workload-identity
There was a problem hiding this comment.
🟡 Not ready to approve
There are correctness/documentation gaps around the Workload Identity token-file handling that should be addressed to avoid misconfiguration and hard-to-diagnose runtime failures.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Review details
Suppressed comments (2)
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/authorization/agent_auth_configuration.py:86
- The docstring describes FEDERATED_TOKEN_FILE as being for "federated credentials authentication", but this field is actually consumed by the new AuthTypes.workload_identity path (msal_auth.py). This mismatch can mislead users configuring Workload Identity.
ANONYMOUS_ALLOWED: Whether anonymous access is allowed (default False).
FEDERATED_TOKEN_FILE: The path to the federated token file (if using federated credentials authentication).
libraries/microsoft-agents-authentication-msal/microsoft_agents/authentication/msal/msal_auth.py:295
- get_assertion() reads and strips the federated token file, but it can return an empty string if the file is empty/whitespace-only, which will cause downstream MSAL/AAD failures that are harder to diagnose. Consider validating that the read token is non-empty and raising a clear error (and optionally wrapping file I/O errors) before returning it.
federated_token_file = self._msal_configuration.FEDERATED_TOKEN_FILE
def get_assertion() -> str:
with open(federated_token_file, encoding="utf-8") as f:
return f.read().strip()
- Files reviewed: 6/6 changed files
- Comments generated: 0 new
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
This pull request adds support for Workload Identity authentication by introducing a new
workload_identityauthentication type and handling federated token files. The main changes involve updating the authentication configuration to accept a federated token file and modifying the MSAL authentication logic to use this file when the new authentication type is selected.Workload Identity Authentication Support
workload_identityvalue to theAuthTypesenum to represent the new authentication type.AgentAuthConfigurationto include aFEDERATED_TOKEN_FILEfield and accept it during initialization, allowing configuration of the federated token file path. [1] [2] [3]MSAL Authentication Logic
workload_identitytype: reads the federated token from the configured file and uses it as the client assertion for authentication. Raises a clear error if the federated token file is not set.