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

fix: missing authority host param and resource management endpoint when init the client #1648

Merged
merged 10 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -243,9 +243,13 @@ private async Task OpenConnectionAsync()
private async Task<ResourceGraphClient> CreateClientAsync()
{
var azureEnvironment = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureEnvironment();
var azureAuthorityHost = _resourceDeclarationMonitor.CurrentValue.AzureLandscape.Cloud.GetAzureAuthorityHost();

var credentials = await AzureAuthenticationFactory.GetTokenCredentialsAsync(azureEnvironment.ManagementEndpoint, TenantId, _azureAuthenticationInfo);
var resourceGraphClient = new ResourceGraphClient(credentials);
var credentials = await AzureAuthenticationFactory.GetTokenCredentialsAsync(azureEnvironment.ManagementEndpoint, TenantId, _azureAuthenticationInfo, azureAuthorityHost);

var baseUri = new Uri(azureEnvironment.ResourceManagerEndpoint);

var resourceGraphClient = new ResourceGraphClient(baseUri ,credentials);
locmai marked this conversation as resolved.
Show resolved Hide resolved

var version = Promitor.Core.Version.Get();
var promitorUserAgent = UserAgent.Generate("Resource-Discovery", version);
Expand Down
18 changes: 18 additions & 0 deletions src/Promitor.Core/Extensions/AzureCloudExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Azure.Identity;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Promitor.Core.Serialization.Enum;

Expand Down Expand Up @@ -27,5 +28,22 @@ public static AzureEnvironment GetAzureEnvironment(this AzureCloud azureCloud)
throw new ArgumentOutOfRangeException(nameof(azureCloud), "No Azure environment is known for");
}
}

public static Uri GetAzureAuthorityHost(this AzureCloud azureCloud)
locmai marked this conversation as resolved.
Show resolved Hide resolved
{
switch (azureCloud)
{
case AzureCloud.Global:
return AzureAuthorityHosts.AzurePublicCloud;
case AzureCloud.China:
return AzureAuthorityHosts.AzureChina;
case AzureCloud.Germany:
return AzureAuthorityHosts.AzureGermany;
case AzureCloud.UsGov:
return AzureAuthorityHosts.AzureGovernment;
default:
throw new ArgumentOutOfRangeException(nameof(azureCloud), "No Azure environment is known for");
}
}
}
}
1 change: 1 addition & 0 deletions src/Promitor.Core/Promitor.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.3.0" />
<PackageReference Include="Guard.Net" Version="1.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.17.0" />
<PackageReference Include="Microsoft.Azure.Management.Monitor.Fluent" Version="1.37.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,26 @@ public static AzureAuthenticationInfo GetConfiguredAzureAuthentication(IConfigur
/// <summary>
/// Gets a valid token using a Service Principal or a Managed Identity
/// </summary>
public static async Task<TokenCredentials> GetTokenCredentialsAsync(string resource, string tenantId, AzureAuthenticationInfo authenticationInfo)
public static async Task<TokenCredentials> GetTokenCredentialsAsync(string resource, string tenantId, AzureAuthenticationInfo authenticationInfo, System.Uri azureAuthorityHost)
{
Guard.NotNullOrWhitespace(resource, nameof(resource));
Guard.NotNullOrWhitespace(tenantId, nameof(tenantId));
Guard.NotNull(authenticationInfo, nameof(authenticationInfo));

TokenCredential tokenCredential;

var tokenCredentialOptions = new TokenCredentialOptions { AuthorityHost = azureAuthorityHost };

switch (authenticationInfo.Mode)
{
case AuthenticationMode.ServicePrincipal:
tokenCredential = new ClientSecretCredential(tenantId, authenticationInfo.IdentityId, authenticationInfo.Secret);
tokenCredential = new ClientSecretCredential(tenantId, authenticationInfo.IdentityId, authenticationInfo.Secret, tokenCredentialOptions);
break;
case AuthenticationMode.UserAssignedManagedIdentity:
tokenCredential = new ManagedIdentityCredential(authenticationInfo.IdentityId);
tokenCredential = new ManagedIdentityCredential(authenticationInfo.IdentityId, tokenCredentialOptions);
break;
case AuthenticationMode.SystemAssignedManagedIdentity:
tokenCredential = new ManagedIdentityCredential();
tokenCredential = new ManagedIdentityCredential( "",tokenCredentialOptions);
locmai marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
tokenCredential = new DefaultAzureCredential();
Expand Down