mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test(ec2): share the instance metadata test fixture (#5466)
This commit is contained in:
1 parent
48d60e7929
commit
2e4cfe7ac5
3 files changed
+119
-120
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import io.vertx.core.Vertx;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
final class Ec2MetadataTestFixture implements AutoCloseable {
|
||||
|
||||
static final String TTL_HEADER = "X-aws-ec2-metadata-token-ttl-seconds";
|
||||
static final String TOKEN_HEADER = "X-aws-ec2-metadata-token";
|
||||
static final String CALLER_IP = "127.0.0.1";
|
||||
|
||||
private final MutableClock clock = new MutableClock();
|
||||
private final HttpClient client = HttpClient.newHttpClient();
|
||||
private final Instance instance = instance("i-0123456789abcdef0");
|
||||
private Vertx vertx;
|
||||
private Ec2MetadataServer server;
|
||||
private String endpoint;
|
||||
|
||||
void start() throws Exception {
|
||||
int port;
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
port = socket.getLocalPort();
|
||||
}
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ec2().imdsPort()).thenReturn(port);
|
||||
vertx = Vertx.vertx();
|
||||
server = new Ec2MetadataServer(vertx, config, null, clock);
|
||||
server.start().get(10, TimeUnit.SECONDS);
|
||||
endpoint = "http://127.0.0.1:" + port;
|
||||
server.registerContainer(CALLER_IP, instance.getInstanceId(), instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
try (client) {
|
||||
try {
|
||||
if (server != null) {
|
||||
server.stop();
|
||||
}
|
||||
} finally {
|
||||
if (vertx != null) {
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MutableClock clock() {
|
||||
return clock;
|
||||
}
|
||||
|
||||
Ec2MetadataServer server() {
|
||||
return server;
|
||||
}
|
||||
|
||||
Instance instance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static Instance instance(String instanceId) {
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId(instanceId);
|
||||
return instance;
|
||||
}
|
||||
|
||||
HttpResponse<String> putToken(String ttl) throws Exception {
|
||||
return client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
.header(TTL_HEADER, ttl).PUT(HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
<T> HttpResponse<T> get(String path, String token, HttpResponse.BodyHandler<T> bodyHandler) throws Exception {
|
||||
HttpRequest.Builder request = HttpRequest.newBuilder(URI.create(endpoint + path));
|
||||
if (token != null) {
|
||||
request.header(TOKEN_HEADER, token);
|
||||
}
|
||||
return client.send(request.GET().build(), bodyHandler);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import io.vertx.core.Vertx;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* IMDSv2 session tokens, as documented in the EC2 User Guide: the PUT must carry a TTL between
|
||||
@@ -30,58 +19,39 @@ import static org.mockito.Mockito.when;
|
||||
*/
|
||||
class Ec2MetadataTokenTest {
|
||||
|
||||
private static final String TTL_HEADER = "X-aws-ec2-metadata-token-ttl-seconds";
|
||||
private static final String TOKEN_HEADER = "X-aws-ec2-metadata-token";
|
||||
private static final String CALLER_IP = "127.0.0.1";
|
||||
|
||||
private final MutableClock clock = new MutableClock();
|
||||
private final HttpClient client = HttpClient.newHttpClient();
|
||||
private Vertx vertx;
|
||||
private Ec2MetadataTestFixture fixture;
|
||||
private Instance instance;
|
||||
private Ec2MetadataServer server;
|
||||
private String endpoint;
|
||||
|
||||
@BeforeEach
|
||||
void startServer() throws Exception {
|
||||
int port;
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
port = socket.getLocalPort();
|
||||
}
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ec2().imdsPort()).thenReturn(port);
|
||||
vertx = Vertx.vertx();
|
||||
server = new Ec2MetadataServer(vertx, config, null, clock);
|
||||
server.start().get(10, TimeUnit.SECONDS);
|
||||
endpoint = "http://127.0.0.1:" + port;
|
||||
|
||||
instance = instance("i-0123456789abcdef0");
|
||||
server.registerContainer(CALLER_IP, instance.getInstanceId(), instance);
|
||||
fixture = new Ec2MetadataTestFixture();
|
||||
fixture.start();
|
||||
instance = fixture.instance();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stopServer() throws Exception {
|
||||
server.stop();
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
fixture.close();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"0", "21601", "-1", "sixty", ""})
|
||||
void tokenRequestWithTtlOutsideOneToSixHoursIsRejected(String ttl) throws Exception {
|
||||
assertEquals(400, putToken(ttl).statusCode());
|
||||
assertEquals(400, fixture.putToken(ttl).statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenWithTheMaximumTtlIsIssued() throws Exception {
|
||||
HttpResponse<String> token = putToken("21600");
|
||||
HttpResponse<String> token = fixture.putToken("21600");
|
||||
|
||||
assertEquals(200, token.statusCode());
|
||||
assertEquals("21600", token.headers().firstValue(TTL_HEADER).orElseThrow());
|
||||
assertEquals("21600", token.headers().firstValue(Ec2MetadataTestFixture.TTL_HEADER).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenIsAcceptedUntilItsTtlElapses() throws Exception {
|
||||
String token = putToken("60").body();
|
||||
clock.advance(Duration.ofSeconds(59));
|
||||
String token = fixture.putToken("60").body();
|
||||
fixture.clock().advance(Duration.ofSeconds(59));
|
||||
|
||||
HttpResponse<String> response = getInstanceId(token);
|
||||
|
||||
@@ -91,8 +61,8 @@ class Ec2MetadataTokenTest {
|
||||
|
||||
@Test
|
||||
void expiredTokenIsRejectedAsUnauthorized() throws Exception {
|
||||
String token = putToken("60").body();
|
||||
clock.advance(Duration.ofSeconds(60));
|
||||
String token = fixture.putToken("60").body();
|
||||
fixture.clock().advance(Duration.ofSeconds(60));
|
||||
|
||||
assertEquals(401, getInstanceId(token).statusCode());
|
||||
}
|
||||
@@ -104,18 +74,18 @@ class Ec2MetadataTokenTest {
|
||||
|
||||
@Test
|
||||
void tokenPresentedFromAnotherInstanceIsRejectedAsUnauthorized() throws Exception {
|
||||
String token = putToken("60").body();
|
||||
Instance other = instance("i-0fedcba9876543210");
|
||||
server.unregisterContainer(CALLER_IP, instance);
|
||||
server.registerContainer(CALLER_IP, other.getInstanceId(), other);
|
||||
String token = fixture.putToken("60").body();
|
||||
Instance other = Ec2MetadataTestFixture.instance("i-0fedcba9876543210");
|
||||
fixture.server().unregisterContainer(Ec2MetadataTestFixture.CALLER_IP, instance);
|
||||
fixture.server().registerContainer(Ec2MetadataTestFixture.CALLER_IP, other.getInstanceId(), other);
|
||||
|
||||
assertEquals(401, getInstanceId(token).statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenIdentifiesItsInstanceWhenTheCallerIpIsNotRegistered() throws Exception {
|
||||
String token = putToken("60").body();
|
||||
server.unregisterContainer(CALLER_IP, instance);
|
||||
String token = fixture.putToken("60").body();
|
||||
fixture.server().unregisterContainer(Ec2MetadataTestFixture.CALLER_IP, instance);
|
||||
|
||||
HttpResponse<String> response = getInstanceId(token);
|
||||
|
||||
@@ -125,28 +95,12 @@ class Ec2MetadataTokenTest {
|
||||
|
||||
@Test
|
||||
void requestWithoutTokenStillUsesImdsV1() throws Exception {
|
||||
HttpResponse<String> response = client.send(
|
||||
HttpRequest.newBuilder(URI.create(endpoint + "/latest/meta-data/instance-id")).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
HttpResponse<String> response = getInstanceId(null);
|
||||
|
||||
assertEquals(200, response.statusCode());
|
||||
}
|
||||
|
||||
private static Instance instance(String instanceId) {
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId(instanceId);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private HttpResponse<String> putToken(String ttl) throws Exception {
|
||||
return client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
.header(TTL_HEADER, ttl).PUT(HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
private HttpResponse<String> getInstanceId(String token) throws Exception {
|
||||
return client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/meta-data/instance-id"))
|
||||
.header(TOKEN_HEADER, token).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
return fixture.get("/latest/meta-data/instance-id", token, HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
}
|
||||
@@ -1,64 +1,34 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import io.vertx.core.Vertx;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class Ec2MetadataUserDataTest {
|
||||
|
||||
private static final String TTL_HEADER = "X-aws-ec2-metadata-token-ttl-seconds";
|
||||
private static final String TOKEN_HEADER = "X-aws-ec2-metadata-token";
|
||||
private static final String CALLER_IP = "127.0.0.1";
|
||||
|
||||
private final MutableClock clock = new MutableClock();
|
||||
private final HttpClient client = HttpClient.newHttpClient();
|
||||
private Vertx vertx;
|
||||
private Ec2MetadataTestFixture fixture;
|
||||
private Instance instance;
|
||||
private Ec2MetadataServer server;
|
||||
private String endpoint;
|
||||
|
||||
@BeforeEach
|
||||
void startServer() throws Exception {
|
||||
int port;
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
port = socket.getLocalPort();
|
||||
}
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ec2().imdsPort()).thenReturn(port);
|
||||
vertx = Vertx.vertx();
|
||||
server = new Ec2MetadataServer(vertx, config, null, clock);
|
||||
server.start().get(10, TimeUnit.SECONDS);
|
||||
endpoint = "http://127.0.0.1:" + port;
|
||||
|
||||
instance = instance("i-0123456789abcdef0");
|
||||
server.registerContainer(CALLER_IP, instance.getInstanceId(), instance);
|
||||
fixture = new Ec2MetadataTestFixture();
|
||||
fixture.start();
|
||||
instance = fixture.instance();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stopServer() throws Exception {
|
||||
server.stop();
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
fixture.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,7 +53,7 @@ class Ec2MetadataUserDataTest {
|
||||
instance.setEncodedUserData(encoded);
|
||||
instance.setUserData(Ec2UserDataDecoder.decode(encoded));
|
||||
assertEquals(script, instance.getUserData());
|
||||
String token = putToken("60").body();
|
||||
String token = fixture.putToken("60").body();
|
||||
HttpResponse<byte[]> response = getUserData(token);
|
||||
assertEquals(200, response.statusCode());
|
||||
assertArrayEquals(bytes.toByteArray(), response.body());
|
||||
@@ -149,23 +119,6 @@ class Ec2MetadataUserDataTest {
|
||||
}
|
||||
|
||||
private HttpResponse<byte[]> getUserData(String token) throws Exception {
|
||||
HttpRequest.Builder request = HttpRequest.newBuilder(URI.create(endpoint + "/latest/user-data"));
|
||||
if (token != null) {
|
||||
request.header(TOKEN_HEADER, token);
|
||||
}
|
||||
return client.send(request.GET().build(), HttpResponse.BodyHandlers.ofByteArray());
|
||||
return fixture.get("/latest/user-data", token, HttpResponse.BodyHandlers.ofByteArray());
|
||||
}
|
||||
|
||||
private static Instance instance(String instanceId) {
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId(instanceId);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private HttpResponse<String> putToken(String ttl) throws Exception {
|
||||
return client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
.header(TTL_HEADER, ttl).PUT(HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user