Add configurable endpoints for renew and revoke of secret leases.

We now allow configuration which endpoints to use for renew/revocation
through configuring spring.cloud.vault.config.lifecycle.lease-endpoints=Legacy/SysLeases.

Vault 0.8 has introduced endpoints below sys/leases and deprecated the previous legacy approach.

Original pull request: gh-334.
This commit is contained in:
Mårten Svantesson
2019-05-14 12:15:16 +02:00
committed by Mark Paluch
parent df0c557666
commit 59091bb7c4
4 changed files with 31 additions and 9 deletions

View File

@@ -1308,6 +1308,7 @@ spring.cloud.vault:
enabled: true
min-renewal: 10s
expiry-threshold: 1m
lease-endpoints: Legacy
----
====
@@ -1315,5 +1316,6 @@ spring.cloud.vault:
* `enabled` controls whether leases associated with secrets are considered to be renewed and expired secrets are rotated. Enabled by default.
* `min-renewal` sets the duration that is at least required before renewing a lease. This setting prevents renewals from happening too often.
* `expiry-threshold` sets the expiry threshold. A lease is renewed the configured period of time before it expires.
* `lease-endpoints` sets the endpoints for renew and revoke. Legacy for vault versions before 0.8 and SysLeases for later.
See also: https://www.vaultproject.io/docs/concepts/lease.html[Vault Documentation: Lease, Renew, and Revoke]

View File

@@ -107,6 +107,10 @@ public class VaultBootstrapPropertySourceConfiguration implements InitializingBe
secretLeaseContainer.setExpiryThreshold(lifecycle.getExpiryThreshold());
}
if (lifecycle.getLeaseEndpoints() != null) {
secretLeaseContainer.setLeaseEndpoints(lifecycle.getLeaseEndpoints());
}
secretLeaseContainer.start();
return new LeasingVaultPropertySourceLocator(vaultProperties, configuration,

View File

@@ -29,6 +29,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.vault.core.lease.LeaseEndpoints;
/**
* @author Spencer Gibb
@@ -499,6 +500,17 @@ public class VaultProperties implements EnvironmentAware {
*/
private Duration expiryThreshold;
/**
* Set the {@link LeaseEndpoints} to delegate renewal/revocation calls to.
* {@link LeaseEndpoints} encapsulates differences between Vault versions that affect
* the location of renewal/revocation endpoints.
*
* Can be SysLeases for version 0.8 or above of vault or Legacy for older versions (the default)
*
* @since 2.3
*/
private LeaseEndpoints leaseEndpoints;
}
}

View File

@@ -16,20 +16,20 @@
package org.springframework.cloud.vault.config;
import java.time.Duration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.vault.core.VaultOperations;
import org.springframework.vault.core.lease.LeaseEndpoints;
import org.springframework.vault.core.lease.SecretLeaseContainer;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Unit tests for {@link VaultBootstrapPropertySourceConfiguration}.
@@ -48,15 +48,15 @@ public class VaultBootstrapPropertySourceConfigurationTests {
this.contextRunner.withUserConfiguration(MockConfiguration.class)
.withPropertyValues("spring.cloud.vault.generic.enabled=false",
"spring.cloud.vault.config.lifecycle.expiry-threshold=5m",
"spring.cloud.vault.config.lifecycle.min-renewal=6m")
"spring.cloud.vault.config.lifecycle.min-renewal=6m",
"spring.cloud.vault.config.lifecycle.lease-endpoints=SysLeases")
.run(context -> {
SecretLeaseContainer container = context
.getBean(SecretLeaseContainer.class);
assertThat(container.getExpiryThreshold())
.isEqualTo(Duration.ofMinutes(5));
assertThat(container.getMinRenewal())
.isEqualTo(Duration.ofMinutes(6));
verify(container).setExpiryThreshold(Duration.ofMinutes(5));
verify(container).setMinRenewal(Duration.ofMinutes(6));
verify(container).setLeaseEndpoints(LeaseEndpoints.SysLeases);
});
}
@@ -74,6 +74,10 @@ public class VaultBootstrapPropertySourceConfigurationTests {
mock(ThreadPoolTaskScheduler.class));
}
@Bean
SecretLeaseContainer secretLeaseContainer() {
return mock(SecretLeaseContainer.class);
}
}
}