BetterCloud / vault-java-driver

Zero-dependency Java client for HashiCorp's Vault
https://bettercloud.github.io/vault-java-driver/
335 stars 224 forks source link

PKI methods check for 200 and 404 REST response codes, versus 200 and 204. #167

Open amozano opened 5 years ago

amozano commented 5 years ago

Pki.issue() and Pki.getRole() are checking for 200 and 404 messages, where as Vault returns 200 and 204 for non-erroneous requests, and 4xx otherwise. This check seems to have been intended per comments: "@throws VaultException If any error occurs or unexpected response is received from Vault."

However, the better implementation would be to return payload when 2xx status is received, and throw exception (along with the rest response body and/or status) otherwise.

Relevant code snippets: public PkiResponse issue( final String roleName, final String commonName, final List altNames, final List ipSans, final String ttl, final CredentialFormat format, final String csr ) throws VaultException { ... // Validate response if (restResponse.getStatus() != 200 && restResponse.getStatus() != 404) { String body = restResponse.getBody() != null ? new String(restResponse.getBody()) : "(no body)"; throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus() + " " + body, restResponse.getStatus()); } return new PkiResponse(restResponse, retryCount);

====================================================== public PkiResponse getRole(final String roleName) throws VaultException { ... // Validate response if (restResponse.getStatus() != 200 && restResponse.getStatus() != 404) { throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus()); }