From fc74987bd3a24d94ab5d185229657376c5ae55ef Mon Sep 17 00:00:00 2001 From: David Date: Thu, 13 Aug 2026 03:50:17 +0100 Subject: [PATCH] fix(ec2): delete key pairs by name through the storage backend (#2187) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DeleteKeyPair with KeyName mutated the detached list returned by StorageBackend.scan, which is documented not to affect the store — so the call reported success while leaving the key pair in place. Resolve matching key pairs to their store key and delete through the backend instead. The only existing DeleteKeyPair test passed KeyPairId, the branch that already worked, and asserted just a 200; DeleteKeyPair always returns true, so the no-op was invisible. Cover the by-name path and assert removal rather than status. Closes #2185 --- .../floci/services/ec2/Ec2Service.java | 8 ++- .../services/ec2/Ec2IntegrationTest.java | 44 +++++++++++++ .../floci/services/ec2/Ec2ServiceTest.java | 66 +++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) 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),