diff --git a/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java b/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java index fe91c09f..b3b8b36e 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java @@ -914,7 +914,7 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I return true; } - if (!lease.hasLeaseId() && requestedSecret.getMode() == Mode.ROTATE) { + if (!lease.hasLeaseId() && !lease.getLeaseDuration().isZero() && requestedSecret.getMode() == Mode.ROTATE) { return true; } @@ -932,7 +932,8 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I return false; } - return lease.hasLeaseId() && !lease.isRenewable() && requestedSecret.getMode() == Mode.ROTATE; + return lease.hasLeaseId() && !lease.getLeaseDuration().isZero() && !lease.isRenewable() + && requestedSecret.getMode() == Mode.ROTATE; } } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTestConfiguration.java b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTestConfiguration.java index c9f90057..de7b9923 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTestConfiguration.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTestConfiguration.java @@ -32,14 +32,15 @@ import org.springframework.vault.core.env.LeaseAwareVaultPropertySource; * @author Steven Swor */ @Configuration +@VaultPropertySource(propertyNamePrefix = "generic.rotating.", value = "versioned/rotating", + renewal = VaultPropertySource.Renewal.ROTATE) public class RotatingGenericSecretsIntegrationTestConfiguration { /** * Utility class which will give our tests a reference to the * {@link LeaseAwareVaultPropertySource} which holds our secrets. */ - @VaultPropertySource(propertyNamePrefix = "generic.rotating.", value = "secret/rotating", - renewal = VaultPropertySource.Renewal.ROTATE) + public static class PropertySourceHolder implements InitializingBean { @Autowired @@ -60,7 +61,7 @@ public class RotatingGenericSecretsIntegrationTestConfiguration { Map leaseAwareVaultPropertySources = this.appContext .getBeansOfType(LeaseAwareVaultPropertySource.class); for (LeaseAwareVaultPropertySource candidate : leaseAwareVaultPropertySources.values()) { - if (candidate.getRequestedSecret().getPath().equals("secret/rotating")) { + if (candidate.getRequestedSecret().getPath().equals("versioned/rotating")) { this.propertySource = candidate; break; } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTests.java new file mode 100644 index 00000000..8327a05d --- /dev/null +++ b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTests.java @@ -0,0 +1,66 @@ +/* + * Copyright 2020 the original author or 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 + * + * https://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. + */ +package org.springframework.vault.core.lease; + +import java.util.Collections; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.vault.core.VaultIntegrationTestConfiguration; +import org.springframework.vault.core.VaultKeyValueOperations; +import org.springframework.vault.core.VaultKeyValueOperationsSupport; +import org.springframework.vault.util.IntegrationTestSupport; +import org.springframework.vault.util.PrepareVault; +import org.springframework.vault.util.VaultInitializer; + +import static org.assertj.core.api.Assertions.*; + +/** + * Integration tests for rotating generic secrets. + * + * @author Mark Paluch + */ +@ExtendWith(SpringExtension.class) +@SpringJUnitConfig( + classes = { VaultIntegrationTestConfiguration.class, RotatingGenericSecretsIntegrationTestConfiguration.class }) +public class RotatingGenericSecretsIntegrationTests extends IntegrationTestSupport { + + @BeforeAll + static void beforeAll() { + + VaultInitializer initializer = new VaultInitializer(); + + initializer.initialize(); + PrepareVault prepare = initializer.prepare(); + + VaultKeyValueOperations versioned = prepare.getVaultOperations().opsForKeyValue("versioned", + VaultKeyValueOperationsSupport.KeyValueBackend.KV_2); + + versioned.put("rotating", Collections.singletonMap("key", "value")); + } + + @Test + void name(@Autowired RotatingGenericSecretsIntegrationTestConfiguration.PropertySourceHolder holder) { + + assertThat(holder.propertySource.getProperty("generic.rotating.key")).isEqualTo("value"); + } + +}