Skip to content

Commit 964d9c7

Browse files
ANS-4: generate a new private key to sign the csr + return the key
1 parent 93479a1 commit 964d9c7

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

plugins/action/horizon_renew.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,33 @@ def run(self, tmp=None, task_vars=None):
3434

3535
# In pop renewal, generate empty csr in decentralized mode
3636
if should_generate_csr:
37-
key_data = client.load_file_or_string(content["private_key"])
38-
if isinstance(key_data, str):
39-
key_data = key_data.encode("utf-8")
40-
private_key = load_pem_private_key(key_data, None)
41-
csr = HorizonCrypto.generate_pckcs10(subject={"cn.1": ""}, private_key=private_key)
42-
content["csr"] = csr
37+
try:
38+
pem_data = client.load_file_or_string(content["certificate_pem"])
39+
key_type = HorizonCrypto.get_key_type(pem_data)
40+
private_key, public_key = HorizonCrypto.generate_key_pair(key_type)
41+
csr = HorizonCrypto.generate_pckcs10(subject={"cn.1": ""}, private_key=private_key)
42+
content['csr'] = csr
43+
except Exception as e:
44+
raise AnsibleError(e)
4345

4446
response = client.renew(**content)
4547

4648
if "certificate" in response:
4749
result["certificate"] = response["certificate"]
4850
result["chain"] = client.chain(result["certificate"]["certificate"])
4951

50-
if "pkcs12" in response.keys():
52+
if should_generate_csr:
53+
result["key"] = HorizonCrypto.get_key_bytes(private_key)
54+
p12, p12_password = HorizonCrypto.get_p12_from_key(result["key"], result["certificate"]["certificate"], content["password"])
55+
result["p12"] = p12
56+
result["p12_password"] = p12_password
57+
elif "pkcs12" in response.keys():
5158
result["p12"] = response["pkcs12"]["value"]
5259
result["p12_password"] = response["password"]["value"]
5360
result["key"] = HorizonCrypto.get_key_from_p12(response["pkcs12"]["value"],
5461
response["password"]["value"])
62+
63+
5564

5665
except HorizonError as e:
5766
raise AnsibleError(e.full_message)

plugins/module_utils/horizon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def certificate(self, certificate_pem, fields=None):
359359
:rtype: dict
360360
"""
361361
pem = self.load_file_or_string(certificate_pem)
362-
pem = urllib.parse.quote(pem, safe='')
362+
pem = urllib.parse.quote(str(pem), safe='')
363363

364364
response = self.get(self.CERTIFICATES_SHOW_URL + pem)
365365

plugins/module_utils/horizon_crypto.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,11 @@ def generate_jwt_token(cert, private_key, nonce=""):
172172

173173
return jwt_token
174174

175+
@staticmethod
176+
def get_key_type(pem_data):
177+
cert = x509.load_pem_x509_certificate(pem_data.encode('utf-8'))
178+
public_key = cert.public_key()
179+
if isinstance(public_key, rsa.RSAPublicKey):
180+
return "rsa-" + str(public_key.key_size)
181+
elif isinstance(public_key, ec.EllipticCurvePublicKey):
182+
return "ec-" + str(public_key.curve.name)

0 commit comments

Comments
 (0)