Skip to content

Commit

Permalink
Expose options from IConfigurationRefresher
Browse files Browse the repository at this point in the history
  • Loading branch information
wdolek committed Jan 18, 2024
1 parent af9b9f5 commit e3ced9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
public interface IConfigurationRefresher
{
/// <summary>
/// Gets name of configuration provider, typically it's secret name.
/// Gets configuration provider options.
/// </summary>
string Name { get; }

SecretsManagerConfigurationProviderOptions Options { get; }

/// <summary>
/// Gets whether configuration source is optional.
/// </summary>
bool IsOptional { get; }

/// <summary>
/// Invoke refresh of configuration.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ internal sealed class SecretsManagerConfigurationProvider : ConfigurationProvide
{
private static readonly TimeSpan UnhandledExceptionDelay = TimeSpan.FromSeconds(5);

private readonly SecretsManagerConfigurationProviderOptions _options;
private readonly SecretFetcher _secretFetcher;
private readonly bool _isOptional;

private int _refreshInProgress;
private string? _currentSecretVersionId;

public SecretsManagerConfigurationProvider(SecretsManagerConfigurationSource source, bool isOptional)
{
_options = source.Options;
Options = source.Options;
IsOptional = isOptional;

_secretFetcher = new SecretFetcher(source.SecretsManager);
_isOptional = isOptional;
}

public string Name => _options.SecretName;

public SecretsManagerConfigurationProviderOptions Options { get; }
public bool IsOptional { get; }

public override void Load()
{
var watch = Stopwatch.StartNew();
try
{
var cts = new CancellationTokenSource(_options.Startup.Timeout);
var cts = new CancellationTokenSource(Options.Startup.Timeout);
LoadAsync(cts.Token).ConfigureAwait(false).GetAwaiter().GetResult();

// start watching for changes; if initial load fails, watcher is not started
_options.ConfigurationWatcher?.Start(this);
Options.ConfigurationWatcher?.Start(this);
}
catch (ArgumentException)
{
throw;
}
catch
{
if (_isOptional)
if (IsOptional)
{
return;
}
Expand All @@ -67,15 +67,15 @@ public async Task RefreshAsync(CancellationToken cancellationToken)

try
{
var secret = await _secretFetcher.GetSecret(_options.SecretName, _options.Version, cancellationToken).ConfigureAwait(false);
var secret = await _secretFetcher.GetSecret(Options.SecretName, Options.Version, cancellationToken).ConfigureAwait(false);
if (string.Equals(secret.VersionId, _currentSecretVersionId, StringComparison.Ordinal))
{
return;
}

SetData(
versionId: secret.VersionId,
data: _options.Processor.GetConfigurationData(_options, secret.Value));
data: Options.Processor.GetConfigurationData(Options, secret.Value));
}
finally
{
Expand All @@ -85,10 +85,10 @@ public async Task RefreshAsync(CancellationToken cancellationToken)

private async Task LoadAsync(CancellationToken cancellationToken)
{
var secret = await _secretFetcher.GetSecret(_options.SecretName, _options.Version, cancellationToken).ConfigureAwait(false);
var secret = await _secretFetcher.GetSecret(Options.SecretName, Options.Version, cancellationToken).ConfigureAwait(false);
SetData(
versionId: secret.VersionId,
data: _options.Processor.GetConfigurationData(_options, secret.Value));
data: Options.Processor.GetConfigurationData(Options, secret.Value));
}

private void SetData(string versionId, Dictionary<string, string?> data)
Expand Down

0 comments on commit e3ced9f

Please sign in to comment.