Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): align AMI guest and launch template parity (#1597)
Jason van Zylfef03c6b0ff5
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.mdindex ff8806fe82..28c4b3db30 100644--- a/docs/configuration/environment-variables.md+++ b/docs/configuration/environment-variables.md@@ -81,6 +81,7 @@ See [Storage Modes](./storage.md) for a full explanation of each mode.|---|---|---|| `FLOCI_DOCKER_DOCKER_HOST` | `unix:///var/run/docker.sock` | Docker daemon socket path or TCP address || `FLOCI_DOCKER_DOCKER_CONFIG_PATH` | _(none)_ | Path to a directory containing Docker's `config.json` for registry auth |+| `FLOCI_DOCKER_IMAGE_REGISTRY_BASE` | _(none)_ | Optional registry/repository base for every Docker image Floci launches. When set, `postgres:16-alpine` resolves as `<base>/postgres:16-alpine` and `public.ecr.aws/docker/library/ubuntu:24.04` resolves as `<base>/public.ecr.aws/docker/library/ubuntu:24.04` || `FLOCI_DOCKER_LOG_MAX_SIZE` | `10m` | Log rotation max size for spawned containers (e.g. `10m`, `1g`) || `FLOCI_DOCKER_LOG_MAX_FILE` | `3` | Number of rotated log files to keep for spawned containers |diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 6dd1a5d38b..8ae0f3dc1c 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -1384,6 +1384,14 @@ public interface EmulatorConfig {@WithDefault("unix:///var/run/docker.sock")String dockerHost();+ /**+ * Optional registry/repository base for every Docker image Floci launches.+ * When set, images such as {@code postgres:16-alpine} and+ * {@code public.ecr.aws/docker/library/ubuntu:24.04} resolve under this+ * base before the container is created.+ */+ Optional<String> imageRegistryBase();+/*** Path to a directory containing Docker's config.json (e.g. /root/.docker).* When set, overrides the system default. Useful when Floci runs inside Dockerdiff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.javaindex 9482a651de..8a033ba280 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.java@@ -62,7 +62,40 @@ public class ContainerBuilder {* @return a new Builder instance*/public Builder newContainer(String image) {- return new Builder(image, config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver);+ return new Builder(resolveImage(image), config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver);+ }++ private String resolveImage(String image) {+ return resolveImage(image, configuredImageRegistryBase(config));+ }++ static String resolveImage(String image, Optional<String> imageRegistryBase) {+ if (image == null || image.isBlank()) {+ return image;+ }+ return normalizeImageRegistryBase(imageRegistryBase)+ .filter(base -> !image.startsWith(base + "/"))+ .map(base -> base + "/" + image)+ .orElse(image);+ }++ private static Optional<String> configuredImageRegistryBase(EmulatorConfig config) {+ if (config == null || config.docker() == null) {+ return Optional.empty();+ }+ return config.docker().imageRegistryBase();+ }++ private static Optional<String> normalizeImageRegistryBase(Optional<String> imageRegistryBase) {+ return imageRegistryBase+ .map(String::trim)+ .filter(value -> !value.isBlank())+ .map(value -> {+ while (value.endsWith("/")) {+ value = value.substring(0, value.length() - 1);+ }+ return value;+ });}/**diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.javaindex 922e257604..50cddfb693 100644--- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java+++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java@@ -394,7 +394,7 @@ public class AutoScalingReconciler {version.getSecurityGroupIds(),version.getInstanceTags(),version.getUserData(),- null,+ version.getIamInstanceProfileArn(),asg.getLaunchTemplateId(),asg.getLaunchTemplateName(),resolvedVersion);@@ -423,7 +423,7 @@ public class AutoScalingReconciler {version.getSecurityGroupIds(),version.getInstanceTags(),version.getUserData(),- null,+ version.getIamInstanceProfileArn(),specification.getLaunchTemplateId() == null? mixedLaunchTemplate.getLaunchTemplateId(): specification.getLaunchTemplateId(),diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.javaindex 8a3359bc24..c4b4b89326 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServer.java@@ -1,7 +1,9 @@package io.github.hectorvent.floci.services.ec2;import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.services.ec2.model.Instance;+import io.github.hectorvent.floci.services.iam.IamService;import io.vertx.core.Vertx;import io.vertx.core.http.HttpServer;import io.vertx.ext.web.Router;@@ -40,6 +42,7 @@ public class Ec2MetadataServer {private final Vertx vertx;private final EmulatorConfig config;+ private final IamService iamService;/** IMDSv2: token value → Instance */private final Map<String, Instance> tokenToInstance = new ConcurrentHashMap<>();@@ -49,9 +52,10 @@ public class Ec2MetadataServer {private volatile HttpServer httpServer;@Inject- public Ec2MetadataServer(Vertx vertx, EmulatorConfig config) {+ public Ec2MetadataServer(Vertx vertx, EmulatorConfig config, IamService iamService) {this.vertx = vertx;this.config = config;+ this.iamService = iamService;}/** Called by Ec2ContainerManager after a container starts to register its IP. */@@ -227,7 +231,7 @@ public class Ec2MetadataServer {ctx.response().setStatusCode(404).end();return;}- String roleName = extractRoleName(profileArn);+ String roleName = resolveRoleName(profileArn);ctx.response().setStatusCode(200).putHeader("content-type", "text/plain").end(roleName);@@ -350,7 +354,23 @@ public class Ec2MetadataServer {// ── Utilities ─────────────────────────────────────────────────────────────- private static String extractRoleName(String profileArn) {+ String resolveRoleName(String profileArn) {+ if (iamService != null) {+ String profileName = extractProfileName(profileArn);+ try {+ var profile = iamService.getInstanceProfile(profileName);+ if (profile.getRoleNames() != null && !profile.getRoleNames().isEmpty()) {+ return profile.getRoleNames().getFirst();+ }+ } catch (AwsException e) {+ LOG.debugf(e, "IMDS: instance profile %s unavailable; falling back to profile name", profileName);+ // Fall back to the profile name when only the EC2 profile ARN was modeled.+ }+ }+ return extractProfileName(profileArn);+ }++ private static String extractProfileName(String profileArn) {// arn:aws:iam::000000000000:instance-profile/my-roleint lastSlash = profileArn.lastIndexOf('/');if (lastSlash >= 0 && lastSlash < profileArn.length() - 1) {diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.javaindex 9a54ebfb83..d241622574 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java@@ -327,6 +327,24 @@ public class Ec2QueryHandler {}}+ LaunchTemplateData launchTemplateData = resolveRunInstancesLaunchTemplateData(p, region);+ if (launchTemplateData != null) {+ imageId = firstNonBlank(imageId, launchTemplateData.getImageId());+ instanceType = firstNonBlank(instanceType, launchTemplateData.getInstanceType());+ keyName = firstNonBlank(keyName, launchTemplateData.getKeyName());+ userData = firstNonBlank(userData, launchTemplateData.getUserData());+ iamInstanceProfileArn = firstNonBlank(iamInstanceProfileArn, launchTemplateData.getIamInstanceProfileArn());+ if (sgIds.isEmpty()) {+ sgIds = new ArrayList<>(launchTemplateData.getSecurityGroupIds());+ }+ if (!launchTemplateData.getInstanceTags().isEmpty()) {+ Map<String, Tag> mergedTags = new LinkedHashMap<>();+ launchTemplateData.getInstanceTags().forEach(tag -> mergedTags.put(tag.getKey(), tag));+ instanceTags.forEach(tag -> mergedTags.put(tag.getKey(), tag));+ instanceTags = new ArrayList<>(mergedTags.values());+ }+ }+Reservation res = service.runInstances(region, imageId, instanceType, minCount, maxCount,keyName, sgIds, subnetId, clientToken, instanceTags, userData, iamInstanceProfileArn);@@ -345,6 +363,20 @@ public class Ec2QueryHandler {return xmlResponse(xml.build());}+ private LaunchTemplateData resolveRunInstancesLaunchTemplateData(MultivaluedMap<String, String> p, String region) {+ String id = p.getFirst("LaunchTemplate.LaunchTemplateId");+ String name = p.getFirst("LaunchTemplate.LaunchTemplateName");+ String version = p.getFirst("LaunchTemplate.Version");+ if ((id == null || id.isBlank()) && (name == null || name.isBlank())) {+ return null;+ }+ return service.resolveLaunchTemplateData(region, id, name, version);+ }++ private static String firstNonBlank(String first, String fallback) {+ return first != null && !first.isBlank() ? first : fallback;+ }+private Response handleDescribeIamInstanceProfileAssociations(MultivaluedMap<String, String> p, String region) {List<String> associationIds = getList(p, "AssociationId");Map<String, List<String>> filters = getFilters(p);@@ -1505,6 +1537,7 @@ public class Ec2QueryHandler {p.getFirst("LaunchTemplateData.KeyName"),parseLaunchTemplateSecurityGroupIds(p),decodeUserData(p.getFirst("LaunchTemplateData.UserData")),+ resolveIamInstanceProfileArn(p, "LaunchTemplateData.IamInstanceProfile"),parseTagsForResource(p, "launch-template"),parseLaunchTemplateDataTagsForResource(p, "instance"));XmlBuilder xml = new XmlBuilder()@@ -1526,6 +1559,7 @@ public class Ec2QueryHandler {p.getFirst("LaunchTemplateData.KeyName"),parseLaunchTemplateSecurityGroupIds(p),decodeUserData(p.getFirst("LaunchTemplateData.UserData")),+ resolveIamInstanceProfileArn(p, "LaunchTemplateData.IamInstanceProfile"),parseLaunchTemplateDataTagsForResource(p, "instance"));XmlBuilder xml = new XmlBuilder().start("CreateLaunchTemplateVersionResponse", AwsNamespaces.EC2)@@ -1823,11 +1857,15 @@ public class Ec2QueryHandler {}private String resolveIamInstanceProfileArn(MultivaluedMap<String, String> p) {- String arn = p.getFirst("IamInstanceProfile.Arn");+ return resolveIamInstanceProfileArn(p, "IamInstanceProfile");+ }++ private String resolveIamInstanceProfileArn(MultivaluedMap<String, String> p, String prefix) {+ String arn = p.getFirst(prefix + ".Arn");if (arn != null && !arn.isBlank()) {return arn;}- String name = p.getFirst("IamInstanceProfile.Name");+ String name = p.getFirst(prefix + ".Name");if (name == null || name.isBlank()) {return null;}@@ -2042,6 +2080,11 @@ public class Ec2QueryHandler {if (launchTemplate.getUserData() != null) {xml.elem("userData", launchTemplate.getUserData());}+ if (launchTemplate.getIamInstanceProfileArn() != null) {+ xml.start("iamInstanceProfile")+ .elem("arn", launchTemplate.getIamInstanceProfileArn())+ .end("iamInstanceProfile");+ }xml.start("securityGroupIdSet");for (String securityGroupId : launchTemplate.getSecurityGroupIds()) {xml.elem("item", securityGroupId);diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.javaindex 0d6efc7255..4003e45075 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java@@ -1372,6 +1372,7 @@ public class Ec2Service {public LaunchTemplate createLaunchTemplate(String region, String name, String imageId,String instanceType, String keyName,List<String> securityGroupIds, String userData,+ String iamInstanceProfileArn,List<Tag> launchTemplateTags, List<Tag> instanceTags) {ensureDefaultResources(region);if (name == null || name.isBlank()) {@@ -1394,6 +1395,7 @@ public class Ec2Service {launchTemplate.setInstanceType(instanceType);launchTemplate.setKeyName(keyName);launchTemplate.setUserData(userData);+ launchTemplate.setIamInstanceProfileArn(iamInstanceProfileArn);if (securityGroupIds != null) {launchTemplate.setSecurityGroupIds(new ArrayList<>(securityGroupIds));}@@ -1413,6 +1415,7 @@ public class Ec2Service {String sourceVersion,String imageId, String instanceType, String keyName,List<String> securityGroupIds, String userData,+ String iamInstanceProfileArn,List<Tag> instanceTags) {ensureDefaultResources(region);LaunchTemplate launchTemplate = findLaunchTemplate(region, id, name);@@ -1433,6 +1436,9 @@ public class Ec2Service {if (userData != null && !userData.isBlank()) {data.setUserData(userData);}+ if (iamInstanceProfileArn != null && !iamInstanceProfileArn.isBlank()) {+ data.setIamInstanceProfileArn(iamInstanceProfileArn);+ }if (securityGroupIds != null && !securityGroupIds.isEmpty()) {data.setSecurityGroupIds(securityGroupIds);}@@ -1500,6 +1506,16 @@ public class Ec2Service {.collect(Collectors.toList());}+ public LaunchTemplateData resolveLaunchTemplateData(String region, String id, String name, String version) {+ ensureDefaultResources(region);+ LaunchTemplate launchTemplate = findLaunchTemplate(region, id, name);+ String resolvedVersion = resolveLaunchTemplateVersion(+ launchTemplate,+ version,+ launchTemplate.getDefaultVersionNumber());+ return new LaunchTemplateData(versionData(launchTemplate, resolvedVersion));+ }+public LaunchTemplate deleteLaunchTemplate(String region, String id, String name) {ensureDefaultResources(region);LaunchTemplate launchTemplate = findLaunchTemplate(region, id, name);@@ -1570,6 +1586,7 @@ public class Ec2Service {data.setInstanceType(launchTemplate.getInstanceType());data.setKeyName(launchTemplate.getKeyName());data.setUserData(launchTemplate.getUserData());+ data.setIamInstanceProfileArn(launchTemplate.getIamInstanceProfileArn());data.setSecurityGroupIds(launchTemplate.getSecurityGroupIds());data.setInstanceTags(launchTemplate.getInstanceTags());return data;@@ -1580,6 +1597,7 @@ public class Ec2Service {launchTemplate.setInstanceType(data.getInstanceType());launchTemplate.setKeyName(data.getKeyName());launchTemplate.setUserData(data.getUserData());+ launchTemplate.setIamInstanceProfileArn(data.getIamInstanceProfileArn());launchTemplate.setSecurityGroupIds(new ArrayList<>(data.getSecurityGroupIds()));launchTemplate.setInstanceTags(data.getInstanceTags());}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplate.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplate.javaindex 91f9d9d39f..24003ef002 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplate.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplate.java@@ -24,6 +24,7 @@ public class LaunchTemplate {private String instanceType;private String keyName;private String userData;+ private String iamInstanceProfileArn;private List<String> securityGroupIds = new ArrayList<>();private List<Tag> tags = new ArrayList<>();private List<Tag> instanceTags = new ArrayList<>();@@ -64,6 +65,9 @@ public class LaunchTemplate {public String getUserData() { return userData; }public void setUserData(String userData) { this.userData = userData; }+ public String getIamInstanceProfileArn() { return iamInstanceProfileArn; }+ public void setIamInstanceProfileArn(String iamInstanceProfileArn) { this.iamInstanceProfileArn = iamInstanceProfileArn; }+public List<String> getSecurityGroupIds() { return securityGroupIds; }public void setSecurityGroupIds(List<String> securityGroupIds) { this.securityGroupIds = securityGroupIds; }diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.javaindex e8963bee2c..5b340e3f72 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.java@@ -14,6 +14,7 @@ public class LaunchTemplateData {private String instanceType;private String keyName;private String userData;+ private String iamInstanceProfileArn;private List<String> securityGroupIds = new ArrayList<>();private List<Tag> instanceTags = new ArrayList<>();@@ -24,6 +25,7 @@ public class LaunchTemplateData {this.instanceType = source.instanceType;this.keyName = source.keyName;this.userData = source.userData;+ this.iamInstanceProfileArn = source.iamInstanceProfileArn;this.securityGroupIds = new ArrayList<>(source.securityGroupIds);this.instanceTags = new ArrayList<>(source.instanceTags);}@@ -40,6 +42,9 @@ public class LaunchTemplateData {public String getUserData() { return userData; }public void setUserData(String userData) { this.userData = userData; }+ public String getIamInstanceProfileArn() { return iamInstanceProfileArn; }+ public void setIamInstanceProfileArn(String iamInstanceProfileArn) { this.iamInstanceProfileArn = iamInstanceProfileArn; }+public List<String> getSecurityGroupIds() { return securityGroupIds; }public void setSecurityGroupIds(List<String> securityGroupIds) {this.securityGroupIds = securityGroupIds != null ? new ArrayList<>(securityGroupIds) : new ArrayList<>();diff --git a/src/main/java/io/github/hectorvent/floci/tools/ami/AmiImageTool.java b/src/main/java/io/github/hectorvent/floci/tools/ami/AmiImageTool.javaindex cc407f071e..70e86fbd5a 100644--- a/src/main/java/io/github/hectorvent/floci/tools/ami/AmiImageTool.java+++ b/src/main/java/io/github/hectorvent/floci/tools/ami/AmiImageTool.java@@ -23,6 +23,7 @@ import java.util.Objects;public final class AmiImageTool {public static final Path DEFAULT_METADATA = Path.of("docker/ec2/ami-images/image-build-metadata.yaml");public static final Path DEFAULT_OUTPUT = Path.of("target/ami-images");+ private static final String NETWORKD_WAIT_ONLINE_DROP_IN = "systemd-networkd-wait-online-floci.conf";private static final ObjectMapper YAML = new ObjectMapper(new YAMLFactory());private static final HttpClient HTTP = HttpClient.newHttpClient();@@ -100,6 +101,7 @@ public final class AmiImageTool {}if (write) {Files.writeString(context.resolve("Dockerfile"), dockerfile(image), StandardCharsets.UTF_8);+ Files.writeString(context.resolve(NETWORKD_WAIT_ONLINE_DROP_IN), networkdWaitOnlineDropIn(), StandardCharsets.UTF_8);Files.writeString(context.resolve("README.md"), readme(image), StandardCharsets.UTF_8);YAML.writerWithDefaultPrettyPrinter().writeValue(context.resolve("provenance.yaml").toFile(), provenance);}@@ -162,10 +164,24 @@ public final class AmiImageTool {LABEL io.floci.ami.catalog-image-id="%s"LABEL io.floci.ami.architecture="%s"ADD %s /+ COPY %s /etc/systemd/system/systemd-networkd-wait-online.service.d/floci.conf+ RUN set -eux; \\+ getent group ubuntu >/dev/null || groupadd --gid 1000 ubuntu; \\+ id ubuntu >/dev/null 2>&1 || useradd --uid 1000 --gid ubuntu --groups sudo --create-home --shell /bin/bash ubuntu; \\+ install -d -o ubuntu -g ubuntu /home/ubuntuENV container=dockerSTOPSIGNAL SIGRTMIN+3CMD ["/sbin/init"]- """.formatted(image.canonical.rootfsUrl(), image.catalogImageId, image.architecture, image.canonical.rootfs);+ """.formatted(image.canonical.rootfsUrl(), image.catalogImageId, image.architecture, image.canonical.rootfs,+ NETWORKD_WAIT_ONLINE_DROP_IN);+ }++ static String networkdWaitOnlineDropIn() {+ return """+ [Service]+ ExecStart=+ ExecStart=/bin/true+ """;}static String readme(ImageSpec image) {diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.javaindex d25d39133e..3b09b25b56 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.java@@ -98,6 +98,31 @@ class ContainerBuilderTest {assertEquals("host", spec.cgroupnsMode());}+ @Test+ void imageRegistryBasePrefixesEveryContainerImage() {+ TestFixture fixture = new TestFixture();+ when(fixture.docker.imageRegistryBase()).thenReturn(Optional.of("ghcr.io/floci-io/mirror/"));++ assertEquals(+ "ghcr.io/floci-io/mirror/postgres:16-alpine",+ fixture.builder.newContainer("postgres:16-alpine").build().image());+ assertEquals(+ "ghcr.io/floci-io/mirror/public.ecr.aws/docker/library/ubuntu:24.04",+ fixture.builder.newContainer("public.ecr.aws/docker/library/ubuntu:24.04").build().image());+ }++ @Test+ void imageRegistryBaseDoesNotDoublePrefixImagesAlreadyUnderBase() {+ TestFixture fixture = new TestFixture();+ when(fixture.docker.imageRegistryBase()).thenReturn(Optional.of("ghcr.io/floci-io/mirror"));++ ContainerSpec spec = fixture.builder+ .newContainer("ghcr.io/floci-io/mirror/floci/ami-ubuntu:24.04-arm64")+ .build();++ assertEquals("ghcr.io/floci-io/mirror/floci/ami-ubuntu:24.04-arm64", spec.image());+ }+private static class TestFixture {final EmulatorConfig config = mock(EmulatorConfig.class);final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);@@ -116,6 +141,7 @@ class ContainerBuilderTest {when(config.docker()).thenReturn(docker);when(docker.logMaxSize()).thenReturn("10m");when(docker.logMaxFile()).thenReturn("3");+ when(docker.imageRegistryBase()).thenReturn(Optional.empty());when(config.dns()).thenReturn(dns);when(dns.containerFallbackEnabled()).thenReturn(true);when(dns.containerFallbackServers()).thenReturn(List.of("8.8.8.8", "8.8.4.4"));diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.javaindex 744e504ca5..8d36e17338 100644--- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.java@@ -75,6 +75,7 @@ class AutoScalingReconcilerTest {version.setLatestVersionNumber("1");version.setImageId("ami-version-1");version.setInstanceType("t3.micro");+ version.setIamInstanceProfileArn("arn:aws:iam::000000000000:instance-profile/app-profile");List<Tag> instanceTags = List.of(new Tag("app.ClusterId", "development"));version.setInstanceTags(instanceTags);when(ec2Service.describeLaunchTemplates("us-east-1", List.of("lt-123"), List.of(), Map.of()))@@ -87,7 +88,7 @@ class AutoScalingReconcilerTest {reservation.setInstances(List.of(ec2Instance));when(ec2Service.runInstances(eq("us-east-1"), eq("ami-version-1"), eq("t3.micro"),eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),- eq(instanceTags), eq(null), eq(null))).thenReturn(reservation);+ eq(instanceTags), eq(null), eq("arn:aws:iam::000000000000:instance-profile/app-profile"))).thenReturn(reservation);reconciler.reconcile(asg);@@ -97,7 +98,7 @@ class AutoScalingReconcilerTest {assertEquals("1", asg.getInstances().getFirst().getLaunchTemplateVersion());verify(ec2Service).runInstances(eq("us-east-1"), eq("ami-version-1"), eq("t3.micro"),eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),- eq(instanceTags), eq(null), eq(null));+ eq(instanceTags), eq(null), eq("arn:aws:iam::000000000000:instance-profile/app-profile"));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.javaindex cbaa5fb2fa..64eb4b1d97 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java@@ -764,13 +764,14 @@ class Ec2IntegrationTest {.formParam("LaunchTemplateData.ImageId", "ami-0abcdef1234567890").formParam("LaunchTemplateData.InstanceType", "t3.micro").formParam("LaunchTemplateData.KeyName", "test-key")+ .formParam("LaunchTemplateData.IamInstanceProfile.Name", "sample-profile").formParam("LaunchTemplateData.SecurityGroupId.1", securityGroupId).formParam("LaunchTemplateData.UserData", gzipBase64("#!/bin/sh\necho launch-template\n")).formParam("LaunchTemplateData.TagSpecification.1.ResourceType", "instance").formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Key", "example:ClusterId").formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Value", "sample-template").formParam("LaunchTemplateData.TagSpecification.1.Tag.2.Key", "example:NodeType")- .formParam("LaunchTemplateData.TagSpecification.1.Tag.2.Value", "COORDINATOR")+ .formParam("LaunchTemplateData.TagSpecification.1.Tag.2.Value", "PRIMARY").formParam("TagSpecification.1.ResourceType", "launch-template").formParam("TagSpecification.1.Tag.1.Key", "Name").formParam("TagSpecification.1.Tag.1.Value", "sample-template")@@ -822,12 +823,14 @@ class Ec2IntegrationTest {equalTo("t3.micro")).body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.userData",equalTo("#!/bin/sh\necho launch-template\n"))+ .body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.iamInstanceProfile.arn",+ equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile")).body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.resourceType",equalTo("instance")).body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:ClusterId' }.value",equalTo("sample-template")).body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",- equalTo("COORDINATOR"));+ equalTo("PRIMARY"));}@Test@@ -842,11 +845,12 @@ class Ec2IntegrationTest {.formParam("LaunchTemplateData.ImageId", "ami-0abcdef1234567890").formParam("LaunchTemplateData.InstanceType", "t3.small").formParam("LaunchTemplateData.KeyName", "test-key")+ .formParam("LaunchTemplateData.IamInstanceProfile.Name", "sample-profile-v2").formParam("LaunchTemplateData.SecurityGroupId.1", securityGroupId).formParam("LaunchTemplateData.UserData", gzipBase64("#!/bin/sh\necho launch-template-version\n")).formParam("LaunchTemplateData.TagSpecification.1.ResourceType", "instance").formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Key", "example:NodeType")- .formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Value", "WORKER")+ .formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Value", "SECONDARY").header("Authorization", AUTH_HEADER).when().post("/")@@ -862,8 +866,10 @@ class Ec2IntegrationTest {equalTo("t3.small")).body("CreateLaunchTemplateVersionResponse.launchTemplateVersion.launchTemplateData.userData",equalTo("#!/bin/sh\necho launch-template-version\n"))+ .body("CreateLaunchTemplateVersionResponse.launchTemplateVersion.launchTemplateData.iamInstanceProfile.arn",+ equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile-v2")).body("CreateLaunchTemplateVersionResponse.launchTemplateVersion.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",- equalTo("WORKER"));+ equalTo("SECONDARY"));given().formParam("Action", "DescribeLaunchTemplateVersions")@@ -883,7 +889,7 @@ class Ec2IntegrationTest {.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.userData",equalTo("#!/bin/sh\necho launch-template\n")).body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",- equalTo("COORDINATOR"));+ equalTo("PRIMARY"));}@Test@@ -928,6 +934,55 @@ class Ec2IntegrationTest {@Test@Order(49)+ void runInstancesResolvesLaunchTemplateDefaultsWithRequestOverrides() {+ String launchedInstanceId = given()+ .formParam("Action", "RunInstances")+ .formParam("LaunchTemplate.LaunchTemplateId", launchTemplateId)+ .formParam("LaunchTemplate.Version", "$Default")+ .formParam("ImageId", "ami-sample-override")+ .formParam("InstanceType", "c7g.large")+ .formParam("MinCount", "1")+ .formParam("MaxCount", "1")+ .formParam("TagSpecification.1.ResourceType", "instance")+ .formParam("TagSpecification.1.Tag.1.Key", "example:NodeType")+ .formParam("TagSpecification.1.Tag.1.Value", "REQUEST")+ .formParam("TagSpecification.1.Tag.2.Key", "sample-name")+ .formParam("TagSpecification.1.Tag.2.Value", "sample-local")+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("RunInstancesResponse.instancesSet.item.imageId", equalTo("ami-sample-override"))+ .body("RunInstancesResponse.instancesSet.item.instanceType", equalTo("c7g.large"))+ .body("RunInstancesResponse.instancesSet.item.keyName", equalTo("test-key"))+ .body("RunInstancesResponse.instancesSet.item.iamInstanceProfile.arn",+ equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile-v2"))+ .body("RunInstancesResponse.instancesSet.item.groupSet.item.groupId", equalTo(securityGroupId))+ .body("RunInstancesResponse.instancesSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",+ equalTo("REQUEST"))+ .body("RunInstancesResponse.instancesSet.item.tagSet.item.findAll { it.key == 'example:NodeType' }.size()",+ equalTo(1))+ .body("RunInstancesResponse.instancesSet.item.tagSet.item.find { it.key == 'sample-name' }.value",+ equalTo("sample-local"))+ .extract().path("RunInstancesResponse.instancesSet.item.instanceId");++ given()+ .formParam("Action", "DescribeInstances")+ .formParam("InstanceId.1", launchedInstanceId)+ .header("Authorization", AUTH_HEADER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.instanceId",+ equalTo(launchedInstanceId))+ .body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.iamInstanceProfile.arn",+ equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile-v2"));+ }++ @Test+ @Order(50)void deleteLaunchTemplate() {given().formParam("Action", "DeleteLaunchTemplate")@@ -1570,8 +1625,7 @@ class Ec2IntegrationTest {.post("/").then().statusCode(200)- .body("DescribeTagsResponse.tagSet.item.key", equalTo("Name"))- .body("DescribeTagsResponse.tagSet.item.value", equalTo("test-instance"));+ .body("DescribeTagsResponse.tagSet.item.find { it.key == 'Name' }.value", equalTo("test-instance"));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServerTest.javaindex 951c197cff..7afafec836 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataServerTest.java@@ -2,12 +2,16 @@ package io.github.hectorvent.floci.services.ec2;import io.github.hectorvent.floci.services.ec2.model.Instance;import io.github.hectorvent.floci.services.ec2.model.Tag;+import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.iam.model.InstanceProfile;import org.junit.jupiter.api.Test;import java.util.List;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;class Ec2MetadataServerTest {@@ -50,7 +54,7 @@ class Ec2MetadataServerTest {@Testvoid staleContainerUnregisterDoesNotRemoveCurrentRegistration() {- Ec2MetadataServer server = new Ec2MetadataServer(null, null);+ Ec2MetadataServer server = new Ec2MetadataServer(null, null, null);Instance oldInstance = new Instance();oldInstance.setInstanceId("i-old");Instance currentInstance = new Instance();@@ -64,4 +68,18 @@ class Ec2MetadataServerTest {currentInstance,server.registeredContainer("192.168.215.7").orElseThrow());}++ @Test+ void iamCredentialRoleNameComesFromInstanceProfileRole() {+ IamService iamService = mock(IamService.class);+ InstanceProfile profile = new InstanceProfile();+ profile.setInstanceProfileName("sample-profile");+ profile.setRoleNames(List.of("sample-role"));+ when(iamService.getInstanceProfile("sample-profile")).thenReturn(profile);++ Ec2MetadataServer server = new Ec2MetadataServer(null, null, iamService);++ assertEquals("sample-role", server.resolveRoleName(+ "arn:aws:iam::000000000000:instance-profile/sample-profile"));+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.javaindex 921b4b6887..bfda192f53 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java@@ -62,10 +62,11 @@ class Ec2ServiceTest {new InMemoryStorageFactory());LaunchTemplate template = service.createLaunchTemplate("us-east-1", "app-template","ami-source", "t3.micro", "app-key", List.of("sg-source"),- "source-user-data", List.of(), List.of(new Tag("Role", "source")));+ "source-user-data", "arn:aws:iam::000000000000:instance-profile/app-profile",+ List.of(), List.of(new Tag("Role", "source")));service.createLaunchTemplateVersion("us-east-1", template.getLaunchTemplateId(), null,- "1", null, "t3.small", null, List.of(), null, List.of());+ "1", null, "t3.small", null, List.of(), null, null, List.of());LaunchTemplate version = service.describeLaunchTemplateVersions("us-east-1", template.getLaunchTemplateId(), null, List.of("2")).getFirst();@@ -74,6 +75,7 @@ class Ec2ServiceTest {assertEquals("app-key", version.getKeyName());assertEquals(List.of("sg-source"), version.getSecurityGroupIds());assertEquals("source-user-data", version.getUserData());+ assertEquals("arn:aws:iam::000000000000:instance-profile/app-profile", version.getIamInstanceProfileArn());assertEquals("2", version.getLatestVersionNumber());assertEquals(1, version.getInstanceTags().size());assertEquals("Role", version.getInstanceTags().getFirst().getKey());diff --git a/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.java b/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.javaindex 2544e45ee9..f8f6a87b68 100644--- a/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.java+++ b/src/test/java/io/github/hectorvent/floci/tools/ami/AmiImageToolTest.java@@ -62,6 +62,9 @@ class AmiImageToolTest {assertTrue(Files.readString(context.resolve("Dockerfile")).contains("FROM scratch"));assertTrue(Files.readString(context.resolve("Dockerfile")).contains("io.floci.ami.catalog-image-id"));assertTrue(Files.readString(context.resolve("Dockerfile")).contains("ADD ubuntu-root.tar.xz /"));+ assertTrue(Files.readString(context.resolve("Dockerfile")).contains("useradd --uid 1000 --gid ubuntu"));+ assertTrue(Files.readString(context.resolve("Dockerfile")).contains("systemd-networkd-wait-online.service.d/floci.conf"));+ assertTrue(Files.readString(context.resolve("systemd-networkd-wait-online-floci.conf")).contains("ExecStart=/bin/true"));assertTrue(Files.readString(context.resolve("provenance.yaml")).contains("manifestSha256"));Path catalog = tempDir.resolve("image-catalog.yaml");