Skip to content

Commit 7ea8b8c

Browse files
committed
Add response body to exception for Auth failures
Similar to BetterCloud#49, it including the response body in the exception thrown for auth failures provides information that is helpful for debugging failed logins. Add the response body to the VaultException thrown when an Auth method fails.
1 parent 14e68b2 commit 7ea8b8c

File tree

1 file changed

+40
-14
lines changed
  • src/main/java/com/bettercloud/vault/api

1 file changed

+40
-14
lines changed

src/main/java/com/bettercloud/vault/api/Auth.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ public AuthResponse createToken(final TokenRequest tokenRequest, final String to
274274

275275
// Validate restResponse
276276
if (restResponse.getStatus() != 200) {
277-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
277+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
278+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
279+
restResponse.getStatus());
278280
}
279281
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
280282
if (!mimeType.equals("application/json")) {
@@ -340,7 +342,9 @@ public AuthResponse loginByAppID(final String path, final String appId, final St
340342

341343
// Validate restResponse
342344
if (restResponse.getStatus() != 200) {
343-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
345+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
346+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
347+
restResponse.getStatus());
344348
}
345349
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
346350
if (!mimeType.equals("application/json")) {
@@ -434,7 +438,9 @@ public AuthResponse loginByAppRole(final String path, final String roleId, final
434438

435439
// Validate restResponse
436440
if (restResponse.getStatus() != 200) {
437-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
441+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
442+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
443+
restResponse.getStatus());
438444
}
439445
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
440446
if (!mimeType.equals("application/json")) {
@@ -517,7 +523,9 @@ public AuthResponse loginByUserPass(final String username, final String password
517523

518524
// Validate restResponse
519525
if (restResponse.getStatus() != 200) {
520-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
526+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
527+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
528+
restResponse.getStatus());
521529
}
522530
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
523531
if (!mimeType.equals("application/json")) {
@@ -639,7 +647,9 @@ public AuthResponse loginByAwsEc2(final String role, final String identity, fina
639647

640648
// Validate restResponse
641649
if (restResponse.getStatus() != 200) {
642-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
650+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
651+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
652+
restResponse.getStatus());
643653
}
644654
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
645655
if (!mimeType.equals("application/json")) {
@@ -715,7 +725,9 @@ public AuthResponse loginByAwsEc2(final String role, final String pkcs7, final S
715725

716726
// Validate restResponse
717727
if (restResponse.getStatus() != 200) {
718-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
728+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
729+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
730+
restResponse.getStatus());
719731
}
720732
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
721733
if (!mimeType.equals("application/json")) {
@@ -794,7 +806,9 @@ public AuthResponse loginByAwsIam(final String role, final String iamRequestUrl,
794806

795807
// Validate restResponse
796808
if (restResponse.getStatus() != 200) {
797-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
809+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
810+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
811+
restResponse.getStatus());
798812
}
799813
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
800814
if (!mimeType.equals("application/json")) {
@@ -879,7 +893,9 @@ public AuthResponse loginByGithub(final String githubToken, final String githubA
879893

880894
// Validate restResponse
881895
if (restResponse.getStatus() != 200) {
882-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
896+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
897+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
898+
restResponse.getStatus());
883899
}
884900
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
885901
if (!mimeType.equals("application/json")) {
@@ -943,7 +959,9 @@ public AuthResponse loginByJwt(final String provider, final String role, final S
943959

944960
// Validate restResponse
945961
if (restResponse.getStatus() != 200) {
946-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
962+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
963+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
964+
restResponse.getStatus());
947965
}
948966
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
949967
if (!mimeType.equals("application/json")) {
@@ -1082,7 +1100,8 @@ public AuthResponse loginByCert(final String certAuthMount) throws VaultExceptio
10821100

10831101
// Validate restResponse
10841102
if (restResponse.getStatus() != 200) {
1085-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(),
1103+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1104+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
10861105
restResponse.getStatus());
10871106
}
10881107
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
@@ -1165,7 +1184,9 @@ public AuthResponse renewSelf(final long increment, final String tokenAuthMount)
11651184

11661185
// Validate restResponse
11671186
if (restResponse.getStatus() != 200) {
1168-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
1187+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1188+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
1189+
restResponse.getStatus());
11691190
}
11701191
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
11711192
if (!mimeType.equals("application/json")) {
@@ -1227,7 +1248,9 @@ public LookupResponse lookupSelf(final String tokenAuthMount) throws VaultExcept
12271248

12281249
// Validate restResponse
12291250
if (restResponse.getStatus() != 200) {
1230-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
1251+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1252+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
1253+
restResponse.getStatus());
12311254
}
12321255
final String mimeType = restResponse.getMimeType();
12331256
if (!"application/json".equals(mimeType)) {
@@ -1351,7 +1374,9 @@ public void revokeSelf(final String tokenAuthMount) throws VaultException {
13511374

13521375
// Validate restResponse
13531376
if (restResponse.getStatus() != 204) {
1354-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
1377+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1378+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
1379+
restResponse.getStatus());
13551380
}
13561381
return;
13571382
} catch (Exception e) {
@@ -1453,7 +1478,8 @@ public AuthResponse unwrap(final String wrappedToken) throws VaultException {
14531478

14541479
// Validate restResponse
14551480
if (restResponse.getStatus() != 200) {
1456-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(),
1481+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1482+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
14571483
restResponse.getStatus());
14581484
}
14591485
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();

0 commit comments

Comments
 (0)