From ba135817fad7cb68588b58554d58d9a9df7be647 Mon Sep 17 00:00:00 2001 From: Pierre-Jean Vardanega Date: Tue, 28 Mar 2017 12:45:20 +0200 Subject: [PATCH] Allow to create RequestedSecret from mode and path. Original pull request: #64. --- .../core/lease/domain/RequestedSecret.java | 25 ++++++++++++++++++- .../lease/domain/RequestedSecretTest.java | 24 ++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 spring-vault-core/src/test/java/org/springframework/vault/core/lease/domain/RequestedSecretTest.java diff --git a/spring-vault-core/src/main/java/org/springframework/vault/core/lease/domain/RequestedSecret.java b/spring-vault-core/src/main/java/org/springframework/vault/core/lease/domain/RequestedSecret.java index 56618efc..c17993fd 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/core/lease/domain/RequestedSecret.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/core/lease/domain/RequestedSecret.java @@ -17,6 +17,9 @@ package org.springframework.vault.core.lease.domain; import org.springframework.util.Assert; +import static org.springframework.vault.core.lease.domain.RequestedSecret.Mode.RENEW; +import static org.springframework.vault.core.lease.domain.RequestedSecret.Mode.ROTATE; + /** * Represents a requested secret from a specific Vault path associated with a lease * {@link Mode}. @@ -50,7 +53,7 @@ public class RequestedSecret { * @return the renewable {@link RequestedSecret}. */ public static RequestedSecret renewable(String path) { - return new RequestedSecret(path, Mode.RENEW); + return new RequestedSecret(path, RENEW); } /** @@ -65,6 +68,26 @@ public class RequestedSecret { return new RequestedSecret(path, Mode.ROTATE); } + /** + * Create a rotating or renewable {@link RequestedSecret} at {@code path}. A lease associated with + * this secret will be renewed if the lease is qualified for renewal. Once the lease + * expires, a new secret with a new lease is obtained if mode is ROTATE, otherwize the lease is no + * longer valid after expiry. + * + * @param mode must not be {@literal null} + * @param path must not be {@literal null} or empty, must not start with a slash. + * @return the rotating {@link RequestedSecret}. + */ + public static RequestedSecret from(Mode mode, String path) { + Assert.notNull(mode, "Mode cannot be null"); + + if (mode == ROTATE) { + return rotating(path); + } else { + return renewable(path); + } + } + /** * @return the Vault path of the requested secret. */ diff --git a/spring-vault-core/src/test/java/org/springframework/vault/core/lease/domain/RequestedSecretTest.java b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/domain/RequestedSecretTest.java new file mode 100644 index 00000000..c4d5c392 --- /dev/null +++ b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/domain/RequestedSecretTest.java @@ -0,0 +1,24 @@ +package org.springframework.vault.core.lease.domain; + +import org.junit.Test; +import org.springframework.vault.core.lease.domain.RequestedSecret.Mode; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RequestedSecretTest { + + @Test + public void should_build_rotating_requested_secret() { + RequestedSecret requestedSecret = RequestedSecret.from(Mode.ROTATE, "my/path"); + + assertThat(requestedSecret.getMode()).isEqualTo(Mode.ROTATE); + } + + @Test + public void should_build_renewal_requested_secret() { + RequestedSecret requestedSecret = RequestedSecret.from(Mode.RENEW, "my/path"); + + assertThat(requestedSecret.getMode()).isEqualTo(Mode.RENEW); + } + +} \ No newline at end of file