fix(ec2): reject key pair requests without KeyName (#3727)

* fix(ec2): reject key pair requests without KeyName

CreateKeyPair accepted a request with no KeyName and stored a nameless key
pair; the duplicate check then dereferenced that name on every later
CreateKeyPair, and the Query catch-all reported it as
"Unexpected error: null". CreateKeyPair and ImportKeyPair now answer
MissingParameter, ImportKeyPair answers InvalidKeyPair.Format instead of an
InternalFailure for material that is not base64, DeleteKeyPair requires a
name or an id and returns the deleted keyPairId, and the name comparisons
tolerate a nameless record already sitting in a persistent store. The EC2
docs still described the placeholder key material that #3307 replaced.

Closes #3356

* fix(ec2): return InvalidKey.Format for malformed key material on ImportKeyPair

The EC2 API error reference lists InvalidKey.Format as the key pair not being in a valid OpenSSH public key format, which is the message this branch sends. InvalidKeyPair.Format is a separate documented code for a different failure.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-16 19:06:32 -04:00
1 parent 8d755c20d6
commit 996a680cdc
6 files changed
+214 -33

No files matched your search

@@ -1948,6 +1948,55 @@ class Ec2IntegrationTest {
.body("Response.Errors.Error.Code", equalTo("InvalidKeyPair.Duplicate"));
}
@Test
@Order(41)
void createKeyPairWithoutKeyNameReturnsMissingParameter() {
// #3356: a nameless key pair used to be stored, and its null name then broke every
// later CreateKeyPair with an InternalFailure.
given()
.formParam("Action", "CreateKeyPair")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
given()
.formParam("Action", "ImportKeyPair")
.formParam("PublicKeyMaterial", "c3NoLXJzYSBBQUFB")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
given()
.formParam("Action", "DeleteKeyPair")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
}
@Test
@Order(41)
void importKeyPairWithInvalidBase64ReturnsInvalidKeyFormat() {
given()
.formParam("Action", "ImportKeyPair")
.formParam("KeyName", "bad-material-key")
.formParam("PublicKeyMaterial", "not base64!")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidKey.Format"));
}
@Test
@Order(42)
void createLaunchTemplateRejectsMalformedUserData() {
@@ -3798,10 +3847,12 @@ class Ec2IntegrationTest {
.when()
.post("/")
.then()
.statusCode(200);
.statusCode(200)
.body("DeleteKeyPairResponse.return", equalTo("true"))
.body("DeleteKeyPairResponse.keyPairId", equalTo(keyPairId));
// DeleteKeyPair always answers 200, so the delete is only proven by a
// follow-up describe.
// DeleteKeyPair answers 200 for a known and an unknown key pair alike, so the
// delete is only proven by a follow-up describe.
given()
.formParam("Action", "DescribeKeyPairs")
.formParam("KeyPairId.1", keyPairId)
@@ -1126,6 +1126,75 @@ class Ec2ServiceTest {
service.importKeyPair("us-east-1", "pinned-ed25519", ed25519Key).getKeyFingerprint());
}
@Test
void createKeyPairRejectsMissingKeyName() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
for (String missing : new String[] {null, "", " "}) {
AwsException error = assertThrows(AwsException.class,
() -> service.createKeyPair("us-east-1", missing));
assertEquals("MissingParameter", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
assertTrue(service.describeKeyPairs("us-east-1", List.of(), List.of()).isEmpty());
}
@Test
void importKeyPairRejectsMissingKeyName() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
AwsException error = assertThrows(AwsException.class,
() -> service.importKeyPair("us-east-1", null, "c3NoLXJzYSBBQUFB"));
assertEquals("MissingParameter", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@Test
void createKeyPairSurvivesANamelessRecordInTheStore() {
// Regression for #3356: a key pair stored without a name (accepted before KeyName was
// validated) made the duplicate check throw on every later CreateKeyPair.
AccountAwareStorageBackend<KeyPair> keyPairStore = AccountAwareStorageBackend.inMemory("000000000000");
KeyPair nameless = new KeyPair();
nameless.setKeyPairId("key-nameless");
nameless.setRegion("us-east-1");
keyPairStore.put("us-east-1:key-nameless", nameless);
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory(Map.of("ec2-key-pairs.json", keyPairStore)));
KeyPair created = service.createKeyPair("us-east-1", "fresh");
KeyPair imported = service.importKeyPair("us-east-1", "fresh-imported",
Ec2KeyMaterial.generateRsa().openSshPublicKey());
assertEquals("fresh", created.getKeyName());
assertEquals("fresh-imported", imported.getKeyName());
assertNull(service.deleteKeyPair("us-east-1", "no-such-name", null));
assertEquals(3, service.describeKeyPairs("us-east-1", List.of(), List.of()).size());
}
@Test
void deleteKeyPairReturnsTheDeletedRecordOrNull() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
KeyPair byName = service.createKeyPair("us-east-1", "delete-by-name");
KeyPair byId = service.createKeyPair("us-east-1", "delete-by-id");
assertEquals(byName.getKeyPairId(), service.deleteKeyPair("us-east-1", "delete-by-name", null).getKeyPairId());
assertEquals(byId.getKeyPairId(), service.deleteKeyPair("us-east-1", null, byId.getKeyPairId()).getKeyPairId());
assertNull(service.deleteKeyPair("us-east-1", "delete-by-name", null));
assertNull(service.deleteKeyPair("us-east-1", null, byId.getKeyPairId()));
assertTrue(service.describeKeyPairs("us-east-1", List.of(), List.of()).isEmpty());
}
@Test
void importKeyPairRejectsDuplicateKeyName() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),