/// <summary> /// Adds Docker secrets (mounted as files in the Docker container) to the secret store. /// </summary> /// <param name="builder">The builder to add the Docker secrets provider to.</param> /// <param name="directoryPath">The path inside the container where the Docker secrets are located.</param> /// <param name="name">The unique name to register this HashiCorp provider in the secret store.</param> /// <param name="mutateSecretName">The optional function to mutate the secret name before looking it up.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Throw when the <paramref name="directoryPath"/> is blank or is not an absolute path.</exception> /// <exception cref="DirectoryNotFoundException">Thrown when the <paramref name="directoryPath"/> is not found on the system.</exception> public static SecretStoreBuilder AddDockerSecrets( this SecretStoreBuilder builder, string directoryPath, string name, Func <string, string> mutateSecretName) { Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the Docker secrets to"); Guard.NotNullOrWhitespace(directoryPath, nameof(directoryPath), "Requires a non-blank directory path inside the Docker container to locate the secrets"); Guard.For(() => !Path.IsPathRooted(directoryPath), new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(directoryPath))); if (!Directory.Exists(directoryPath)) { throw new DirectoryNotFoundException($"The directory {directoryPath} which is configured as secretsDirectoryPath does not exist."); } var configuration = new KeyPerFileConfigurationSource { FileProvider = new PhysicalFileProvider(directoryPath), Optional = false }; var provider = new KeyPerFileConfigurationProvider(configuration); provider.Load(); return(builder.AddProvider(new DockerSecretsSecretProvider(directoryPath), options => { options.Name = name; options.MutateSecretName = mutateSecretName; })); }
/// <summary> /// Adds Docker secrets (mounted as files in the Docker container) to the secret store. /// </summary> /// <param name="builder">The builder to add the Docker secrets provider to.</param> /// <param name="directoryPath">The path inside the container where the Docker secrets are located.</param> /// <param name="mutateSecretName">The optional function to mutate the secret name before looking it up.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> is <c>null</c></exception> /// <exception cref="ArgumentException">Throw when the <paramref name="directoryPath"/> is blank</exception> public static SecretStoreBuilder AddDockerSecrets(this SecretStoreBuilder builder, string directoryPath, Func <string, string> mutateSecretName = null) { Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the Docker secrets to"); Guard.NotNullOrWhitespace(directoryPath, nameof(directoryPath), "Requires a non-blank directory path to locate the Docker secrets"); return(builder.AddProvider(new DockerSecretsSecretProvider(directoryPath), mutateSecretName)); }
/// <summary> /// Adds a secret source to the secret store of the application that gets its secrets from the <see cref="IConfiguration"/>. /// </summary> /// <param name="builder">The builder to create the secret store.</param> /// <param name="configuration">The configuration of the application, containing secrets.</param> public static SecretStoreBuilder AddConfiguration( this SecretStoreBuilder builder, IConfiguration configuration) { Guard.NotNull(builder, nameof(builder)); return(builder.AddProvider(new ConfigurationSecretProvider(configuration))); }
/// <summary> /// Adds a secret source to the secret store of the application that gets its secrets from the environment. /// </summary> /// <param name="builder">The builder to create the secret store.</param> /// <param name="target">The target on which the environment variables should be retrieved.</param> public static SecretStoreBuilder AddEnvironmentVariables( this SecretStoreBuilder builder, EnvironmentVariableTarget target = EnvironmentVariableSecretProvider.DefaultTarget) { Guard.NotNull(builder, nameof(builder)); return(builder.AddProvider(new EnvironmentVariableSecretProvider(target))); }
/// <summary> /// Adds a secret source to the secret store of the application that gets its secrets from the <see cref="IConfiguration"/>. /// </summary> /// <param name="builder">The builder to create the secret store.</param> /// <param name="configuration">The configuration of the application, containing secrets.</param> /// <param name="mutateSecretName">The function to mutate the secret name before looking it up.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> is <c>null</c>.</exception> public static SecretStoreBuilder AddConfiguration( this SecretStoreBuilder builder, IConfiguration configuration, Func <string, string> mutateSecretName = null) { Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the configuration secrets"); return(builder.AddProvider(new ConfigurationSecretProvider(configuration), mutateSecretName)); }
/// <summary> /// Adds Azure Key Vault as a secret source. /// </summary> /// <param name="builder">The builder to create the secret store.</param> /// <param name="authentication">The requested authentication type for connecting to the Azure Key Vault instance.</param> /// <param name="configuration">The configuration related to the Azure Key Vault instance to use.</param> /// <param name="cacheConfiguration">The configuration to control how the caching will be done.</param> public static SecretStoreBuilder AddAzureKeyVault( this SecretStoreBuilder builder, IKeyVaultAuthentication authentication, IKeyVaultConfiguration configuration, ICacheConfiguration cacheConfiguration) { Guard.NotNull(builder, nameof(builder)); Guard.NotNull(authentication, nameof(authentication)); Guard.NotNull(configuration, nameof(configuration)); var keyVaultSecretProvider = new KeyVaultSecretProvider(authentication, configuration); if (cacheConfiguration != null) { var cachedSecretProvider = new CachedSecretProvider(keyVaultSecretProvider, cacheConfiguration); return(builder.AddProvider(cachedSecretProvider)); } return(builder.AddProvider(keyVaultSecretProvider)); }
private static SecretStoreBuilder AddUserSecrets(SecretStoreBuilder builder, string userSecretsId, Action <SecretProviderOptions> configureOptions) { string directoryPath = GetUserSecretsDirectoryPath(userSecretsId); JsonConfigurationSource source = CreateJsonFileSource(directoryPath); var provider = new JsonConfigurationProvider(source); provider.Load(); return(builder.AddProvider(new UserSecretsSecretProvider(provider), configureOptions)); }
/// <summary> /// Adds a secret source to the secret store of the application that gets its secrets from the environment. /// </summary> /// <param name="builder">The builder to create the secret store.</param> /// <param name="target">The target on which the environment variables should be retrieved.</param> /// <param name="prefix">The optional prefix which will be prepended to the secret name when retrieving environment variables.</param> /// <param name="mutateSecretName">The optional function to mutate the secret name before looking it up.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="target"/> is outside the bounds of the enumeration.</exception> public static SecretStoreBuilder AddEnvironmentVariables( this SecretStoreBuilder builder, EnvironmentVariableTarget target = EnvironmentVariableSecretProvider.DefaultTarget, string prefix = null, Func <string, string> mutateSecretName = null) { Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the environment secrets"); Guard.For <ArgumentException>(() => !Enum.IsDefined(typeof(EnvironmentVariableTarget), target), $"Requires an environment variable target of either '{EnvironmentVariableTarget.Process}', '{EnvironmentVariableTarget.Machine}', or '{EnvironmentVariableTarget.User}'"); return(builder.AddProvider(new EnvironmentVariableSecretProvider(target, prefix), mutateSecretName)); }
/// <summary> /// Adds a secret source to the secret store of the application that gets its secrets from the <see cref="IConfiguration"/>. /// </summary> /// <param name="builder">The builder to create the secret store.</param> /// <param name="configuration">The configuration of the application, containing secrets.</param> /// <param name="name">The unique name to register this Configuration provider in the secret store.</param> /// <param name="mutateSecretName">The optional function to mutate the secret name before looking it up.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> is <c>null</c>.</exception> public static SecretStoreBuilder AddConfiguration( this SecretStoreBuilder builder, IConfiguration configuration, string name, Func <string, string> mutateSecretName) { Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the configuration secrets"); Guard.NotNull(configuration, nameof(configuration), "Requires a configuration instance to retrieve the secrets from"); return(builder.AddProvider(new ConfigurationSecretProvider(configuration), options => { options.Name = name; options.MutateSecretName = mutateSecretName; })); }
/// <summary> /// Adds command line arguments as secrets to the secret store. /// </summary> /// <param name="builder">The secret store to add the command line arguments to.</param> /// <param name="arguments">The command line arguments that will be considered secrets.</param> /// <param name="name">The unique name to register this provider in the secret store.</param> /// <param name="mutateSecretName">The function to mutate the secret name before looking it up.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception> public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, string name, Func <string, string> mutateSecretName) { Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store"); Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store"); var configProvider = new CommandLineConfigurationProvider(arguments); configProvider.Load(); var secretProvider = new CommandLineSecretProvider(configProvider); return(builder.AddProvider(secretProvider, options => { options.Name = name; options.MutateSecretName = mutateSecretName; })); }