From e9c5cb230870abfeec1e7d7a866322e6353e4555 Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Thu, 11 Aug 2022 02:59:54 +0530 Subject: [PATCH] EC2: Generate IDs for all key pairs (#5370) --- moto/ec2/models/key_pairs.py | 2 ++ moto/ec2/responses/key_pairs.py | 3 +++ moto/ec2/utils.py | 5 +++++ tests/test_ec2/test_key_pairs.py | 3 +++ 4 files changed, 13 insertions(+) diff --git a/moto/ec2/models/key_pairs.py b/moto/ec2/models/key_pairs.py index be895d049..99a46b456 100644 --- a/moto/ec2/models/key_pairs.py +++ b/moto/ec2/models/key_pairs.py @@ -10,11 +10,13 @@ from ..utils import ( rsa_public_key_fingerprint, rsa_public_key_parse, generic_filter, + random_key_pair_id, ) class KeyPair(BaseModel): def __init__(self, name, fingerprint, material): + self.id = random_key_pair_id() self.name = name self.fingerprint = fingerprint self.material = material diff --git a/moto/ec2/responses/key_pairs.py b/moto/ec2/responses/key_pairs.py index 5cacbce0e..041b3aaa0 100644 --- a/moto/ec2/responses/key_pairs.py +++ b/moto/ec2/responses/key_pairs.py @@ -38,6 +38,7 @@ DESCRIBE_KEY_PAIRS_RESPONSE = """ + {{ keypair.id }} {{ keypair.name }} {{ keypair.fingerprint }} {{ keypair.material }} @@ -61,6 +63,7 @@ DELETE_KEY_PAIR_RESPONSE = """ 471f9fdd-8fe2-4a84-86b0-bd3d3e350979 + {{ keypair.id }} {{ keypair.name }} {{ keypair.fingerprint }} """ diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 9511a7cfa..1a992586c 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -54,6 +54,7 @@ EC2_RESOURCE_TO_PREFIX = { "vpn-gateway": "vgw", "iam-instance-profile-association": "iip-assoc", "carrier-gateway": "cagw", + "key-pair": "key", } @@ -143,6 +144,10 @@ def random_volume_id(): return random_id(prefix=EC2_RESOURCE_TO_PREFIX["volume"]) +def random_key_pair_id(): + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["key-pair"]) + + def random_vpc_id(): return random_id(prefix=EC2_RESOURCE_TO_PREFIX["vpc"]) diff --git a/tests/test_ec2/test_key_pairs.py b/tests/test_ec2/test_key_pairs.py index 6ad6e69ab..5826dc500 100644 --- a/tests/test_ec2/test_key_pairs.py +++ b/tests/test_ec2/test_key_pairs.py @@ -98,6 +98,7 @@ def test_key_pairs_create_boto3(): kps = client.describe_key_pairs(KeyNames=[key_name])["KeyPairs"] kps.should.have.length_of(1) + kps[0].should.have.key("KeyPairId") kps[0].should.have.key("KeyName").equal(key_name) kps[0].should.have.key("KeyFingerprint") @@ -160,6 +161,7 @@ def test_key_pairs_import_boto3(): KeyName=key_name, PublicKeyMaterial=RSA_PUBLIC_KEY_OPENSSH ) + kp1.should.have.key("KeyPairId") kp1.should.have.key("KeyName").equal(key_name) kp1.should.have.key("KeyFingerprint").equal(RSA_PUBLIC_KEY_FINGERPRINT) @@ -167,6 +169,7 @@ def test_key_pairs_import_boto3(): kp2 = client.import_key_pair( KeyName=key_name2, PublicKeyMaterial=RSA_PUBLIC_KEY_RFC4716 ) + kp2.should.have.key("KeyPairId") kp2.should.have.key("KeyName").equal(key_name2) kp2.should.have.key("KeyFingerprint").equal(RSA_PUBLIC_KEY_FINGERPRINT)