Skip to content

Commit 7142936

Browse files
authored
Merge pull request Azure#9887 from erich-wang/fix-user-msi
fix issue that user MSI doens't work in Azure Function
2 parents 5808001 + 04cade5 commit 7142936

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020
## Upcoming Release
2121
* Fixed miscellaneous typos across module
22+
* Support user-assigned MSI in Azure Functiosn Authentication (#9479)
2223

2324
## Version 1.6.1
2425
* Update common code to use latest version of ClientRuntime

src/Accounts/Authentication.Test/AuthenticationFactoryTests.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,9 @@ public void AppServiceManagedIdentity()
365365
{
366366
AzureSessionInitializer.InitializeAzureSession();
367367
var tenant = Guid.NewGuid().ToString();
368-
var userId = Guid.NewGuid().ToString();
369368
var environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
370369
var account = new AzureAccount
371370
{
372-
Id = userId,
373371
Type = AzureAccount.AccountType.ManagedService
374372
};
375373
const string resource = @"https://management.azure.com/";
@@ -402,7 +400,7 @@ public void AppServiceManagedIdentity()
402400

403401
[Fact]
404402
[Trait(Category.AcceptanceType, Category.CheckIn)]
405-
public void AppServiceManagedIdentityWithDataPlane()
403+
public void AppServiceUserManagedIdentityWithDataPlane()
406404
{
407405
AzureSessionInitializer.InitializeAzureSession();
408406
var tenant = Guid.NewGuid().ToString();
@@ -415,6 +413,45 @@ public void AppServiceManagedIdentityWithDataPlane()
415413
};
416414
const string resource = @"https://vault.azure.com/";
417415
const string endpoint = @"http://127.0.0.1:41217/MSI/token/";
416+
var expectedUri = $"{endpoint}?resource={resource}&api-version=2017-09-01&clientid={userId}";
417+
account.SetProperty(AzureAccount.Property.MSILoginUri, endpoint);
418+
account.SetProperty(AzureAccount.Property.MSILoginSecret, @"bar");
419+
const string expectedAccessToken = "foo";
420+
var expectedExpiresOn = DateTimeOffset.Parse("1/23/2019 7:15:42 AM +00:00");
421+
var responses = new Dictionary<string, ManagedServiceAppServiceTokenInfo>(StringComparer.OrdinalIgnoreCase)
422+
{
423+
{
424+
expectedUri,
425+
new ManagedServiceAppServiceTokenInfo()
426+
{
427+
AccessToken = expectedAccessToken,
428+
ExpiresOn = expectedExpiresOn,
429+
Resource = resource,
430+
TokenType = "Bearer",
431+
}
432+
}
433+
};
434+
AzureSession.Instance.RegisterComponent(HttpClientOperationsFactory.Name, () => TestHttpOperationsFactory.Create(responses, _output), true);
435+
var msat = new ManagedServiceAppServiceAccessToken(account, environment, environment.GetEndpoint(resource) ?? resource, tenant);
436+
Assert.Equal(expectedUri, msat.RequestUris.Peek());
437+
var accessToken = msat.AccessToken;
438+
Assert.Equal(expectedAccessToken, accessToken);
439+
Assert.Equal(expectedExpiresOn, msat.ExpiresOn);
440+
}
441+
442+
[Fact]
443+
[Trait(Category.AcceptanceType, Category.CheckIn)]
444+
public void AppServiceManagedIdentityWithDataPlane()
445+
{
446+
AzureSessionInitializer.InitializeAzureSession();
447+
var tenant = Guid.NewGuid().ToString();
448+
var environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
449+
var account = new AzureAccount
450+
{
451+
Type = AzureAccount.AccountType.ManagedService
452+
};
453+
const string resource = @"https://vault.azure.com/";
454+
const string endpoint = @"http://127.0.0.1:41217/MSI/token/";
418455
var expectedUri = $"{endpoint}?resource={resource}&api-version=2017-09-01";
419456
account.SetProperty(AzureAccount.Property.MSILoginUri, endpoint);
420457
account.SetProperty(AzureAccount.Property.MSILoginSecret, @"bar");
@@ -447,11 +484,9 @@ public void AppServiceManagedIdentityWithServiceManagement()
447484
{
448485
AzureSessionInitializer.InitializeAzureSession();
449486
var tenant = Guid.NewGuid().ToString();
450-
var userId = Guid.NewGuid().ToString();
451487
var environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
452488
var account = new AzureAccount
453489
{
454-
Id = userId,
455490
Type = AzureAccount.AccountType.ManagedService
456491
};
457492
const string resource = @"https://management.azure.com/";

src/Accounts/Authentication/Authentication/ManagedServiceAccessTokenBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public abstract class ManagedServiceAccessTokenBase<TManagedServiceTokenInfo> :
3434

3535
protected ManagedServiceAccessTokenBase(IAzureAccount account, IAzureEnvironment environment, string resourceId, string tenant = "Common")
3636
{
37-
if (string.IsNullOrEmpty(account?.Id) || !account.IsPropertySet(AzureAccount.Property.MSILoginUri))
37+
if (!account.IsPropertySet(AzureAccount.Property.MSILoginUri))
3838
{
3939
throw new ArgumentNullException(nameof(account));
4040
}

src/Accounts/Authentication/Authentication/ManagedServiceAppServiceAccessToken.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
16+
using System;
1617
using System.Collections.Generic;
18+
using System.Text;
1719

1820
namespace Microsoft.Azure.Commands.Common.Authentication
1921
{
@@ -32,7 +34,14 @@ public ManagedServiceAppServiceAccessToken(IAzureAccount account, IAzureEnvironm
3234
protected override IEnumerable<string> BuildTokenUri(string baseUri, IAzureAccount account, IdentityType identityType,
3335
string resourceId)
3436
{
35-
yield return $"{baseUri}?resource={resourceId}&api-version=2017-09-01";;
37+
StringBuilder query = new StringBuilder($"{baseUri}?resource={resourceId}&api-version=2017-09-01");
38+
39+
if(identityType == IdentityType.ClientId || identityType == IdentityType.ObjectId)
40+
{
41+
query.Append($"&clientid={Uri.EscapeDataString(account.Id)}");
42+
}
43+
44+
yield return query.ToString();
3645
}
3746

3847
protected override void SetToken(ManagedServiceAppServiceTokenInfo infoWebApps)

0 commit comments

Comments
 (0)