-
Notifications
You must be signed in to change notification settings - Fork 293
Add Entra ID (Azure Active Directory) authentication support #3054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
100 changes: 100 additions & 0 deletions
100
...cationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryConfigurationAadTests.cs
This file contains hidden or 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,100 @@ | ||
| namespace Microsoft.ApplicationInsights.Tests | ||
| { | ||
| using System; | ||
| using Azure.Core; | ||
| using Azure.Monitor.OpenTelemetry.Exporter; | ||
| using Microsoft.ApplicationInsights.Extensibility; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using Xunit; | ||
|
|
||
| /// <summary> | ||
| /// Tests for Azure Active Directory (AAD) authentication support in TelemetryConfiguration. | ||
| /// </summary> | ||
| public class TelemetryConfigurationAadTests : IDisposable | ||
| { | ||
| private TelemetryConfiguration telemetryConfiguration; | ||
|
|
||
| public TelemetryConfigurationAadTests() | ||
| { | ||
| this.telemetryConfiguration = new TelemetryConfiguration(); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this.telemetryConfiguration?.Dispose(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetAzureTokenCredential_WithValidCredential_SetsCredentialInExporterOptions() | ||
| { | ||
| // Arrange | ||
| var credential = new MockTokenCredential(); | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act | ||
| this.telemetryConfiguration.SetAzureTokenCredential(credential); | ||
|
|
||
| // Build the configuration to apply the builder actions | ||
| var client = new TelemetryClient(this.telemetryConfiguration); | ||
|
|
||
| // Assert - credential should be configured in the service provider | ||
| // We can't easily assert on the internal state, but we can verify no exceptions are thrown | ||
| Assert.NotNull(client); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetAzureTokenCredential_WithNullCredential_ThrowsArgumentNullException() | ||
| { | ||
| // Act & Assert | ||
| Assert.Throws<ArgumentNullException>(() => | ||
| this.telemetryConfiguration.SetAzureTokenCredential(null)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetAzureTokenCredential_AfterBuild_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange | ||
| var credential = new MockTokenCredential(); | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Build the configuration | ||
| var client = new TelemetryClient(this.telemetryConfiguration); | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Act & Assert | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| this.telemetryConfiguration.SetAzureTokenCredential(credential)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetAzureTokenCredential_CanBeCalledBeforeBuild_DoesNotThrow() | ||
| { | ||
| // Arrange | ||
| var credential = new MockTokenCredential(); | ||
| this.telemetryConfiguration.ConnectionString = "InstrumentationKey=test-ikey"; | ||
|
|
||
| // Act - should not throw | ||
| this.telemetryConfiguration.SetAzureTokenCredential(credential); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(this.telemetryConfiguration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Mock TokenCredential for testing purposes. | ||
| /// </summary> | ||
| private class MockTokenCredential : TokenCredential | ||
| { | ||
| public override AccessToken GetToken(TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) | ||
| { | ||
| return new AccessToken("mock-token", DateTimeOffset.UtcNow.AddHours(1)); | ||
| } | ||
|
|
||
| public override System.Threading.Tasks.ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) | ||
| { | ||
| return new System.Threading.Tasks.ValueTask<AccessToken>( | ||
| new AccessToken("mock-token", DateTimeOffset.UtcNow.AddHours(1))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
121 changes: 121 additions & 0 deletions
121
...ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsAadIntegrationTests.cs
This file contains hidden or 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,121 @@ | ||
| namespace Microsoft.ApplicationInsights.AspNetCore.Tests.Extensions | ||
| { | ||
| using System; | ||
| using System.Linq; | ||
| using Azure.Core; | ||
| using Azure.Monitor.OpenTelemetry.Exporter; | ||
| using Microsoft.ApplicationInsights.AspNetCore.Extensions; | ||
| using Microsoft.ApplicationInsights.Extensibility; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using Xunit; | ||
|
|
||
| /// <summary> | ||
| /// Integration tests for Azure Active Directory (AAD) authentication support in AspNetCore. | ||
| /// </summary> | ||
| public class ApplicationInsightsAadIntegrationTests | ||
| { | ||
| /// <summary> | ||
| /// Tests that credential flows from ApplicationInsightsServiceOptions to AzureMonitorExporterOptions. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AddApplicationInsightsTelemetry_WithCredential_FlowsCredentialToExporter() | ||
| { | ||
| // Arrange | ||
| var services = new ServiceCollection(); | ||
| var credential = new MockTokenCredential(); | ||
| var connectionString = "InstrumentationKey=test-ikey;IngestionEndpoint=https://test.endpoint.com/"; | ||
|
|
||
| // Act | ||
| services.AddApplicationInsightsTelemetry(options => | ||
| { | ||
| options.ConnectionString = connectionString; | ||
| options.Credential = credential; | ||
| }); | ||
|
|
||
| var serviceProvider = services.BuildServiceProvider(); | ||
|
|
||
| // Get the configured AzureMonitorExporterOptions | ||
| var exporterOptions = serviceProvider.GetService<IOptions<AzureMonitorExporterOptions>>(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(exporterOptions); | ||
| Assert.NotNull(exporterOptions.Value); | ||
| Assert.Same(credential, exporterOptions.Value.Credential); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that credential is null when not set. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AddApplicationInsightsTelemetry_WithoutCredential_HasNullCredentialInExporter() | ||
| { | ||
| // Arrange | ||
| var services = new ServiceCollection(); | ||
| var connectionString = "InstrumentationKey=test-ikey;IngestionEndpoint=https://test.endpoint.com/"; | ||
|
|
||
| // Act | ||
| services.AddApplicationInsightsTelemetry(options => | ||
| { | ||
| options.ConnectionString = connectionString; | ||
| // No credential set | ||
| }); | ||
|
|
||
| var serviceProvider = services.BuildServiceProvider(); | ||
|
|
||
| // Get the configured AzureMonitorExporterOptions | ||
| var exporterOptions = serviceProvider.GetService<IOptions<AzureMonitorExporterOptions>>(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(exporterOptions); | ||
| Assert.NotNull(exporterOptions.Value); | ||
| Assert.Null(exporterOptions.Value.Credential); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that TelemetryClient can be created with credential configured. | ||
| /// </summary> | ||
| [Fact] | ||
| public void TelemetryClient_WithCredential_CreatesSuccessfully() | ||
| { | ||
| // Arrange | ||
| var services = new ServiceCollection(); | ||
| var credential = new MockTokenCredential(); | ||
| var connectionString = "InstrumentationKey=test-ikey;IngestionEndpoint=https://test.endpoint.com/"; | ||
|
|
||
| services.AddApplicationInsightsTelemetry(options => | ||
| { | ||
| options.ConnectionString = connectionString; | ||
| options.Credential = credential; | ||
| }); | ||
|
|
||
| var serviceProvider = services.BuildServiceProvider(); | ||
|
|
||
| // Act | ||
| var telemetryClient = serviceProvider.GetService<TelemetryClient>(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(telemetryClient); | ||
| Assert.NotNull(telemetryClient.TelemetryConfiguration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Mock TokenCredential for testing purposes. | ||
| /// </summary> | ||
| private class MockTokenCredential : TokenCredential | ||
| { | ||
| public override AccessToken GetToken(TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) | ||
| { | ||
| return new AccessToken("mock-token", DateTimeOffset.UtcNow.AddHours(1)); | ||
| } | ||
|
|
||
| public override System.Threading.Tasks.ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) | ||
| { | ||
| return new System.Threading.Tasks.ValueTask<AccessToken>( | ||
| new AccessToken("mock-token", DateTimeOffset.UtcNow.AddHours(1))); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.