forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
aws-fixture-utils
(elastic#119319)
Extracts some common utils for creating AWS service test fixtures out of the `s3-fixture` module and into a separate library independent of S3.
- Loading branch information
1 parent
f333a79
commit a4d4762
Showing
19 changed files
with
197 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
apply plugin: 'elasticsearch.java' | ||
|
||
description = 'Utils for AWS-related fixtures' | ||
|
||
dependencies { | ||
implementation project(':server') | ||
implementation project(':test:framework') | ||
} |
66 changes: 66 additions & 0 deletions
66
test/fixtures/aws-fixture-utils/src/main/java/fixture/aws/AwsCredentialsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package fixture.aws; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
|
||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.function.BiPredicate; | ||
import java.util.function.Supplier; | ||
|
||
import static fixture.aws.AwsFixtureUtils.sendError; | ||
|
||
public enum AwsCredentialsUtils { | ||
; | ||
|
||
/** | ||
* @return an authorization predicate that ensures the access key matches the given values. | ||
*/ | ||
public static BiPredicate<String, String> fixedAccessKey(String accessKey) { | ||
return mutableAccessKey(() -> accessKey); | ||
} | ||
|
||
/** | ||
* @return an authorization predicate that ensures the access key matches one supplied by the given supplier. | ||
*/ | ||
public static BiPredicate<String, String> mutableAccessKey(Supplier<String> accessKeySupplier) { | ||
return (authorizationHeader, sessionTokenHeader) -> authorizationHeader != null | ||
&& authorizationHeader.contains(accessKeySupplier.get()); | ||
} | ||
|
||
/** | ||
* @return an authorization predicate that ensures the access key and session token both match the given values. | ||
*/ | ||
public static BiPredicate<String, String> fixedAccessKeyAndToken(String accessKey, String sessionToken) { | ||
Objects.requireNonNull(sessionToken); | ||
final var accessKeyPredicate = fixedAccessKey(accessKey); | ||
return (authorizationHeader, sessionTokenHeader) -> accessKeyPredicate.test(authorizationHeader, sessionTokenHeader) | ||
&& sessionToken.equals(sessionTokenHeader); | ||
} | ||
|
||
/** | ||
* Check the authorization headers of the given {@param exchange} against the given {@param authorizationPredicate}. If they match, | ||
* returns {@code true}. If they do not match, sends a {@code 403 Forbidden} response and returns {@code false}. | ||
*/ | ||
public static boolean checkAuthorization(BiPredicate<String, String> authorizationPredicate, HttpExchange exchange) throws IOException { | ||
if (authorizationPredicate.test( | ||
exchange.getRequestHeaders().getFirst("Authorization"), | ||
exchange.getRequestHeaders().getFirst("x-amz-security-token") | ||
)) { | ||
return true; | ||
} | ||
|
||
sendError(exchange, RestStatus.FORBIDDEN, "AccessDenied", "Access denied by " + authorizationPredicate); | ||
return false; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
test/fixtures/aws-fixture-utils/src/main/java/fixture/aws/AwsFixtureUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package fixture.aws; | ||
|
||
import com.sun.net.httpserver.Headers; | ||
import com.sun.net.httpserver.HttpExchange; | ||
|
||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public enum AwsFixtureUtils { | ||
; | ||
|
||
/** | ||
* @return an {@link InetSocketAddress} for a test fixture running on {@code localhost} which binds to any available port. | ||
*/ | ||
public static InetSocketAddress getLocalFixtureAddress() { | ||
try { | ||
return new InetSocketAddress(InetAddress.getByName("localhost"), 0); | ||
} catch (UnknownHostException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Send an XML-formatted error response typical of an AWS service. | ||
*/ | ||
public static void sendError(final HttpExchange exchange, final RestStatus status, final String errorCode, final String message) | ||
throws IOException { | ||
final Headers headers = exchange.getResponseHeaders(); | ||
headers.add("Content-Type", "application/xml"); | ||
|
||
final String requestId = exchange.getRequestHeaders().getFirst("x-amz-request-id"); | ||
if (requestId != null) { | ||
headers.add("x-amz-request-id", requestId); | ||
} | ||
|
||
if (errorCode == null || "HEAD".equals(exchange.getRequestMethod())) { | ||
exchange.sendResponseHeaders(status.getStatus(), -1L); | ||
exchange.close(); | ||
} else { | ||
final byte[] response = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Error>" | ||
+ "<Code>" | ||
+ errorCode | ||
+ "</Code>" | ||
+ "<Message>" | ||
+ message | ||
+ "</Message>" | ||
+ "<RequestId>" | ||
+ requestId | ||
+ "</RequestId>" | ||
+ "</Error>").getBytes(StandardCharsets.UTF_8); | ||
exchange.sendResponseHeaders(status.getStatus(), response.length); | ||
exchange.getResponseBody().write(response); | ||
exchange.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.