示例#1
0
        /// <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;
            }));
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DockerSecretsSecretProvider"/> class.
        /// </summary>
        /// <param name="secretsDirectoryPath">The path inside the docker container where the secrets are located.</param>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="secretsDirectoryPath"/> is blank.</exception>
        public DockerSecretsSecretProvider(string secretsDirectoryPath)
        {
            Guard.NotNullOrWhitespace(secretsDirectoryPath, nameof(secretsDirectoryPath));

            if (!Path.IsPathRooted(secretsDirectoryPath))
            {
                throw new ArgumentException($"The {nameof(secretsDirectoryPath)} must be an absolute path", nameof(secretsDirectoryPath));
            }

            if (!Directory.Exists(secretsDirectoryPath))
            {
                throw new DirectoryNotFoundException($"The directory {secretsDirectoryPath} which is configured as secretsDirectoryPath does not exist.");
            }

            KeyPerFileConfigurationSource configuration = new KeyPerFileConfigurationSource
            {
                FileProvider = new PhysicalFileProvider(secretsDirectoryPath),
                Optional     = false
            };

            var provider = new KeyPerFileConfigurationProvider(configuration);

            provider.Load();

            _provider = provider;
        }
        public IConfigurationProvider Build(IConfigurationBuilder builder)
        {
            var source = new KeyPerFileConfigurationSource()
            {
                FileProvider = FileProvider,
                Optional     = true,
            };

            return(source.Build(builder));
        }
示例#4
0
    /// <summary>
    /// Initializes a new instance.
    /// </summary>
    /// <param name="source">The settings.</param>
    public KeyPerFileConfigurationProvider(KeyPerFileConfigurationSource source)
    {
        Source = source ?? throw new ArgumentNullException(nameof(source));

        if (Source.ReloadOnChange && Source.FileProvider != null)
        {
            _changeTokenRegistration = ChangeToken.OnChange(
                () => Source.FileProvider.Watch("*"),
                () =>
            {
                Thread.Sleep(Source.ReloadDelay);
                Load(reload: true);
            });
        }
    }
        /// <summary>
        /// Initializes a new instance of the <see cref="DockerSecretsSecretProvider"/> class.
        /// </summary>
        /// <param name="secretsDirectoryPath">The directory path inside the Docker container where the secrets are located.</param>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="secretsDirectoryPath"/> is blank or not an absolute path.</exception>
        /// <exception cref="DirectoryNotFoundException">Thrown when the <paramref name="secretsDirectoryPath"/> is not found on the system.</exception>
        public DockerSecretsSecretProvider(string secretsDirectoryPath)
        {
            Guard.NotNullOrWhitespace(secretsDirectoryPath, nameof(secretsDirectoryPath), "Requires a directory path inside the Docker container where the secrets are located");
            Guard.For(() => !Path.IsPathRooted(secretsDirectoryPath), 
                new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(secretsDirectoryPath)));

            if (!Directory.Exists(secretsDirectoryPath))
            {
                throw new DirectoryNotFoundException($"The directory {secretsDirectoryPath} which is configured as secretsDirectoryPath does not exist.");
            }

            var configuration = new KeyPerFileConfigurationSource
            {
                FileProvider = new PhysicalFileProvider(secretsDirectoryPath),
                Optional = false
            };

            var provider = new KeyPerFileConfigurationProvider(configuration);
            provider.Load();

            _provider = provider;
        }