diff --git a/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java b/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java index 253ecd01..d4ba4ce5 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java @@ -572,6 +572,16 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements return renewed; } + catch (VaultException e) { + + // Workaround as there is no HttpStatusCodeException attached. + if (e.getMessage().contains("Status 400")) { + onLeaseExpired(requestedSecret, lease); + } + + onError(requestedSecret, lease, + new VaultException(String.format("Cannot renew lease: %s", e))); + } catch (HttpStatusCodeException e) { if (e.getStatusCode() == HttpStatus.BAD_REQUEST) { diff --git a/spring-vault-core/src/test/java/org/springframework/vault/core/lease/SecretLeaseContainerUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/SecretLeaseContainerUnitTests.java index 4145cfab..9afdb80f 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/core/lease/SecretLeaseContainerUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/core/lease/SecretLeaseContainerUnitTests.java @@ -38,6 +38,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.Trigger; import org.springframework.vault.VaultException; +import org.springframework.vault.client.VaultResponses; import org.springframework.vault.core.RestOperationsCallback; import org.springframework.vault.core.VaultOperations; import org.springframework.vault.core.lease.domain.Lease; @@ -500,6 +501,23 @@ public class SecretLeaseContainerUnitTests { any(AfterSecretLeaseRevocationEvent.class)); } + @Test + public void expiredLeaseShouldRenew() { + + prepareRenewal(); + + VaultException exception = VaultResponses + .buildException(new HttpClientErrorException(HttpStatus.BAD_REQUEST)); + + when(vaultOperations.doWithSession(any(RestOperationsCallback.class))).thenThrow( + exception); + + secretLeaseContainer.doRenewLease(requestedSecret, Lease.of("foo", 1, true)); + + verify(leaseListenerAdapter).onLeaseEvent(any(SecretLeaseExpiredEvent.class)); + verify(vaultOperations).doWithSession(any(RestOperationsCallback.class)); + } + @Test public void shouldNotRevokeSecretsWithoutLease() throws Exception {