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

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
This commit is contained in:
David authored and GitHub committed 2026-08-12 22:50:17 -04:00
1 parent 4215006fe2
commit fc74987bd3
3 files changed
+117 -1

No files matched your search

@@ -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)));
}
}
@@ -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
@@ -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),