-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
net/JonGallant.Azure.Identity.Extensions.Tests/Fluent/ResourceGroupTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using DotNetEnv; | ||
using Microsoft.Azure.Management.ResourceManager.Fluent; | ||
using System; | ||
using Xunit; | ||
|
||
namespace JonGallant.Azure.Identity.Extensions.Tests.Fluent | ||
{ | ||
public class ResourceGroupTests | ||
{ | ||
[Fact] | ||
public void CreateAzCredsTest() | ||
{ | ||
Env.Load("../../../.env"); | ||
|
||
var creds = new DefaultAzureFluentCredential(Environment.GetEnvironmentVariable("AZURE_TENANT_ID"), AzureEnvironment.AzureGlobalCloud); | ||
|
||
var name = Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP") + Guid.NewGuid().ToString("n").Substring(0, 8); | ||
|
||
var resourceGroup = Microsoft.Azure.Management.Fluent.Azure.Authenticate(creds). | ||
WithSubscription(Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID")). | ||
ResourceGroups.Define(name).WithRegion(Environment.GetEnvironmentVariable("AZURE_REGION")).Create(); | ||
|
||
Assert.Equal(resourceGroup.Name, name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
net/JonGallant.Azure.Identity.Extensions.Tests/Mgmt/ResourceGroupTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using DotNetEnv; | ||
using Microsoft.Azure.Management.ResourceManager; | ||
using Microsoft.Azure.Management.ResourceManager.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace JonGallant.Azure.Identity.Extensions.Tests.Mgmt | ||
{ | ||
public class ResourceGroupTests | ||
{ | ||
[Fact] | ||
public void CreateResourceGroupTest() | ||
{ | ||
Env.Load("../../../.env"); | ||
|
||
var client = new ResourceManagementClient(new DefaultAzureMgmtCredential()); | ||
client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID"); | ||
|
||
var name = Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP") + Guid.NewGuid().ToString("n").Substring(0, 8); | ||
|
||
var rg = new ResourceGroup(location:Environment.GetEnvironmentVariable("AZURE_REGION"), name:name); | ||
|
||
var result = client.ResourceGroups.CreateOrUpdate(name, rg); | ||
|
||
Assert.Equal(result.Name, name); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
net/JonGallant.Azure.Identity.Extensions/DefaultAzureFluentCredential.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.Azure.Management.ResourceManager.Fluent; | ||
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication; | ||
using Microsoft.Rest; | ||
using Microsoft.Rest.Azure.Authentication; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace JonGallant.Azure.Identity.Extensions | ||
{ | ||
public class DefaultAzureFluentCredential : AzureCredentials | ||
{ | ||
private IDictionary<Uri, ServiceClientCredentials> credentialsCache; | ||
|
||
public DefaultAzureFluentCredential(string tenantId, AzureEnvironment environment) : base(default(DeviceCredentialInformation), tenantId, environment) | ||
{ | ||
credentialsCache = new ConcurrentDictionary<Uri, ServiceClientCredentials>(); | ||
} | ||
|
||
public async override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
|
||
// BEING COPY FROM FLUENT | ||
var adSettings = new ActiveDirectoryServiceSettings | ||
{ | ||
AuthenticationEndpoint = new Uri(Environment.AuthenticationEndpoint), | ||
TokenAudience = new Uri(Environment.ManagementEndpoint), | ||
ValidateAuthority = true | ||
}; | ||
|
||
string url = request.RequestUri.ToString(); | ||
if (url.StartsWith(Environment.GraphEndpoint, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
adSettings.TokenAudience = new Uri(Environment.GraphEndpoint); | ||
} | ||
|
||
string host = request.RequestUri.Host; | ||
if (host.EndsWith(Environment.KeyVaultSuffix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var resource = new Uri(Regex.Replace(Environment.KeyVaultSuffix, "^.", "https://")); | ||
if (credentialsCache.ContainsKey(new Uri(Regex.Replace(Environment.KeyVaultSuffix, "^.", "https://")))) | ||
{ | ||
adSettings.TokenAudience = resource; | ||
} | ||
else | ||
{ | ||
using (var r = new HttpRequestMessage(request.Method, url)) | ||
{ | ||
var response = await new HttpClient().SendAsync(r).ConfigureAwait(false); | ||
|
||
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized && response.Headers.WwwAuthenticate != null) | ||
{ | ||
var header = response.Headers.WwwAuthenticate.ElementAt(0).ToString(); | ||
var regex = new Regex("authorization=\"([^\"]+)\""); | ||
var match = regex.Match(header); | ||
adSettings.AuthenticationEndpoint = new Uri(match.Groups[1].Value); | ||
regex = new Regex("resource=\"([^\"]+)\""); | ||
match = regex.Match(header); | ||
adSettings.TokenAudience = new Uri(match.Groups[1].Value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// END COPY FROM FLUENT | ||
|
||
if (!credentialsCache.ContainsKey(adSettings.TokenAudience)) | ||
{ | ||
credentialsCache[adSettings.TokenAudience] = new DefaultAzureMgmtCredential(); | ||
} | ||
await credentialsCache[adSettings.TokenAudience].ProcessHttpRequestAsync(request, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters