|
7 | 7 | import com.bettercloud.vault.json.JsonObject; |
8 | 8 | import com.bettercloud.vault.response.AuthResponse; |
9 | 9 | import com.bettercloud.vault.response.LookupResponse; |
10 | | -import com.bettercloud.vault.rest.RestResponse; |
11 | 10 | import com.bettercloud.vault.rest.Rest; |
| 11 | +import com.bettercloud.vault.rest.RestResponse; |
12 | 12 | import lombok.Getter; |
13 | 13 |
|
14 | 14 | import java.io.Serializable; |
@@ -1196,4 +1196,111 @@ public void revokeSelf(final String tokenAuthMount) throws VaultException { |
1196 | 1196 | } |
1197 | 1197 | } |
1198 | 1198 |
|
| 1199 | + /** |
| 1200 | + * <p>Returns the original response inside the wrapped auth token. This method is useful if you need to unwrap a |
| 1201 | + * token without being authenticated. See {@link #unwrap(String)} if you need to do that authenticated.</p> |
| 1202 | + * |
| 1203 | + * <p>In the example below, you cannot use twice the {@code VaultConfig}, since |
| 1204 | + * after the first usage of the {@code wrappingToken}, it is not usable anymore. You need to use the |
| 1205 | + * {@code unwrappedToken} in a new vault configuration to continue. Example usage:</p> |
| 1206 | + * |
| 1207 | + * <blockquote> |
| 1208 | + * <pre>{@code |
| 1209 | + * final String wrappingToken = "..."; |
| 1210 | + * final VaultConfig config = new VaultConfig().address(...).token(wrappingToken).build(); |
| 1211 | + * final Vault vault = new Vault(config); |
| 1212 | + * final AuthResponse response = vault.auth().unwrap(); |
| 1213 | + * final String unwrappedToken = response.getAuthClientToken(); |
| 1214 | + * }</pre> |
| 1215 | + * </blockquote> |
| 1216 | + * |
| 1217 | + * @return The response information returned from Vault |
| 1218 | + * @throws VaultException If any error occurs, or unexpected response received from Vault |
| 1219 | + * @see #unwrap(String) |
| 1220 | + */ |
| 1221 | + public AuthResponse unwrap() throws VaultException { |
| 1222 | + return unwrap(null); |
| 1223 | + } |
| 1224 | + |
| 1225 | + /** |
| 1226 | + * <p>Returns the original response inside the given wrapped auth token. This method is useful if you need to unwrap |
| 1227 | + * a token, while being already authenticated. Do NOT authenticate in vault with your wrapping token, since it will |
| 1228 | + * both fail authentication and invalidate the wrapping token at the same time. See {@link #unwrap()} if you need to |
| 1229 | + * do that without being authenticated.</p> |
| 1230 | + * |
| 1231 | + * <p>In the example below, {@code authToken} is NOT your wrapped token, and should have unwrapping permissions. |
| 1232 | + * The unwrapped token in {@code unwrappedToken}. Example usage:</p> |
| 1233 | + * |
| 1234 | + * <blockquote> |
| 1235 | + * <pre>{@code |
| 1236 | + * final String authToken = "..."; |
| 1237 | + * final String wrappingToken = "..."; |
| 1238 | + * final VaultConfig config = new VaultConfig().address(...).token(authToken).build(); |
| 1239 | + * final Vault vault = new Vault(config); |
| 1240 | + * final AuthResponse response = vault.auth().unwrap(wrappingToken); |
| 1241 | + * final String unwrappedToken = response.getAuthClientToken(); |
| 1242 | + * }</pre> |
| 1243 | + * </blockquote> |
| 1244 | + * |
| 1245 | + * @param wrappedToken Specifies the wrapping token ID, do NOT also put this in your {@link VaultConfig#token}, |
| 1246 | + * if token is {@code null}, this method will unwrap the auth token in {@link VaultConfig#token} |
| 1247 | + * @return The response information returned from Vault |
| 1248 | + * @throws VaultException If any error occurs, or unexpected response received from Vault |
| 1249 | + * @see #unwrap() |
| 1250 | + */ |
| 1251 | + public AuthResponse unwrap(final String wrappedToken) throws VaultException { |
| 1252 | + int retryCount = 0; |
| 1253 | + while (true) { |
| 1254 | + try { |
| 1255 | + // Parse parameters to JSON |
| 1256 | + final JsonObject jsonObject = Json.object(); |
| 1257 | + if (wrappedToken != null) { |
| 1258 | + jsonObject.add("token", wrappedToken); |
| 1259 | + } |
| 1260 | + |
| 1261 | + final String requestJson = jsonObject.toString(); |
| 1262 | + final String url = config.getAddress() + "/v1/sys/wrapping/unwrap"; |
| 1263 | + |
| 1264 | + // HTTP request to Vault |
| 1265 | + final RestResponse restResponse = new Rest() |
| 1266 | + .url(url) |
| 1267 | + .header("X-Vault-Token", config.getToken()) |
| 1268 | + .body(requestJson.getBytes("UTF-8")) |
| 1269 | + .connectTimeoutSeconds(config.getOpenTimeout()) |
| 1270 | + .readTimeoutSeconds(config.getReadTimeout()) |
| 1271 | + .sslVerification(config.getSslConfig().isVerify()) |
| 1272 | + .sslContext(config.getSslConfig().getSslContext()) |
| 1273 | + .post(); |
| 1274 | + |
| 1275 | + // Validate restResponse |
| 1276 | + if (restResponse.getStatus() != 200) { |
| 1277 | + throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus(), |
| 1278 | + restResponse.getStatus()); |
| 1279 | + } |
| 1280 | + final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType(); |
| 1281 | + if (!mimeType.equals("application/json")) { |
| 1282 | + throw new VaultException("Vault responded with MIME type: " + mimeType, restResponse.getStatus()); |
| 1283 | + } |
| 1284 | + return new AuthResponse(restResponse, retryCount); |
| 1285 | + } catch (final Exception e) { |
| 1286 | + // If there are retries to perform, then pause for the configured interval and then execute the |
| 1287 | + // loop again... |
| 1288 | + if (retryCount < config.getMaxRetries()) { |
| 1289 | + retryCount++; |
| 1290 | + try { |
| 1291 | + final int retryIntervalMilliseconds = config.getRetryIntervalMilliseconds(); |
| 1292 | + Thread.sleep(retryIntervalMilliseconds); |
| 1293 | + } catch (InterruptedException e1) { |
| 1294 | + e1.printStackTrace(); |
| 1295 | + } |
| 1296 | + } else if (e instanceof VaultException) { |
| 1297 | + // ... otherwise, give up. |
| 1298 | + throw (VaultException) e; |
| 1299 | + } else { |
| 1300 | + throw new VaultException(e); |
| 1301 | + } |
| 1302 | + } |
| 1303 | + } |
| 1304 | + } |
| 1305 | + |
1199 | 1306 | } |
0 commit comments