Skip to content

Commit 03f4b6d

Browse files
gladjohnGladwinJohnsonneha-bhargava
authored
Add ML MSI Source (#5053)
* ese nit Update ManagedIdentity environment variables and add MachineLearning source# src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Unshipped.txt * pr comments * Update src/client/Microsoft.Identity.Client/ManagedIdentity/MachineLearningManagedIdentitySource.cs Co-authored-by: Neha Bhargava <[email protected]> * tests * Metadata * improve tests --------- Co-authored-by: Gladwin Johnson <[email protected]> Co-authored-by: Neha Bhargava <[email protected]>
1 parent 1dc9597 commit 03f4b6d

File tree

14 files changed

+367
-4
lines changed

14 files changed

+367
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal class EnvironmentVariables
1212
public static string PodIdentityEndpoint => Environment.GetEnvironmentVariable("AZURE_POD_IDENTITY_AUTHORITY_HOST");
1313
public static string ImdsEndpoint => Environment.GetEnvironmentVariable("IMDS_ENDPOINT");
1414
public static string MsiEndpoint => Environment.GetEnvironmentVariable("MSI_ENDPOINT");
15+
public static string MsiSecret => Environment.GetEnvironmentVariable("MSI_SECRET");
1516
public static string IdentityServerThumbprint => Environment.GetEnvironmentVariable("IDENTITY_SERVER_THUMBPRINT");
1617
}
1718
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Globalization;
6+
using Microsoft.Identity.Client.Core;
7+
using Microsoft.Identity.Client.Internal;
8+
9+
namespace Microsoft.Identity.Client.ManagedIdentity
10+
{
11+
internal class MachineLearningManagedIdentitySource : AbstractManagedIdentity
12+
{
13+
private const string MachineLearningMsiApiVersion = "2017-09-01";
14+
private const string SecretHeaderName = "secret";
15+
16+
private readonly Uri _endpoint;
17+
private readonly string _secret;
18+
19+
public static AbstractManagedIdentity Create(RequestContext requestContext)
20+
{
21+
requestContext.Logger.Info(() => "[Managed Identity] Machine learning managed identity is available.");
22+
23+
return TryValidateEnvVars(EnvironmentVariables.MsiEndpoint, requestContext.Logger, out Uri endpointUri)
24+
? new MachineLearningManagedIdentitySource(requestContext, endpointUri, EnvironmentVariables.MsiSecret)
25+
: null;
26+
}
27+
28+
private MachineLearningManagedIdentitySource(RequestContext requestContext, Uri endpoint, string secret)
29+
: base(requestContext, ManagedIdentitySource.MachineLearning)
30+
{
31+
_endpoint = endpoint;
32+
_secret = secret;
33+
}
34+
35+
private static bool TryValidateEnvVars(string msiEndpoint, ILoggerAdapter logger, out Uri endpointUri)
36+
{
37+
endpointUri = null;
38+
39+
try
40+
{
41+
endpointUri = new Uri(msiEndpoint);
42+
}
43+
catch (FormatException ex)
44+
{
45+
string errorMessage = string.Format(
46+
CultureInfo.InvariantCulture,
47+
MsalErrorMessage.ManagedIdentityEndpointInvalidUriError,
48+
"MSI_ENDPOINT", msiEndpoint, "Machine learning");
49+
50+
// Use the factory to create and throw the exception
51+
var exception = MsalServiceExceptionFactory.CreateManagedIdentityException(
52+
MsalError.InvalidManagedIdentityEndpoint,
53+
errorMessage,
54+
ex,
55+
ManagedIdentitySource.MachineLearning,
56+
null); // statusCode is null in this case
57+
58+
throw exception;
59+
}
60+
61+
logger.Info($"[Managed Identity] Environment variables validation passed for machine learning managed identity. Endpoint URI: {endpointUri}. Creating machine learning managed identity.");
62+
return true;
63+
}
64+
65+
protected override ManagedIdentityRequest CreateRequest(string resource)
66+
{
67+
ManagedIdentityRequest request = new(System.Net.Http.HttpMethod.Get, _endpoint);
68+
69+
request.Headers.Add("Metadata", "true");
70+
request.Headers.Add(SecretHeaderName, _secret);
71+
request.QueryParameters["api-version"] = MachineLearningMsiApiVersion;
72+
request.QueryParameters["resource"] = resource;
73+
74+
switch (_requestContext.ServiceBundle.Config.ManagedIdentityId.IdType)
75+
{
76+
case AppConfig.ManagedIdentityIdType.ClientId:
77+
_requestContext.Logger.Info("[Managed Identity] Adding user assigned client id to the request.");
78+
request.QueryParameters[Constants.ManagedIdentityClientId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
79+
break;
80+
81+
case AppConfig.ManagedIdentityIdType.ResourceId:
82+
_requestContext.Logger.Info("[Managed Identity] Adding user assigned resource id to the request.");
83+
request.QueryParameters[Constants.ManagedIdentityResourceId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
84+
break;
85+
86+
case AppConfig.ManagedIdentityIdType.ObjectId:
87+
_requestContext.Logger.Info("[Managed Identity] Adding user assigned object id to the request.");
88+
request.QueryParameters[Constants.ManagedIdentityObjectId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
89+
break;
90+
}
91+
92+
return request;
93+
}
94+
}
95+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private static AbstractManagedIdentity SelectManagedIdentitySource(RequestContex
4141
{
4242
ManagedIdentitySource.ServiceFabric => ServiceFabricManagedIdentitySource.Create(requestContext),
4343
ManagedIdentitySource.AppService => AppServiceManagedIdentitySource.Create(requestContext),
44+
ManagedIdentitySource.MachineLearning => MachineLearningManagedIdentitySource.Create(requestContext),
4445
ManagedIdentitySource.CloudShell => CloudShellManagedIdentitySource.Create(requestContext),
4546
ManagedIdentitySource.AzureArc => AzureArcManagedIdentitySource.Create(requestContext),
4647
_ => new ImdsManagedIdentitySource(requestContext)
@@ -57,11 +58,15 @@ internal static ManagedIdentitySource GetManagedIdentitySource(ILoggerAdapter lo
5758
string identityServerThumbprint = EnvironmentVariables.IdentityServerThumbprint;
5859
string msiSecret = EnvironmentVariables.IdentityHeader;
5960
string msiEndpoint = EnvironmentVariables.MsiEndpoint;
61+
string msiSecretMachineLearning = EnvironmentVariables.MsiSecret;
6062
string imdsEndpoint = EnvironmentVariables.ImdsEndpoint;
6163
string podIdentityEndpoint = EnvironmentVariables.PodIdentityEndpoint;
6264

63-
64-
if (!string.IsNullOrEmpty(identityEndpoint) && !string.IsNullOrEmpty(identityHeader))
65+
if (!string.IsNullOrEmpty(msiSecretMachineLearning) && !string.IsNullOrEmpty(msiEndpoint))
66+
{
67+
return ManagedIdentitySource.MachineLearning;
68+
}
69+
else if (!string.IsNullOrEmpty(identityEndpoint) && !string.IsNullOrEmpty(identityHeader))
6570
{
6671
if (!string.IsNullOrEmpty(identityServerThumbprint))
6772
{

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public enum ManagedIdentitySource
4848
/// Indicates that the source is defaulted to IMDS since no environment variables are set.
4949
/// This is used to detect the managed identity source.
5050
/// </summary>
51-
DefaultToImds
51+
DefaultToImds,
52+
53+
/// <summary>
54+
/// The source to acquire token for managed identity is Machine Learning Service.
55+
/// </summary>
56+
MachineLearning
5257
}
5358
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource

0 commit comments

Comments
 (0)