Skip to content

Commit 8b08e7d

Browse files
committed
Add secret repository metrics. Fixes #3942
1 parent fcdf517 commit 8b08e7d

File tree

7 files changed

+301
-173
lines changed

7 files changed

+301
-173
lines changed

src/WebJobs.Script.WebHost/Security/KeyManagement/DefaultSecretManagerProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Threading;
77
using Microsoft.Azure.WebJobs.Host.Executors;
8+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
89
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.Logging;
1011
using Microsoft.Extensions.Options;
@@ -15,14 +16,15 @@ public sealed class DefaultSecretManagerProvider : ISecretManagerProvider
1516
{
1617
private const string FileStorage = "Files";
1718
private readonly ILogger _logger;
19+
private readonly IMetricsLogger _metricsLogger;
1820
private readonly IOptionsMonitor<ScriptApplicationHostOptions> _options;
1921
private readonly IHostIdProvider _hostIdProvider;
2022
private readonly IConfiguration _configuration;
2123
private readonly IEnvironment _environment;
2224
private Lazy<ISecretManager> _secretManagerLazy;
2325

2426
public DefaultSecretManagerProvider(IOptionsMonitor<ScriptApplicationHostOptions> options, IHostIdProvider hostIdProvider,
25-
IConfiguration configuration, IEnvironment environment, ILoggerFactory loggerFactory)
27+
IConfiguration configuration, IEnvironment environment, ILoggerFactory loggerFactory, IMetricsLogger metricsLogger)
2628
{
2729
if (loggerFactory == null)
2830
{
@@ -35,6 +37,7 @@ public DefaultSecretManagerProvider(IOptionsMonitor<ScriptApplicationHostOptions
3537
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
3638

3739
_logger = loggerFactory.CreateLogger(ScriptConstants.LogCategoryHostGeneral);
40+
_metricsLogger = metricsLogger ?? throw new ArgumentNullException(nameof(metricsLogger));
3841
_secretManagerLazy = new Lazy<ISecretManager>(Create);
3942

4043
// When these options change (due to specialization), we need to reset the secret manager.
@@ -45,7 +48,7 @@ public DefaultSecretManagerProvider(IOptionsMonitor<ScriptApplicationHostOptions
4548

4649
private void ResetSecretManager() => Interlocked.Exchange(ref _secretManagerLazy, new Lazy<ISecretManager>(Create));
4750

48-
private ISecretManager Create() => new SecretManager(CreateSecretsRepository(), _logger);
51+
private ISecretManager Create() => new SecretManager(CreateSecretsRepository(), _logger, _metricsLogger);
4952

5053
internal ISecretsRepository CreateSecretsRepository()
5154
{

src/WebJobs.Script.WebHost/Security/KeyManagement/SecretManager.cs

Lines changed: 184 additions & 150 deletions
Large diffs are not rendered by default.

src/WebJobs.Script.WebHost/WebHostServiceCollectionExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Authorization;
77
using Microsoft.Azure.WebJobs.Extensions.Http;
88
using Microsoft.Azure.WebJobs.Script.Config;
9+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
910
using Microsoft.Azure.WebJobs.Script.Rpc;
1011
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
1112
using Microsoft.Azure.WebJobs.Script.WebHost.ContainerManagement;
@@ -102,6 +103,9 @@ public static void AddWebJobsScriptHost(this IServiceCollection services, IConfi
102103
services.AddTransient<VirtualFileSystem>();
103104
services.AddTransient<VirtualFileSystemMiddleware>();
104105

106+
// Logging and diagnostics
107+
services.AddSingleton<IMetricsLogger, WebHostMetricsLogger>();
108+
105109
// Secret management
106110
services.TryAddSingleton<ISecretManagerProvider, DefaultSecretManagerProvider>();
107111

src/WebJobs.Script/Diagnostics/MetricEventNames.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@ public static class MetricEventNames
2929
public const string FunctionUserLog = "function.userlog";
3030
public const string FunctionInvokeSucceeded = "function.invoke.succeeded";
3131
public const string FunctionInvokeFailed = "function.invoke.failed";
32+
33+
// secret managment events
34+
public const string SecretManagerDeleteSecret = "secretmanager.deletesecret.{0}";
35+
public const string SecretManagerGetFunctionSecrets = "secretmanager.getfunctionsecrets.{0}";
36+
public const string SecretManagerGetHostSecrets = "secretmanager.gethostsecrets.{0}";
37+
public const string SecretManagerAddOrUpdateFunctionSecret = "secretmanager.addorupdatefunctionsecret.{0}";
38+
public const string SecretManagerSetMasterKey = "secretmanager.setmasterkey.{0}";
39+
public const string SecretManagerPurgeOldSecrets = "secretmanager.purgeoldsecrets.{0}";
3240
}
3341
}

test/WebJobs.Script.Tests.Integration/Host/StandbyManager/StandbyManagerE2ETestBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.AspNetCore.Hosting;
1414
using Microsoft.AspNetCore.TestHost;
1515
using Microsoft.Azure.WebJobs.Host.Executors;
16+
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1617
using Microsoft.Azure.WebJobs.Script.WebHost;
1718
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
1819
using Microsoft.Extensions.DependencyInjection;
@@ -32,8 +33,9 @@ public class StandbyManagerE2ETestBase : IDisposable
3233
protected HttpClient _httpClient;
3334
protected TestServer _httpServer;
3435
protected readonly object _originalTimeZoneInfoCache = GetCachedTimeZoneInfo();
36+
protected TestMetricsLogger _metricsLogger;
3537

36-
public StandbyManagerE2ETestBase()
38+
public StandbyManagerE2ETestBase()
3739
{
3840
_testRootPath = Path.Combine(Path.GetTempPath(), "StandbyManagerTests");
3941
CleanupTestDirectory();
@@ -53,6 +55,7 @@ protected async Task InitializeTestHostAsync(string testDirName, IEnvironment en
5355
await TestHelpers.Await(() => File.Exists(proxyConfigPath));
5456

5557
_loggerProvider = new TestLoggerProvider();
58+
_metricsLogger = new TestMetricsLogger();
5659

5760
if (environment.IsAppServiceEnvironment())
5861
{
@@ -90,6 +93,7 @@ protected async Task InitializeTestHostAsync(string testDirName, IEnvironment en
9093

9194
c.AddSingleton<IEnvironment>(_ => environment);
9295
c.AddSingleton<IConfigureBuilder<ILoggingBuilder>>(new DelegatedConfigureBuilder<ILoggingBuilder>(b => b.AddProvider(_loggerProvider)));
96+
c.AddSingleton<IMetricsLogger>(_ => _metricsLogger);
9397
});
9498

9599
_httpServer = new TestServer(webHostBuilder);

test/WebJobs.Script.Tests/Security/SecretManagerProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public SecretManagerProviderTests()
3636
.ReturnsAsync("testhostid");
3737

3838
_provider = new DefaultSecretManagerProvider(optionsMonitor, mockIdProvider.Object, config,
39-
new TestEnvironment(), NullLoggerFactory.Instance);
39+
new TestEnvironment(), NullLoggerFactory.Instance, new TestMetricsLogger());
4040
}
4141

4242
[Fact]

0 commit comments

Comments
 (0)