Skip to content

Commit

Permalink
[Messaging] Fix Functions test host credential use (#48389)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsquire authored Feb 22, 2025
1 parent af60779 commit 311d8f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,7 @@ await AssertCanSendReceiveMessage(host =>
configurationBuilder.AddInMemoryCollection(new Dictionary<string, string>()
{
{"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()}
})));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, string>()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -144,9 +144,6 @@ protected IHost BuildHost<TJobClass>(
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
{
Expand All @@ -156,6 +153,11 @@ protected IHost BuildHost<TJobClass>(
var hostBuilder = new HostBuilder()
.ConfigureServices(s =>
{
s.AddAzureClients(clientBuilder =>
{
clientBuilder.UseCredential(ServiceBusTestEnvironment.Instance.Credential);
});

s.Configure<HostOptions>(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).
Expand Down

0 comments on commit 311d8f1

Please sign in to comment.