Skip to content

Fix keyvault access token issue #13317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Fixed an issue causing `Connect-AzAccount -KeyVaultAccessToken` not working [#13127]
* Fixed null reference and method case insensitive in `Invoke-AzRestMethod`

## Version 2.1.0
Expand Down
48 changes: 48 additions & 0 deletions src/Accounts/Authentication.Test/AuthenticationFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
using Xunit;
using Xunit.Abstractions;
using System.Text.RegularExpressions;
using System.Net.Http;
using System.Threading;

namespace Common.Authentication.Test
{
Expand Down Expand Up @@ -561,5 +563,51 @@ private string GetFunctionsResourceId(string resourceIdOrEndpointName, IAzureEnv

return resourceId;
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void CanGetServiceClientCredentialsWithAccessToken()
{
AzureSessionInitializer.InitializeAzureSession();
IAuthenticatorBuilder authenticatorBuilder = new DefaultAuthenticatorBuilder();
AzureSession.Instance.RegisterComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, () => authenticatorBuilder);
PowerShellTokenCacheProvider factory = new InMemoryTokenCacheProvider();
AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => factory);
string tenant = Guid.NewGuid().ToString();
string userId = "[email protected]";
var armToken = Guid.NewGuid().ToString();
var graphToken = Guid.NewGuid().ToString();
var kvToken = Guid.NewGuid().ToString();
var account = new AzureAccount
{
Id = userId,
Type = AzureAccount.AccountType.AccessToken
};
account.SetTenants(tenant);
account.SetAccessToken(armToken);
account.SetProperty(AzureAccount.Property.GraphAccessToken, graphToken);
account.SetProperty(AzureAccount.Property.KeyVaultAccessToken, kvToken);
var authFactory = new AuthenticationFactory();
var environment = AzureEnvironment.PublicEnvironments.Values.First();
var mockContext = new AzureContext()
{
Account = account
};
var credentials = authFactory.GetServiceClientCredentials(mockContext);
VerifyAccessTokenInServiceClientCredentials(credentials, armToken);
credentials = authFactory.GetServiceClientCredentials(mockContext, AzureEnvironment.Endpoint.Graph);
VerifyAccessTokenInServiceClientCredentials(credentials, graphToken);
credentials = authFactory.GetServiceClientCredentials(mockContext, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId);
VerifyAccessTokenInServiceClientCredentials(credentials, kvToken);
}

private void VerifyAccessTokenInServiceClientCredentials(Microsoft.Rest.ServiceClientCredentials cred, string expected)
{
using (var request = new HttpRequestMessage())
{
cred.ProcessHttpRequestAsync(request, new CancellationToken()).ConfigureAwait(false).GetAwaiter().GetResult();
Assert.Equal(expected, request.Headers.Authorization.Parameter);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,14 @@ private string GetFunctionsResourceId(string resourceIdOrEndpointName, IAzureEnv
private string GetEndpointToken(IAzureAccount account, string targetEndpoint)
{
string tokenKey = AzureAccount.Property.AccessToken;
if (targetEndpoint == AzureEnvironment.Endpoint.Graph)
if (string.Equals(targetEndpoint, AzureEnvironment.Endpoint.Graph, StringComparison.OrdinalIgnoreCase))
{
tokenKey = AzureAccount.Property.GraphAccessToken;
}

if (string.Equals(targetEndpoint, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId, StringComparison.OrdinalIgnoreCase))
{
tokenKey = AzureAccount.Property.KeyVaultAccessToken;
}
return account.GetProperty(tokenKey);
}

Expand Down