From f492d328eed6f4b6c818894fcc9b87b8101fb6f3 Mon Sep 17 00:00:00 2001 From: Jesse Squire Date: Fri, 21 Feb 2025 10:07:45 -0800 Subject: [PATCH] [Messaging] Fix Functions test host credential use The focus of these changes is to fix the approach used for token credential management in the Function extensions end-to-end tests for Event Hubs and Service Bus. The previous approach used simple logic to set in-memory configuration with assumptions about the test environment. These assumptions are no longer accurate with the security changes in the testing pipelines. The new approach delegates to the test environment for credential management, as it has the conditional logic for environmental factors needed for credential selection and creation. The configuration-to-credential mapping and creation logic is part of the `Microsoft.Extensions.Azure` package and has full test coverage there; there is no benefit to duplicating test environment logic just to force Function configuration to control the credential locally for the test host. --- .../tests/EventHubEndToEndTests.cs | 8 +----- .../tests/WebJobsEventHubTestBase.cs | 9 ++++++- .../tests/WebJobsServiceBusTestBase.cs | 26 ++++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/EventHubEndToEndTests.cs b/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/EventHubEndToEndTests.cs index 2414e2d15397..c68b29833633 100644 --- a/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/EventHubEndToEndTests.cs +++ b/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/EventHubEndToEndTests.cs @@ -242,13 +242,7 @@ await AssertCanSendReceiveMessage(host => configurationBuilder.AddInMemoryCollection(new Dictionary() { {"TestConnection:fullyQualifiedNamespace", EventHubsTestEnvironment.Instance.FullyQualifiedNamespace}, - {"TestConnection:clientId", EventHubsTestEnvironment.Instance.ClientId}, - {"TestConnection:clientSecret", EventHubsTestEnvironment.Instance.ClientSecret}, - {"TestConnection:tenantId", EventHubsTestEnvironment.Instance.TenantId}, - {"AzureWebJobsStorage:serviceUri", GetServiceUri()}, - {"AzureWebJobsStorage:clientId", EventHubsTestEnvironment.Instance.ClientId}, - {"AzureWebJobsStorage:clientSecret", EventHubsTestEnvironment.Instance.ClientSecret}, - {"AzureWebJobsStorage:tenantId", EventHubsTestEnvironment.Instance.TenantId}, + {"AzureWebJobsStorage:serviceUri", GetServiceUri()} }))); } diff --git a/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/WebJobsEventHubTestBase.cs b/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/WebJobsEventHubTestBase.cs index 3a571edae9d2..122abb0daf8a 100644 --- a/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/WebJobsEventHubTestBase.cs +++ b/sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/WebJobsEventHubTestBase.cs @@ -4,9 +4,9 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Azure.Core.Diagnostics; using Azure.Messaging.EventHubs.Tests; using Microsoft.Azure.WebJobs.Host.TestCommon; +using Microsoft.Extensions.Azure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -64,6 +64,13 @@ protected void ConfigureTestEventHub(IHostBuilder builder) var hostBuilder = new HostBuilder(); hostBuilder + .ConfigureServices(services => + { + services.AddAzureClients(clientBuilder => + { + clientBuilder.UseCredential(EventHubsTestEnvironment.Instance.Credential); + }); + }) .ConfigureAppConfiguration(builder => { builder.AddInMemoryCollection(new Dictionary() diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs index d7e9c98bc39d..a2413a3aa669 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs @@ -1,25 +1,25 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.Serialization; -using System.Threading; -using System.Threading.Tasks; -using System.Xml; using Azure.Core.TestFramework; using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; using Azure.Messaging.ServiceBus.Tests; using Microsoft.Azure.WebJobs.Host.TestCommon; -using Microsoft.Azure.WebJobs.ServiceBus; +using Microsoft.Extensions.Azure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.Serialization; +using System.Threading; +using System.Threading.Tasks; +using System.Xml; using static Azure.Messaging.ServiceBus.Tests.ServiceBusScope; namespace Microsoft.Azure.WebJobs.Host.EndToEndTests @@ -144,9 +144,6 @@ protected IHost BuildHost( if (useTokenCredential) { settings.Add("AzureWebJobsServiceBus:fullyQualifiedNamespace", ServiceBusTestEnvironment.Instance.FullyQualifiedNamespace); - settings.Add("AzureWebJobsServiceBus:clientId", ServiceBusTestEnvironment.Instance.ClientId); - settings.Add("AzureWebJobsServiceBus:clientSecret", ServiceBusTestEnvironment.Instance.ClientSecret); - settings.Add("AzureWebJobsServiceBus:tenantId", ServiceBusTestEnvironment.Instance.TenantId); } else { @@ -156,6 +153,11 @@ protected IHost BuildHost( var hostBuilder = new HostBuilder() .ConfigureServices(s => { + s.AddAzureClients(clientBuilder => + { + clientBuilder.UseCredential(ServiceBusTestEnvironment.Instance.Credential); + }); + s.Configure(opts => opts.ShutdownTimeout = HostShutdownTimeout); // Configure ServiceBusEndToEndTestService before WebJobs stuff so that the ServiceBusEndToEndTestService.StopAsync will be called after // the WebJobsHost.StopAsync (service that is started first will be stopped last by the IHost).