Skip to content

Commit 70e58e4

Browse files
Tess Stoddardtessstoddard
authored andcommitted
feat: improve vault error message
1 parent f40bf22 commit 70e58e4

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

encryption-service-vault/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
coppuccino {
22
coverage {
3-
minimumCoverage = 0.89
3+
minimumCoverage = 0.88
44
}
55
dependencies {
66
excludePreReleaseVersions = false

encryption-service-vault/src/main/java/com/mx/path/service/facility/security/vault/VaultEncryptionService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ private void validateVaultAuthenticationResponse(VaultResponse response, String
324324
@SuppressWarnings("checkstyle:MagicNumber")
325325
private void validateVaultOperationResponse(VaultResponse response, String errorMessage) {
326326
if (response != null && response.getRestResponse() != null && (response.getRestResponse().getStatus() < 200 || response.getRestResponse().getStatus() >= 300)) {
327+
byte[] body = response.getRestResponse().getBody();
328+
if (body != null) {
329+
throw new VaultEncryptionOperationException(errorMessage + " (" + response.getRestResponse().getStatus() + "): " + new String(body, StandardCharsets.UTF_8));
330+
}
327331
throw new VaultEncryptionOperationException(errorMessage + " (" + response.getRestResponse().getStatus() + ")");
328332
}
329333
}

encryption-service-vault/src/test/groovy/com/mx/path/service/facility/security/vault/VaultEncryptionServiceTest.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import com.bettercloud.vault.api.Auth
1717
import com.bettercloud.vault.api.Logical
1818
import com.bettercloud.vault.response.AuthResponse
1919
import com.bettercloud.vault.response.LogicalResponse
20+
import com.bettercloud.vault.response.VaultResponse
2021
import com.bettercloud.vault.rest.RestResponse
2122
import com.google.common.collect.ImmutableMap
23+
import com.mx.path.core.common.accessor.PathResponseStatus
2224

2325
import spock.lang.Specification
2426
import spock.lang.Unroll
@@ -659,4 +661,21 @@ class VaultEncryptionServiceTest extends Specification {
659661
then:
660662
subject.getConfiguration() == config
661663
}
664+
665+
def "validateVaultOperationResponse throws exception"() {
666+
given:
667+
subject = new VaultEncryptionService(configWithAppId())
668+
subject.setDriver(vaultDriver)
669+
670+
def decryptResponse = new LogicalResponse(new RestResponse(400, "mimeType", "bad response".getBytes()), 2, null)
671+
when(logicalDriver.write(eq("transit/decrypt/test-key"), any())).thenReturn(decryptResponse)
672+
673+
when:
674+
subject.decrypt("vault-12345")
675+
676+
then:
677+
def ex = thrown(VaultEncryptionOperationException)
678+
ex.status == PathResponseStatus.INTERNAL_ERROR
679+
ex.message == "Vault decrypt failed (400): bad response"
680+
}
662681
}

0 commit comments

Comments
 (0)