EC2: Generate IDs for all key pairs (#5370)

This commit is contained in:
Viren Nadkarni 2022-08-11 02:59:54 +05:30 committed by GitHub
parent c4965d1e06
commit e9c5cb2308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 0 deletions

View File

@ -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

View File

@ -38,6 +38,7 @@ DESCRIBE_KEY_PAIRS_RESPONSE = """<DescribeKeyPairsResponse xmlns="http://ec2.ama
<keySet>
{% for keypair in keypairs %}
<item>
<keyPairId>{{ keypair.id }}</keyPairId>
<keyName>{{ keypair.name }}</keyName>
<keyFingerprint>{{ keypair.fingerprint }}</keyFingerprint>
</item>
@ -47,6 +48,7 @@ DESCRIBE_KEY_PAIRS_RESPONSE = """<DescribeKeyPairsResponse xmlns="http://ec2.ama
CREATE_KEY_PAIR_RESPONSE = """<CreateKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
<keyPairId>{{ keypair.id }}</keyPairId>
<keyName>{{ keypair.name }}</keyName>
<keyFingerprint>{{ keypair.fingerprint }}</keyFingerprint>
<keyMaterial>{{ keypair.material }}</keyMaterial>
@ -61,6 +63,7 @@ DELETE_KEY_PAIR_RESPONSE = """<DeleteKeyPairResponse xmlns="http://ec2.amazonaws
IMPORT_KEYPAIR_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<ImportKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
<requestId>471f9fdd-8fe2-4a84-86b0-bd3d3e350979</requestId>
<keyPairId>{{ keypair.id }}</keyPairId>
<keyName>{{ keypair.name }}</keyName>
<keyFingerprint>{{ keypair.fingerprint }}</keyFingerprint>
</ImportKeyPairResponse>"""

View File

@ -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"])

View File

@ -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)