-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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]>
- Loading branch information
1 parent
1dc9597
commit 03f4b6d
Showing
14 changed files
with
367 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/client/Microsoft.Identity.Client/ManagedIdentity/MachineLearningManagedIdentitySource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Globalization; | ||
using Microsoft.Identity.Client.Core; | ||
using Microsoft.Identity.Client.Internal; | ||
|
||
namespace Microsoft.Identity.Client.ManagedIdentity | ||
{ | ||
internal class MachineLearningManagedIdentitySource : AbstractManagedIdentity | ||
{ | ||
private const string MachineLearningMsiApiVersion = "2017-09-01"; | ||
private const string SecretHeaderName = "secret"; | ||
|
||
private readonly Uri _endpoint; | ||
private readonly string _secret; | ||
|
||
public static AbstractManagedIdentity Create(RequestContext requestContext) | ||
{ | ||
requestContext.Logger.Info(() => "[Managed Identity] Machine learning managed identity is available."); | ||
|
||
return TryValidateEnvVars(EnvironmentVariables.MsiEndpoint, requestContext.Logger, out Uri endpointUri) | ||
? new MachineLearningManagedIdentitySource(requestContext, endpointUri, EnvironmentVariables.MsiSecret) | ||
: null; | ||
} | ||
|
||
private MachineLearningManagedIdentitySource(RequestContext requestContext, Uri endpoint, string secret) | ||
: base(requestContext, ManagedIdentitySource.MachineLearning) | ||
{ | ||
_endpoint = endpoint; | ||
_secret = secret; | ||
} | ||
|
||
private static bool TryValidateEnvVars(string msiEndpoint, ILoggerAdapter logger, out Uri endpointUri) | ||
{ | ||
endpointUri = null; | ||
|
||
try | ||
{ | ||
endpointUri = new Uri(msiEndpoint); | ||
} | ||
catch (FormatException ex) | ||
{ | ||
string errorMessage = string.Format( | ||
CultureInfo.InvariantCulture, | ||
MsalErrorMessage.ManagedIdentityEndpointInvalidUriError, | ||
"MSI_ENDPOINT", msiEndpoint, "Machine learning"); | ||
|
||
// Use the factory to create and throw the exception | ||
var exception = MsalServiceExceptionFactory.CreateManagedIdentityException( | ||
MsalError.InvalidManagedIdentityEndpoint, | ||
errorMessage, | ||
ex, | ||
ManagedIdentitySource.MachineLearning, | ||
null); // statusCode is null in this case | ||
|
||
throw exception; | ||
} | ||
|
||
logger.Info($"[Managed Identity] Environment variables validation passed for machine learning managed identity. Endpoint URI: {endpointUri}. Creating machine learning managed identity."); | ||
return true; | ||
} | ||
|
||
protected override ManagedIdentityRequest CreateRequest(string resource) | ||
{ | ||
ManagedIdentityRequest request = new(System.Net.Http.HttpMethod.Get, _endpoint); | ||
|
||
request.Headers.Add("Metadata", "true"); | ||
request.Headers.Add(SecretHeaderName, _secret); | ||
request.QueryParameters["api-version"] = MachineLearningMsiApiVersion; | ||
request.QueryParameters["resource"] = resource; | ||
|
||
switch (_requestContext.ServiceBundle.Config.ManagedIdentityId.IdType) | ||
{ | ||
case AppConfig.ManagedIdentityIdType.ClientId: | ||
_requestContext.Logger.Info("[Managed Identity] Adding user assigned client id to the request."); | ||
request.QueryParameters[Constants.ManagedIdentityClientId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId; | ||
break; | ||
|
||
case AppConfig.ManagedIdentityIdType.ResourceId: | ||
_requestContext.Logger.Info("[Managed Identity] Adding user assigned resource id to the request."); | ||
request.QueryParameters[Constants.ManagedIdentityResourceId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId; | ||
break; | ||
|
||
case AppConfig.ManagedIdentityIdType.ObjectId: | ||
_requestContext.Logger.Info("[Managed Identity] Adding user assigned object id to the request."); | ||
request.QueryParameters[Constants.ManagedIdentityObjectId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId; | ||
break; | ||
} | ||
|
||
return request; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource.MachineLearning = 7 -> Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/MachineLearningTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Identity.Client.AppConfig; | ||
using Microsoft.Identity.Client.ManagedIdentity; | ||
using Microsoft.Identity.Test.Common; | ||
using Microsoft.Identity.Test.Common.Core.Helpers; | ||
using Microsoft.Identity.Test.Common.Core.Mocks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using static Microsoft.Identity.Test.Common.Core.Helpers.ManagedIdentityTestUtil; | ||
|
||
namespace Microsoft.Identity.Test.Unit.ManagedIdentityTests | ||
{ | ||
[TestClass] | ||
public class MachineLearningTests : TestBase | ||
{ | ||
private const string MachineLearning = "Machine learning"; | ||
|
||
[TestMethod] | ||
public async Task MachineLearningTestsInvalidEndpointAsync() | ||
{ | ||
using (new EnvVariableContext()) | ||
using (var httpManager = new MockHttpManager(isManagedIdentity: true)) | ||
{ | ||
SetEnvironmentVariables(ManagedIdentitySource.MachineLearning, "127.0.0.1:41564/msi/token"); | ||
|
||
var miBuilder = ManagedIdentityApplicationBuilder.Create(ManagedIdentityId.SystemAssigned) | ||
.WithHttpManager(httpManager); | ||
|
||
// Disabling shared cache options to avoid cross test pollution. | ||
miBuilder.Config.AccessorOptions = null; | ||
|
||
var mi = miBuilder.Build(); | ||
|
||
MsalServiceException ex = await Assert.ThrowsExceptionAsync<MsalServiceException>(async () => | ||
await mi.AcquireTokenForManagedIdentity(ManagedIdentityTests.Resource) | ||
.ExecuteAsync().ConfigureAwait(false)).ConfigureAwait(false); | ||
|
||
Assert.IsNotNull(ex); | ||
Assert.AreEqual(MsalError.InvalidManagedIdentityEndpoint, ex.ErrorCode); | ||
Assert.AreEqual(ManagedIdentitySource.MachineLearning.ToString(), ex.AdditionalExceptionData[MsalException.ManagedIdentitySource]); | ||
Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, MsalErrorMessage.ManagedIdentityEndpointInvalidUriError, "MSI_ENDPOINT", "127.0.0.1:41564/msi/token", MachineLearning), ex.Message); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.