Skip to content

Commit 783e18e

Browse files
authored
Automate Managed Identity manual tests - Azure Functions (Azure#38393)
1 parent 322b8c2 commit 783e18e

16 files changed

+256
-41
lines changed

sdk/identity/Azure.Identity/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "net",
44
"TagPrefix": "net/identity/Azure.Identity",
5-
"Tag": "net/identity/Azure.Identity_7f050cb3f3"
5+
"Tag": "net/identity/Azure.Identity_f0e02fe424"
66
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Azure.Storage.Blobs" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\Azure.Identity.csproj" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Azure.Identity;
2+
using Azure.Storage.Blobs;
3+
using Azure.Core;
4+
5+
namespace Integration.Identity.Common;
6+
7+
public static class ManagedIdentityTests
8+
{
9+
public static void AuthToStorage()
10+
{
11+
string resourceId = Environment.GetEnvironmentVariable("IDENTITY_WEBAPP_USER_DEFINED_IDENTITY")!;
12+
string account1 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_1")!;
13+
string account2 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_2")!;
14+
15+
var credential1 = new ManagedIdentityCredential();
16+
var credential2 = new ManagedIdentityCredential(new ResourceIdentifier(resourceId));
17+
var client1 = new BlobServiceClient(new Uri($"https://{account1}.blob.core.windows.net/"), credential1);
18+
var client2 = new BlobServiceClient(new Uri($"https://{account2}.blob.core.windows.net/"), credential2);
19+
client1.GetBlobContainers().ToList();
20+
client2.GetBlobContainers().ToList();
21+
}
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Azure.WebJobs;
6+
using Microsoft.Azure.WebJobs.Extensions.Http;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Logging;
9+
using Newtonsoft.Json;
10+
using Integration.Identity.Common;
11+
12+
namespace Integration.Identity.Func
13+
{
14+
public static class Function1
15+
{
16+
[FunctionName("Function1")]
17+
public static async Task<IActionResult> Run(
18+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
19+
ILogger log)
20+
{
21+
log.LogInformation("C# HTTP trigger function processed a request.");
22+
23+
try
24+
{
25+
ManagedIdentityTests.AuthToStorage();
26+
return new OkObjectResult("Successfully acquired a token from ManagedIdentityCredential");
27+
}
28+
catch (Exception ex)
29+
{
30+
return new BadRequestObjectResult(ex.ToString());
31+
}
32+
}
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
8+
</ItemGroup>
9+
<ItemGroup>
10+
<None Update="host.json">
11+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
12+
</None>
13+
<None Update="local.settings.json">
14+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
16+
</None>
17+
</ItemGroup>
18+
<ItemGroup>
19+
<ProjectReference Include="..\Integration.Identity.Common\Integration.Identity.Common.csproj" />
20+
</ItemGroup>
21+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"appInsights1": {
4+
"type": "appInsights"
5+
}
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"appInsights1": {
4+
"type": "appInsights.sdk"
5+
}
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "2.0",
3+
"logging": {
4+
"fileLoggingMode": "always",
5+
"logLevel": {
6+
"Function.MyFunction": "Information",
7+
"default": "None"
8+
}
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "",
5+
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
6+
}
7+
}

sdk/identity/Azure.Identity/integration/WebApp/Controllers/TestController.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Azure.Identity;
55
using Azure.Storage.Blobs;
66
using Microsoft.AspNetCore.Mvc;
7+
using Integration.Identity.Common;
78

89
namespace WebApp.Controllers
910
{
@@ -16,18 +17,9 @@ public class TestController : ControllerBase
1617
[HttpGet(Name = "GetTest")]
1718
public IActionResult Get()
1819
{
19-
string resourceId = Environment.GetEnvironmentVariable("IDENTITY_WEBAPP_USER_DEFINED_IDENTITY")!;
20-
string account1 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_1")!;
21-
string account2 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_2")!;
22-
23-
var credential1 = new ManagedIdentityCredential();
24-
var credential2 = new ManagedIdentityCredential(new ResourceIdentifier(resourceId));
25-
var client1 = new BlobServiceClient(new Uri($"https://{account1}.blob.core.windows.net/"), credential1);
26-
var client2 = new BlobServiceClient(new Uri($"https://{account2}.blob.core.windows.net/"), credential2);
2720
try
2821
{
29-
var results = client1.GetBlobContainers().ToList();
30-
results = client2.GetBlobContainers().ToList();
22+
ManagedIdentityTests.AuthToStorage();
3123
return Ok("Successfully acquired a token from ManagedIdentityCredential");
3224
}
3325
catch (Exception ex)

0 commit comments

Comments
 (0)