Enabling hex serial numbers without breaking backward compatibility. (#779)

* Enabling hex serial numbers without breaking backward compatibility.

* Fixing tests.
This commit is contained in:
kevgliss 2017-05-05 11:04:09 -07:00 committed by GitHub
parent 381cd2e1ff
commit 9c9ca37586
4 changed files with 19 additions and 9 deletions

View File

@ -6,7 +6,7 @@
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com> .. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
""" """
from flask import current_app from flask import current_app
from marshmallow import fields, validate, validates_schema, post_load, pre_load, post_dump from marshmallow import fields, validate, validates_schema, post_load, pre_load
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
from lemur.schemas import AssociatedAuthoritySchema, AssociatedDestinationSchema, AssociatedCertificateSchema, \ from lemur.schemas import AssociatedAuthoritySchema, AssociatedDestinationSchema, AssociatedCertificateSchema, \
@ -23,7 +23,7 @@ from lemur.common.schema import LemurInputSchema, LemurOutputSchema
from lemur.common import validators, missing from lemur.common import validators, missing
from lemur.notifications import service as notification_service from lemur.notifications import service as notification_service
from lemur.common.fields import ArrowDateTime from lemur.common.fields import ArrowDateTime, Hex
class CertificateSchema(LemurInputSchema): class CertificateSchema(LemurInputSchema):
@ -181,6 +181,7 @@ class CertificateOutputSchema(LemurOutputSchema):
owner = fields.Email() owner = fields.Email()
san = fields.Boolean() san = fields.Boolean()
serial = fields.String() serial = fields.String()
serial_hex = Hex(attribute='serial')
signing_algorithm = fields.String() signing_algorithm = fields.String()
status = fields.Boolean() status = fields.Boolean()
@ -198,12 +199,6 @@ class CertificateOutputSchema(LemurOutputSchema):
endpoints = fields.Nested(EndpointNestedOutputSchema, many=True, missing=[]) endpoints = fields.Nested(EndpointNestedOutputSchema, many=True, missing=[])
replaced_by = fields.Nested(CertificateNestedOutputSchema, many=True, attribute='replaced') replaced_by = fields.Nested(CertificateNestedOutputSchema, many=True, attribute='replaced')
@post_dump
def convert_serial_to_hex(self, data):
if data:
data['serial'] = hex(int(data['serial']))[2:].upper()
return data
class CertificateUploadInputSchema(CertificateCreationSchema): class CertificateUploadInputSchema(CertificateCreationSchema):
name = fields.String() name = fields.String()

View File

@ -19,6 +19,16 @@ from marshmallow.fields import Field
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
class Hex(Field):
"""
A hex formatted string.
"""
def _serialize(self, value, attr, obj):
if value:
value = hex(int(value))[2:].upper()
return value
class ArrowDateTime(Field): class ArrowDateTime(Field):
"""A formatted datetime string in UTC. """A formatted datetime string in UTC.

View File

@ -108,6 +108,10 @@
<strong>Serial</strong> <strong>Serial</strong>
<span class="pull-right">{{ certificate.serial }}</span> <span class="pull-right">{{ certificate.serial }}</span>
</li> </li>
<li class="list-group-item">
<strong>Serial (Hex)</strong>
<span class="pull-right">{{ certificate.serialHex }}</span>
</li>
<li <li
uib-tooltip="Lemur will attempt to check a certificates validity, this is used to track whether a certificate as been revoked" uib-tooltip="Lemur will attempt to check a certificates validity, this is used to track whether a certificate as been revoked"
class="list-group-item"> class="list-group-item">

View File

@ -366,7 +366,8 @@ def test_certificate_get(client, token, status):
def test_certificate_get_body(client): def test_certificate_get_body(client):
response_body = client.get(api.url_for(Certificates, certificate_id=1), headers=VALID_USER_HEADER_TOKEN).json response_body = client.get(api.url_for(Certificates, certificate_id=1), headers=VALID_USER_HEADER_TOKEN).json
assert response_body['serial'] == "3E9" assert response_body['serial'] == '1001'
assert response_body['serialHex'] == '3E9'
@pytest.mark.parametrize("token,status", [ @pytest.mark.parametrize("token,status", [