Merge pull request #268 from Y-Sindo/move

Move identity-based service endpoint parsing methods here
This commit is contained in:
yzt 2021-09-06 19:06:56 +08:00 коммит произвёл GitHub
Родитель 78eea3c12d c26741cc01
Коммит 7de04c1873
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 112 добавлений и 7 удалений

Просмотреть файл

@ -5,7 +5,7 @@
</PropertyGroup>
<PropertyGroup Label="Package Versions">
<MicroBuildCorePackageVersion>0.3.0</MicroBuildCorePackageVersion>
<MicrosoftAzureSignalRManagement>1.11.0-preview1-10835</MicrosoftAzureSignalRManagement>
<MicrosoftAzureSignalRManagement>1.11.0-preview1-10841</MicrosoftAzureSignalRManagement>
<MicrosoftAzureFunctionsExtensionsVersion>1.0.0</MicrosoftAzureFunctionsExtensionsVersion>
<MicrosoftNETTestSdkPackageVersion>15.8.0</MicrosoftNETTestSdkPackageVersion>
<MoqPackageVersion>4.9.0</MoqPackageVersion>
@ -20,6 +20,7 @@
<MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson3_1>3.0.0</MicrosoftAspNetCoreSignalRProtocolsNewtonsoftJson3_1>
<MicrosoftAspNetCoreSignalRClient>5.0.3</MicrosoftAspNetCoreSignalRClient>
<MicrosoftExtensionsConfiguration>5.0.0</MicrosoftExtensionsConfiguration>
<MicrosoftExtensionsAzureVersion>1.1.0</MicrosoftExtensionsAzureVersion>
<!--E2E test functions-->
<MicrosoftNETSdkFunctionsV2>1.0.30</MicrosoftNETSdkFunctionsV2>

Просмотреть файл

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Azure.Core;
using Microsoft.Azure.SignalR;
using Microsoft.Extensions.Azure;
@ -11,10 +10,6 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
/// <summary>
/// A helper class to parse <see cref="ServiceEndpoint"/> from configuration.
/// </summary>
/// <remarks>
/// Copyied from https://github.com/Azure/azure-signalr/blob/e5a69b20daf7f7aa2786ff59df61433c4050c6ab/src/Microsoft.Azure.SignalR.Common/Endpoints/IConfigurationExtension.cs.
/// Although we have friend access to that internal class, referencing internal methods are not recommended as changes of internal methods might break this package.
/// </remarks>
internal static class IConfigurationExtensions
{
public static IEnumerable<ServiceEndpoint> GetEndpoints(this IConfiguration config, AzureComponentFactory azureComponentFactory)

Просмотреть файл

@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="$(MicrosoftAzureFunctionsExtensionsVersion)" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="$(SystemIdentityModelTokensJwtVersion)" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="$(MicrosoftAspNetCoreSignalRPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="$(MicrosoftExtensionsAzureVersion)"/>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">

Просмотреть файл

@ -0,0 +1,106 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Linq;
using Azure.Identity;
using Microsoft.Azure.SignalR;
using Microsoft.Azure.SignalR.Tests.Common;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace SignalRServiceExtension.Tests.Config
{
public class IConfigurationExtensionsFacts
{
[Fact]
public void TestGetNamedEndpointFromIdentityWithNoUri()
{
var services = new ServiceCollection();
services.AddAzureClientsCore();
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>();
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
Assert.False(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out _));
}
[Fact]
public void TestGetNamedEndpointFromIdentityWithOnlyUri()
{
var services = new ServiceCollection();
services.AddAzureClientsCore();
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>();
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
var uri = "http://signalr.service.uri.com:441";
config["eastus:serviceUri"] = uri;
Assert.True(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out var endpoint));
Assert.Equal("eastus", endpoint.Name);
Assert.Equal(uri, endpoint.Endpoint);
Assert.IsType<DefaultAzureCredential>((endpoint.AccessKey as AadAccessKey).TokenCredential);
Assert.Equal(EndpointType.Primary, endpoint.EndpointType);
}
[Fact]
public void TestGetNamedEndpointFromIdentityWithAllEndpointField()
{
var services = new ServiceCollection();
services.AddAzureClientsCore();
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>();
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
var uri = "http://signalr.service.uri.com:441";
config["eastus:serviceUri"] = uri;
config["eastus:credential"] = "managedidentity";
config["eastus:type"] = "secondary";
Assert.True(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out var endpoint));
Assert.Equal("eastus", endpoint.Name);
Assert.Equal(uri, endpoint.Endpoint);
Assert.IsType<ManagedIdentityCredential>((endpoint.AccessKey as AadAccessKey).TokenCredential);
Assert.Equal(EndpointType.Secondary, endpoint.EndpointType);
}
[Fact]
public void TestGetNamedEndpointsFromConnectionString()
{
var connectionString = FakeEndpointUtils.GetFakeConnectionString(1).Single();
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
config["eastus"] = connectionString;
config["eastus:primary"] = connectionString;
config["eastus:secondary"] = connectionString;
var endpoints = config.GetSection("eastus").GetNamedEndpointsFromConnectionString().ToArray();
Assert.Equal(3, endpoints.Length);
Assert.All(endpoints, e => Assert.Equal("eastus", e.Name));
Assert.Equal(EndpointType.Primary, endpoints[0].EndpointType);
Assert.Equal(EndpointType.Primary, endpoints[1].EndpointType);
Assert.Equal(EndpointType.Secondary, endpoints[2].EndpointType);
}
[Fact]
public void TestGetEndpointsFromIdentityAndConnectionString()
{
var services = new ServiceCollection();
services.AddAzureClientsCore();
var factory = services.BuildServiceProvider().GetRequiredService<AzureComponentFactory>();
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
var serviceUri = "http://signalr.service.uri.com:441";
config["endpoints:eastus:serviceUri"] = serviceUri;
config["endpoints:westus:secondary"] = FakeEndpointUtils.GetFakeConnectionString(1).Single();
var endpoints = config.GetSection("endpoints").GetEndpoints(factory).ToArray();
Assert.Collection(endpoints, e =>
{
Assert.Equal("eastus", e.Name);
Assert.Equal(serviceUri, e.Endpoint);
}, e =>
{
Assert.Equal("westus", e.Name);
Assert.Equal(EndpointType.Secondary, e.EndpointType);
});
}
}
}

Просмотреть файл

@ -63,7 +63,8 @@ namespace SignalRServiceExtension.Tests
var host = builder
.ConfigureAppConfiguration(b => b.AddInMemoryCollection(
new Dictionary<string, string> {
{ "key", FakeEndpointUtils.GetFakeConnectionString(1).Single() },
{ "Azure:SignalR:Endpoints:A", FakeEndpointUtils.GetFakeConnectionString(1).Single() },
{ "Azure:SignalR:Endpoints:B", FakeEndpointUtils.GetFakeConnectionString(1).Single() },
{ Constants.ServiceTransportTypeName, ServiceTransportType.Persistent.ToString() }
}))
.ConfigureWebJobs(b => b.AddSignalR().Services.AddAzureClientsCore()).Build();

Просмотреть файл

@ -10,6 +10,7 @@
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" />
<PackageReference Include="xunit" Version="$(XunitPackageVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitRunnerVisualstudioPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="$(MicrosoftExtensionsAzureVersion)"/>
</ItemGroup>
<ItemGroup>