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;
}
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;
}
}
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);
}
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();
}
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));
}
Aggregations