Skip to content

Commit

Permalink
Support custom\external ServiceClientCredentials (#195)
Browse files Browse the repository at this point in the history
* Avoid liskov substitution violation by guaranteeing only AzureCredentials are available from the RestClient

Additionally, facilitate consumers supplying their own ServiceClientCredentials via a new constructor on AzureCredentials.

* Removing redundant typecasts
  • Loading branch information
eric-winkler authored and Hovsep committed Apr 19, 2018
1 parent c2c7662 commit f5ecf87
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/ResourceManagement/Compute/ComputeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ComputeManager(RestClient restClient, string subscriptionId) :
{
storageManager = StorageManager.Authenticate(restClient, subscriptionId);
networkManager = NetworkManager.Authenticate(restClient, subscriptionId);
rbacManager = GraphRbacManager.Authenticate(restClient, ((AzureCredentials)(restClient.Credentials)).TenantId);
rbacManager = GraphRbacManager.Authenticate(restClient, restClient.Credentials.TenantId);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public CertificateCredentialImpl<T> WithDuration(TimeSpan duration)
AzureEnvironment environment = null;
if (restClient.Credentials is AzureCredentials)
{
environment = ((AzureCredentials)restClient.Credentials).Environment;
environment = restClient.Credentials.Environment;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceManagement/Graph.RBAC/GraphRBACManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public GraphRbacManager(RestClient restClient, string tenantId)
string graphEndpoint = AzureEnvironment.AzureGlobalCloud.GraphEndpoint;
if (restClient.Credentials is AzureCredentials)
{
graphEndpoint = ((AzureCredentials)restClient.Credentials).Environment.GraphEndpoint;
graphEndpoint = restClient.Credentials.Environment.GraphEndpoint;
}
inner = new GraphRbacManagementClient(new Uri(graphEndpoint),
restClient.Credentials,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public PasswordCredentialImpl<T> WithDuration(TimeSpan duration)
AzureEnvironment environment = null;
if (restClient.Credentials is AzureCredentials)
{
environment = ((AzureCredentials)restClient.Credentials).Environment;
environment = restClient.Credentials.Environment;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceManagement/Msi/MsiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private MsiManager(RestClient restClient, string subscriptionId) :
SubscriptionId = subscriptionId
})
{
this.graphRbacManager = Microsoft.Azure.Management.Graph.RBAC.Fluent.GraphRbacManager.Authenticate(restClient, ((AzureCredentials)(restClient.Credentials)).TenantId);
this.graphRbacManager = Microsoft.Azure.Management.Graph.RBAC.Fluent.GraphRbacManager.Authenticate(restClient, restClient.Credentials.TenantId);
}

#region MsiManager builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public AzureCredentials(MSILoginInformation msiLoginInformation, AzureEnvironmen
this.msiTokenProviderFactory = new MSITokenProviderFactory(msiLoginInformation);
}

public AzureCredentials(ServiceClientCredentials credentials, string tenantId, AzureEnvironment environment)
: this(tenantId, environment)
{
credentialsCache[new Uri(Environment.ManagementEndpoint)] = credentials;
}

private AzureCredentials(string tenantId, AzureEnvironment environment)
{
TenantId = tenantId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;

namespace Microsoft.Azure.Management.ResourceManager.Fluent.Core
{
Expand All @@ -37,7 +38,7 @@ public string BaseUri
get; private set;
}

public ServiceClientCredentials Credentials
public AzureCredentials Credentials
{
get; private set;
}
Expand Down Expand Up @@ -77,7 +78,7 @@ public static RestClientBuilder.IBlank Configure()
public class RestClientBuilder : RestClientBuilder.IBlank, RestClientBuilder.IBuildable
{
private string baseUri;
private ServiceClientCredentials credentials;
private AzureCredentials credentials;
private List<DelegatingHandler> handlers;
private RetryPolicy retryPolicy;
private HttpLoggingDelegatingHandler loggingDelegatingHandler;
Expand Down Expand Up @@ -155,7 +156,7 @@ public interface IBuildable : IWithEnvironment, IWithBaseUri

IBuildable WithLogLevel(HttpLoggingDelegatingHandler.Level level);

IBuildable WithCredentials(ServiceClientCredentials credentials);
IBuildable WithCredentials(AzureCredentials credentials);

RestClient Build();
}
Expand Down Expand Up @@ -214,7 +215,7 @@ public IBuildable WithLogLevel(HttpLoggingDelegatingHandler.Level level)
return this;
}

public IBuildable WithCredentials(ServiceClientCredentials credentials)
public IBuildable WithCredentials(AzureCredentials credentials)
{
this.credentials = credentials;
return this;
Expand Down

0 comments on commit f5ecf87

Please sign in to comment.