Exemplo n.º 1
0
    public async Task RaiseChangeEventAfterProviderSetToNull()
    {
        var testFileProvider = new TestFileProvider(
            new TestFile("Secret1", "SecretValue1"),
            new TestFile("Secret2", "SecretValue2"));
        var configurationSource = new KeyPerFileConfigurationSource
        {
            FileProvider = testFileProvider,
            Optional     = true,
        };
        var keyPerFileProvider = new KeyPerFileConfigurationProvider(configurationSource);
        var config             = new ConfigurationRoot(new[] { keyPerFileProvider });

        var changeToken          = config.GetReloadToken();
        var changeTaskCompletion = new TaskCompletionSource <object>();

        changeToken.RegisterChangeCallback(state =>
                                           ((TaskCompletionSource <object>)state).TrySetResult(null), changeTaskCompletion);

        configurationSource.FileProvider = null;
        config.Reload();

        await changeTaskCompletion.Task;

        Assert.Empty(config.AsEnumerable());
    }
Exemplo n.º 2
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;
            }));
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
        protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(
            TestSection testConfig)
        {
            var testFiles = new List <IFileInfo>();

            SectionToTestFiles(testFiles, "", testConfig);

            var provider = new KeyPerFileConfigurationProvider(
                new KeyPerFileConfigurationSource
            {
                Optional     = true,
                FileProvider = new TestFileProvider(testFiles.ToArray())
            });

            return(provider, () => { });
        }
        /// <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;
        }