2016-05-05 21:52:08 +02:00
|
|
|
"""
|
|
|
|
.. module: lemur.certificates.schemas
|
|
|
|
:platform: unix
|
|
|
|
:copyright: (c) 2015 by Netflix Inc., see AUTHORS for more
|
|
|
|
:license: Apache, see LICENSE for more details.
|
|
|
|
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
|
|
|
|
"""
|
|
|
|
from flask import current_app
|
2016-06-29 18:05:39 +02:00
|
|
|
from marshmallow import fields, validates_schema, post_load, pre_load
|
2016-05-05 21:52:08 +02:00
|
|
|
from marshmallow.exceptions import ValidationError
|
|
|
|
|
|
|
|
from lemur.schemas import AssociatedAuthoritySchema, AssociatedDestinationSchema, AssociatedCertificateSchema, \
|
2016-06-27 23:40:46 +02:00
|
|
|
AssociatedNotificationSchema, PluginInputSchema, ExtensionSchema, AssociatedRoleSchema, EndpointNestedOutputSchema
|
2016-05-16 20:09:50 +02:00
|
|
|
|
|
|
|
from lemur.authorities.schemas import AuthorityNestedOutputSchema
|
|
|
|
from lemur.destinations.schemas import DestinationNestedOutputSchema
|
|
|
|
from lemur.notifications.schemas import NotificationNestedOutputSchema
|
2016-05-20 21:48:12 +02:00
|
|
|
from lemur.roles.schemas import RoleNestedOutputSchema
|
2016-05-23 20:28:25 +02:00
|
|
|
from lemur.domains.schemas import DomainNestedOutputSchema
|
2016-05-16 20:09:50 +02:00
|
|
|
from lemur.users.schemas import UserNestedOutputSchema
|
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
from lemur.common.schema import LemurInputSchema, LemurOutputSchema
|
2016-06-29 18:05:39 +02:00
|
|
|
from lemur.common import validators, missing
|
2016-05-20 18:03:34 +02:00
|
|
|
from lemur.notifications import service as notification_service
|
2016-05-05 21:52:08 +02:00
|
|
|
|
2016-11-09 19:56:22 +01:00
|
|
|
from lemur.common.fields import ArrowDateTime
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
|
2016-05-20 18:03:34 +02:00
|
|
|
class CertificateSchema(LemurInputSchema):
|
2016-05-05 21:52:08 +02:00
|
|
|
owner = fields.Email(required=True)
|
|
|
|
description = fields.String()
|
2016-05-20 18:03:34 +02:00
|
|
|
|
2016-07-07 20:44:11 +02:00
|
|
|
|
|
|
|
class CertificateCreationSchema(CertificateSchema):
|
2016-05-20 18:03:34 +02:00
|
|
|
@post_load
|
2016-07-07 20:44:11 +02:00
|
|
|
def default_notification(self, data):
|
2016-05-20 18:03:34 +02:00
|
|
|
if not data['notifications']:
|
|
|
|
notification_name = "DEFAULT_{0}".format(data['owner'].split('@')[0].upper())
|
|
|
|
data['notifications'] += notification_service.create_default_expiration_notifications(notification_name, [data['owner']])
|
|
|
|
|
|
|
|
notification_name = 'DEFAULT_SECURITY'
|
|
|
|
data['notifications'] += notification_service.create_default_expiration_notifications(notification_name, current_app.config.get('LEMUR_SECURITY_TEAM_EMAIL'))
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2016-07-07 20:44:11 +02:00
|
|
|
class CertificateInputSchema(CertificateCreationSchema):
|
2016-05-20 18:03:34 +02:00
|
|
|
name = fields.String()
|
2016-05-09 20:00:16 +02:00
|
|
|
common_name = fields.String(required=True, validate=validators.sensitive_domain)
|
2016-05-05 21:52:08 +02:00
|
|
|
authority = fields.Nested(AssociatedAuthoritySchema, required=True)
|
|
|
|
|
2016-11-09 19:56:22 +01:00
|
|
|
validity_start = ArrowDateTime()
|
|
|
|
validity_end = ArrowDateTime()
|
2016-05-05 21:52:08 +02:00
|
|
|
validity_years = fields.Integer()
|
|
|
|
|
|
|
|
destinations = fields.Nested(AssociatedDestinationSchema, missing=[], many=True)
|
|
|
|
notifications = fields.Nested(AssociatedNotificationSchema, missing=[], many=True)
|
|
|
|
replacements = fields.Nested(AssociatedCertificateSchema, missing=[], many=True)
|
2016-05-20 21:48:12 +02:00
|
|
|
roles = fields.Nested(AssociatedRoleSchema, missing=[], many=True)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
csr = fields.String(validate=validators.csr)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
2016-11-12 18:29:42 +01:00
|
|
|
notify = fields.Boolean(default=True)
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
# certificate body fields
|
|
|
|
organizational_unit = fields.String(missing=lambda: current_app.config.get('LEMUR_DEFAULT_ORGANIZATIONAL_UNIT'))
|
|
|
|
organization = fields.String(missing=lambda: current_app.config.get('LEMUR_DEFAULT_ORGANIZATION'))
|
|
|
|
location = fields.String(missing=lambda: current_app.config.get('LEMUR_DEFAULT_LOCATION'))
|
|
|
|
country = fields.String(missing=lambda: current_app.config.get('LEMUR_DEFAULT_COUNTRY'))
|
|
|
|
state = fields.String(missing=lambda: current_app.config.get('LEMUR_DEFAULT_STATE'))
|
|
|
|
|
|
|
|
extensions = fields.Nested(ExtensionSchema)
|
|
|
|
|
|
|
|
@validates_schema
|
|
|
|
def validate_dates(self, data):
|
2016-05-09 20:00:16 +02:00
|
|
|
validators.dates(data)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
2016-06-29 18:05:39 +02:00
|
|
|
@pre_load
|
|
|
|
def ensure_dates(self, data):
|
2016-10-15 09:04:35 +02:00
|
|
|
return missing.convert_validity_years(data)
|
2016-06-29 18:05:39 +02:00
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
|
2016-05-20 18:03:34 +02:00
|
|
|
class CertificateEditInputSchema(CertificateSchema):
|
2016-10-15 09:12:11 +02:00
|
|
|
notify = fields.Boolean()
|
2016-10-18 08:23:14 +02:00
|
|
|
owner = fields.String()
|
2016-05-16 20:09:50 +02:00
|
|
|
destinations = fields.Nested(AssociatedDestinationSchema, missing=[], many=True)
|
|
|
|
notifications = fields.Nested(AssociatedNotificationSchema, missing=[], many=True)
|
|
|
|
replacements = fields.Nested(AssociatedCertificateSchema, missing=[], many=True)
|
2016-05-20 21:48:12 +02:00
|
|
|
roles = fields.Nested(AssociatedRoleSchema, missing=[], many=True)
|
2016-10-18 08:23:14 +02:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def enforce_notifications(self, data):
|
|
|
|
"""
|
|
|
|
Ensures that when an owner changes, default notifications are added for the new owner.
|
|
|
|
Old owner notifications are retained unless explicitly removed.
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if data['owner']:
|
|
|
|
notification_name = "DEFAULT_{0}".format(data['owner'].split('@')[0].upper())
|
|
|
|
data['notifications'] += notification_service.create_default_expiration_notifications(notification_name, [data['owner']])
|
|
|
|
return data
|
2016-05-16 20:09:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CertificateNestedOutputSchema(LemurOutputSchema):
|
|
|
|
__envelope__ = False
|
|
|
|
id = fields.Integer()
|
|
|
|
active = fields.Boolean()
|
|
|
|
bits = fields.Integer()
|
|
|
|
body = fields.String()
|
|
|
|
chain = fields.String()
|
|
|
|
description = fields.String()
|
|
|
|
name = fields.String()
|
2016-11-18 01:19:52 +01:00
|
|
|
|
|
|
|
# Note aliasing is the first step in deprecating these fields.
|
2016-05-16 20:09:50 +02:00
|
|
|
cn = fields.String()
|
2016-11-18 01:19:52 +01:00
|
|
|
common_name = fields.String(attribute='cn')
|
|
|
|
|
2016-05-16 20:09:50 +02:00
|
|
|
not_after = fields.DateTime()
|
2016-11-18 01:19:52 +01:00
|
|
|
validity_end = ArrowDateTime(attribute='not_after')
|
|
|
|
|
2016-05-16 20:09:50 +02:00
|
|
|
not_before = fields.DateTime()
|
2016-11-18 01:19:52 +01:00
|
|
|
validity_start = ArrowDateTime(attribute='not_before')
|
|
|
|
|
2016-05-16 20:09:50 +02:00
|
|
|
owner = fields.Email()
|
|
|
|
status = fields.Boolean()
|
|
|
|
creator = fields.Nested(UserNestedOutputSchema)
|
|
|
|
issuer = fields.Nested(AuthorityNestedOutputSchema)
|
|
|
|
|
|
|
|
|
2016-10-31 19:00:15 +01:00
|
|
|
class CertificateCloneSchema(LemurOutputSchema):
|
|
|
|
__envelope__ = False
|
|
|
|
description = fields.String()
|
|
|
|
common_name = fields.String()
|
|
|
|
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
class CertificateOutputSchema(LemurOutputSchema):
|
|
|
|
id = fields.Integer()
|
|
|
|
bits = fields.Integer()
|
|
|
|
body = fields.String()
|
|
|
|
chain = fields.String()
|
|
|
|
deleted = fields.Boolean(default=False)
|
|
|
|
description = fields.String()
|
|
|
|
issuer = fields.String()
|
|
|
|
name = fields.String()
|
2016-11-18 01:19:52 +01:00
|
|
|
|
|
|
|
# Note aliasing is the first step in deprecating these fields.
|
|
|
|
notify = fields.Boolean()
|
|
|
|
active = fields.Boolean(attribute='notify')
|
|
|
|
|
2016-05-16 20:09:50 +02:00
|
|
|
cn = fields.String()
|
2016-11-18 01:19:52 +01:00
|
|
|
common_name = fields.String(attribute='cn')
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
not_after = fields.DateTime()
|
2016-11-18 01:19:52 +01:00
|
|
|
validity_end = ArrowDateTime(attribute='not_after')
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
not_before = fields.DateTime()
|
2016-11-18 01:19:52 +01:00
|
|
|
validity_start = ArrowDateTime(attribute='not_before')
|
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
owner = fields.Email()
|
|
|
|
san = fields.Boolean()
|
|
|
|
serial = fields.String()
|
|
|
|
signing_algorithm = fields.String()
|
2016-11-18 01:19:52 +01:00
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
status = fields.Boolean()
|
2016-05-16 20:09:50 +02:00
|
|
|
user = fields.Nested(UserNestedOutputSchema)
|
2016-11-18 01:19:52 +01:00
|
|
|
|
|
|
|
extensions = fields.Nested(ExtensionSchema)
|
|
|
|
|
|
|
|
# associated objects
|
2016-05-25 02:10:19 +02:00
|
|
|
domains = fields.Nested(DomainNestedOutputSchema, many=True)
|
2016-05-16 20:09:50 +02:00
|
|
|
destinations = fields.Nested(DestinationNestedOutputSchema, many=True)
|
|
|
|
notifications = fields.Nested(NotificationNestedOutputSchema, many=True)
|
|
|
|
replaces = fields.Nested(CertificateNestedOutputSchema, many=True)
|
|
|
|
authority = fields.Nested(AuthorityNestedOutputSchema)
|
2016-05-20 21:48:12 +02:00
|
|
|
roles = fields.Nested(RoleNestedOutputSchema, many=True)
|
2016-06-27 23:40:46 +02:00
|
|
|
endpoints = fields.Nested(EndpointNestedOutputSchema, many=True, missing=[])
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
2016-07-07 20:44:11 +02:00
|
|
|
class CertificateUploadInputSchema(CertificateCreationSchema):
|
2016-05-05 21:52:08 +02:00
|
|
|
name = fields.String()
|
2016-10-15 09:12:11 +02:00
|
|
|
notify = fields.Boolean(missing=True)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
private_key = fields.String(validate=validators.private_key)
|
2016-05-20 18:03:34 +02:00
|
|
|
body = fields.String(required=True, validate=validators.public_certificate)
|
2016-11-17 23:47:10 +01:00
|
|
|
chain = fields.String(validate=validators.public_certificate, missing=None) # TODO this could be multiple certificates
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
destinations = fields.Nested(AssociatedDestinationSchema, missing=[], many=True)
|
|
|
|
notifications = fields.Nested(AssociatedNotificationSchema, missing=[], many=True)
|
|
|
|
replacements = fields.Nested(AssociatedCertificateSchema, missing=[], many=True)
|
2016-05-20 21:48:12 +02:00
|
|
|
roles = fields.Nested(AssociatedRoleSchema, missing=[], many=True)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
@validates_schema
|
|
|
|
def keys(self, data):
|
|
|
|
if data.get('destinations'):
|
|
|
|
if not data.get('private_key'):
|
|
|
|
raise ValidationError('Destinations require private key.')
|
|
|
|
|
|
|
|
|
|
|
|
class CertificateExportInputSchema(LemurInputSchema):
|
2016-05-17 00:59:40 +02:00
|
|
|
plugin = fields.Nested(PluginInputSchema)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
certificate_input_schema = CertificateInputSchema()
|
|
|
|
certificate_output_schema = CertificateOutputSchema()
|
|
|
|
certificates_output_schema = CertificateOutputSchema(many=True)
|
|
|
|
certificate_upload_input_schema = CertificateUploadInputSchema()
|
|
|
|
certificate_export_input_schema = CertificateExportInputSchema()
|
2016-05-16 20:09:50 +02:00
|
|
|
certificate_edit_input_schema = CertificateEditInputSchema()
|