Skip to content

Commit 2a23172

Browse files
Added support for DEFAULT_IDENTITY_CLIENT_ID environment variable in Machine Learning Managed Identity (#5351)
1 parent 6154ac9 commit 2a23172

File tree

14 files changed

+156
-37
lines changed

14 files changed

+156
-37
lines changed

src/client/Microsoft.Identity.Client/ManagedIdentity/EnvironmentVariables.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ internal class EnvironmentVariables
1414
public static string MsiEndpoint => Environment.GetEnvironmentVariable("MSI_ENDPOINT");
1515
public static string MsiSecret => Environment.GetEnvironmentVariable("MSI_SECRET");
1616
public static string IdentityServerThumbprint => Environment.GetEnvironmentVariable("IDENTITY_SERVER_THUMBPRINT");
17+
public static string MachineLearningDefaultClientId => Environment.GetEnvironmentVariable("DEFAULT_IDENTITY_CLIENT_ID");
1718
}
1819
}

src/client/Microsoft.Identity.Client/ManagedIdentity/MachineLearningManagedIdentitySource.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ namespace Microsoft.Identity.Client.ManagedIdentity
1010
{
1111
internal class MachineLearningManagedIdentitySource : AbstractManagedIdentity
1212
{
13+
private const string MachineLearning = "Machine Learning";
14+
1315
private const string MachineLearningMsiApiVersion = "2017-09-01";
1416
private const string SecretHeaderName = "secret";
1517

1618
private readonly Uri _endpoint;
1719
private readonly string _secret;
1820

21+
public const string UnsupportedIdTypeError = "Only client id is supported for user-assigned managed identity in Machine Learning."; // referenced in unit test
22+
1923
public static AbstractManagedIdentity Create(RequestContext requestContext)
2024
{
2125
requestContext.Logger.Info(() => "[Managed Identity] Machine learning managed identity is available.");
@@ -47,15 +51,12 @@ private static bool TryValidateEnvVars(string msiEndpoint, ILoggerAdapter logger
4751
MsalErrorMessage.ManagedIdentityEndpointInvalidUriError,
4852
"MSI_ENDPOINT", msiEndpoint, "Machine learning");
4953

50-
// Use the factory to create and throw the exception
51-
var exception = MsalServiceExceptionFactory.CreateManagedIdentityException(
54+
throw MsalServiceExceptionFactory.CreateManagedIdentityException(
5255
MsalError.InvalidManagedIdentityEndpoint,
5356
errorMessage,
5457
ex,
5558
ManagedIdentitySource.MachineLearning,
5659
null); // statusCode is null in this case
57-
58-
throw exception;
5960
}
6061

6162
logger.Info($"[Managed Identity] Environment variables validation passed for machine learning managed identity. Endpoint URI: {endpointUri}. Creating machine learning managed identity.");
@@ -73,21 +74,37 @@ protected override ManagedIdentityRequest CreateRequest(string resource)
7374

7475
switch (_requestContext.ServiceBundle.Config.ManagedIdentityId.IdType)
7576
{
77+
case AppConfig.ManagedIdentityIdType.SystemAssigned:
78+
_requestContext.Logger.Info("[Managed Identity] Adding system assigned client id to the request.");
79+
80+
// this environment variable is always set in an Azure Machine Learning source, but check if null just in case
81+
if (EnvironmentVariables.MachineLearningDefaultClientId == null)
82+
{
83+
throw MsalServiceExceptionFactory.CreateManagedIdentityException(
84+
MsalError.InvalidManagedIdentityIdType,
85+
"The DEFAULT_IDENTITY_CLIENT_ID environment variable is null.",
86+
null, // configuration error
87+
ManagedIdentitySource.MachineLearning,
88+
null); // statusCode is null in this case
89+
}
90+
91+
// Use the new 2017 constant for older ML-based environment
92+
request.QueryParameters[Constants.ManagedIdentityClientId2017] = EnvironmentVariables.MachineLearningDefaultClientId;
93+
break;
94+
7695
case AppConfig.ManagedIdentityIdType.ClientId:
7796
_requestContext.Logger.Info("[Managed Identity] Adding user assigned client id to the request.");
7897
// Use the new 2017 constant for older ML-based environment
7998
request.QueryParameters[Constants.ManagedIdentityClientId2017] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
8099
break;
81100

82-
case AppConfig.ManagedIdentityIdType.ResourceId:
83-
_requestContext.Logger.Info("[Managed Identity] Adding user assigned resource id to the request.");
84-
request.QueryParameters[Constants.ManagedIdentityResourceId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
85-
break;
86-
87-
case AppConfig.ManagedIdentityIdType.ObjectId:
88-
_requestContext.Logger.Info("[Managed Identity] Adding user assigned object id to the request.");
89-
request.QueryParameters[Constants.ManagedIdentityObjectId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
90-
break;
101+
default:
102+
throw MsalServiceExceptionFactory.CreateManagedIdentityException(
103+
MsalError.InvalidManagedIdentityIdType,
104+
UnsupportedIdTypeError,
105+
null, // configuration error
106+
ManagedIdentitySource.MachineLearning,
107+
null); // statusCode is null in this case
91108
}
92109

93110
return request;

src/client/Microsoft.Identity.Client/MsalError.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,16 @@ public static class MsalError
11051105
/// </summary>
11061106
public const string InvalidManagedIdentityResponse = "invalid_managed_identity_response";
11071107

1108+
/// <summary>
1109+
/// The managed identity's source does not select a specific id type.
1110+
/// </summary>
1111+
public const string InvalidManagedIdentityIdType = "invalid_managed_identity_id_type";
1112+
1113+
/// <summary>
1114+
/// The managed identity is missing a required environment variable.
1115+
/// </summary>
1116+
public const string MissingManagedIdentityEnvVar = "missing_managed_identity_env_var";
1117+
11081118
/// <summary>
11091119
/// Managed Identity error response was received.
11101120
/// </summary>

src/client/Microsoft.Identity.Client/MsalErrorMessage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ public static string InvalidTokenProviderResponseValue(string invalidValueName)
415415

416416
public const string ManagedIdentityNoResponseReceived = "[Managed Identity] Authentication unavailable. No response received from the managed identity endpoint.";
417417
public const string ManagedIdentityInvalidResponse = "[Managed Identity] Invalid response, the authentication response received did not contain the expected fields.";
418+
public const string ManagedIdentityInvalidIdType = "Only {0} supported for user-assigned managed identity in {1}";
418419
public const string ManagedIdentityJsonParseFailure = "[Managed Identity] MSI returned 200 OK, but the response could not be parsed.";
419420
public const string ManagedIdentityUnexpectedResponse = "[Managed Identity] Unexpected exception occurred when parsing the response. See the inner exception for details.";
420421
public const string ManagedIdentityExactlyOneScopeExpected = "[Managed Identity] To acquire token for managed identity, exactly one scope must be passed.";
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
const Microsoft.Identity.Client.MsalError.InvalidManagedIdentityIdType = "invalid_managed_identity_id_type" -> string
2+
const Microsoft.Identity.Client.MsalError.MissingManagedIdentityEnvVar = "missing_managed_identity_env_var" -> string
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
const Microsoft.Identity.Client.MsalError.InvalidManagedIdentityIdType = "invalid_managed_identity_id_type" -> string
2+
const Microsoft.Identity.Client.MsalError.MissingManagedIdentityEnvVar = "missing_managed_identity_env_var" -> string
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Microsoft.Identity.Client.MsalError.InvalidManagedIdentityIdType = "invalid_managed_identity_id_type" -> string
2+
const Microsoft.Identity.Client.MsalError.MissingManagedIdentityEnvVar = "missing_managed_identity_env_var" -> string
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Microsoft.Identity.Client.MsalError.InvalidManagedIdentityIdType = "invalid_managed_identity_id_type" -> string
2+
const Microsoft.Identity.Client.MsalError.MissingManagedIdentityEnvVar = "missing_managed_identity_env_var" -> string
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
const Microsoft.Identity.Client.MsalError.InvalidManagedIdentityIdType = "invalid_managed_identity_id_type" -> string
2+
const Microsoft.Identity.Client.MsalError.MissingManagedIdentityEnvVar = "missing_managed_identity_env_var" -> string
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Microsoft.Identity.Client.MsalError.InvalidManagedIdentityIdType = "invalid_managed_identity_id_type" -> string
2+
const Microsoft.Identity.Client.MsalError.MissingManagedIdentityEnvVar = "missing_managed_identity_env_var" -> string

0 commit comments

Comments
 (0)