|
5 | 5 | import com.bettercloud.vault.json.Json; |
6 | 6 | import com.bettercloud.vault.json.JsonObject; |
7 | 7 | import com.bettercloud.vault.response.AuthResponse; |
| 8 | +import com.bettercloud.vault.response.LookupResponse; |
8 | 9 | import com.bettercloud.vault.rest.RestResponse; |
9 | 10 | import com.bettercloud.vault.rest.Rest; |
10 | 11 |
|
@@ -315,7 +316,7 @@ public AuthResponse createToken(TokenRequest tokenRequest) throws VaultException |
315 | 316 | .connectTimeoutSeconds(config.getOpenTimeout()) |
316 | 317 | .readTimeoutSeconds(config.getReadTimeout()) |
317 | 318 | .sslPemUTF8(config.getSslPemUTF8()) |
318 | | - .sslVerification(config.isSslVerify() != null ? config.isSslVerify() : null) |
| 319 | + .sslVerification(config.isSslVerify()) |
319 | 320 | .post(); |
320 | 321 |
|
321 | 322 | // Validate restResponse |
@@ -380,7 +381,7 @@ public AuthResponse loginByAppID(final String path, final String appId, final St |
380 | 381 | .connectTimeoutSeconds(config.getOpenTimeout()) |
381 | 382 | .readTimeoutSeconds(config.getReadTimeout()) |
382 | 383 | .sslPemUTF8(config.getSslPemUTF8()) |
383 | | - .sslVerification(config.isSslVerify() != null ? config.isSslVerify() : null) |
| 384 | + .sslVerification(config.isSslVerify()) |
384 | 385 | .post(); |
385 | 386 |
|
386 | 387 | // Validate restResponse |
@@ -441,7 +442,7 @@ public AuthResponse loginByAppRole(final String path, final String roleId, final |
441 | 442 | .connectTimeoutSeconds(config.getOpenTimeout()) |
442 | 443 | .readTimeoutSeconds(config.getReadTimeout()) |
443 | 444 | .sslPemUTF8(config.getSslPemUTF8()) |
444 | | - .sslVerification(config.isSslVerify() != null ? config.isSslVerify() : null) |
| 445 | + .sslVerification(config.isSslVerify()) |
445 | 446 | .post(); |
446 | 447 |
|
447 | 448 | // Validate restResponse |
@@ -501,7 +502,7 @@ public AuthResponse loginByUserPass(final String username, final String password |
501 | 502 | .connectTimeoutSeconds(config.getOpenTimeout()) |
502 | 503 | .readTimeoutSeconds(config.getReadTimeout()) |
503 | 504 | .sslPemUTF8(config.getSslPemUTF8()) |
504 | | - .sslVerification(config.isSslVerify() != null ? config.isSslVerify() : null) |
| 505 | + .sslVerification(config.isSslVerify()) |
505 | 506 | .post(); |
506 | 507 |
|
507 | 508 | // Validate restResponse |
@@ -563,7 +564,7 @@ public AuthResponse loginByGithub(final String githubToken) throws VaultExceptio |
563 | 564 | .connectTimeoutSeconds(config.getOpenTimeout()) |
564 | 565 | .readTimeoutSeconds(config.getReadTimeout()) |
565 | 566 | .sslPemUTF8(config.getSslPemUTF8()) |
566 | | - .sslVerification(config.isSslVerify() != null ? config.isSslVerify() : null) |
| 567 | + .sslVerification(config.isSslVerify()) |
567 | 568 | .post(); |
568 | 569 |
|
569 | 570 | // Validate restResponse |
@@ -628,7 +629,7 @@ public AuthResponse renewSelf(final long increment) throws VaultException { |
628 | 629 | .connectTimeoutSeconds(config.getOpenTimeout()) |
629 | 630 | .readTimeoutSeconds(config.getReadTimeout()) |
630 | 631 | .sslPemUTF8(config.getSslPemUTF8()) |
631 | | - .sslVerification(config.isSslVerify() != null ? config.isSslVerify() : null) |
| 632 | + .sslVerification(config.isSslVerify()) |
632 | 633 | .post(); |
633 | 634 | // Validate restResponse |
634 | 635 | if (restResponse.getStatus() != 200) { |
@@ -659,4 +660,52 @@ public AuthResponse renewSelf(final long increment) throws VaultException { |
659 | 660 | } |
660 | 661 | } |
661 | 662 |
|
| 663 | + /** |
| 664 | + * <p>Returns information about the current client token.</p> |
| 665 | + * |
| 666 | + * @return The response information returned from Vault |
| 667 | + * @throws VaultException If any error occurs, or unexpected response received from Vault |
| 668 | + */ |
| 669 | + public LookupResponse lookupSelf() throws VaultException { |
| 670 | + int retryCount = 0; |
| 671 | + while (true) { |
| 672 | + try { |
| 673 | + // HTTP request to Vault |
| 674 | + final RestResponse restResponse = new Rest()//NOPMD |
| 675 | + .url(config.getAddress() + "/v1/auth/token/lookup-self") |
| 676 | + .header("X-Vault-Token", config.getToken()) |
| 677 | + .connectTimeoutSeconds(config.getOpenTimeout()) |
| 678 | + .readTimeoutSeconds(config.getReadTimeout()) |
| 679 | + .sslPemUTF8(config.getSslPemUTF8()) |
| 680 | + .sslVerification(config.isSslVerify()) |
| 681 | + .post(); |
| 682 | + // Validate restResponse |
| 683 | + if (restResponse.getStatus() != 200) { |
| 684 | + throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), restResponse.getStatus()); |
| 685 | + } |
| 686 | + final String mimeType = restResponse.getMimeType(); |
| 687 | + if (mimeType == null || !"application/json".equals(mimeType)) { |
| 688 | + throw new VaultException("Vault responded with MIME type: " + mimeType, restResponse.getStatus()); |
| 689 | + } |
| 690 | + return new LookupResponse(restResponse, retryCount); |
| 691 | + } catch (Exception e) { |
| 692 | + // If there are retries to perform, then pause for the configured interval and then execute the loop again... |
| 693 | + if (retryCount < config.getMaxRetries()) { |
| 694 | + retryCount++; |
| 695 | + try { |
| 696 | + final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds(); |
| 697 | + Thread.sleep(retryIntervalMilliseconds); |
| 698 | + } catch (InterruptedException e1) { |
| 699 | + e1.printStackTrace(); //NOPMD |
| 700 | + } |
| 701 | + } else if (e instanceof VaultException) { //NOPMD |
| 702 | + // ... otherwise, give up. |
| 703 | + throw (VaultException) e; |
| 704 | + } else { |
| 705 | + throw new VaultException(e); |
| 706 | + } |
| 707 | + } |
| 708 | + } |
| 709 | + } |
| 710 | + |
662 | 711 | } |
0 commit comments