Skip to content

Commit

Permalink
EC2: Fix inconsistent private/public key pair attribute (#7732)
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-nadkarni authored May 30, 2024
1 parent 4c098ae commit e2e4ce4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 7 additions & 4 deletions moto/ec2/models/key_pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ def __init__(
self,
name: str,
fingerprint: str,
material: str,
material: Optional[str],
material_public: str,
tags: Dict[str, str],
ec2_backend: Any,
):
self.id = random_key_pair_id()
self.name = name
self.fingerprint = fingerprint
self.material = material
self.fingerprint = fingerprint # public key fingerprint
self.material = material # PEM encoded private key
self.material_public = material_public # public key in OpenSSH format
self.create_time = utcnow()
self.ec2_backend = ec2_backend
self.add_tags(tags or {})
Expand Down Expand Up @@ -108,7 +110,8 @@ def import_key_pair(
fingerprint = public_key_fingerprint(public_key)
keypair = KeyPair(
key_name,
material=public_key_material,
material_public=public_key_material,
material=None,
fingerprint=fingerprint,
tags=tags,
ec2_backend=self,
Expand Down
16 changes: 14 additions & 2 deletions moto/ec2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,17 @@ def random_ed25519_key_pair() -> Dict[str, str]:
format=serialization.PrivateFormat.OpenSSH,
encryption_algorithm=serialization.NoEncryption(),
)
fingerprint = public_key_fingerprint(private_key.public_key())
public_key = private_key.public_key()
public_key_material = public_key.public_bytes(
encoding=serialization.Encoding.OpenSSH,
format=serialization.PublicFormat.OpenSSH,
)
fingerprint = public_key_fingerprint(public_key)

return {
"fingerprint": fingerprint,
"material": private_key_material.decode("ascii"),
"material_public": public_key_material.decode("ascii"),
}


Expand All @@ -586,11 +592,17 @@ def random_rsa_key_pair() -> Dict[str, str]:
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
fingerprint = public_key_fingerprint(private_key.public_key())
public_key = private_key.public_key()
public_key_material = public_key.public_bytes(
encoding=serialization.Encoding.OpenSSH,
format=serialization.PublicFormat.OpenSSH,
)
fingerprint = public_key_fingerprint(public_key)

return {
"fingerprint": fingerprint,
"material": private_key_material.decode("ascii"),
"material_public": public_key_material.decode("ascii"),
}


Expand Down

0 comments on commit e2e4ce4

Please sign in to comment.