From 7a226241db381e36c0efffac777ca7319e51f3df Mon Sep 17 00:00:00 2001 From: sayali Date: Mon, 28 Sep 2020 18:13:00 -0700 Subject: [PATCH 1/2] Add key_type to CertificateUploadInputSchema Parse cert body to determine algo --- lemur/certificates/schemas.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lemur/certificates/schemas.py b/lemur/certificates/schemas.py index ac7add38..1e5fe6a6 100644 --- a/lemur/certificates/schemas.py +++ b/lemur/certificates/schemas.py @@ -326,6 +326,7 @@ class CertificateUploadInputSchema(CertificateCreationSchema): body = fields.String(required=True) chain = fields.String(missing=None, allow_none=True) csr = fields.String(required=False, allow_none=True, validate=validators.csr) + key_type = fields.String() destinations = fields.Nested(AssociatedDestinationSchema, missing=[], many=True) notifications = fields.Nested(AssociatedNotificationSchema, missing=[], many=True) @@ -373,6 +374,10 @@ class CertificateUploadInputSchema(CertificateCreationSchema): # Throws ValidationError validators.verify_cert_chain([cert] + chain) + @pre_load + def load_data(self, data): + data["key_type"] = utils.get_key_type_from_certificate(data["body"]) + class CertificateExportInputSchema(LemurInputSchema): plugin = fields.Nested(PluginInputSchema) From aaff0f7581a20add80c7fb778f7abd0236604c0b Mon Sep 17 00:00:00 2001 From: sayali Date: Mon, 28 Sep 2020 19:03:21 -0700 Subject: [PATCH 2/2] Fixing UT for key_type on upload schema --- lemur/certificates/schemas.py | 8 +++++++- lemur/tests/test_pending_certificates.py | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lemur/certificates/schemas.py b/lemur/certificates/schemas.py index 1e5fe6a6..f393aa49 100644 --- a/lemur/certificates/schemas.py +++ b/lemur/certificates/schemas.py @@ -376,7 +376,13 @@ class CertificateUploadInputSchema(CertificateCreationSchema): @pre_load def load_data(self, data): - data["key_type"] = utils.get_key_type_from_certificate(data["body"]) + if data.get("body"): + try: + data["key_type"] = utils.get_key_type_from_certificate(data["body"]) + except ValueError: + raise ValidationError( + "Public certificate presented is not valid.", field_names=["body"] + ) class CertificateExportInputSchema(LemurInputSchema): diff --git a/lemur/tests/test_pending_certificates.py b/lemur/tests/test_pending_certificates.py index 3e755574..3718ef0a 100644 --- a/lemur/tests/test_pending_certificates.py +++ b/lemur/tests/test_pending_certificates.py @@ -55,6 +55,7 @@ def test_create_pending(pending_certificate, user, session): assert real_cert.notify == pending_certificate.notify assert real_cert.private_key == pending_certificate.private_key assert real_cert.external_id == "54321" + assert real_cert.key_type == "RSA2048" @pytest.mark.parametrize(