Skip to content
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

Added creation of AutoRest ARM client in the ClientFactory #1291

Merged
merged 2 commits into from
Jul 25, 2015
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 @@ -36,6 +36,14 @@
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.5.207081303-alpha\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.1.1.1\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.Azure.1.0.18-preview\lib\net45\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Management.Storage">
<HintPath>$(LibraryNugetPackageFolder)\Microsoft.WindowsAzure.Management.Storage.5.0.0\lib\net40\Microsoft.WindowsAzure.Management.Storage.dll</HintPath>
</Reference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@ public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext
{
return new CertificateCloudCredentials(context.Subscription.Id.ToString(), Certificate);
}


public Rest.ServiceClientCredentials GetServiceClientCredentials(AzureContext context)
{
throw new System.NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,16 @@ public List<System.Net.Http.Headers.ProductInfoHeaderValue> UserAgents
throw new NotImplementedException();
}
}

public TClient CreateArmClient<TClient>(AzureContext context, AzureEnvironment.Endpoint endpoint) where TClient : Rest.ServiceClient<TClient>
{
var creds = AzureSession.AuthenticationFactory.GetServiceClientCredentials(context);
return CreateCustomArmClient<TClient>(creds, context.Environment.GetEndpointAsUri(endpoint));
}

public TClient CreateCustomArmClient<TClient>(params object[] parameters) where TClient : Rest.ServiceClient<TClient>
{
return ManagementClients.FirstOrDefault(o => o is TClient) as TClient;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Azure;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Rest;
using System;
using System.Security;

Expand Down Expand Up @@ -68,5 +69,11 @@ public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext
{
return new AccessTokenCredential(context.Subscription.Id, Token);
}


public ServiceClientCredentials GetServiceClientCredentials(AzureContext context)
{
return new TokenCredentials(Token.AccessToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.5.207081303-alpha" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
<package id="Microsoft.Rest.ClientRuntime" version="1.1.1" targetFramework="net45" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="1.0.18-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Storage" version="5.0.0" targetFramework="net45" />
<package id="Moq" version="4.2.1409.1722" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AdalConfiguration
// These constants define the default values to use for AD authentication
// against RDFE
//
private const string PowerShellClientId = "1950a258-227b-4e31-a9cf-717495945fc2";
public const string PowerShellClientId = "1950a258-227b-4e31-a9cf-717495945fc2";

public static readonly Uri PowerShellRedirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ public static SecureString GetKey(string appId, string tenantId)
}
return null;
}
catch
{
// we could be running in an environment that does not have credentials store
}
finally
{
if (pCredential != IntPtr.Zero)
{
CredStore.NativeMethods.CredFree(pCredential);
}
}

return null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.5.207081303-alpha\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.1.1.1\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.Azure.1.0.18-preview\lib\net45\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
using System.Linq;
using System.Security;
using Hyak.Common;
using Microsoft.Rest;
using Microsoft.Rest.Azure.Authentication;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace Microsoft.Azure.Common.Authentication.Factories
{
Expand All @@ -32,7 +35,12 @@ public AuthenticationFactory()

public ITokenProvider TokenProvider { get; set; }

public IAccessToken Authenticate(AzureAccount account, AzureEnvironment environment, string tenant, SecureString password, ShowDialog promptBehavior,
public IAccessToken Authenticate(
AzureAccount account,
AzureEnvironment environment,
string tenant,
SecureString password,
ShowDialog promptBehavior,
AzureEnvironment.Endpoint resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)
{
var configuration = GetAdalConfiguration(environment, tenant, resourceId);
Expand Down Expand Up @@ -109,5 +117,79 @@ private AdalConfiguration GetAdalConfiguration(AzureEnvironment environment, str
ValidateAuthority = !environment.OnPremise
};
}

public ServiceClientCredentials GetServiceClientCredentials(AzureContext context)
{
if (context.Subscription == null)
{
throw new ApplicationException(Resources.InvalidDefaultSubscription);
}

if (context.Account == null)
{
throw new ArgumentException(Resources.AccountNotFound);
}

if (context.Account.Type == AzureAccount.AccountType.Certificate)
{
throw new NotSupportedException(AzureAccount.AccountType.Certificate.ToString());
}

if (context.Account.Type == AzureAccount.AccountType.AccessToken)
{
return new TokenCredentials(context.Account.GetProperty(AzureAccount.Property.AccessToken));
}

var tenant = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
.Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants))
.FirstOrDefault();

if (tenant == null)
{
throw new ArgumentException(Resources.TenantNotFound);
}

try
{
TracingAdapter.Information(Resources.UPNAuthenticationTrace,
context.Account.Id, context.Environment.Name, tenant);

// TODO: When we will refactor the code, need to add tracing
/*TracingAdapter.Information(Resources.UPNAuthenticationTokenTrace,
token.LoginType, token.TenantId, token.UserId);*/

var env = new ActiveDirectoryEnvironment
{
AuthenticationEndpoint = context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ActiveDirectory),
TokenAudience = context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId),
ValidateAuthority = !context.Environment.OnPremise
};

if(context.Account.Type == AzureAccount.AccountType.User)
{
return new UserTokenCredentials(
AdalConfiguration.PowerShellClientId,
tenant,
AdalConfiguration.PowerShellRedirectUri,
env,
new PlatformParameters(PromptBehavior.Never, null),
AzureSession.TokenCache);
}
else if (context.Account.Type == AzureAccount.AccountType.ServicePrincipal)
{
return new Microsoft.Rest.Azure.Authentication.ApplicationTokenCredentials(
context.Account.Id,
tenant,
UserTokenProvider.ConvertToString(ServicePrincipalKeyStore.GetKey(context.Account.Id, tenant)),
env);
}
throw new NotSupportedException(context.Account.Type.ToString());
}
catch (Exception ex)
{
TracingAdapter.Information(Resources.AdalAuthException, ex.Message);
throw new ArgumentException(Resources.InvalidSubscriptionState, ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,50 @@ public ClientFactory()
UserAgents = new List<ProductInfoHeaderValue>();
}

public virtual TClient CreateArmClient<TClient>(AzureContext context, AzureEnvironment.Endpoint endpoint) where TClient : Microsoft.Rest.ServiceClient<TClient>
{
if (context == null)
{
throw new ApplicationException(Resources.InvalidDefaultSubscription);
}

var creds = AzureSession.AuthenticationFactory.GetServiceClientCredentials(context);
TClient client = CreateCustomArmClient<TClient>(context.Environment.GetEndpointAsUri(endpoint), creds, new DelegatingHandler[]{});

var subscriptionId = typeof(TClient).GetProperty("SubscriptionId");
if (subscriptionId != null && context.Subscription != null)
{
subscriptionId.SetValue(client, context.Subscription.Id.ToString());
}

return client;
}

public virtual TClient CreateCustomArmClient<TClient>(params object[] parameters) where TClient : Microsoft.Rest.ServiceClient<TClient>
{
List<Type> types = new List<Type>();
foreach (object obj in parameters)
{
types.Add(obj.GetType());
}

var constructor = typeof(TClient).GetConstructor(types.ToArray());

if (constructor == null)
{
throw new InvalidOperationException(string.Format(Resources.InvalidManagementClientType, typeof(TClient).Name));
}

TClient client = (TClient)constructor.Invoke(parameters);

foreach (ProductInfoHeaderValue userAgent in UserAgents)
{
client.UserAgent.Add(userAgent);
}

return client;
}

public virtual TClient CreateClient<TClient>(AzureContext context, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
{
if (context == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Rest;
using System.Security;

namespace Microsoft.Azure.Common.Authentication
Expand All @@ -33,5 +34,7 @@ IAccessToken Authenticate(AzureAccount account, AzureEnvironment environment, st
AzureEnvironment.Endpoint resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId);

SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context);

ServiceClientCredentials GetServiceClientCredentials(AzureContext context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace Microsoft.Azure.Common.Authentication
{
public interface IClientFactory
{
TClient CreateArmClient<TClient>(AzureContext context, AzureEnvironment.Endpoint endpoint) where TClient : Microsoft.Rest.ServiceClient<TClient>;

TClient CreateCustomArmClient<TClient>(params object[] parameters) where TClient : Microsoft.Rest.ServiceClient<TClient>;

TClient CreateClient<TClient>(AzureContext context, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>;

TClient CreateClient<TClient>(AzureProfile profile, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Microsoft.Azure.Common.Authentication
-->
<SdkNuGetPackage Include="Microsoft.Azure.Common.Authentication">
<PackageVersion>1.1.0-preview</PackageVersion>
<PackageVersion>1.1.1-preview</PackageVersion>
<Folder>$(MSBuildThisFileDirectory)</Folder>
</SdkNuGetPackage>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
[assembly: CLSCompliant(false)]
[assembly: Guid("4f3ab2e4-cc7a-43ac-bb15-f481fcf94d58")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]

#if CODESIGN
[assembly: InternalsVisibleTo("Common.Authentication.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
Expand Down
2 changes: 2 additions & 0 deletions src/Authentication/Common.Authentication/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.5.207081303-alpha" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
<package id="Microsoft.Rest.ClientRuntime" version="1.1.1" targetFramework="net45" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="1.0.18-preview" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>