Skip to content

Commit

Permalink
Add support for MSI credential in configuration (Azure#18459)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored and jongio committed Feb 9, 2021
1 parent eeebae4 commit 3fd50af
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sdk/extensions/Microsoft.Extensions.Azure/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.1.0-beta.2 (Unreleased)

### Added

- The ability to use `ManagedIdentityCredential` from the configuration using the `"credential": "managedidentity"`

## 1.1.0-beta.1 (2020-11-10)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ public static object CreateClient(Type clientType, Type optionsType, object opti

internal static TokenCredential CreateCredential(IConfiguration configuration, TokenCredentialOptions identityClientOptions = null)
{
var credentialType = configuration["credential"];
var clientId = configuration["clientId"];
var tenantId = configuration["tenantId"];
var clientSecret = configuration["clientSecret"];
var certificate = configuration["clientCertificate"];
var certificateStoreName = configuration["clientCertificateStoreName"];
var certificateStoreLocation = configuration["clientCertificateStoreLocation"];

if (string.Equals(credentialType, "managedidentity", StringComparison.OrdinalIgnoreCase))
{
return new ManagedIdentityCredential(clientId);
}

if (!string.IsNullOrWhiteSpace(tenantId) &&
!string.IsNullOrWhiteSpace(clientId) &&
!string.IsNullOrWhiteSpace(clientSecret))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using Azure.Identity;
using Microsoft.Extensions.Azure;
Expand Down Expand Up @@ -155,6 +156,43 @@ public void CreatesClientSecretCredentials()
Assert.AreEqual("ConfigurationTenantId", clientSecretCredential.TenantId);
}

[Test]
public void CreatesManagedServiceIdentityCredentialsWithClientId()
{
IConfiguration configuration = GetConfiguration(
new KeyValuePair<string, string>("clientId", "ConfigurationClientId"),
new KeyValuePair<string, string>("credential", "managedidentity")
);

var credential = ClientFactory.CreateCredential(configuration);

Assert.IsInstanceOf<ManagedIdentityCredential>(credential);
var managedIdentityCredential = (ManagedIdentityCredential)credential;

var client = (ManagedIdentityClient)typeof(ManagedIdentityCredential).GetField("_client", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(managedIdentityCredential);
var clientId = typeof(ManagedIdentityClient).GetProperty("ClientId", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(client);

Assert.AreEqual("ConfigurationClientId", clientId);
}

[Test]
public void CreatesManagedServiceIdentityCredentials()
{
IConfiguration configuration = GetConfiguration(
new KeyValuePair<string, string>("credential", "managedidentity")
);

var credential = ClientFactory.CreateCredential(configuration);

Assert.IsInstanceOf<ManagedIdentityCredential>(credential);
var managedIdentityCredential = (ManagedIdentityCredential)credential;

var client = (ManagedIdentityClient)typeof(ManagedIdentityCredential).GetField("_client", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(managedIdentityCredential);
var clientId = typeof(ManagedIdentityClient).GetProperty("ClientId", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(client);

Assert.Null(clientId);
}

[Test]
public void IgnoresConstructorWhenCredentialsNull()
{
Expand Down

0 comments on commit 3fd50af

Please sign in to comment.