Skip to content

Commit 9003654

Browse files
isra-felYeming Liu
andauthored
Fix keyvault access token issue (#13317)
* keyvault can get access token from account * use string.equal Co-authored-by: Yeming Liu <[email protected]>
1 parent 9ddd575 commit 9003654

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed an issue causing `Connect-AzAccount -KeyVaultAccessToken` not working [#13127]
2122
* Fixed null reference and method case insensitive in `Invoke-AzRestMethod`
2223

2324
## Version 2.1.0

src/Accounts/Authentication.Test/AuthenticationFactoryTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
using Xunit;
2929
using Xunit.Abstractions;
3030
using System.Text.RegularExpressions;
31+
using System.Net.Http;
32+
using System.Threading;
3133

3234
namespace Common.Authentication.Test
3335
{
@@ -561,5 +563,51 @@ private string GetFunctionsResourceId(string resourceIdOrEndpointName, IAzureEnv
561563

562564
return resourceId;
563565
}
566+
567+
[Fact]
568+
[Trait(Category.AcceptanceType, Category.CheckIn)]
569+
public void CanGetServiceClientCredentialsWithAccessToken()
570+
{
571+
AzureSessionInitializer.InitializeAzureSession();
572+
IAuthenticatorBuilder authenticatorBuilder = new DefaultAuthenticatorBuilder();
573+
AzureSession.Instance.RegisterComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, () => authenticatorBuilder);
574+
PowerShellTokenCacheProvider factory = new InMemoryTokenCacheProvider();
575+
AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => factory);
576+
string tenant = Guid.NewGuid().ToString();
577+
string userId = "[email protected]";
578+
var armToken = Guid.NewGuid().ToString();
579+
var graphToken = Guid.NewGuid().ToString();
580+
var kvToken = Guid.NewGuid().ToString();
581+
var account = new AzureAccount
582+
{
583+
Id = userId,
584+
Type = AzureAccount.AccountType.AccessToken
585+
};
586+
account.SetTenants(tenant);
587+
account.SetAccessToken(armToken);
588+
account.SetProperty(AzureAccount.Property.GraphAccessToken, graphToken);
589+
account.SetProperty(AzureAccount.Property.KeyVaultAccessToken, kvToken);
590+
var authFactory = new AuthenticationFactory();
591+
var environment = AzureEnvironment.PublicEnvironments.Values.First();
592+
var mockContext = new AzureContext()
593+
{
594+
Account = account
595+
};
596+
var credentials = authFactory.GetServiceClientCredentials(mockContext);
597+
VerifyAccessTokenInServiceClientCredentials(credentials, armToken);
598+
credentials = authFactory.GetServiceClientCredentials(mockContext, AzureEnvironment.Endpoint.Graph);
599+
VerifyAccessTokenInServiceClientCredentials(credentials, graphToken);
600+
credentials = authFactory.GetServiceClientCredentials(mockContext, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId);
601+
VerifyAccessTokenInServiceClientCredentials(credentials, kvToken);
602+
}
603+
604+
private void VerifyAccessTokenInServiceClientCredentials(Microsoft.Rest.ServiceClientCredentials cred, string expected)
605+
{
606+
using (var request = new HttpRequestMessage())
607+
{
608+
cred.ProcessHttpRequestAsync(request, new CancellationToken()).ConfigureAwait(false).GetAwaiter().GetResult();
609+
Assert.Equal(expected, request.Headers.Authorization.Parameter);
610+
}
611+
}
564612
}
565613
}

src/Accounts/Authentication/Factories/AuthenticationFactory.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,14 @@ private string GetFunctionsResourceId(string resourceIdOrEndpointName, IAzureEnv
438438
private string GetEndpointToken(IAzureAccount account, string targetEndpoint)
439439
{
440440
string tokenKey = AzureAccount.Property.AccessToken;
441-
if (targetEndpoint == AzureEnvironment.Endpoint.Graph)
441+
if (string.Equals(targetEndpoint, AzureEnvironment.Endpoint.Graph, StringComparison.OrdinalIgnoreCase))
442442
{
443443
tokenKey = AzureAccount.Property.GraphAccessToken;
444444
}
445-
445+
if (string.Equals(targetEndpoint, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId, StringComparison.OrdinalIgnoreCase))
446+
{
447+
tokenKey = AzureAccount.Property.KeyVaultAccessToken;
448+
}
446449
return account.GetProperty(tokenKey);
447450
}
448451

0 commit comments

Comments
 (0)