Allow to create RequestedSecret from mode and path.

Original pull request: #64.
This commit is contained in:
Pierre-Jean Vardanega
2017-03-28 12:45:20 +02:00
committed by Mark Paluch
parent 8b929011d9
commit ba135817fa
2 changed files with 48 additions and 1 deletions

View File

@@ -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.
*/

View File

@@ -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);
}
}