-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First iteration of a simple ManagedIdentity integration test (#33017)
* First iteration of a simple ManagedIdentity integration test
- Loading branch information
1 parent
a7a6b0b
commit 141bae7
Showing
18 changed files
with
362 additions
and
46 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
39 changes: 39 additions & 0 deletions
39
sdk/identity/Azure.Identity/integration/WebApp/Controllers/TestController.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,39 @@ | ||
using System; | ||
using System.Linq; | ||
using Azure.Core; | ||
using Azure.Identity; | ||
using Azure.Storage.Blobs; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace WebApp.Controllers | ||
{ | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class TestController : ControllerBase | ||
{ | ||
|
||
[HttpGet(Name = "GetTest")] | ||
public IActionResult Get() | ||
{ | ||
string resourceId = Environment.GetEnvironmentVariable("IDENTITY_WEBAPP_USER_DEFINED_IDENTITY")!; | ||
string account1 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_1")!; | ||
string account2 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_2")!; | ||
|
||
var credential1 = new ManagedIdentityCredential(); | ||
var credential2 = new ManagedIdentityCredential(new ResourceIdentifier(resourceId)); | ||
var client1 = new BlobServiceClient(new Uri($"https://{account1}.blob.core.windows.net/"), credential1); | ||
var client2 = new BlobServiceClient(new Uri($"https://{account2}.blob.core.windows.net/"), credential2); | ||
try | ||
{ | ||
var results = client1.GetBlobContainers().ToList(); | ||
results = client2.GetBlobContainers().ToList(); | ||
return Ok("Successfully acquired a token from ManagedIdentityCredential"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return BadRequest(ex.ToString()); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
sdk/identity/Azure.Identity/integration/WebApp/Integration.Identity.WebApp.csproj
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Storage.Blobs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Azure.Identity.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,21 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
8 changes: 8 additions & 0 deletions
8
sdk/identity/Azure.Identity/integration/WebApp/appsettings.Development.json
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
sdk/identity/Azure.Identity/integration/WebApp/appsettings.json
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
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,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<config> | ||
<!-- | ||
<add key="repositoryPath" value="./NugetInstall" /> | ||
<add key="globalPackagesFolder" value="./Nuget" /> | ||
--> | ||
<add key="repositoryPath" value="%AGENT_WORKFOLDER%/NugetInstall" /> | ||
<add key="globalPackagesFolder" value="%AGENT_WORKFOLDER%/Nuget" /> | ||
</config> | ||
<packageSources> | ||
<add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" /> | ||
</packageSources> | ||
</configuration> |
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
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
44 changes: 44 additions & 0 deletions
44
sdk/identity/Azure.Identity/tests/ManagedIdentityCredentialWebAppTests.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,44 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Core.Pipeline; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Identity.Tests | ||
{ | ||
public class ManagedIdentityCredentialWebAppTests : IdentityRecordedTestBase | ||
{ | ||
private HttpPipeline _pipeline; | ||
private Uri _testEndpoint; | ||
|
||
public ManagedIdentityCredentialWebAppTests(bool isAsync) : base(isAsync) | ||
{ | ||
} | ||
|
||
[SetUp] | ||
public void Setup() { | ||
var options = new TokenCredentialOptions(); | ||
_testEndpoint = new Uri($"https://{TestEnvironment.IdentityTestWebName}.azurewebsites.net/test"); | ||
_pipeline = HttpPipelineBuilder.Build(InstrumentClientOptions(options), Array.Empty<HttpPipelinePolicy>(), Array.Empty<HttpPipelinePolicy>(), new ResponseClassifier()); | ||
} | ||
|
||
[RecordedTest] | ||
[SyncOnly] | ||
// This test leverages the test app found in Azure.Identity\integration\WebApp | ||
// It validates that ManagedIdentityCredential can acquire a token in an actual Azure Web App environment | ||
public async Task CallTestWebApp() | ||
{ | ||
Request request = _pipeline.CreateRequest(); | ||
request.Uri.Reset(_testEndpoint); | ||
Response response = await _pipeline.SendRequestAsync(request, default); | ||
|
||
Assert.AreEqual((int)HttpStatusCode.OK, response.Status); | ||
Assert.AreEqual("Successfully acquired a token from ManagedIdentityCredential", response.Content.ToString(), response.Content.ToString()); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...re.Identity/tests/SessionRecords/ManagedIdentityCredentialWebAppTests/CallTestWebApp.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.