Polishing.
Add author tag. Update LoginToken javadoc. Refactor numeric literals representing milliseconds to TimeUnit use. Add tests. See gh-96. Original pull request: gh-97.
This commit is contained in:
@@ -31,7 +31,6 @@ import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.scheduling.TriggerContext;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.NumberUtils;
|
||||
import org.springframework.vault.VaultException;
|
||||
import org.springframework.vault.client.VaultHttpHeaders;
|
||||
import org.springframework.vault.client.VaultResponses;
|
||||
@@ -55,6 +54,7 @@ import org.springframework.web.client.RestOperations;
|
||||
* login attempt.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Steven Swor
|
||||
* @see LoginToken
|
||||
* @see SessionManager
|
||||
* @see AsyncTaskExecutor
|
||||
@@ -172,15 +172,14 @@ public class LifecycleAwareSessionManager implements SessionManager, DisposableB
|
||||
catch (HttpStatusCodeException e) {
|
||||
|
||||
if (e.getStatusCode().is4xxClientError()) {
|
||||
logger.debug(String.format(
|
||||
"Cannot refresh token, resetting token and performing re-login: %s",
|
||||
VaultResponses.getError(e.getResponseBodyAsString())));
|
||||
logger.debug(String
|
||||
.format("Cannot refresh token, resetting token and performing re-login: %s",
|
||||
VaultResponses.getError(e.getResponseBodyAsString())));
|
||||
token = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new VaultException(
|
||||
VaultResponses.getError(e.getResponseBodyAsString()));
|
||||
throw new VaultException(VaultResponses.getError(e.getResponseBodyAsString()));
|
||||
}
|
||||
catch (RestClientException e) {
|
||||
throw new VaultException("Cannot refresh token", e);
|
||||
@@ -318,12 +317,10 @@ public class LifecycleAwareSessionManager implements SessionManager, DisposableB
|
||||
@Override
|
||||
public Date nextExecutionTime(LoginToken loginToken) {
|
||||
|
||||
long milliseconds = NumberUtils
|
||||
.convertNumberToTargetClass(
|
||||
Math.max(1000,
|
||||
1000 * loginToken.getLeaseDuration()
|
||||
- timeUnit.toMillis(duration)),
|
||||
Integer.class);
|
||||
long milliseconds = Math.max(
|
||||
TimeUnit.SECONDS.toMillis(1),
|
||||
TimeUnit.SECONDS.toMillis(loginToken.getLeaseDuration())
|
||||
- timeUnit.toMillis(duration));
|
||||
|
||||
return new Date(System.currentTimeMillis() + milliseconds);
|
||||
}
|
||||
|
||||
@@ -30,13 +30,16 @@ public class LoginToken extends VaultToken {
|
||||
|
||||
private final boolean renewable;
|
||||
|
||||
/**
|
||||
* Duration in seconds.
|
||||
*/
|
||||
private final long leaseDuration;
|
||||
|
||||
private LoginToken(String token, long leaseDuration, boolean renewable) {
|
||||
private LoginToken(String token, long leaseDurationSeconds, boolean renewable) {
|
||||
|
||||
super(token);
|
||||
|
||||
this.leaseDuration = leaseDuration;
|
||||
this.leaseDuration = leaseDurationSeconds;
|
||||
this.renewable = renewable;
|
||||
}
|
||||
|
||||
@@ -51,35 +54,35 @@ public class LoginToken extends VaultToken {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link LoginToken} with a {@code leaseDuration}.
|
||||
* Create a new {@link LoginToken} with a {@code leaseDurationSeconds}.
|
||||
*
|
||||
* @param token must not be {@literal null}.
|
||||
* @param leaseDuration the lease duration.
|
||||
* @param leaseDurationSeconds the lease duration in seconds.
|
||||
* @return the created {@link VaultToken}
|
||||
*/
|
||||
public static LoginToken of(String token, long leaseDuration) {
|
||||
public static LoginToken of(String token, long leaseDurationSeconds) {
|
||||
|
||||
Assert.hasText(token, "Token must not be empty");
|
||||
|
||||
return new LoginToken(token, leaseDuration, false);
|
||||
return new LoginToken(token, leaseDurationSeconds, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new renewable {@link LoginToken} with a {@code leaseDuration}.
|
||||
* Create a new renewable {@link LoginToken} with a {@code leaseDurationSeconds}.
|
||||
*
|
||||
* @param token must not be {@literal null}.
|
||||
* @param leaseDuration the lease duration.
|
||||
* @param leaseDurationSeconds the lease duration in seconds.
|
||||
* @return the created {@link VaultToken}
|
||||
*/
|
||||
public static LoginToken renewable(String token, long leaseDuration) {
|
||||
public static LoginToken renewable(String token, long leaseDurationSeconds) {
|
||||
|
||||
Assert.hasText(token, "Token must not be empty");
|
||||
|
||||
return new LoginToken(token, leaseDuration, true);
|
||||
return new LoginToken(token, leaseDurationSeconds, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the lease duration. May be {@literal 0} if none.
|
||||
* @return the lease duration in seconds. May be {@literal 0} if none.
|
||||
*/
|
||||
public long getLeaseDuration() {
|
||||
return leaseDuration;
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.vault.authentication;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -27,6 +30,7 @@ import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.vault.authentication.LifecycleAwareSessionManager.FixedTimeoutRefreshTrigger;
|
||||
import org.springframework.vault.client.VaultHttpHeaders;
|
||||
import org.springframework.vault.support.VaultToken;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
@@ -107,7 +111,7 @@ public class LifecycleAwareSessionManagerUnitTests {
|
||||
|
||||
when(
|
||||
restOperations.postForObject(anyString(), any(),
|
||||
ArgumentMatchers.<Class>any())).thenThrow(
|
||||
ArgumentMatchers.<Class> any())).thenThrow(
|
||||
new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
|
||||
sessionManager.renewToken();
|
||||
@@ -187,7 +191,7 @@ public class LifecycleAwareSessionManagerUnitTests {
|
||||
when(clientAuthentication.login()).thenReturn(LoginToken.renewable("login", 5));
|
||||
when(
|
||||
restOperations.postForObject(anyString(), any(),
|
||||
ArgumentMatchers.<Class>any())).thenThrow(
|
||||
ArgumentMatchers.<Class> any())).thenThrow(
|
||||
new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
|
||||
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||
@@ -217,13 +221,37 @@ public class LifecycleAwareSessionManagerUnitTests {
|
||||
|
||||
when(clientAuthentication.login()).thenReturn(LoginToken.renewable("login", 5));
|
||||
when(
|
||||
restOperations.postForObject(anyString(), ArgumentMatchers.<Object>any(),
|
||||
ArgumentMatchers.<Class>any())).thenThrow(
|
||||
new HttpServerErrorException(HttpStatus.BAD_REQUEST));
|
||||
restOperations.postForObject(anyString(),
|
||||
ArgumentMatchers.<Object> any(), ArgumentMatchers.<Class> any()))
|
||||
.thenThrow(new HttpServerErrorException(HttpStatus.BAD_REQUEST));
|
||||
|
||||
sessionManager.getSessionToken();
|
||||
|
||||
assertThat(sessionManager.renewToken()).isFalse();
|
||||
verify(clientAuthentication, times(1)).login();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldScheduleNextExecutionTimeCorrectly() {
|
||||
|
||||
FixedTimeoutRefreshTrigger trigger = new FixedTimeoutRefreshTrigger(5,
|
||||
TimeUnit.SECONDS);
|
||||
|
||||
Date nextExecutionTime = trigger.nextExecutionTime(LoginToken.of("foo", 60));
|
||||
assertThat(nextExecutionTime).isBetween(
|
||||
new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(52)),
|
||||
new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(56)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldScheduleNextExecutionIfValidityLessThanTimeout() {
|
||||
|
||||
FixedTimeoutRefreshTrigger trigger = new FixedTimeoutRefreshTrigger(5,
|
||||
TimeUnit.SECONDS);
|
||||
|
||||
Date nextExecutionTime = trigger.nextExecutionTime(LoginToken.of("foo", 2));
|
||||
assertThat(nextExecutionTime).isBetween(
|
||||
new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(0)),
|
||||
new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link LoginToken}.
|
||||
*
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class LoginTokenUnitTests {
|
||||
|
||||
Reference in New Issue
Block a user