mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
test(ec2): share IMDS credential and restoration setup (#5470)
This commit is contained in:
1 parent
b86268e459
commit
b42d9cc007
3 files changed
+88
-121
No files matched your search
+22
-58
@@ -4,23 +4,16 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import jakarta.inject.Inject;
|
||||
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.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -51,28 +44,18 @@ class Ec2InstanceCredentialsIntegrationTest {
|
||||
.formParam("Action", "AddRoleToInstanceProfile").formParam("InstanceProfileName", profile)
|
||||
.formParam("RoleName", role).post("/").then().statusCode(200);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
int port;
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
port = socket.getLocalPort();
|
||||
}
|
||||
when(config.services().ec2().imdsPort()).thenReturn(port);
|
||||
Vertx vertx = Vertx.vertx();
|
||||
Ec2MetadataServer server = new Ec2MetadataServer(vertx, config, iam);
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId("i-credentials");
|
||||
instance.setIamInstanceProfileArn("arn:aws:iam::" + account + ":instance-profile/" + profile);
|
||||
String endpoint = "http://127.0.0.1:" + port;
|
||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||
server.start().get(10, TimeUnit.SECONDS);
|
||||
server.registerContainer("127.0.0.1", instance.getInstanceId(), instance);
|
||||
HttpResponse<String> token = client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
.timeout(Duration.ofSeconds(5)).header("X-aws-ec2-metadata-token-ttl-seconds", "60")
|
||||
.PUT(HttpRequest.BodyPublishers.noBody()).build(), HttpResponse.BodyHandlers.ofString());
|
||||
try (Ec2MetadataTestFixture fixture = new Ec2MetadataTestFixture()) {
|
||||
fixture.start(iam, config, instance);
|
||||
Ec2MetadataServer server = fixture.server();
|
||||
HttpResponse<String> token = fixture.putToken("60");
|
||||
assertEquals(200, token.statusCode());
|
||||
String path = "/latest/meta-data/iam/security-credentials/";
|
||||
assertEquals(role, get(client, endpoint + path, token.body()).body());
|
||||
assertEquals(404, get(client, endpoint + path + "wrong-role", token.body()).statusCode());
|
||||
HttpResponse<String> response = get(client, endpoint + path + role, token.body());
|
||||
assertEquals(role, fixture.get(path, token.body()).body());
|
||||
assertEquals(404, fixture.get(path + "wrong-role", token.body()).statusCode());
|
||||
HttpResponse<String> response = fixture.get(path + role, token.body());
|
||||
assertEquals(200, response.statusCode());
|
||||
JsonObject credential = new JsonObject(response.body());
|
||||
assertEquals("Success", credential.getString("Code"));
|
||||
@@ -84,18 +67,16 @@ class Ec2InstanceCredentialsIntegrationTest {
|
||||
assertEquals(account, iam.resolveAccountId(key).orElseThrow());
|
||||
assertEquals("arn:aws:sts::" + account + ":assumed-role/" + role + "/i-credentials",
|
||||
iam.resolveCallerArn(key).orElseThrow());
|
||||
assertEquals(response.body(), get(client, endpoint + path + role, token.body()).body());
|
||||
assertEquals(response.body(), fixture.get(path + role, token.body()).body());
|
||||
if (stopServer) {
|
||||
server.stop();
|
||||
} else {
|
||||
server.unregisterInstance(instance);
|
||||
assertNotEquals(200, get(client, endpoint + path + role, token.body()).statusCode());
|
||||
assertNotEquals(200, fixture.get(path + role, token.body()).statusCode());
|
||||
}
|
||||
assertTrue(iam.findSecretKey(key, sessionToken).isEmpty());
|
||||
assertTrue(server.registeredContainer("127.0.0.1").isEmpty());
|
||||
} finally {
|
||||
server.stop();
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
given().header("Authorization", auth).contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "RemoveRoleFromInstanceProfile").formParam("InstanceProfileName", profile)
|
||||
.formParam("RoleName", role).post("/").then().statusCode(200);
|
||||
@@ -111,38 +92,28 @@ class Ec2InstanceCredentialsIntegrationTest {
|
||||
@Test
|
||||
void identityCredentialsIdentifyTheLaunchingAccountWithoutAnInstanceProfile() throws Exception {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
int port;
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
port = socket.getLocalPort();
|
||||
}
|
||||
when(config.services().ec2().imdsPort()).thenReturn(port);
|
||||
when(config.defaultAccountId()).thenReturn("123456789012");
|
||||
Vertx vertx = Vertx.vertx();
|
||||
Ec2MetadataServer server = new Ec2MetadataServer(vertx, config, iam);
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId("i-identity");
|
||||
instance.setRegion("us-east-1");
|
||||
instance.setOwnerId("246813579012");
|
||||
String endpoint = "http://127.0.0.1:" + port;
|
||||
String base = endpoint + "/latest/meta-data/identity-credentials";
|
||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||
server.start().get(10, TimeUnit.SECONDS);
|
||||
server.registerContainer("127.0.0.1", instance.getInstanceId(), instance);
|
||||
HttpResponse<String> token = client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
.timeout(Duration.ofSeconds(5)).header("X-aws-ec2-metadata-token-ttl-seconds", "60")
|
||||
.PUT(HttpRequest.BodyPublishers.noBody()).build(), HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals("ec2/", get(client, base, token.body()).body());
|
||||
assertEquals("ec2/", get(client, base + "/", token.body()).body());
|
||||
assertEquals("info\nsecurity-credentials/", get(client, base + "/ec2/", token.body()).body());
|
||||
assertEquals("ec2-instance", get(client, base + "/ec2/security-credentials/", token.body()).body());
|
||||
String base = "/latest/meta-data/identity-credentials";
|
||||
try (Ec2MetadataTestFixture fixture = new Ec2MetadataTestFixture()) {
|
||||
fixture.start(iam, config, instance);
|
||||
Ec2MetadataServer server = fixture.server();
|
||||
HttpResponse<String> token = fixture.putToken("60");
|
||||
assertEquals("ec2/", fixture.get(base, token.body()).body());
|
||||
assertEquals("ec2/", fixture.get(base + "/", token.body()).body());
|
||||
assertEquals("info\nsecurity-credentials/", fixture.get(base + "/ec2/", token.body()).body());
|
||||
assertEquals("ec2-instance", fixture.get(base + "/ec2/security-credentials/", token.body()).body());
|
||||
|
||||
JsonObject info = new JsonObject(get(client, base + "/ec2/info", token.body()).body());
|
||||
JsonObject info = new JsonObject(fixture.get(base + "/ec2/info", token.body()).body());
|
||||
assertEquals("Success", info.getString("Code"));
|
||||
assertEquals("246813579012", info.getString("AccountId"));
|
||||
assertNotNull(Instant.parse(info.getString("LastUpdated")));
|
||||
|
||||
String path = base + "/ec2/security-credentials/ec2-instance";
|
||||
HttpResponse<String> response = get(client, path, token.body());
|
||||
HttpResponse<String> response = fixture.get(path, token.body());
|
||||
assertEquals(200, response.statusCode());
|
||||
JsonObject credential = new JsonObject(response.body());
|
||||
assertEquals("Success", credential.getString("Code"));
|
||||
@@ -158,20 +129,13 @@ class Ec2InstanceCredentialsIntegrationTest {
|
||||
assertEquals("246813579012:aws:ec2-instance:i-identity", iam.resolveCallerUserId(key, sessionToken).orElseThrow());
|
||||
List<String> grants = iam.resolveCallerContext(key).identityPolicies();
|
||||
assertTrue(grants == null || grants.isEmpty());
|
||||
assertEquals(response.body(), get(client, path, token.body()).body());
|
||||
assertEquals(response.body(), fixture.get(path, token.body()).body());
|
||||
|
||||
assertEquals(401, get(client, path, "not-a-token").statusCode());
|
||||
assertEquals(401, fixture.get(path, "not-a-token").statusCode());
|
||||
|
||||
server.unregisterInstance(instance);
|
||||
assertTrue(iam.findSecretKey(key, sessionToken).isEmpty());
|
||||
} finally {
|
||||
server.stop();
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
private static HttpResponse<String> get(HttpClient client, String url, String token) throws Exception {
|
||||
return client.send(HttpRequest.newBuilder(URI.create(url)).timeout(Duration.ofSeconds(5))
|
||||
.header("X-aws-ec2-metadata-token", token).GET().build(), HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ 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.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import io.vertx.core.Vertx;
|
||||
|
||||
@@ -10,6 +11,8 @@ import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
@@ -30,17 +33,31 @@ final class Ec2MetadataTestFixture implements AutoCloseable {
|
||||
private String endpoint;
|
||||
|
||||
void start() throws Exception {
|
||||
start(mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), null, instance, clock);
|
||||
}
|
||||
|
||||
void start(IamService iam, EmulatorConfig config, Instance registeredInstance) throws Exception {
|
||||
start(config, iam, registeredInstance, Clock.systemUTC());
|
||||
}
|
||||
|
||||
void startUnregistered(EmulatorConfig config) throws Exception {
|
||||
start(config, null, null, Clock.systemUTC());
|
||||
}
|
||||
|
||||
private void start(EmulatorConfig config, IamService iam, Instance registeredInstance,
|
||||
Clock serverClock) 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 = new Ec2MetadataServer(vertx, config, iam, serverClock);
|
||||
server.start().get(10, TimeUnit.SECONDS);
|
||||
endpoint = "http://127.0.0.1:" + port;
|
||||
server.registerContainer(CALLER_IP, instance.getInstanceId(), instance);
|
||||
if (registeredInstance != null) {
|
||||
server.registerContainer(CALLER_IP, registeredInstance.getInstanceId(), registeredInstance);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,12 +95,17 @@ final class Ec2MetadataTestFixture implements AutoCloseable {
|
||||
|
||||
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(),
|
||||
.timeout(Duration.ofSeconds(5)).header(TTL_HEADER, ttl).PUT(HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
HttpResponse<String> get(String path, String token) throws Exception {
|
||||
return get(path, token, 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));
|
||||
HttpRequest.Builder request = HttpRequest.newBuilder(URI.create(endpoint + path))
|
||||
.timeout(Duration.ofSeconds(5));
|
||||
if (token != null) {
|
||||
request.header(TOKEN_HEADER, token);
|
||||
}
|
||||
|
||||
+39
-58
@@ -17,18 +17,11 @@ import io.github.hectorvent.floci.core.common.docker.PortAllocator;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.vertx.core.Vertx;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -37,58 +30,46 @@ class Ec2MultiNetworkMetadataIntegrationTest {
|
||||
@Test
|
||||
void restoredGuestCanReadMetadataThroughItsSharedNetworkAddress() throws Exception {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
int port;
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
port = socket.getLocalPort();
|
||||
}
|
||||
when(config.services().ec2().imdsPort()).thenReturn(port);
|
||||
Vertx vertx = Vertx.vertx();
|
||||
Ec2MetadataServer server = new Ec2MetadataServer(vertx, config, null);
|
||||
DockerClient docker = mock(DockerClient.class);
|
||||
InspectContainerCmd command = mock(InspectContainerCmd.class);
|
||||
InspectContainerResponse response = mock(InspectContainerResponse.class);
|
||||
NetworkSettings settings = mock(NetworkSettings.class);
|
||||
when(docker.inspectContainerCmd("guest")).thenReturn(command);
|
||||
when(command.exec()).thenReturn(response);
|
||||
when(response.getNetworkSettings()).thenReturn(settings);
|
||||
Map<String, ContainerNetwork> networks = new LinkedHashMap<>();
|
||||
networks.put("vpc", new ContainerNetwork().withIpv4Address("10.0.1.10"));
|
||||
networks.put("bridge", new ContainerNetwork().withIpv4Address("172.17.0.4"));
|
||||
// Use loopback for the actual HTTP client's source, standing in for the shared network.
|
||||
networks.put("shared", new ContainerNetwork().withIpv4Address("127.0.0.1"));
|
||||
when(settings.getNetworks()).thenReturn(networks);
|
||||
ContainerLifecycleManager lifecycle = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycle.isContainerRunning("guest")).thenReturn(true);
|
||||
Ec2ContainerManager manager = new Ec2ContainerManager(mock(ContainerBuilder.class), lifecycle,
|
||||
mock(ContainerLogStreamer.class), mock(ContainerDetector.class), mock(DockerHostResolver.class),
|
||||
docker, mock(PortAllocator.class), config, server, mock(Ec2PortForwardManager.class),
|
||||
mock(RegionResolver.class), mock(ContainerNetworkReachability.class), mock(VpcNetworkManager.class),
|
||||
mock(ContainerReachableEndpoint.class));
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId("i-multinetwork");
|
||||
instance.setDockerContainerId("guest");
|
||||
instance.setUserData("#!/bin/sh\necho metadata\n");
|
||||
String endpoint = "http://127.0.0.1:" + port;
|
||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||
server.start().get(10, TimeUnit.SECONDS);
|
||||
assertTrue(manager.restoreMetadataRegistration(instance));
|
||||
HttpResponse<String> token = client.send(HttpRequest.newBuilder(URI.create(endpoint + "/latest/api/token"))
|
||||
.timeout(Duration.ofSeconds(5)).header("X-aws-ec2-metadata-token-ttl-seconds", "60")
|
||||
.PUT(HttpRequest.BodyPublishers.noBody()).build(), HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(200, token.statusCode());
|
||||
for (Map.Entry<String, String> entry : Map.of("meta-data/instance-id", "i-multinetwork",
|
||||
"user-data", instance.getUserData()).entrySet()) {
|
||||
HttpResponse<String> metadata = client.send(HttpRequest.newBuilder(
|
||||
URI.create(endpoint + "/latest/" + entry.getKey()))
|
||||
.timeout(Duration.ofSeconds(5)).header("X-aws-ec2-metadata-token", token.body())
|
||||
.GET().build(), HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(200, metadata.statusCode());
|
||||
assertEquals(entry.getValue(), metadata.body());
|
||||
try (Ec2MetadataTestFixture fixture = new Ec2MetadataTestFixture()) {
|
||||
fixture.startUnregistered(config);
|
||||
Ec2MetadataServer server = fixture.server();
|
||||
DockerClient docker = mock(DockerClient.class);
|
||||
InspectContainerCmd command = mock(InspectContainerCmd.class);
|
||||
InspectContainerResponse response = mock(InspectContainerResponse.class);
|
||||
NetworkSettings settings = mock(NetworkSettings.class);
|
||||
when(docker.inspectContainerCmd("guest")).thenReturn(command);
|
||||
when(command.exec()).thenReturn(response);
|
||||
when(response.getNetworkSettings()).thenReturn(settings);
|
||||
Map<String, ContainerNetwork> networks = new LinkedHashMap<>();
|
||||
networks.put("vpc", new ContainerNetwork().withIpv4Address("10.0.1.10"));
|
||||
networks.put("bridge", new ContainerNetwork().withIpv4Address("172.17.0.4"));
|
||||
// Use loopback for the actual HTTP client's source, standing in for the shared network.
|
||||
networks.put("shared", new ContainerNetwork().withIpv4Address("127.0.0.1"));
|
||||
when(settings.getNetworks()).thenReturn(networks);
|
||||
ContainerLifecycleManager lifecycle = mock(ContainerLifecycleManager.class);
|
||||
when(lifecycle.isContainerRunning("guest")).thenReturn(true);
|
||||
Ec2ContainerManager manager = new Ec2ContainerManager(mock(ContainerBuilder.class), lifecycle,
|
||||
mock(ContainerLogStreamer.class), mock(ContainerDetector.class), mock(DockerHostResolver.class),
|
||||
docker, mock(PortAllocator.class), config, server, mock(Ec2PortForwardManager.class),
|
||||
mock(RegionResolver.class), mock(ContainerNetworkReachability.class), mock(VpcNetworkManager.class),
|
||||
mock(ContainerReachableEndpoint.class));
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId("i-multinetwork");
|
||||
instance.setDockerContainerId("guest");
|
||||
instance.setUserData("#!/bin/sh\necho metadata\n");
|
||||
try {
|
||||
assertTrue(manager.restoreMetadataRegistration(instance));
|
||||
HttpResponse<String> token = fixture.putToken("60");
|
||||
assertEquals(200, token.statusCode());
|
||||
for (Map.Entry<String, String> entry : Map.of("meta-data/instance-id", "i-multinetwork",
|
||||
"user-data", instance.getUserData()).entrySet()) {
|
||||
HttpResponse<String> metadata = fixture.get("/latest/" + entry.getKey(), token.body());
|
||||
assertEquals(200, metadata.statusCode());
|
||||
assertEquals(entry.getValue(), metadata.body());
|
||||
}
|
||||
} finally {
|
||||
manager.stop();
|
||||
}
|
||||
} finally {
|
||||
manager.stop();
|
||||
server.stop();
|
||||
vertx.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user