mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): keep the KMS key a volume is created with (#6756)
* fix(ec2): keep the KMS key a volume is created with CreateVolume ignored KmsKeyId, so neither CreateVolume nor DescribeVolumes reported kmsKeyId, even for a volume created encrypted with a named key. A volume CreateVolume makes encrypted now keeps the request's KmsKeyId as given, or gets the region's default EBS key when it names none, and both responses report it. A volume that is not encrypted keeps no key, and volumes stored before this change load as they were, with no kmsKeyId. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): read volume responses with XmlParser The volume KMS key tests read CreateVolume and DescribeVolumes responses with a regular expression, substring offsets and plain string matches. They now read them with XmlParser, as other EC2 tests do, so element values are parsed and decoded as XML. Test-only; the assertions are unchanged. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(ec2): keep the KMS key a launch's block device mapping names A launch's encrypted volumes got the region's default EBS key even when their block device mapping, or the launch template's, named Ebs.KmsKeyId. The key now goes from the mapping to the volume, root included, and a mapping that names none still gets the default. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): pin the launch volume key rules Pin that a launch mapping naming a key without Encrypted leaves the volume unencrypted and keyless, and that a request entry's key wins over its launch template's. Delete the test's launch template, reuse the launch helpers in the root-volume test, and document that a launch volume encrypted only by its snapshot gets the region's default key, since snapshots record no key. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> --------- Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
db86fc7d72
commit
a83fa9c9e4
7 files changed
+632
-9
No files matched your search
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Address;
|
||||
@@ -35,6 +36,7 @@ import io.github.hectorvent.floci.services.ec2.model.Vpc;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
@@ -694,6 +696,113 @@ class Ec2ServicePersistenceTest {
|
||||
assertFalse(xml.contains("<createTime>"), xml);
|
||||
}
|
||||
|
||||
@Test
|
||||
void volumeKmsKeyIdSurvivesRestart(@TempDir Path dir) {
|
||||
String keyArn = "arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
|
||||
String created = query(newService(dir), "CreateVolume", "AvailabilityZone", "us-east-1a", "Size", "1",
|
||||
"Encrypted", "true", "KmsKeyId", keyArn);
|
||||
String volumeId = XmlParser.extractFirst(created, "volumeId", null);
|
||||
|
||||
String described = query(newService(dir), "DescribeVolumes", "VolumeId.1", volumeId);
|
||||
|
||||
assertEquals(List.of("true"), XmlParser.extractAll(described, "encrypted"), described);
|
||||
assertEquals(List.of(keyArn), XmlParser.extractAll(described, "kmsKeyId"), described);
|
||||
}
|
||||
|
||||
/**
|
||||
* A volume store as Floci wrote it before a volume kept its KMS key: an encrypted volume
|
||||
* attached to an instance and an unencrypted one. Both load as they were stored, and
|
||||
* DescribeVolumes leaves {@code kmsKeyId} out: the key the encrypted one was created with was
|
||||
* never recorded, and the region's default may have changed since.
|
||||
*/
|
||||
@Test
|
||||
void volumesSavedBeforeTheirKmsKeyWasKeptLoadWithoutOne(@TempDir Path dir) throws IOException {
|
||||
String legacyJson = """
|
||||
{
|
||||
"us-east-1::vol-0aaaaaaaaaaaaaaaa" : {
|
||||
"volumeId" : "vol-0aaaaaaaaaaaaaaaa",
|
||||
"volumeType" : "gp3",
|
||||
"size" : 20,
|
||||
"state" : "in-use",
|
||||
"availabilityZone" : "us-east-1a",
|
||||
"encrypted" : true,
|
||||
"iops" : 0,
|
||||
"throughput" : 125,
|
||||
"snapshotId" : null,
|
||||
"createTime" : "2026-10-20T01:38:17.105Z",
|
||||
"multiAttachEnabled" : null,
|
||||
"region" : "us-east-1",
|
||||
"tags" : [ {
|
||||
"key" : "Team",
|
||||
"value" : "blue"
|
||||
} ],
|
||||
"attachments" : [ {
|
||||
"instanceId" : "i-0aaaaaaaaaaaaaaaa",
|
||||
"device" : "/dev/sdf",
|
||||
"state" : "attached",
|
||||
"attachTime" : "2026-10-20T01:39:02.411Z",
|
||||
"deleteOnTermination" : false,
|
||||
"volumeId" : "vol-0aaaaaaaaaaaaaaaa"
|
||||
} ]
|
||||
},
|
||||
"us-east-1::vol-0bbbbbbbbbbbbbbbb" : {
|
||||
"volumeId" : "vol-0bbbbbbbbbbbbbbbb",
|
||||
"volumeType" : "gp2",
|
||||
"size" : 8,
|
||||
"state" : "available",
|
||||
"availabilityZone" : "us-east-1b",
|
||||
"encrypted" : false,
|
||||
"iops" : 0,
|
||||
"throughput" : null,
|
||||
"snapshotId" : null,
|
||||
"createTime" : "2026-10-20T01:38:17.112Z",
|
||||
"multiAttachEnabled" : null,
|
||||
"region" : "us-east-1",
|
||||
"tags" : [ ],
|
||||
"attachments" : [ ]
|
||||
}
|
||||
}
|
||||
""";
|
||||
Files.writeString(dir.resolve("ec2-volumes.json"), legacyJson);
|
||||
|
||||
Ec2Service restarted = newService(dir);
|
||||
|
||||
Map<String, Volume> restored = new HashMap<>();
|
||||
for (Volume volume : restarted.describeVolumes(REGION, List.of(), Map.of())) {
|
||||
restored.put(volume.getVolumeId(), volume);
|
||||
}
|
||||
assertEquals(2, restored.size());
|
||||
Volume encrypted = restored.get("vol-0aaaaaaaaaaaaaaaa");
|
||||
assertTrue(encrypted.isEncrypted());
|
||||
assertEquals(20, encrypted.getSize());
|
||||
assertEquals(125, encrypted.getThroughput());
|
||||
assertEquals("i-0aaaaaaaaaaaaaaaa", encrypted.getAttachments().getFirst().getInstanceId());
|
||||
assertEquals("blue", encrypted.getTags().getFirst().getValue());
|
||||
assertFalse(restored.get("vol-0bbbbbbbbbbbbbbbb").isEncrypted());
|
||||
|
||||
String described = query(restarted, "DescribeVolumes");
|
||||
List<String> volumeIds = XmlParser.extractAll(described, "volumeId");
|
||||
assertTrue(volumeIds.contains("vol-0aaaaaaaaaaaaaaaa"), described);
|
||||
assertTrue(volumeIds.contains("vol-0bbbbbbbbbbbbbbbb"), described);
|
||||
assertTrue(XmlParser.extractAll(described, "encrypted").contains("true"), described);
|
||||
assertEquals(List.of(), XmlParser.extractAll(described, "kmsKeyId"), described);
|
||||
}
|
||||
|
||||
private static String query(Ec2Service service, String action, String... params) {
|
||||
MultivaluedHashMap<String, String> form = new MultivaluedHashMap<>();
|
||||
for (int i = 0; i < params.length; i += 2) {
|
||||
form.add(params[i], params[i + 1]);
|
||||
}
|
||||
Response response = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
|
||||
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
|
||||
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
new RegionResolver(REGION, "000000000000"))
|
||||
.handle(action, form, REGION);
|
||||
String body = String.valueOf(response.getEntity());
|
||||
assertEquals(200, response.getStatus(), action + ": " + body);
|
||||
return body;
|
||||
}
|
||||
|
||||
private static String jsonString(String value) {
|
||||
return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\"";
|
||||
}
|
||||
|
||||
+318
@@ -0,0 +1,318 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.CreateVolumeResponse;
|
||||
import software.amazon.awssdk.services.ec2.model.Volume;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* The KMS key a volume is created with, as the wire reports it: {@code kmsKeyId} on CreateVolume's
|
||||
* response and on the volume DescribeVolumes lists, beside {@code encrypted}.
|
||||
*
|
||||
* <p>Runs in its own region: the EBS encryption defaults are region-scoped and persist across test
|
||||
* classes in the same application, and the tests that change them put them back in a
|
||||
* {@code finally}.</p>
|
||||
*/
|
||||
@QuarkusTest
|
||||
class Ec2VolumeKmsKeyIdIntegrationTest {
|
||||
|
||||
private static final String REGION = "ap-southeast-5";
|
||||
private static final String AZ = REGION + "a";
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/" + REGION + "/ec2/aws4_request";
|
||||
private static final String KEY_ARN =
|
||||
"arn:aws:kms:" + REGION + ":000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
|
||||
private static final String DEFAULT_KEY_ARN =
|
||||
"arn:aws:kms:" + REGION + ":000000000000:key/0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0";
|
||||
|
||||
private static RequestSpecification ec2(String action, String... params) {
|
||||
RequestSpecification request = given()
|
||||
.formParam("Action", action)
|
||||
.header("Authorization", AUTH_HEADER);
|
||||
for (int i = 0; i + 1 < params.length; i += 2) {
|
||||
request = request.formParam(params[i], params[i + 1]);
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
private static RequestSpecification createVolume(String... extraParams) {
|
||||
String[] params = new String[extraParams.length + 4];
|
||||
params[0] = "AvailabilityZone";
|
||||
params[1] = AZ;
|
||||
params[2] = "Size";
|
||||
params[3] = "1";
|
||||
System.arraycopy(extraParams, 0, params, 4, extraParams.length);
|
||||
return ec2("CreateVolume", params);
|
||||
}
|
||||
|
||||
/** The key an encrypted volume was created with, or an empty string when the element is missing. */
|
||||
private static String createdVolumeKey(String... extraParams) {
|
||||
return createVolume(extraParams).when().post("/").then().statusCode(200)
|
||||
.body("CreateVolumeResponse.encrypted", equalTo("true"))
|
||||
.extract().xmlPath().getString("CreateVolumeResponse.kmsKeyId");
|
||||
}
|
||||
|
||||
private static String describedVolumeKey(String volumeId) {
|
||||
return ec2("DescribeVolumes", "VolumeId.1", volumeId)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().xmlPath().getString("DescribeVolumesResponse.volumeSet.item.kmsKeyId");
|
||||
}
|
||||
|
||||
private static void assertNoKmsKeyId(String xml) {
|
||||
assertEquals(List.of(), XmlParser.extractAll(xml, "kmsKeyId"), xml);
|
||||
}
|
||||
|
||||
private static void call(String action, String... params) {
|
||||
ec2(action, params).when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createVolumeAndDescribeVolumesReportTheKeyAnEncryptedVolumeWasCreatedWith() {
|
||||
String volumeId = createVolume("Encrypted", "true", "KmsKeyId", KEY_ARN)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("CreateVolumeResponse.volumeId", startsWith("vol-"))
|
||||
.body("CreateVolumeResponse.encrypted", equalTo("true"))
|
||||
.body("CreateVolumeResponse.kmsKeyId", equalTo(KEY_ARN))
|
||||
.extract().path("CreateVolumeResponse.volumeId");
|
||||
|
||||
ec2("DescribeVolumes", "VolumeId.1", volumeId)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("DescribeVolumesResponse.volumeSet.item.volumeId", equalTo(volumeId))
|
||||
.body("DescribeVolumesResponse.volumeSet.item.encrypted", equalTo("true"))
|
||||
.body("DescribeVolumesResponse.volumeSet.item.kmsKeyId", equalTo(KEY_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVolumeThatIsNotEncryptedHasNoKmsKeyIdElement() {
|
||||
String created = createVolume("KmsKeyId", KEY_ARN)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("CreateVolumeResponse.encrypted", equalTo("false"))
|
||||
.extract().asString();
|
||||
assertNoKmsKeyId(created);
|
||||
|
||||
assertNoKmsKeyId(ec2("DescribeVolumes", "VolumeId.1", XmlParser.extractFirst(created, "volumeId", null))
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("DescribeVolumesResponse.volumeSet.item.encrypted", equalTo("false"))
|
||||
.extract().asString());
|
||||
}
|
||||
|
||||
/**
|
||||
* An encrypted volume that names no key gets the region's default EBS key as it is when the
|
||||
* volume is created; changing the default later leaves the volume's key alone.
|
||||
*/
|
||||
@Test
|
||||
void anEncryptedVolumeThatNamesNoKeyGetsTheRegionsDefaultKeyOfTheMoment() {
|
||||
assertEquals("alias/aws/ebs", createdVolumeKey("Encrypted", "true"));
|
||||
try {
|
||||
call("ModifyEbsDefaultKmsKeyId", "KmsKeyId", DEFAULT_KEY_ARN);
|
||||
String volumeId = createVolume("Encrypted", "true").when().post("/").then().statusCode(200)
|
||||
.body("CreateVolumeResponse.kmsKeyId", equalTo(DEFAULT_KEY_ARN))
|
||||
.extract().path("CreateVolumeResponse.volumeId");
|
||||
|
||||
call("ResetEbsDefaultKmsKeyId");
|
||||
assertEquals(DEFAULT_KEY_ARN, describedVolumeKey(volumeId));
|
||||
} finally {
|
||||
call("ResetEbsDefaultKmsKeyId");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* With encryption by default on, a CreateVolume volume that names no key gets the default key,
|
||||
* and one that names a key keeps it. AWS refuses a KmsKeyId without Encrypted=true even then;
|
||||
* Floci accepts it, and the volume, encrypted by default, keeps the key it names.
|
||||
*/
|
||||
@Test
|
||||
void volumesEncryptedByDefaultGetTheDefaultKeyUnlessTheyNameOne() {
|
||||
call("EnableEbsEncryptionByDefault");
|
||||
try {
|
||||
call("ModifyEbsDefaultKmsKeyId", "KmsKeyId", DEFAULT_KEY_ARN);
|
||||
assertEquals(DEFAULT_KEY_ARN, createdVolumeKey());
|
||||
assertEquals(KEY_ARN, createdVolumeKey("Encrypted", "true", "KmsKeyId", KEY_ARN));
|
||||
assertEquals(KEY_ARN, createdVolumeKey("KmsKeyId", KEY_ARN));
|
||||
} finally {
|
||||
// Encryption by default first: it is the setting the other tests here depend on.
|
||||
try {
|
||||
call("DisableEbsEncryptionByDefault");
|
||||
} finally {
|
||||
call("ResetEbsDefaultKmsKeyId");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInstanceRootVolumeThatIsNotEncryptedHasNoKey() {
|
||||
String instanceId = launch();
|
||||
try {
|
||||
assertNoKmsKeyId(attachedVolume(instanceId, "/dev/xvda", "false"));
|
||||
} finally {
|
||||
call("TerminateInstances", "InstanceId.1", instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS refuses a launch mapping that names a key without Encrypted=true; Floci accepts it, and
|
||||
* with encryption by default off the volume stays unencrypted and keeps no key.
|
||||
*/
|
||||
@Test
|
||||
void aLaunchMappingKeyWithoutEncryptedLeavesTheVolumeUnencrypted() {
|
||||
String instanceId = launch(
|
||||
"BlockDeviceMapping.1.DeviceName", "/dev/sdf",
|
||||
"BlockDeviceMapping.1.Ebs.VolumeSize", "1",
|
||||
"BlockDeviceMapping.1.Ebs.KmsKeyId", KEY_ARN);
|
||||
try {
|
||||
assertNoKmsKeyId(attachedVolume(instanceId, "/dev/sdf", "false"));
|
||||
} finally {
|
||||
call("TerminateInstances", "InstanceId.1", instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A launch's encrypted volumes keep the key their block device mapping names, the root
|
||||
* included, and one that names none gets the region's default.
|
||||
*/
|
||||
@Test
|
||||
void launchVolumesKeepTheKeyTheirMappingNames() {
|
||||
String instanceId = launch(
|
||||
"BlockDeviceMapping.1.DeviceName", "/dev/xvda",
|
||||
"BlockDeviceMapping.1.Ebs.Encrypted", "true",
|
||||
"BlockDeviceMapping.1.Ebs.KmsKeyId", KEY_ARN,
|
||||
"BlockDeviceMapping.2.DeviceName", "/dev/sdf",
|
||||
"BlockDeviceMapping.2.Ebs.VolumeSize", "1",
|
||||
"BlockDeviceMapping.2.Ebs.Encrypted", "true",
|
||||
"BlockDeviceMapping.2.Ebs.KmsKeyId", DEFAULT_KEY_ARN,
|
||||
"BlockDeviceMapping.3.DeviceName", "/dev/sdg",
|
||||
"BlockDeviceMapping.3.Ebs.VolumeSize", "1",
|
||||
"BlockDeviceMapping.3.Ebs.Encrypted", "true");
|
||||
try {
|
||||
assertEquals(List.of(KEY_ARN), XmlParser.extractAll(attachedVolume(instanceId, "/dev/xvda"), "kmsKeyId"));
|
||||
assertEquals(List.of(DEFAULT_KEY_ARN),
|
||||
XmlParser.extractAll(attachedVolume(instanceId, "/dev/sdf"), "kmsKeyId"));
|
||||
assertEquals(List.of("alias/aws/ebs"),
|
||||
XmlParser.extractAll(attachedVolume(instanceId, "/dev/sdg"), "kmsKeyId"));
|
||||
} finally {
|
||||
call("TerminateInstances", "InstanceId.1", instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A launch template's mapping key reaches the volume a launch from the template creates, and a
|
||||
* request entry for the same device that names another key wins over it.
|
||||
*/
|
||||
@Test
|
||||
void aLaunchTemplateMappingKeyReachesTheVolume() {
|
||||
String templateId = ec2("CreateLaunchTemplate", "LaunchTemplateName", "kms-key-" + System.nanoTime(),
|
||||
"LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/sdf",
|
||||
"LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeSize", "1",
|
||||
"LaunchTemplateData.BlockDeviceMapping.1.Ebs.Encrypted", "true",
|
||||
"LaunchTemplateData.BlockDeviceMapping.1.Ebs.KmsKeyId", KEY_ARN)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
|
||||
try {
|
||||
String fromTemplate = launch("LaunchTemplate.LaunchTemplateId", templateId);
|
||||
String overridden = launch("LaunchTemplate.LaunchTemplateId", templateId,
|
||||
"BlockDeviceMapping.1.DeviceName", "/dev/sdf",
|
||||
"BlockDeviceMapping.1.Ebs.KmsKeyId", DEFAULT_KEY_ARN);
|
||||
try {
|
||||
assertEquals(List.of(KEY_ARN),
|
||||
XmlParser.extractAll(attachedVolume(fromTemplate, "/dev/sdf"), "kmsKeyId"));
|
||||
assertEquals(List.of(DEFAULT_KEY_ARN),
|
||||
XmlParser.extractAll(attachedVolume(overridden, "/dev/sdf"), "kmsKeyId"));
|
||||
} finally {
|
||||
call("TerminateInstances", "InstanceId.1", fromTemplate, "InstanceId.2", overridden);
|
||||
}
|
||||
} finally {
|
||||
call("DeleteLaunchTemplate", "LaunchTemplateId", templateId);
|
||||
}
|
||||
}
|
||||
|
||||
/** With encryption by default on, a launch's volumes that name no key get the default key. */
|
||||
@Test
|
||||
void launchVolumesEncryptedByDefaultGetTheDefaultKey() {
|
||||
call("EnableEbsEncryptionByDefault");
|
||||
try {
|
||||
call("ModifyEbsDefaultKmsKeyId", "KmsKeyId", DEFAULT_KEY_ARN);
|
||||
String instanceId = launch();
|
||||
try {
|
||||
assertEquals(List.of(DEFAULT_KEY_ARN),
|
||||
XmlParser.extractAll(attachedVolume(instanceId, "/dev/xvda"), "kmsKeyId"));
|
||||
} finally {
|
||||
call("TerminateInstances", "InstanceId.1", instanceId);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
call("DisableEbsEncryptionByDefault");
|
||||
} finally {
|
||||
call("ResetEbsDefaultKmsKeyId");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String launch(String... extraParams) {
|
||||
String[] params = new String[extraParams.length + 8];
|
||||
params[0] = "ImageId";
|
||||
params[1] = "ami-0abcdef1234567890";
|
||||
params[2] = "InstanceType";
|
||||
params[3] = "t3.micro";
|
||||
params[4] = "MinCount";
|
||||
params[5] = "1";
|
||||
params[6] = "MaxCount";
|
||||
params[7] = "1";
|
||||
System.arraycopy(extraParams, 0, params, 8, extraParams.length);
|
||||
return ec2("RunInstances", params).when().post("/").then().statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
}
|
||||
|
||||
/** DescribeVolumes for the one encrypted volume attached to the instance at the device. */
|
||||
private static String attachedVolume(String instanceId, String device) {
|
||||
return attachedVolume(instanceId, device, "true");
|
||||
}
|
||||
|
||||
/** DescribeVolumes for the one volume attached to the instance at the device. */
|
||||
private static String attachedVolume(String instanceId, String device, String encrypted) {
|
||||
String xml = ec2("DescribeVolumes",
|
||||
"Filter.1.Name", "attachment.instance-id", "Filter.1.Value.1", instanceId,
|
||||
"Filter.2.Name", "attachment.device", "Filter.2.Value.1", device)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().asString();
|
||||
assertEquals(1, XmlParser.extractAll(xml, "size").size(), xml);
|
||||
assertEquals(List.of(encrypted), XmlParser.extractAll(xml, "encrypted"), xml);
|
||||
return xml;
|
||||
}
|
||||
|
||||
@Test
|
||||
void theAwsSdkReadsTheKeyFromCreateVolumeAndDescribeVolumes() {
|
||||
try (Ec2Client client = Ec2Client.builder()
|
||||
.endpointOverride(URI.create("http://localhost:" + RestAssured.port))
|
||||
.region(Region.of(REGION))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder()).build()) {
|
||||
CreateVolumeResponse created = client.createVolume(request -> request
|
||||
.availabilityZone(AZ).size(1).encrypted(true).kmsKeyId(KEY_ARN));
|
||||
assertEquals(KEY_ARN, created.kmsKeyId());
|
||||
Volume described = client.describeVolumes(request -> request.volumeIds(created.volumeId()))
|
||||
.volumes().getFirst();
|
||||
assertEquals(KEY_ARN, described.kmsKeyId());
|
||||
|
||||
String plainId = client.createVolume(request -> request.availabilityZone(AZ).size(1)).volumeId();
|
||||
assertNull(client.describeVolumes(request -> request.volumeIds(plainId))
|
||||
.volumes().getFirst().kmsKeyId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* CreateVolume's KmsKeyId, sent through the Query handler to a real service. An encrypted volume
|
||||
* keeps the key it was created with, or the region's default EBS key when it names none, and
|
||||
* CreateVolume and DescribeVolumes report it as {@code kmsKeyId}; a volume that is not encrypted
|
||||
* has no key, so the element is left out. The service here holds no EBS encryption settings, so
|
||||
* the default is the AWS-managed {@code alias/aws/ebs} every account starts with.
|
||||
*/
|
||||
class Ec2VolumeKmsKeyIdTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String AZ = REGION + "a";
|
||||
private static final String KEY_ARN =
|
||||
"arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab";
|
||||
|
||||
private Ec2QueryHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.Ec2ServiceConfig ec2 = mock(EmulatorConfig.Ec2ServiceConfig.class);
|
||||
when(config.defaultAccountId()).thenReturn("000000000000");
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.ec2()).thenReturn(ec2);
|
||||
when(ec2.mock()).thenReturn(true);
|
||||
when(ec2.mockPublicAddresses()).thenReturn(EmulatorConfig.Ec2ServiceConfig.MockPublicAddresses.NONE);
|
||||
when(ec2.mockPublicAddress()).thenReturn(Optional.empty());
|
||||
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
|
||||
Ec2Service service = new Ec2Service(config, mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), new AmiImageResolver(imageCatalog), imageCatalog,
|
||||
new Ec2InstanceTypeCatalog(), new MemoryFactory());
|
||||
handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
/** Each form CreateVolume documents for KmsKeyId is kept as the caller gave it. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"1234abcd-12ab-34cd-56ef-1234567890ab",
|
||||
"alias/ExampleAlias",
|
||||
KEY_ARN,
|
||||
"arn:aws:kms:us-east-1:000000000000:alias/ExampleAlias"
|
||||
})
|
||||
void anEncryptedVolumeReportsTheKeyItWasCreatedWith(String kmsKeyId) {
|
||||
String created = ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1",
|
||||
"Encrypted", "true", "KmsKeyId", kmsKeyId);
|
||||
assertEquals("true", first(created, "encrypted"));
|
||||
assertEquals(kmsKeyId, first(created, "kmsKeyId"));
|
||||
|
||||
String described = ok("DescribeVolumes", "VolumeId.1", first(created, "volumeId"));
|
||||
assertEquals("true", first(described, "encrypted"));
|
||||
assertEquals(kmsKeyId, first(described, "kmsKeyId"));
|
||||
}
|
||||
|
||||
/** A blank KmsKeyId names no key, so the volume gets the default rather than an empty key. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"absent", "", " "})
|
||||
void anEncryptedVolumeThatNamesNoKeyGetsTheDefaultKey(String kmsKeyId) {
|
||||
String created = kmsKeyId.equals("absent")
|
||||
? ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "Encrypted", "true")
|
||||
: ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "Encrypted", "true",
|
||||
"KmsKeyId", kmsKeyId);
|
||||
assertEquals("alias/aws/ebs", first(created, "kmsKeyId"));
|
||||
|
||||
String described = ok("DescribeVolumes", "VolumeId.1", first(created, "volumeId"));
|
||||
assertEquals("alias/aws/ebs", first(described, "kmsKeyId"));
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS requires Encrypted=true beside a KmsKeyId. Floci still accepts the request, as it did
|
||||
* before, but an unencrypted volume has no key to report.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "false"})
|
||||
void aKeyIsNotKeptOnAVolumeThatIsNotEncrypted(String encrypted) {
|
||||
String created = encrypted.isEmpty()
|
||||
? ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "KmsKeyId", KEY_ARN)
|
||||
: ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "Encrypted", encrypted,
|
||||
"KmsKeyId", KEY_ARN);
|
||||
assertEquals("false", first(created, "encrypted"));
|
||||
assertEquals(List.of(), XmlParser.extractAll(created, "kmsKeyId"), created);
|
||||
|
||||
String described = ok("DescribeVolumes", "VolumeId.1", first(created, "volumeId"));
|
||||
assertEquals("false", first(described, "encrypted"));
|
||||
assertEquals(List.of(), XmlParser.extractAll(described, "kmsKeyId"), described);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theKeyStaysWithTheVolumeWhenItIsModifiedAndTagged() {
|
||||
String volumeId = first(ok("CreateVolume", "AvailabilityZone", AZ, "Size", "1", "VolumeType", "gp3",
|
||||
"Encrypted", "true", "KmsKeyId", KEY_ARN), "volumeId");
|
||||
|
||||
ok("ModifyVolume", "VolumeId", volumeId, "Size", "2");
|
||||
ok("CreateTags", "ResourceId.1", volumeId, "Tag.1.Key", "Team", "Tag.1.Value", "blue");
|
||||
|
||||
String described = ok("DescribeVolumes", "VolumeId.1", volumeId);
|
||||
assertEquals("2", first(described, "size"));
|
||||
assertEquals(KEY_ARN, first(described, "kmsKeyId"));
|
||||
}
|
||||
|
||||
private String ok(String action, String... params) {
|
||||
MultivaluedMap<String, String> form = new MultivaluedHashMap<>();
|
||||
for (int i = 0; i < params.length; i += 2) {
|
||||
form.add(params[i], params[i + 1]);
|
||||
}
|
||||
Response response = handler.handle(action, form, REGION);
|
||||
String body = String.valueOf(response.getEntity());
|
||||
assertEquals(200, response.getStatus(), action + ": " + body);
|
||||
return body;
|
||||
}
|
||||
|
||||
private static String first(String xml, String element) {
|
||||
List<String> values = XmlParser.extractAll(xml, element);
|
||||
assertFalse(values.isEmpty(), "no <" + element + "> in " + xml);
|
||||
return values.getFirst();
|
||||
}
|
||||
|
||||
private static final class MemoryFactory extends StorageFactory {
|
||||
private MemoryFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory("000000000000");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user