mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
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:
1 parent
8d755c20d6
commit
996a680cdc
6 files changed
+214
-33
No files matched your search
@@ -3029,8 +3029,18 @@ public class Ec2QueryHandler {
|
||||
|
||||
// ─── Key Pair handlers ────────────────────────────────────────────────────
|
||||
|
||||
private Response handleCreateKeyPair(MultivaluedMap<String, String> p, String region) {
|
||||
// KeyName is required on CreateKeyPair and ImportKeyPair. Accepting its absence used to store
|
||||
// a nameless key pair, and the first nameless record broke every later CreateKeyPair (#3356).
|
||||
private static String requireKeyName(MultivaluedMap<String, String> p) {
|
||||
String keyName = p.getFirst("KeyName");
|
||||
if (keyName == null || keyName.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter KeyName", 400);
|
||||
}
|
||||
return keyName;
|
||||
}
|
||||
|
||||
private Response handleCreateKeyPair(MultivaluedMap<String, String> p, String region) {
|
||||
String keyName = requireKeyName(p);
|
||||
KeyPair kp = service.createKeyPair(region, keyName);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("CreateKeyPairResponse", AwsNamespaces.EC2)
|
||||
@@ -3066,14 +3076,36 @@ public class Ec2QueryHandler {
|
||||
private Response handleDeleteKeyPair(MultivaluedMap<String, String> p, String region) {
|
||||
String keyName = p.getFirst("KeyName");
|
||||
String keyPairId = p.getFirst("KeyPairId");
|
||||
service.deleteKeyPair(region, keyName, keyPairId);
|
||||
return booleanResponse("DeleteKeyPair");
|
||||
boolean noName = keyName == null || keyName.isBlank();
|
||||
boolean noId = keyPairId == null || keyPairId.isBlank();
|
||||
if (noName && noId) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter KeyName", 400);
|
||||
}
|
||||
KeyPair deleted = service.deleteKeyPair(region, keyName, keyPairId);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DeleteKeyPairResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.elem("return", "true");
|
||||
if (deleted != null) {
|
||||
xml.elem("keyPairId", deleted.getKeyPairId());
|
||||
}
|
||||
xml.end("DeleteKeyPairResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleImportKeyPair(MultivaluedMap<String, String> p, String region) {
|
||||
String keyName = p.getFirst("KeyName");
|
||||
String keyName = requireKeyName(p);
|
||||
String encoded = p.getFirst("PublicKeyMaterial");
|
||||
String publicKeyMaterial = new String(Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
|
||||
if (encoded == null || encoded.isBlank()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The request must contain the parameter PublicKeyMaterial", 400);
|
||||
}
|
||||
String publicKeyMaterial;
|
||||
try {
|
||||
publicKeyMaterial = new String(Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidKey.Format", "Key is not in valid OpenSSH public key format", 400);
|
||||
}
|
||||
KeyPair kp = service.importKeyPair(region, keyName, publicKeyMaterial);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("ImportKeyPairResponse", AwsNamespaces.EC2)
|
||||
|
||||
@@ -4546,11 +4546,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
|
||||
public KeyPair createKeyPair(String region, String keyName) {
|
||||
ensureDefaultResources(region);
|
||||
boolean exists = keyPairs.scan(k -> true).stream()
|
||||
.anyMatch(k -> k.getRegion().equals(region) && k.getKeyName().equals(keyName));
|
||||
if (exists) {
|
||||
throw new AwsException("InvalidKeyPair.Duplicate", "The keypair '" + keyName + "' already exists", 400);
|
||||
}
|
||||
requireKeyName(keyName);
|
||||
rejectDuplicateKeyName(region, keyName);
|
||||
String keyPairId = "key-" + randomHex(17);
|
||||
KeyPair kp = new KeyPair();
|
||||
kp.setKeyPairId(keyPairId);
|
||||
@@ -4594,28 +4591,30 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteKeyPair(String region, String keyName, String keyPairId) {
|
||||
/**
|
||||
* Deletes by id when one is given, otherwise by name, and returns the deleted key pair so
|
||||
* the response can carry its id. Null when nothing matched: AWS answers a delete of an
|
||||
* unknown key pair with plain success.
|
||||
*/
|
||||
public KeyPair deleteKeyPair(String region, String keyName, String keyPairId) {
|
||||
ensureDefaultResources(region);
|
||||
if (keyPairId != null && !keyPairId.isEmpty()) {
|
||||
keyPairs.delete(key(region, keyPairId));
|
||||
} else {
|
||||
// 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)));
|
||||
boolean byId = keyPairId != null && !keyPairId.isEmpty();
|
||||
// scan() returns detached copies, so each match is resolved to its store key and deleted
|
||||
// through the backend; mutating the scan result does not touch the store.
|
||||
List<KeyPair> matches = keyPairs.scan(k -> true).stream()
|
||||
.filter(k -> region.equals(k.getRegion()))
|
||||
.filter(k -> byId ? keyPairId.equals(k.getKeyPairId()) : keyName != null && keyName.equals(k.getKeyName()))
|
||||
.toList();
|
||||
for (KeyPair match : matches) {
|
||||
keyPairs.delete(key(region, match.getKeyPairId()));
|
||||
}
|
||||
return matches.isEmpty() ? null : matches.getFirst();
|
||||
}
|
||||
|
||||
public KeyPair importKeyPair(String region, String keyName, String publicKeyMaterial) {
|
||||
ensureDefaultResources(region);
|
||||
boolean exists = keyPairs.scan(k -> true).stream()
|
||||
.anyMatch(k -> k.getRegion().equals(region) && k.getKeyName().equals(keyName));
|
||||
if (exists) {
|
||||
throw new AwsException("InvalidKeyPair.Duplicate", "The keypair '" + keyName + "' already exists", 400);
|
||||
}
|
||||
requireKeyName(keyName);
|
||||
rejectDuplicateKeyName(region, keyName);
|
||||
String keyPairId = "key-" + randomHex(17);
|
||||
KeyPair kp = new KeyPair();
|
||||
kp.setKeyPairId(keyPairId);
|
||||
@@ -4656,6 +4655,22 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
return containerManager.isContainerRunning(instance.getDockerContainerId());
|
||||
}
|
||||
|
||||
private static void requireKeyName(String keyName) {
|
||||
if (keyName == null || keyName.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter KeyName", 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Compared name-first so a nameless record can never throw here. One such record, stored
|
||||
// before KeyName was validated, used to fail every later CreateKeyPair in the account (#3356).
|
||||
private void rejectDuplicateKeyName(String region, String keyName) {
|
||||
boolean exists = keyPairs.scan(k -> true).stream()
|
||||
.anyMatch(k -> region.equals(k.getRegion()) && keyName.equals(k.getKeyName()));
|
||||
if (exists) {
|
||||
throw new AwsException("InvalidKeyPair.Duplicate", "The keypair '" + keyName + "' already exists", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public KeyPair findKeyPair(String region, String keyName) {
|
||||
if (keyName == null) {
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user