// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Dapr.Client;
using Microsoft.Extensions.Configuration;
using Dapr.Extensions.Configuration.DaprSecretStore;
using System.Linq;
using System.Threading;
namespace Dapr.Extensions.Configuration
{
///
/// Extension methods for registering with .
///
public static class DaprSecretStoreConfigurationExtensions
{
///
/// Adds an that reads configuration values from the Dapr Secret Store.
///
/// The to add to.
/// Dapr secret store name.
/// The secrets to retrieve.
/// The Dapr client
/// The .
public static IConfigurationBuilder AddDaprSecretStore(
this IConfigurationBuilder configurationBuilder,
string store,
IEnumerable secretDescriptors,
DaprClient client)
{
ArgumentVerifier.ThrowIfNullOrEmpty(store, nameof(store));
ArgumentVerifier.ThrowIfNull(secretDescriptors, nameof(secretDescriptors));
ArgumentVerifier.ThrowIfNull(client, nameof(client));
configurationBuilder.Add(new DaprSecretStoreConfigurationSource()
{
Store = store,
SecretDescriptors = secretDescriptors,
Client = client
});
return configurationBuilder;
}
///
/// Adds an that reads configuration values from the Dapr Secret Store.
///
/// The to add to.
/// Dapr secret store name.
/// The secrets to retrieve.
/// The Dapr client.
/// The used to configure the timeout waiting for Dapr.
/// The .
public static IConfigurationBuilder AddDaprSecretStore(
this IConfigurationBuilder configurationBuilder,
string store,
IEnumerable secretDescriptors,
DaprClient client,
TimeSpan sidecarWaitTimeout)
{
ArgumentVerifier.ThrowIfNullOrEmpty(store, nameof(store));
ArgumentVerifier.ThrowIfNull(secretDescriptors, nameof(secretDescriptors));
ArgumentVerifier.ThrowIfNull(client, nameof(client));
configurationBuilder.Add(new DaprSecretStoreConfigurationSource()
{
Store = store,
SecretDescriptors = secretDescriptors,
Client = client,
SidecarWaitTimeout = sidecarWaitTimeout
});
return configurationBuilder;
}
///
/// Adds an that reads configuration values from the Dapr Secret Store.
///
/// The to add to.
/// Dapr secret store name.
/// A collection of metadata key-value pairs that will be provided to the secret store. The valid metadata keys and values are determined by the type of secret store used.
/// The Dapr client
/// The .
public static IConfigurationBuilder AddDaprSecretStore(
this IConfigurationBuilder configurationBuilder,
string store,
DaprClient client,
IReadOnlyDictionary? metadata = null)
{
ArgumentVerifier.ThrowIfNullOrEmpty(store, nameof(store));
ArgumentVerifier.ThrowIfNull(client, nameof(client));
configurationBuilder.Add(new DaprSecretStoreConfigurationSource()
{
Store = store,
Metadata = metadata,
Client = client
});
return configurationBuilder;
}
///
/// Adds an that reads configuration values from the Dapr Secret Store.
///
/// The to add to.
/// Dapr secret store name.
/// A collection of metadata key-value pairs that will be provided to the secret store. The valid metadata keys and values are determined by the type of secret store used.
/// The Dapr client
/// The used to configure the timeout waiting for Dapr.
/// The .
public static IConfigurationBuilder AddDaprSecretStore(
this IConfigurationBuilder configurationBuilder,
string store,
DaprClient client,
TimeSpan sidecarWaitTimeout,
IReadOnlyDictionary? metadata = null)
{
ArgumentVerifier.ThrowIfNullOrEmpty(store, nameof(store));
ArgumentVerifier.ThrowIfNull(client, nameof(client));
configurationBuilder.Add(new DaprSecretStoreConfigurationSource()
{
Store = store,
Metadata = metadata,
Client = client,
SidecarWaitTimeout = sidecarWaitTimeout
});
return configurationBuilder;
}
///
/// Adds an that reads configuration values from the Dapr Secret Store.
///
/// The to add to.
/// Dapr secret store name.
/// A collection of delimiters that will be replaced by ':' in the key of every secret.
/// The Dapr client
/// The .
public static IConfigurationBuilder AddDaprSecretStore(
this IConfigurationBuilder configurationBuilder,
string store,
DaprClient client,
IEnumerable? keyDelimiters)
{
ArgumentVerifier.ThrowIfNullOrEmpty(store, nameof(store));
ArgumentVerifier.ThrowIfNull(client, nameof(client));
var source = new DaprSecretStoreConfigurationSource
{
Store = store,
Client = client
};
if (keyDelimiters != null)
{
source.KeyDelimiters = keyDelimiters.ToList();
}
configurationBuilder.Add(source);
return configurationBuilder;
}
///
/// Adds an that reads configuration values from the Dapr Secret Store.
///
/// The to add to.
/// Dapr secret store name.
/// A collection of delimiters that will be replaced by ':' in the key of every secret.
/// The Dapr client
/// The used to configure the timeout waiting for Dapr.
/// The .
public static IConfigurationBuilder AddDaprSecretStore(
this IConfigurationBuilder configurationBuilder,
string store,
DaprClient client,
IEnumerable? keyDelimiters,
TimeSpan sidecarWaitTimeout)
{
ArgumentVerifier.ThrowIfNullOrEmpty(store, nameof(store));
ArgumentVerifier.ThrowIfNull(client, nameof(client));
var source = new DaprSecretStoreConfigurationSource
{
Store = store,
Client = client,
SidecarWaitTimeout = sidecarWaitTimeout
};
if (keyDelimiters != null)
{
source.KeyDelimiters = keyDelimiters.ToList();
}
configurationBuilder.Add(source);
return configurationBuilder;
}
///
/// Adds an that reads configuration values from the command line.
///
/// The to add to.
/// Configures the source.
/// The .
public static IConfigurationBuilder AddDaprSecretStore(this IConfigurationBuilder configurationBuilder, Action configureSource)
=> configurationBuilder.Add(configureSource);
}
}