Skip to content

Instantly share code, notes, and snippets.

View danigian's full-sized avatar

Daniele Antonio Maggio danigian

View GitHub Profile
@danigian
danigian / ServiceBus.cs
Last active February 17, 2020 08:51
ServiceBus authentication with Managed Identities
static async Task ServiceBusTest()
{
// Managed identity needs "Azure Service Bus Data Owner" role assigned and scoped to ServiceBus
var tokenProvider = Microsoft.Azure.ServiceBus.Primitives.TokenProvider.CreateManagedIdentityTokenProvider();
TopicClient topicSendClient = new TopicClient($"sb://{sbNamespace}.servicebus.windows.net/", sbEntity, tokenProvider, Microsoft.Azure.ServiceBus.TransportType.Amqp, null);
await topicSendClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hello world from Managed Identity")));
await topicSendClient.CloseAsync();
Console.WriteLine($"Message sent to {sbNamespace}");
}
@danigian
danigian / Cosmos.cs
Last active February 20, 2020 09:13
CosmosDB authentication with Managed Identities
private static async Task<RestClient> GetRestClientAsync()
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false);
var restTokenCredentials = new Microsoft.Rest.TokenCredentials(accessToken);
var azCred = new AzureCredentials(restTokenCredentials, null, tenantId, AzureEnvironment.AzureGlobalCloud);
return RestClient.Configure().WithEnvironment(AzureEnvironment.AzureGlobalCloud).WithCredentials(azCred).Build();
}
static async Task CosmosDBTest()
@danigian
danigian / Storage.cs
Last active February 17, 2020 08:49
Storage authentication with Managed Identities
static async Task StorageTest()
{
#region Using management SDK to get the keys. (to be used, for instance, with Event Host Processor)
var storManageCredentials = new Microsoft.Azure.Management.Storage.StorageManagementClient(tokenCredentials);
storManageCredentials.SubscriptionId = subscriptionId;
var accountsas = await storManageCredentials.StorageAccounts.ListKeysWithHttpMessagesAsync(resourceGroupName, storageAccountName);
var storCred = new StorageCredentials(resourceGroupName, accountsas.Body.Keys.FirstOrDefault().Value);
Console.WriteLine($"Primary key for storage account: {accountsas.Body.Keys.FirstOrDefault().Value}");
#endregion
@danigian
danigian / EventHub.cs
Created February 14, 2020 09:10
Event Hub authentication with Managed Identities
//The managed identity needs the Azure Event Hub Data Owner/Sender/Receiver role (depending on what you need to achieve)
static async Task GetEventHubClient()
{
var ehClient = Microsoft.Azure.EventHubs.EventHubClient.CreateWithManagedIdentity(new Uri($"sb://{ehNamespace}.servicebus.windows.net"), entityPath);
await ehClient.SendAsync(new EventData(Encoding.UTF8.GetBytes("Hello from Managed Identity")));
}
@danigian
danigian / IoTHub.cs
Created February 14, 2020 09:00
IoT Hub Connection string retrieval with Managed Identities
static async Task GetIoTHubResource()
{
var azureServiceTokenProvider = new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider();
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/");
var tokenCredentials = new Microsoft.Rest.TokenCredentials(accessToken);
var iotHubClient = new Microsoft.Azure.Management.IotHub.IotHubClient(tokenCredentials);
iotHubClient.SubscriptionId = subscriptionId;
var iotHubDescription = await iotHubClient.IotHubResource.GetAsync(resourceGroupName,iotHubName);
@danigian
danigian / Get OpenFaaS on ACS.md
Last active September 26, 2017 16:11
Get OpenFaaS running on Azure Container Service with Kubernetes

Get OpenFaaS running on Azure Container Service with Kubernetes

Pre-requisites

Deploying ACS with Kubernetes