示例#1
0
        public (Mock <IVaultApi>, IVaultHelper) GetMocks(SecretAuth auth = null)
        {
            auth = auth ?? new SecretAuth
            {
                LeaseDuration = 0
            };
            var mockClient        = new Mock <IVaultApi>(MockBehavior.Strict);
            var mockLoginStrategy = new Mock <ILoginStrategy>(MockBehavior.Strict);

            mockLoginStrategy.Setup(m => m.LoginAsync(It.IsAny <string>(), It.IsAny <ILogger>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new LoginResult()
            {
                Client     = mockClient.Object,
                SecretAuth = auth
            }));

            Config.LoginStrategies = new List <ILoginStrategy>()
            {
                mockLoginStrategy.Object
            };

            var sut = VaultHelper.CreateAsync(Config).Result;

            return(mockClient, sut);
        }
示例#2
0
        public async Task CanGetJwt()
        {
            var sut = await VaultHelper.CreateAsync(Config);

            var secret = await sut.GetLiveSecretAsync("JWT", SecretGetters.MakeJwtGetter("auth", TimeSpan.FromSeconds(1), "vandelay", "tester"));

            var firstValue = secret.Value;

            firstValue.Should().NotBeEmpty();

            await Task.Delay(TimeSpan.FromSeconds(1));

            var secondValue = secret.Value;

            secondValue.Should().NotBeEmpty();

            secondValue.Should().NotBe(firstValue, "the token should have been refreshed");

            secret.Dispose();

            await Task.Delay(TimeSpan.FromSeconds(1));

            Func <string> lateGet = () => secret.Value;

            lateGet.Should().Throw <Exception>("the secret value cannot be read after it is disposed");
        }
示例#3
0
        public async Task CanStartClient()
        {
            var sut = await VaultHelper.CreateAsync(Config);

            _testOutputHelper.WriteLine("Got result.");
            sut.Should().NotBeNull();
        }
示例#4
0
        public async Task CanGetMongoCreds()
        {
            var sut = await VaultHelper.CreateAsync(Config);

            var secret = await sut.GetLiveSecretAsync("MongoDB", SecretGetters.MakeSecretTemplateGetter("mongodb://(vault://database/creds/mongodb-admin?template={{.username}}:{{.password}})@mongodb:27017/go-between?readPreference=primary"));

            var firstValue = secret.Value;

            firstValue.Should().NotBeEmpty();
            firstValue.Should().MatchRegex(@"mongodb:\/\/[^:]+[^@]+@", "the username and password should be mapped in");
        }
示例#5
0
        public async Task CanReadAndWriteThroughClient()
        {
            var sut = await VaultHelper.CreateAsync(Config);

            var input = new Dictionary <string, object>
            {
                ["a"] = "A",
                ["b"] = "B"
            };

            var path = "secret/data/test-secret";

            await sut.WriteAsync <Dictionary <string, string> >(path, input);

            var readActual = await sut.ReadAsync <Dictionary <string, string> >(path);

            readActual.Data.Should().BeEquivalentTo(input);
        }
示例#6
0
        public async Task ClientRenewsOwnToken()
        {
            var helperWithOriginalToken = await VaultHelper.CreateAsync(Config);

            var secret = await helperWithOriginalToken.WriteAsync <NoData>("auth/token/create", new Dictionary <string, string>()
            {
                ["ttl"] = "1s",
                ["explicit_max_ttl"] = "1m",
                ["renewable"]        = "true"
            });

            Exception          ex           = null;
            Action <Exception> errorHandler = (e) => ex = e;

            var sut = await VaultHelper.CreateAsync(new VaultHelperConfig(Config.Address)
                                                    .UsingTokenLogin(secret.Auth.ClientToken)
                                                    .WithErrorHandler(errorHandler));

            await Task.Delay(TimeSpan.FromSeconds(1));

            ex.Should().BeNull("the renewal should not have errored");
        }