Refactored (un)seal methods to throw Exception instead of catching it (#12)

This commit is contained in:
2017-08-26 11:01:48 +02:00
parent df7de5dd73
commit a1784245a3
4 changed files with 44 additions and 36 deletions

View File

@ -211,32 +211,25 @@ public class HTTPVaultConnector implements VaultConnector {
}
@Override
public final SealResponse sealStatus() {
public final SealResponse sealStatus() throws VaultConnectorException {
try {
String response = requestGet(PATH_SEAL_STATUS, new HashMap<>());
return jsonMapper.readValue(response, SealResponse.class);
} catch (VaultConnectorException | IOException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
throw new InvalidRequestException("Unable to parse response", e);
} catch (URISyntaxException ignored) {
/* this should never occur and may leak sensible information */
return null;
throw new InvalidRequestException("Invalid URI format");
}
}
@Override
public final boolean seal() {
try {
requestPut(PATH_SEAL, new HashMap<>());
return true;
} catch (VaultConnectorException e) {
e.printStackTrace();
return false;
}
public final void seal() throws VaultConnectorException {
requestPut(PATH_SEAL, new HashMap<>());
}
@Override
public final SealResponse unseal(final String key, final Boolean reset) {
public final SealResponse unseal(final String key, final Boolean reset) throws VaultConnectorException {
Map<String, String> param = new HashMap<>();
param.put("key", key);
if (reset != null)
@ -244,9 +237,8 @@ public class HTTPVaultConnector implements VaultConnector {
try {
String response = requestPut(PATH_UNSEAL, param);
return jsonMapper.readValue(response, SealResponse.class);
} catch (VaultConnectorException | IOException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
throw new InvalidResponseException("Unable to parse response", e);
}
}

View File

@ -42,32 +42,35 @@ public interface VaultConnector extends AutoCloseable {
* Retrieve status of vault seal.
*
* @return Seal status
* @throws VaultConnectorException on error
*/
SealResponse sealStatus();
SealResponse sealStatus() throws VaultConnectorException;
/**
* Seal vault.
*
* @return TRUE on success
* @throws VaultConnectorException on error
*/
boolean seal();
void seal() throws VaultConnectorException;
/**
* Unseal vault.
*
* @param key A single master share key
* @param reset Discard previously provided keys (optional)
* @return TRUE on success
* @return Response with seal status
* @throws VaultConnectorException on error
*/
SealResponse unseal(final String key, final Boolean reset);
SealResponse unseal(final String key, final Boolean reset) throws VaultConnectorException;
/**
* Unseal vault.
*
* @param key A single master share key
* @return TRUE on success
* @return Response with seal status
* @throws VaultConnectorException on error
*/
default SealResponse unseal(final String key) {
default SealResponse unseal(final String key) throws VaultConnectorException {
return unseal(key, null);
}