Skip to content

Commit fef74b8

Browse files
Merge pull request #198 from mikegrass/login-error-response-bodies
Add response body to exception for Auth failures
2 parents 2e7ed8d + 1693f8c commit fef74b8

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

src/main/java/com/bettercloud/vault/VaultConfig.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,18 @@ public VaultConfig readTimeout(final Integer readTimeout) {
209209
}
210210

211211
/**
212-
* Sets the "path depth" of the prefix path. This defaults to 1, corresponding to one
213-
* path element in the prefix path. To use a longer prefix path, set this value.
212+
* <p>Set the "path depth" of the prefix path. Normally this is just
213+
* 1, to correspond to one path element in the prefix path. To use
214+
* a longer prefix path, set this value.</p>
214215
*
215216
* @param pathLength integer number of path elements in the prefix path
216217
*/
217-
public VaultConfig prefixPathDepth(int pathLength) {
218-
if (pathLength < 1) {
218+
public VaultConfig prefixPathDepth(int prefixPathDepth) {
219+
if (prefixPathDepth < 1) {
219220
throw new IllegalArgumentException("pathLength must be > 1");
220221
}
221222

222-
this.prefixPathDepth = pathLength;
223+
this.prefixPathDepth = prefixPathDepth;
223224
return this;
224225
}
225226

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

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

356356
// Validate restResponse
357357
if (restResponse.getStatus() != 200) {
358-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
358+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
359+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
360+
restResponse.getStatus());
359361
}
360362
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
361363
if (!mimeType.equals("application/json")) {
@@ -421,7 +423,9 @@ public AuthResponse loginByAppID(final String path, final String appId, final St
421423

422424
// Validate restResponse
423425
if (restResponse.getStatus() != 200) {
424-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
426+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
427+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
428+
restResponse.getStatus());
425429
}
426430
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
427431
if (!mimeType.equals("application/json")) {
@@ -515,7 +519,9 @@ public AuthResponse loginByAppRole(final String path, final String roleId, final
515519

516520
// Validate restResponse
517521
if (restResponse.getStatus() != 200) {
518-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
522+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
523+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
524+
restResponse.getStatus());
519525
}
520526
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
521527
if (!mimeType.equals("application/json")) {
@@ -598,7 +604,9 @@ public AuthResponse loginByUserPass(final String username, final String password
598604

599605
// Validate restResponse
600606
if (restResponse.getStatus() != 200) {
601-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
607+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
608+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
609+
restResponse.getStatus());
602610
}
603611
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
604612
if (!mimeType.equals("application/json")) {
@@ -720,7 +728,9 @@ public AuthResponse loginByAwsEc2(final String role, final String identity, fina
720728

721729
// Validate restResponse
722730
if (restResponse.getStatus() != 200) {
723-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
731+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
732+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
733+
restResponse.getStatus());
724734
}
725735
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
726736
if (!mimeType.equals("application/json")) {
@@ -796,7 +806,9 @@ public AuthResponse loginByAwsEc2(final String role, final String pkcs7, final S
796806

797807
// Validate restResponse
798808
if (restResponse.getStatus() != 200) {
799-
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());
800812
}
801813
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
802814
if (!mimeType.equals("application/json")) {
@@ -875,7 +887,9 @@ public AuthResponse loginByAwsIam(final String role, final String iamRequestUrl,
875887

876888
// Validate restResponse
877889
if (restResponse.getStatus() != 200) {
878-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
890+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
891+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
892+
restResponse.getStatus());
879893
}
880894
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
881895
if (!mimeType.equals("application/json")) {
@@ -960,7 +974,9 @@ public AuthResponse loginByGithub(final String githubToken, final String githubA
960974

961975
// Validate restResponse
962976
if (restResponse.getStatus() != 200) {
963-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
977+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
978+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
979+
restResponse.getStatus());
964980
}
965981
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
966982
if (!mimeType.equals("application/json")) {
@@ -1024,7 +1040,9 @@ public AuthResponse loginByJwt(final String provider, final String role, final S
10241040

10251041
// Validate restResponse
10261042
if (restResponse.getStatus() != 200) {
1027-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
1043+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1044+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
1045+
restResponse.getStatus());
10281046
}
10291047
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
10301048
if (!mimeType.equals("application/json")) {
@@ -1163,7 +1181,8 @@ public AuthResponse loginByCert(final String certAuthMount) throws VaultExceptio
11631181

11641182
// Validate restResponse
11651183
if (restResponse.getStatus() != 200) {
1166-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(),
1184+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1185+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
11671186
restResponse.getStatus());
11681187
}
11691188
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
@@ -1246,7 +1265,9 @@ public AuthResponse renewSelf(final long increment, final String tokenAuthMount)
12461265

12471266
// Validate restResponse
12481267
if (restResponse.getStatus() != 200) {
1249-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
1268+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1269+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
1270+
restResponse.getStatus());
12501271
}
12511272
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
12521273
if (!mimeType.equals("application/json")) {
@@ -1308,7 +1329,9 @@ public LookupResponse lookupSelf(final String tokenAuthMount) throws VaultExcept
13081329

13091330
// Validate restResponse
13101331
if (restResponse.getStatus() != 200) {
1311-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
1332+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1333+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
1334+
restResponse.getStatus());
13121335
}
13131336
final String mimeType = restResponse.getMimeType();
13141337
if (!"application/json".equals(mimeType)) {
@@ -1432,7 +1455,9 @@ public void revokeSelf(final String tokenAuthMount) throws VaultException {
14321455

14331456
// Validate restResponse
14341457
if (restResponse.getStatus() != 204) {
1435-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus());
1458+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1459+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
1460+
restResponse.getStatus());
14361461
}
14371462
return;
14381463
} catch (Exception e) {
@@ -1534,7 +1559,8 @@ public AuthResponse unwrap(final String wrappedToken) throws VaultException {
15341559

15351560
// Validate restResponse
15361561
if (restResponse.getStatus() != 200) {
1537-
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(),
1562+
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
1563+
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
15381564
restResponse.getStatus());
15391565
}
15401566
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();

0 commit comments

Comments
 (0)