Skip to content

Commit

Permalink
Allow dropping tokens from the session manager for easier recovery on…
Browse files Browse the repository at this point in the history
… lookup failures.

Closes gh-684
  • Loading branch information
mp911de committed Mar 20, 2023
1 parent 4e794ed commit 04a41c7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.vault.VaultException;
import org.springframework.vault.authentication.event.*;
import org.springframework.vault.client.VaultHttpHeaders;
Expand Down Expand Up @@ -149,11 +148,18 @@ protected void setToken(Optional<TokenWrapper> token) {

@Override
public void destroy() {
revoke();
}

Optional<TokenWrapper> token = getToken();
setToken(Optional.empty());
/**
* Revoke and drop the current {@link VaultToken}.
* @since 3.0.2
*/
public void revoke() {

Optional<TokenWrapper> token = getToken();
token.filter(TokenWrapper::isRevocable).map(TokenWrapper::getToken).ifPresent(this::revoke);
setToken(Optional.empty());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.vault.VaultException;
import org.springframework.vault.authentication.event.*;
import org.springframework.vault.client.VaultHttpHeaders;
Expand Down Expand Up @@ -145,6 +144,23 @@ public void destroy() {
revokeNow(tokenMono);
}

/**
* Revoke and drop the current {@link VaultToken}.
* @return a mono emitting completion upon successful revocation.
* @since 3.0.2
*/
public Mono<Void> revoke() {
return doRevoke(this.token.get()).doOnSuccess(unused -> this.token.set(EMPTY));
}

/**
* Revoke and drop the current {@link VaultToken} now.
* @since 3.0.2
*/
public void revokeNow() {
revoke().block(Duration.ofSeconds(5));
}

/**
* Revoke a {@link VaultToken} now and block execution until revocation completes.
* @param tokenMono
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ public VaultToken getSessionToken() {
sessionManager.renewToken();
}

@Test
void shouldRevokeToken() {

final LoginToken loginToken = createLoginToken();
TokenAuthentication tokenAuthentication = new TokenAuthentication(loginToken);

LifecycleAwareSessionManager sessionManager = new LifecycleAwareSessionManager(tokenAuthentication,
this.taskScheduler, prepare().getRestTemplate());

sessionManager.getSessionToken();
sessionManager.revoke();

prepare().getVaultOperations().doWithSession(restOperations -> {

try {
restOperations.getForEntity("auth/token/lookup/{token}", Map.class, loginToken.toCharArray());
fail("Missing HttpStatusCodeException");
}
catch (HttpStatusCodeException e) {
// Compatibility across Vault versions.
assertThat(e.getStatusCode()).isIn(HttpStatus.BAD_REQUEST, HttpStatus.NOT_FOUND, HttpStatus.FORBIDDEN);
}

return null;
});
}

@Test
void shouldRevokeOnDisposal() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import org.assertj.core.util.Files;
Expand Down Expand Up @@ -171,6 +172,36 @@ void shouldRevokeOnDisposal() {
});
}

@Test
void shouldRevokeToken() {

LoginToken loginToken = createLoginToken();

ReactiveLifecycleAwareSessionManager sessionManager = new ReactiveLifecycleAwareSessionManager(
() -> Flux.fromStream(Stream.of((VaultToken) loginToken)).next(), this.taskScheduler,
prepare().getWebClient());

sessionManager.getSessionToken() //
.as(StepVerifier::create) //
.expectNext(loginToken) //
.verifyComplete();
sessionManager.revokeNow();

prepare().getVaultOperations().doWithSession(restOperations -> {

try {
restOperations.getForEntity("auth/token/lookup/{token}", Map.class, loginToken.toCharArray());
fail("Missing HttpStatusCodeException");
}
catch (HttpStatusCodeException e) {
// Compatibility across Vault versions.
assertThat(e.getStatusCode()).isIn(HttpStatus.BAD_REQUEST, HttpStatus.NOT_FOUND, HttpStatus.FORBIDDEN);
}

return null;
});
}

private LoginToken createLoginToken() {

VaultTokenOperations tokenOperations = prepare().getVaultOperations().opsForToken();
Expand Down

0 comments on commit 04a41c7

Please sign in to comment.