Skip to content

Commit

Permalink
Change .net to AzureIdentityCredentialAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jongio committed Jun 19, 2020
1 parent fb27aa2 commit d408ee1
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 174 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
*.factorypath
*.project
*.prefs
.vscode/*
.vscode/*
terraform.
.terraform
terraform.tfstate*
tf.plan
56 changes: 44 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@ The classes contained in this repo are only meant to be a temporary stopgap betw

## .NET

### DefaultAzureMgmtCredential.cs
### AzureIdentityCredentialAdapter.cs

The `DefaultAzureMgmtCredential` class allows you to use all the goodness of `Azure.Identity.DefaultAzureCredential` in the Azure Management libraries. You can use it in place of `ServiceClientCredential` when calling your Azure Management APIs. The Azure Management libraries will be updated to support Azure Identity and Azure Core in early 2020, so this should just be used a a stopgap between now and then.
The `AzureIdentityCredentialAdapter` class allows you to use all the goodness of `Azure.Identity.DefaultAzureCredential` in the Azure Management libraries. You can use it in place of `ServiceClientCredential` when calling your Azure Management APIs. The Azure Management libraries will be updated to support Azure Identity and Azure Core in early 2020, so this should just be used a a stopgap between now and then.

```cmd
dotnet add package Microsoft.Azure.Management.ApplicationInsights --version 0.2.0-preview
```

Use DefaultAzureMgmtCredential in place of ServiceClientCredential:
Use `AzureIdentityCredentialAdapter` in place of `ServiceClientCredential`:

```csharp
using JonGallant.Azure.Identity.Extensions;
using Microsoft.Azure.Management.ApplicationInsights.Management;

var appInsightsClient = new ApplicationInsightsManagementClient(new DefaultAzureMgmtCredential());
var appInsightsClient = new ApplicationInsightsManagementClient(new AzureIdentityCredentialAdapter());
```

### DefaultAzureFluentCredential.cs
### AzureIdentityFluentCredentialAdapter.cs

The `DefaultAzureFluentCredential` class allows you to use all the goodness of `Azure.Identity.DefaultAzureCredential` in the [Azure Management **Fluent** libraries](https://github.com/Azure/azure-libraries-for-net). You can use it in place of `AzureCredentials` when calling your Azure Management Fluent APIs.
The `AzureIdentityFluentCredentialAdapter` class allows you to use all the goodness of `Azure.Identity.DefaultAzureCredential` in the [Azure Management **Fluent** libraries](https://github.com/Azure/azure-libraries-for-net). You can use it in place of `AzureCredentials` when calling your Azure Management Fluent APIs.

```cmd
dotnet add package Microsoft.Azure.Management.Fluent --version 1.30.0
```

Use `DefaultAzureFluentCredential` in place of `AzureCredentials`:
Use `AzureIdentityFluentCredentialAdapter` in place of `AzureCredentials`:

```csharp
using JonGallant.Azure.Identity.Extensions;
using Microsoft.Azure.Management.ResourceManager.Fluent;

var creds = new DefaultAzureFluentCredential(tenantId, AzureEnvironment.AzureGlobalCloud);
var creds = new AzureIdentityFluentCredentialAdapter(tenantId, AzureEnvironment.AzureGlobalCloud);

var resourceGroup = Azure.Authenticate(creds)
.WithSubscription(subId)
Expand All @@ -58,9 +58,9 @@ var resourceGroup = Azure.Authenticate(creds)
.Create();
```

### DefaultAzureServiceBusCredential.cs
### AzureIdentityServiceBusCredentialAdapter.cs

The `DefaultAzureServiceBusCredential` class allows you to use all of the goodness of `Azure.Identity.DefaultAzureCredential` with the Service Bus SDKs. Service Bus will officially be supported by the new SDKs soon, this is a stopgap that enables you to use the same credential flow throughout your application.
The `AzureIdentityServiceBusCredentialAdapter` class allows you to use all of the goodness of `Azure.Identity.DefaultAzureCredential` with the Service Bus SDKs. Service Bus will officially be supported by the new SDKs soon, this is a stopgap that enables you to use the same credential flow throughout your application.

```cmd
dotnet add package Microsoft.Azure.ServiceBus --version 4.1.1
Expand All @@ -70,9 +70,14 @@ dotnet add package Microsoft.Azure.ServiceBus --version 4.1.1
using JonGallant.Azure.Identity.Extensions;
using Microsoft.Azure.ServiceBus;

var client = new TopicClient("sbendpoint", "entitypath", new DefaultAzureServiceBusCredential());
var client = new TopicClient("sbendpoint", "entitypath", new AzureIdentityServiceBusCredentialAdapter());
```

## Testing .NET

1. Setup test resources with "Test Setup" section below.
2. Open the .Tests project and run dotnet build.

## Java

### DefaultAzureServiceBusCredential.java
Expand Down Expand Up @@ -102,7 +107,7 @@ Azure azure = Azure.authenticate(new DefaultAzureCredentialAdapter(tenantId)).wi

Above code will provide an instance of `Azure` fluent type from which you can access all Azure Resource Managers.

#### Testing DefaultAzureCredentialAdapter
#### Testing AzureIdentityCredentialAdapter

This repository has a test class called `DefaultAzureCredentailAdapterTest` that tests creation of a storage account, listing all storage accounts in a resource group to validate successful creation, then deleting the account created earlier in this test and listing again to ensure successful deletion.

Expand Down Expand Up @@ -170,3 +175,30 @@ Once you have the `.env` file configured and the venv loaded, run the tests simp
More to come soon. Please file a GitHub issue with any questions/suggestions.


## Test Setup

1. Create a service principal with `az ad sp create-for-rbac`
2. Rename .env.tmp to .env and update the the following values from the SP

`AZURE_CLIENT_ID=appId`
`AZURE_CLIENT_SECRET=password`
`AZURE_TENANT_ID=tenantId`

3. Run `az account show` to get your subscription id and update the .env file with that.

`AZURE_SUBSCRIPTION_ID=`

4. Deploy the Service Bus resources with terraform files in iac/terraform

- Open variables.tf and change the basename value to something unique.
- Run the following commands:
- `terraform init`
- `terraform plan --out tf.plan`
- `terraform apply tf.plan`

5. Update AZURE_BASE_NAME in .env file to the base name you used for terraform deployment

- AZURE_BASE_NAME=azidexttest1


6. See each language "Test" section above for instructions on how to run the tests.
29 changes: 29 additions & 0 deletions iac/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
resource "azurerm_resource_group" "rg" {
name = "${var.basename}rg"
location = var.location
}

resource "azurerm_servicebus_namespace" "sbns" {
name = "${var.basename}sbns"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku = "Standard"
}

resource "azurerm_servicebus_topic" "sbtopic" {
name = "topic1"
resource_group_name = azurerm_resource_group.rg.name
namespace_name = azurerm_servicebus_namespace.sbns.name
}

resource "azurerm_servicebus_subscription" "sbsub" {
name = "sub1"
resource_group_name = azurerm_resource_group.rg.name
namespace_name = azurerm_servicebus_namespace.sbns.name
topic_name = azurerm_servicebus_topic.sbtopic.name
max_delivery_count = 1
}

output "sb_connection_string" {
value = azurerm_servicebus_namespace.sbns.default_primary_connection_string
}
24 changes: 24 additions & 0 deletions iac/terraform/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#Set the terraform required version
terraform {
required_version = ">= 0.12.6"
}

# Configure the Azure Provider
provider "azurerm" {
# It is recommended to pin to a given version of the Provider
version = "~>2"
features {}
}

provider "random" {
version = "~>2"
}

provider "null" {
version = "~> 2.1"
}

# Data

# Make client_id, tenant_id, subscription_id and object_id variables
data "azurerm_client_config" "current" {}
11 changes: 11 additions & 0 deletions iac/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "basename" {
type = string
description = "The base name for all resources"
default = "azidexttest1"
}

variable "location" {
type = string
description = "Azure region where to create resources."
default = "West US"
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
using DotNetEnv;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using System;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using DotNetEnv;
using JonGallant.Azure.Identity.Extensions;
using Xunit;
using System.Threading.Tasks;
using static DotNetEnv.Env;

namespace JonGallant.Azure.Identity.Extensions.Tests.Fluent
{
public class ResourceGroupTests
{
[Fact]
public void CreateAzCredsTest()
public async Task CheckIfResourceGroupExistsTest()
{
Env.Load("../../../.env");
Env.Load("../../../../../.env");

var creds = new AzureIdentityFluentCredentialAdapter(
Environment.GetEnvironmentVariable("AZURE_TENANT_ID"),
AzureEnvironment.AzureGlobalCloud);

var name = Guid.NewGuid().ToString("n").Substring(0, 8);

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).
var resourceGroupExists = await Microsoft.Azure.Management.Fluent.Azure.Authenticate(creds).
WithSubscription(Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID")).
ResourceGroups.Define(name).WithRegion(Environment.GetEnvironmentVariable("AZURE_REGION")).Create();
ResourceGroups.ContainAsync(name);

Assert.Equal(resourceGroup.Name, name);
Assert.False(resourceGroupExists);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@

<ItemGroup>
<PackageReference Include="DotNetEnv" Version="1.4.0" />
<PackageReference Include="Microsoft.Azure.Management.ApplicationInsights" Version="0.2.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.ApplicationInsights" Version="0.3.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Authorization" Version="2.11.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.CosmosDB" Version="1.0.1" />
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.30.0" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.0.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" Version="1.30.0" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="14.3.0" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="4.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.Azure.Management.CosmosDB" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.34.0" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.7.2-preview" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" Version="1.34.0" />
<PackageReference Include="Microsoft.Azure.Management.ServiceBus" Version="2.1.0" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="17.0.0" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="4.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0-preview-20200519-01" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using DotNetEnv;
using Microsoft.Azure.Management.ApplicationInsights.Management;
using Microsoft.Azure.Management.ApplicationInsights.Management.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using System;
using Xunit;

Expand All @@ -9,19 +11,27 @@ namespace JonGallant.Azure.Identity.Extensions.Tests.Mgmt
public class AppInsightsTests
{
[Fact]
public async void CreateAppInsightsTest()
public async void CreateAndDeleteAppInsightsTest()
{
Env.Load("../../../../../.env");

var client = new ApplicationInsightsManagementClient(new DefaultAzureMgmtCredential());
var baseName = Environment.GetEnvironmentVariable("AZURE_BASE_NAME");
var rgName = string.Format("{0}rg", baseName);

// App Insights
var client = new ApplicationInsightsManagementClient(new AzureIdentityCredentialAdapter());
client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");

var component = new ApplicationInsightsComponent(Environment.GetEnvironmentVariable("AZURE_REGION"), "web", "web");
var name = Environment.GetEnvironmentVariable("APPINSIGHTS_NAME") + Guid.NewGuid().ToString("n").Substring(0, 8);
var component = new ApplicationInsightsComponent("westus", "web", "web");
var aiName = "appinsightsname" + Guid.NewGuid().ToString("n").Substring(0, 8);

component = await client.Components.CreateOrUpdateAsync(Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP"), name, component);
component = await client.Components.CreateOrUpdateAsync(rgName, aiName, component);

Assert.NotNull(component.CreationDate);

await client.Components.DeleteAsync(rgName, aiName);


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public async void CheckCosmosNameExistsTest()
Env.Load("../../../../../.env");


var client = new CosmosDBManagementClient(new DefaultAzureMgmtCredential());
var client = new CosmosDBManagementClient(new AzureIdentityCredentialAdapter());
client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");

var name = Environment.GetEnvironmentVariable("COSMOSDB_NAME") + Guid.NewGuid().ToString("n").Substring(0, 8);
var name = "cosmos" + Guid.NewGuid().ToString("n").Substring(0, 8);

var results = await client.DatabaseAccounts.CheckNameExistsAsync(name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
using Microsoft.Azure.Management.Storage;
using System;
using Xunit;
using Azure.Identity;

namespace JonGallant.Azure.Identity.Extensions.Tests.Mgmt
{
public class InteractiveBrowserTests
{
[Fact]
public async void InteractiveBrowserTest()
[Fact(Skip = "Requires user interaction")]
public async void CheckIfStorageNameAvailableWithInteractiveBrowserTest()
{
// Pre-req: Storage account created.
Env.Load("../../../../../.env");
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", "");
Environment.SetEnvironmentVariable("AZURE_CLIENT_SECRET", "");
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", "");

var client = new StorageManagementClient(new DefaultAzureMgmtCredential(true));

var client = new StorageManagementClient(new AzureIdentityCredentialAdapter(new DefaultAzureCredential(true)));
client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");

var props = await client.StorageAccounts.GetPropertiesAsync(
Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP"),
Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT_NAME"));
var name = "azidext" + Guid.NewGuid().ToString("n").Substring(0, 8);

var nameAvailable = await client.StorageAccounts.CheckNameAvailabilityAsync(name);

Assert.Equal(props.Location, Environment.GetEnvironmentVariable("AZURE_REGION"));
Assert.True(nameAvailable.NameAvailable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ namespace JonGallant.Azure.Identity.Extensions.Tests.Mgmt
public class ResourceGroupTests
{
[Fact]
public async void CreateResourceGroupTest()
public async void CreateAndDeleteResourceGroupTest()
{
Env.Load("../../../../../.env");

var client = new ResourceManagementClient(new DefaultAzureMgmtCredential());
var client = new ResourceManagementClient(new AzureIdentityCredentialAdapter());
client.SubscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");

var name = Environment.GetEnvironmentVariable("AZURE_RESOURCE_GROUP") + Guid.NewGuid().ToString("n").Substring(0, 8);
var name = "azidextrg" + Guid.NewGuid().ToString("n").Substring(0, 8);

var rg = new ResourceGroup(location:Environment.GetEnvironmentVariable("AZURE_REGION"), name:name);
var rg = new ResourceGroup(location:"westus", name:name);

var result = await client.ResourceGroups.CreateOrUpdateAsync(name, rg);

Assert.Equal(result.Name, name);

await client.ResourceGroups.DeleteAsync(name);
}
}
}
Loading

0 comments on commit d408ee1

Please sign in to comment.