Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): delete key pairs by name through the storage backend (#2187)

Davidfc74987bd3a2
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.java
index 22c7481898..668776c5f5 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
@@ -1830,7 +1830,13 @@ public class Ec2Service implements ContainerTeardown {
if (keyPairId != null && !keyPairId.isEmpty()) {
keyPairs.delete(key(region, keyPairId));
} else {
- keyPairs.scan(k -> true).removeIf(k -> k.getRegion().equals(region) && k.getKeyName().equals(keyName));
+ // scan() returns a detached copy, so the key pair has to be resolved to its
+ // store key and deleted through the backend — mutating the scan result does
+ // not touch the store.
+ keyPairs.scan(k -> true).stream()
+ .filter(k -> k.getRegion().equals(region) && k.getKeyName().equals(keyName))
+ .map(KeyPair::getKeyPairId)
+ .forEach(id -> keyPairs.delete(key(region, id)));
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
index 75e71afb04..4a9ef20443 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
@@ -2699,6 +2699,50 @@ class Ec2IntegrationTest {
.post("/")
.then()
.statusCode(200);
+
+ // DeleteKeyPair always answers 200, so the delete is only proven by a
+ // follow-up describe.
+ given()
+ .formParam("Action", "DescribeKeyPairs")
+ .formParam("KeyPairId.1", keyPairId)
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("Response.Errors.Error.Code", equalTo("InvalidKeyPair.NotFound"));
+ }
+
+ @Test
+ @Order(108)
+ void deleteKeyPairByName() {
+ given()
+ .formParam("Action", "CreateKeyPair")
+ .formParam("KeyName", "delete-by-name-key")
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .formParam("Action", "DeleteKeyPair")
+ .formParam("KeyName", "delete-by-name-key")
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .formParam("Action", "DescribeKeyPairs")
+ .formParam("KeyName.1", "delete-by-name-key")
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("Response.Errors.Error.Code", equalTo("InvalidKeyPair.NotFound"));
}
@Test
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.java
index 4765860cee..28cd00248c 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
@@ -375,6 +375,72 @@ class Ec2ServiceTest {
assertEquals("InvalidKeyPair.NotFound", error.getErrorCode());
}
+ @Test
+ void deleteKeyPairByNameRemovesItFromTheStore() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+
+ service.createKeyPair("us-east-1", "by-name");
+ service.deleteKeyPair("us-east-1", "by-name", null);
+
+ // A deleted key pair is gone for good: describe by name must report NotFound
+ // rather than returning the key that DeleteKeyPair claimed to remove.
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.describeKeyPairs("us-east-1", List.of("by-name"), List.of()));
+ assertEquals("InvalidKeyPair.NotFound", error.getErrorCode());
+ assertTrue(service.describeKeyPairs("us-east-1", List.of(), List.of()).isEmpty());
+ }
+
+ @Test
+ void deleteKeyPairByNameLeavesOtherKeysAndRegionsIntact() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+
+ service.createKeyPair("us-east-1", "target");
+ service.createKeyPair("us-east-1", "bystander");
+ service.createKeyPair("eu-west-1", "target");
+
+ service.deleteKeyPair("us-east-1", "target", null);
+
+ // Deleting resolves through the store key, so it must not take the same-named
+ // key in another region — nor any other key in the same region — with it.
+ assertEquals(1, service.describeKeyPairs("us-east-1", List.of("bystander"), List.of()).size());
+ assertEquals(1, service.describeKeyPairs("eu-west-1", List.of("target"), List.of()).size());
+ }
+
+ @Test
+ void deleteKeyPairByIdRemovesItFromTheStore() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+
+ String keyPairId = service.createKeyPair("us-east-1", "by-id").getKeyPairId();
+ service.deleteKeyPair("us-east-1", null, keyPairId);
+
+ assertTrue(service.describeKeyPairs("us-east-1", List.of(), List.of()).isEmpty());
+ }
+
+ @Test
+ void deleteKeyPairForUnknownNameIsANoOp() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+
+ service.createKeyPair("us-east-1", "present-key");
+
+ // Real EC2 DeleteKeyPair is idempotent — deleting a key that does not exist
+ // succeeds rather than raising InvalidKeyPair.NotFound.
+ service.deleteKeyPair("us-east-1", "never-existed", null);
+
+ assertEquals(1, service.describeKeyPairs("us-east-1", List.of("present-key"), List.of()).size());
+ }
+
@Test
void registerImageReusingSnapshotDoesNotOverwriteSnapshotMetadata() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),