Search in sources :

Example 1 with VaultException

use of org.springframework.vault.VaultException in project spring-vault by spring-projects.

the class VaultTransformTemplate method toEncodedResults.

private static List<VaultTransformEncodeResult> toEncodedResults(VaultResponse vaultResponse, List<TransformPlaintext> batchRequest) {
    List<VaultTransformEncodeResult> result = new ArrayList<>(batchRequest.size());
    List<Map<String, String>> batchData = getBatchData(vaultResponse);
    for (int i = 0; i < batchRequest.size(); i++) {
        VaultTransformEncodeResult encoded;
        TransformPlaintext plaintext = batchRequest.get(i);
        if (batchData.size() > i) {
            Map<String, String> data = batchData.get(i);
            if (StringUtils.hasText(data.get("error"))) {
                encoded = new VaultTransformEncodeResult(new VaultException(data.get("error")));
            } else {
                encoded = new VaultTransformEncodeResult(toCiphertext(data, plaintext.getContext()));
            }
        } else {
            encoded = new VaultTransformEncodeResult(new VaultException("No result for plaintext #" + i));
        }
        result.add(encoded);
    }
    return result;
}
Also used : VaultException(org.springframework.vault.VaultException) VaultTransformEncodeResult(org.springframework.vault.support.VaultTransformEncodeResult) TransformPlaintext(org.springframework.vault.support.TransformPlaintext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with VaultException

use of org.springframework.vault.VaultException in project spring-vault by spring-projects.

the class VaultKeyValue2Template method patch.

@Override
public boolean patch(String path, Map<String, ?> patch) {
    Assert.hasText(path, "Path must not be empty");
    Assert.notNull(patch, "Patch body must not be null");
    // To do patch operation, we need to do a read operation first
    VaultResponse readResponse = get(path);
    if (readResponse == null || readResponse.getData() == null) {
        throw new SecretNotFoundException(String.format("No data found at %s; patch only works on existing data", createDataPath(path)), String.format("%s/%s", this.path, path));
    }
    if (readResponse.getMetadata() == null) {
        throw new VaultException("Metadata must not be null");
    }
    Map<String, Object> metadata = readResponse.getMetadata();
    Map<String, Object> data = new LinkedHashMap<>(readResponse.getRequiredData());
    data.putAll(patch);
    Map<String, Object> body = new HashMap<>();
    body.put("data", data);
    body.put("options", Collections.singletonMap("cas", metadata.get("version")));
    try {
        doWrite(createDataPath(path), body);
        return true;
    } catch (VaultException e) {
        if (e.getMessage() != null && (e.getMessage().contains("check-and-set") || e.getMessage().contains("did not match the current version"))) {
            return false;
        }
        throw e;
    }
}
Also used : VaultException(org.springframework.vault.VaultException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) VaultResponse(org.springframework.vault.support.VaultResponse) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with VaultException

use of org.springframework.vault.VaultException in project spring-vault by spring-projects.

the class VaultWrappingTemplate method lookup.

@Nullable
@Override
public WrappedMetadata lookup(VaultToken token) {
    Assert.notNull(token, "token VaultToken not be null");
    VaultResponse response = null;
    try {
        response = this.vaultOperations.write("sys/wrapping/lookup", Collections.singletonMap("token", token.getToken()));
    } catch (VaultException e) {
        if (e.getMessage() != null && e.getMessage().contains("does not exist")) {
            return null;
        }
        throw e;
    }
    if (response == null) {
        return null;
    }
    return getWrappedMetadata(response.getData(), token);
}
Also used : VaultException(org.springframework.vault.VaultException) VaultResponse(org.springframework.vault.support.VaultResponse) Nullable(org.springframework.lang.Nullable)

Example 4 with VaultException

use of org.springframework.vault.VaultException in project spring-vault by spring-projects.

the class VaultPropertySourceUnitTests method shouldIgnoreFetchErrorByDefault.

@Test
void shouldIgnoreFetchErrorByDefault() {
    when(this.vaultTemplate.read("secret/myapp")).thenThrow(new VaultException("HTTP error"));
    VaultPropertySource source = new VaultPropertySource("hello", this.vaultTemplate, "secret/myapp", PropertyTransformers.noop());
    assertThat(source.getPropertyNames()).isEmpty();
}
Also used : VaultException(org.springframework.vault.VaultException) Test(org.junit.jupiter.api.Test)

Example 5 with VaultException

use of org.springframework.vault.VaultException in project spring-vault by spring-projects.

the class SecretLeaseContainerUnitTests method shouldRetainLeaseAfterRenewalFailure.

@Test
@SuppressWarnings("unchecked")
void shouldRetainLeaseAfterRenewalFailure() {
    prepareRenewal();
    when(this.vaultOperations.doWithSession(any(RestOperationsCallback.class))).thenThrow(new VaultException("Renewal failure"));
    this.secretLeaseContainer.setLeaseStrategy(LeaseStrategy.retainOnError());
    this.secretLeaseContainer.start();
    ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).schedule(captor.capture(), any(Trigger.class));
    captor.getValue().run();
    verify(this.taskScheduler, times(2)).schedule(captor.capture(), any(Trigger.class));
    captor.getValue().run();
    verify(this.vaultOperations, times(2)).doWithSession(any(RestOperationsCallback.class));
}
Also used : VaultException(org.springframework.vault.VaultException) Trigger(org.springframework.scheduling.Trigger) RestOperationsCallback(org.springframework.vault.core.RestOperationsCallback) Test(org.junit.jupiter.api.Test)

Aggregations

VaultException (org.springframework.vault.VaultException)27 Test (org.junit.jupiter.api.Test)12 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 VaultResponse (org.springframework.vault.support.VaultResponse)5 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)5 Map (java.util.Map)4 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)4 IOException (java.io.IOException)3 Nullable (org.springframework.lang.Nullable)3 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Supplier (java.util.function.Supplier)2 Log (org.apache.commons.logging.Log)2 PropertySource (org.springframework.core.env.PropertySource)2 Trigger (org.springframework.scheduling.Trigger)2 Assert (org.springframework.util.Assert)2 RestOperationsCallback (org.springframework.vault.core.RestOperationsCallback)2 LeaseAwareVaultPropertySource (org.springframework.vault.core.env.LeaseAwareVaultPropertySource)2 SecretLeaseContainer (org.springframework.vault.core.lease.SecretLeaseContainer)2