Skip to content

Adding code to remove dependency with AzureKeyVaultServiceEndpointResourceId #2544

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 1 commit into from
Jul 2, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,23 @@ namespace Microsoft.Azure.Commands.KeyVault.Models
{
internal class DataServiceCredential
{
private readonly IAuthenticationFactory _authenticationFactory;
private readonly AzureContext _context;
private readonly AzureEnvironment.Endpoint _endpointName;

public DataServiceCredential(IAuthenticationFactory authFactory, AzureContext context, AzureEnvironment.Endpoint resourceIdEndpoint)
{
if (authFactory == null)
throw new ArgumentNullException("authFactory");
if (context == null)
throw new ArgumentNullException("context");

var bundle = GetToken(authFactory, context, resourceIdEndpoint);
this.token = bundle.Item1;
_authenticationFactory = authFactory;
_context = context;
_endpointName = resourceIdEndpoint;
this.TenantId = GetTenantId(context);
}

public string AccessToken
{
get
{
return token.AccessToken;
}
}

public string TenantId { get; set; }
public string TenantId { get; private set; }

/// <summary>
/// Authentication callback method required by KeyVaultClient
Expand All @@ -56,16 +53,29 @@ public Task<string> OnAuthentication(string authority, string resource, string s
{
// TODO: Add trace to log tokenType, resource, authority, scope etc
string tokenStr = string.Empty;
this.token.AuthorizeRequest((tokenType, tokenValue) =>

// overriding the cached resourceId value to resource returned from the server
if (!string.IsNullOrEmpty(resource))
{
_context.Environment.Endpoints[_endpointName] = resource;
}

var bundle = GetTokenInternal(this.TenantId, this._authenticationFactory, this._context, this._endpointName);
bundle.Item1.AuthorizeRequest((tokenType, tokenValue) =>
{
tokenStr = tokenValue;
});

return Task.FromResult<string>(tokenStr);
}

private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory, AzureContext context, AzureEnvironment.Endpoint resourceIdEndpoint)
public string GetToken()
{
return GetTokenInternal(this.TenantId, this._authenticationFactory, this._context, this._endpointName).Item1.AccessToken;
}

private static string GetTenantId(AzureContext context)
{
var tenantId = string.Empty;
if (context.Account == null)
throw new ArgumentException(KeyVaultProperties.Resources.ArmAccountNotFound);

Expand All @@ -74,14 +84,18 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
throw new ArgumentException(string.Format(KeyVaultProperties.Resources.UnsupportedAccountType, context.Account.Type));

if (context.Subscription != null && context.Account != null)
TenantId = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
tenantId = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
Copy link
Member

@markcowl markcowl Jul 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use context.tenant as the first option?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might, didn't wanted to change logic for tenant ID

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this will be integrated into release 1.2.4

.Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants))
.FirstOrDefault();

if (string.IsNullOrWhiteSpace(TenantId) && context.Tenant != null && context.Tenant.Id != Guid.Empty)
TenantId = context.Tenant.Id.ToString();
if (string.IsNullOrWhiteSpace(tenantId) && context.Tenant != null && context.Tenant.Id != Guid.Empty)
tenantId = context.Tenant.Id.ToString();
return tenantId;
}

if (string.IsNullOrWhiteSpace(TenantId))
private static Tuple<IAccessToken, string> GetTokenInternal(string tenantId, IAuthenticationFactory authFactory, AzureContext context, AzureEnvironment.Endpoint resourceIdEndpoint)
{
if (string.IsNullOrWhiteSpace(tenantId))
throw new ArgumentException(KeyVaultProperties.Resources.NoTenantInContext);

try
Expand All @@ -92,7 +106,7 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
tokenCache = new TokenCache(context.TokenCache);
}

var accesstoken = authFactory.Authenticate(context.Account, context.Environment, TenantId, null, ShowDialog.Never,
var accesstoken = authFactory.Authenticate(context.Account, context.Environment, tenantId, null, ShowDialog.Never,
tokenCache, resourceIdEndpoint);

if (context.TokenCache != null && context.TokenCache.Length > 0)
Expand All @@ -107,7 +121,5 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
throw new ArgumentException(KeyVaultProperties.Resources.InvalidSubscriptionState, ex);
}
}

private IAccessToken token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ActiveDirectoryClient ActiveDirectoryClient
_dataServiceCredential = new DataServiceCredential(AzureSession.AuthenticationFactory, DefaultProfile.Context, AzureEnvironment.Endpoint.Graph);
_activeDirectoryClient = new ActiveDirectoryClient(new Uri(string.Format("{0}/{1}",
DefaultProfile.Context.Environment.Endpoints[AzureEnvironment.Endpoint.Graph], _dataServiceCredential.TenantId)),
() => Task.FromResult(_dataServiceCredential.AccessToken));
() => Task.FromResult(_dataServiceCredential.GetToken()));
}
return this._activeDirectoryClient;
}
Expand Down