Skip to content
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

Support WebJobs configuration fallback logic #14825

Merged
merged 3 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class Function1
[FunctionName("Function1")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[AzureClient("StorageConnection")] BlobServiceClient client)
[AzureClient("MyStorageConnection")] BlobServiceClient client)
{
return new OkObjectResult(client.GetBlobContainers().ToArray());
}
Expand All @@ -33,21 +33,29 @@ public static class Function1

The connection name should correspond to a configuration section with a connection string or a set of connection parameters that correspond to a client constructor.

For example to construct a BlobClient using a connection string use the following configuration:
For example to construct a `BlobServiceClient` using a connection string use the following configuration:

```json
{
"StorageConnection": "UseDevelopmentStorage=true"
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"MyStorageConnection": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
```

To construct a client using a `blobUri`:
To construct a client using a `serviceUri`:

```json
{
"StorageConnection": {
"blobUri": "https://{storage_account}.blob.core.windows.net/"
}
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"MyStorageConnection__serviceUri": "http://127.0.0.1:10000/devstoreaccount1/container/blob",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class Function1
[FunctionName("Function1")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[AzureClient("StorageConnection")] BlobServiceClient client)
[AzureClient("MyStorageConnection")] BlobServiceClient client)
{
return new OkObjectResult(client.GetBlobContainers().ToArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
"isEnabled": true
}
}
},
"StorageConnection": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"MyStorageConnection": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting. While this file is JSON it doesn't support nesting configuration as ASP.NET Core does. Filed #14826 to track

"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Extensions.Azure;
Expand All @@ -15,10 +16,12 @@ namespace Microsoft.Extensions.Hosting
internal class AzureClientsExtensionConfigProvider : IExtensionConfigProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly INameResolver _nameResolver;

public AzureClientsExtensionConfigProvider(IServiceProvider serviceProvider)
public AzureClientsExtensionConfigProvider(IServiceProvider serviceProvider, INameResolver nameResolver)
{
_serviceProvider = serviceProvider;
_nameResolver = nameResolver;
}

public void Initialize(ExtensionConfigContext context)
Expand All @@ -29,7 +32,10 @@ public void Initialize(ExtensionConfigContext context)

private IValueBinder CreateValueBinder(Type type, AzureClientAttribute attribute)
{
return (IValueBinder)Activator.CreateInstance(typeof(AzureClientValueProvider<>).MakeGenericType(type), _serviceProvider, attribute.Connection);
return (IValueBinder)Activator.CreateInstance(
typeof(AzureClientValueProvider<>).MakeGenericType(type),
_serviceProvider,
_nameResolver.ResolveWholeString(attribute.Connection));
}

private class AzureClientValueProvider<TClient> : IValueBinder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Licensed under the MIT License.

using System;
using Azure.Extensions.WebJobs;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Hosting
{
Expand All @@ -29,7 +30,7 @@ public static IWebJobsBuilder AddAzureClients(this IWebJobsBuilder builder)
}

builder.Services.AddAzureClients(builder =>
builder.UseConfiguration(provider => provider.GetRequiredService<IConfiguration>().GetWebJobsRootConfiguration()));
builder.UseConfiguration(provider => new WebJobsConfiguration(provider.GetRequiredService<IConfiguration>())));
builder.AddExtension<AzureClientsExtensionConfigProvider>();

return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: InternalsVisibleTo("Microsoft.Azure.WebJobs.Extensions.Clients.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// Wraps the <see cref="IConfiguration"/> instance and applies fallback rules similar to https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Host/Extensions/IConfigurationExtensions.cs.
/// </summary>
internal class WebJobsConfiguration : IConfiguration
{
private readonly IConfiguration _configuration;

public WebJobsConfiguration(IConfiguration configuration)
{
_configuration = configuration;
}

private const string DefaultConfigurationRootSectionName = "AzureWebJobs";

public string this[string key]
{
get => _configuration[key];
set => _configuration[key] = value;
}

public IEnumerable<IConfigurationSection> GetChildren() => _configuration.GetChildren();

public IChangeToken GetReloadToken() => _configuration.GetReloadToken();

public IConfigurationSection GetSection(string key)
{
var section = _configuration.GetSection(key);
if (section.Exists())
{
return section;
}

var prefixedKey = DefaultConfigurationRootSectionName + key;
section = _configuration.GetSection(prefixedKey);
if (section.Exists())
{
return section;
}

return _configuration.GetSection("ConnectionStrings").GetSection(key);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.TestFramework;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Azure.WebJobs.Host.TestCommon;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Azure.WebJobs.Tests;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace Microsoft.Azure.WebJobs.Extensions.Clients.Tests
{
public class AzureClientAttributeFunctionalTests : RecordedTestBase<WebJobsTestEnvironment>
{
public AzureClientAttributeFunctionalTests(bool isAsync) : base(isAsync)
{
Matcher = new RecordMatcher()
{
VolatileQueryParameters =
{
// Ignore KeyVault client API Version when matching
"api-version"
}
};
}

[RecordedTest]
public async Task CanInjectKeyVaultClient()
{
var host = new HostBuilder()
.ConfigureServices(services => services.AddAzureClients(builder => builder
.ConfigureDefaults(options => Recording.InstrumentClientOptions<ClientOptions>(options))
.UseCredential(TestEnvironment.Credential)))
.ConfigureAppConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
{
{ "AzureWebJobsConnection:vaultUri", TestEnvironment.KeyVaultUrl }
});
})
.ConfigureDefaultTestHost<FunctionWithAzureClient>(builder =>
{
builder.AddAzureClients();
}).Build();

var jobHost = host.GetJobHost<FunctionWithAzureClient>();
await jobHost.CallAsync(nameof(FunctionWithAzureClient.Run));
}

public class FunctionWithAzureClient
{
public async Task Run([AzureClient("Connection")] SecretClient keyClient)
{
await keyClient.SetSecretAsync("TestSecret", "Secret value");
}
}
}
}
Loading