|
6 | 6 | import com.bettercloud.vault.json.Json; |
7 | 7 | import com.bettercloud.vault.json.JsonObject; |
8 | 8 | import com.bettercloud.vault.response.AuthResponse; |
| 9 | +import com.bettercloud.vault.response.LogicalResponse; |
9 | 10 | import com.bettercloud.vault.response.LookupResponse; |
10 | 11 | import com.bettercloud.vault.rest.Rest; |
11 | 12 | import com.bettercloud.vault.rest.RestResponse; |
12 | 13 | import lombok.Getter; |
13 | 14 |
|
14 | 15 | import java.io.Serializable; |
| 16 | +import java.net.URI; |
15 | 17 | import java.util.List; |
16 | 18 | import java.util.Map; |
17 | 19 | import java.util.UUID; |
@@ -1084,17 +1086,17 @@ public AuthResponse renewSelf(final long increment, final String tokenAuthMount) |
1084 | 1086 |
|
1085 | 1087 | /** |
1086 | 1088 | * <p>Returns information about the current client token.</p> |
1087 | | - * |
| 1089 | + * |
1088 | 1090 | * @return The response information returned from Vault |
1089 | 1091 | * @throws VaultException If any error occurs, or unexpected response received from Vault |
1090 | 1092 | */ |
1091 | 1093 | public LookupResponse lookupSelf() throws VaultException { |
1092 | 1094 | return lookupSelf("token"); |
1093 | 1095 | } |
1094 | | - |
| 1096 | + |
1095 | 1097 | /** |
1096 | 1098 | * <p>Returns information about the current client token.</p> |
1097 | | - * |
| 1099 | + * |
1098 | 1100 | * @param tokenAuthMount The mount name of the token authentication back end. If null, defaults to "token" |
1099 | 1101 | * @return The response information returned from Vault |
1100 | 1102 | * @throws VaultException If any error occurs, or unexpected response received from Vault |
@@ -1142,6 +1144,68 @@ public LookupResponse lookupSelf(final String tokenAuthMount) throws VaultExcept |
1142 | 1144 | } |
1143 | 1145 | } |
1144 | 1146 |
|
| 1147 | + /** |
| 1148 | + * <p>Returns information about the current client token for a wrapped token, for which the lookup endpoint is |
| 1149 | + * different at "sys/wrapping/lookup". Example usage:</p> |
| 1150 | + * |
| 1151 | + * <blockquote> |
| 1152 | + * <pre>{@code |
| 1153 | + * final String wrappingToken = "..."; |
| 1154 | + * final VaultConfig config = new VaultConfig().address(...).token(wrappingToken).build(); |
| 1155 | + * final Vault vault = new Vault(config); |
| 1156 | + * final LogicalResponse response = vault.auth().lookupWarp(); |
| 1157 | + * // Then you can validate "path" for example ... |
| 1158 | + * final String path = response.getData().get("path"); |
| 1159 | + * }</pre> |
| 1160 | + * </blockquote> |
| 1161 | + * |
| 1162 | + * @return The response information returned from Vault |
| 1163 | + * @throws VaultException If any error occurs, or unexpected response received from Vault |
| 1164 | + */ |
| 1165 | + public LogicalResponse lookupWrap() throws VaultException { |
| 1166 | + int retryCount = 0; |
| 1167 | + while (true) { |
| 1168 | + try { |
| 1169 | + // HTTP request to Vault |
| 1170 | + final RestResponse restResponse = new Rest()//NOPMD |
| 1171 | + .url(config.getAddress() + "/v1/sys/wrapping/lookup") |
| 1172 | + .header("X-Vault-Token", config.getToken()) |
| 1173 | + .connectTimeoutSeconds(config.getOpenTimeout()) |
| 1174 | + .readTimeoutSeconds(config.getReadTimeout()) |
| 1175 | + .sslVerification(config.getSslConfig().isVerify()) |
| 1176 | + .sslContext(config.getSslConfig().getSslContext()) |
| 1177 | + .get(); |
| 1178 | + // Validate restResponse |
| 1179 | + if (restResponse.getStatus() != 200) { |
| 1180 | + throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), |
| 1181 | + restResponse.getStatus()); |
| 1182 | + } |
| 1183 | + final String mimeType = restResponse.getMimeType(); |
| 1184 | + if (mimeType == null || !"application/json".equals(mimeType)) { |
| 1185 | + throw new VaultException("Vault responded with MIME type: " + mimeType, restResponse.getStatus()); |
| 1186 | + } |
| 1187 | + return new LogicalResponse(restResponse, retryCount); |
| 1188 | + } catch (Exception e) { |
| 1189 | + // If there are retries to perform, then pause for the configured interval and then execute the loop |
| 1190 | + // again... |
| 1191 | + if (retryCount < config.getMaxRetries()) { |
| 1192 | + retryCount++; |
| 1193 | + try { |
| 1194 | + final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds(); |
| 1195 | + Thread.sleep(retryIntervalMilliseconds); |
| 1196 | + } catch (InterruptedException e1) { |
| 1197 | + e1.printStackTrace(); //NOPMD |
| 1198 | + } |
| 1199 | + } else if (e instanceof VaultException) { //NOPMD |
| 1200 | + // ... otherwise, give up. |
| 1201 | + throw (VaultException) e; |
| 1202 | + } else { |
| 1203 | + throw new VaultException(e); |
| 1204 | + } |
| 1205 | + } |
| 1206 | + } |
| 1207 | + } |
| 1208 | + |
1145 | 1209 | /** |
1146 | 1210 | * <p>Revokes current client token.</p> |
1147 | 1211 | * |
|
0 commit comments