2016-05-05 21:52:08 +02:00
|
|
|
"""
|
|
|
|
.. module: lemur.schemas
|
|
|
|
:platform: unix
|
2018-05-29 19:18:16 +02:00
|
|
|
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more
|
2016-05-05 21:52:08 +02:00
|
|
|
:license: Apache, see LICENSE for more details.
|
|
|
|
|
|
|
|
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
|
|
|
|
|
|
|
|
"""
|
2016-07-04 23:32:46 +02:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
2017-01-27 21:31:29 +01:00
|
|
|
from marshmallow import fields, post_load, pre_load, post_dump
|
2016-07-04 23:32:46 +02:00
|
|
|
from marshmallow.exceptions import ValidationError
|
2016-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
from lemur.common import validators
|
2016-05-12 21:38:44 +02:00
|
|
|
from lemur.common.schema import LemurSchema, LemurInputSchema, LemurOutputSchema
|
2019-05-16 16:57:02 +02:00
|
|
|
from lemur.common.fields import (
|
|
|
|
KeyUsageExtension,
|
|
|
|
ExtendedKeyUsageExtension,
|
|
|
|
BasicConstraintsExtension,
|
|
|
|
SubjectAlternativeNameExtension,
|
|
|
|
)
|
2016-07-04 23:32:46 +02:00
|
|
|
|
2016-05-05 21:52:08 +02:00
|
|
|
from lemur.plugins import plugins
|
2017-03-03 21:59:16 +01:00
|
|
|
from lemur.plugins.utils import get_plugin_option
|
2016-05-23 20:28:25 +02:00
|
|
|
from lemur.roles.models import Role
|
|
|
|
from lemur.users.models import User
|
2016-07-04 23:32:46 +02:00
|
|
|
from lemur.authorities.models import Authority
|
2018-05-29 19:18:16 +02:00
|
|
|
from lemur.dns_providers.models import DnsProvider
|
2017-07-13 01:46:11 +02:00
|
|
|
from lemur.policies.models import RotationPolicy
|
2016-07-04 23:32:46 +02:00
|
|
|
from lemur.certificates.models import Certificate
|
|
|
|
from lemur.destinations.models import Destination
|
|
|
|
from lemur.notifications.models import Notification
|
|
|
|
|
|
|
|
|
2017-03-03 21:59:16 +01:00
|
|
|
def validate_options(options):
|
|
|
|
"""
|
|
|
|
Ensures that the plugin options are valid.
|
|
|
|
:param options:
|
|
|
|
:return:
|
|
|
|
"""
|
2019-05-16 16:57:02 +02:00
|
|
|
interval = get_plugin_option("interval", options)
|
|
|
|
unit = get_plugin_option("unit", options)
|
2017-03-03 21:59:16 +01:00
|
|
|
|
2017-03-07 05:38:04 +01:00
|
|
|
if not interval and not unit:
|
|
|
|
return
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
if unit == "month":
|
2017-04-30 21:23:34 +02:00
|
|
|
interval *= 30
|
2017-03-03 21:59:16 +01:00
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
elif unit == "week":
|
2017-04-30 21:23:34 +02:00
|
|
|
interval *= 7
|
2017-03-03 21:59:16 +01:00
|
|
|
|
2017-04-30 21:23:34 +02:00
|
|
|
if interval > 90:
|
2019-05-16 16:57:02 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"Notification cannot be more than 90 days into the future."
|
|
|
|
)
|
2017-03-03 21:59:16 +01:00
|
|
|
|
|
|
|
|
2016-10-16 12:56:13 +02:00
|
|
|
def get_object_attribute(data, many=False):
|
|
|
|
if many:
|
2019-05-16 16:57:02 +02:00
|
|
|
ids = [d.get("id") for d in data]
|
|
|
|
names = [d.get("name") for d in data]
|
2016-10-16 12:56:13 +02:00
|
|
|
|
|
|
|
if None in ids:
|
|
|
|
if None in names:
|
2019-05-16 16:57:02 +02:00
|
|
|
raise ValidationError("Associated object require a name or id.")
|
2016-10-16 12:56:13 +02:00
|
|
|
else:
|
2019-05-16 16:57:02 +02:00
|
|
|
return "name"
|
|
|
|
return "id"
|
2016-10-16 12:56:13 +02:00
|
|
|
else:
|
2019-05-16 16:57:02 +02:00
|
|
|
if data.get("id"):
|
|
|
|
return "id"
|
|
|
|
elif data.get("name"):
|
|
|
|
return "name"
|
2016-10-16 12:56:13 +02:00
|
|
|
else:
|
2019-05-16 16:57:02 +02:00
|
|
|
raise ValidationError("Associated object require a name or id.")
|
2016-07-04 23:32:46 +02:00
|
|
|
|
|
|
|
|
2016-10-16 12:56:13 +02:00
|
|
|
def fetch_objects(model, data, many=False):
|
|
|
|
attr = get_object_attribute(data, many=many)
|
2016-07-04 23:32:46 +02:00
|
|
|
|
2016-10-16 12:56:13 +02:00
|
|
|
if many:
|
|
|
|
values = [v[attr] for v in data]
|
|
|
|
items = model.query.filter(getattr(model, attr).in_(values)).all()
|
|
|
|
found = [getattr(i, attr) for i in items]
|
|
|
|
diff = set(values).symmetric_difference(set(found))
|
2016-07-04 23:32:46 +02:00
|
|
|
|
2016-10-16 12:56:13 +02:00
|
|
|
if diff:
|
2019-05-16 16:57:02 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"Unable to locate {model} with {attr} {diff}".format(
|
|
|
|
model=model, attr=attr, diff=",".join(list(diff))
|
|
|
|
)
|
|
|
|
)
|
2016-10-16 12:56:13 +02:00
|
|
|
|
|
|
|
return items
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return model.query.filter(getattr(model, attr) == data[attr]).one()
|
|
|
|
except NoResultFound:
|
2019-05-16 16:57:02 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"Unable to find {model} with {attr}: {data}".format(
|
|
|
|
model=model, attr=attr, data=data[attr]
|
|
|
|
)
|
|
|
|
)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AssociatedAuthoritySchema(LemurInputSchema):
|
2016-05-09 20:00:16 +02:00
|
|
|
id = fields.Int()
|
|
|
|
name = fields.String()
|
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2016-10-16 12:56:13 +02:00
|
|
|
return fetch_objects(Authority, data, many=many)
|
2016-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
|
2018-05-10 21:48:49 +02:00
|
|
|
class AssociatedDnsProviderSchema(LemurInputSchema):
|
|
|
|
id = fields.Int()
|
|
|
|
name = fields.String()
|
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2018-05-29 19:18:16 +02:00
|
|
|
return fetch_objects(DnsProvider, data, many=many)
|
2018-05-10 21:48:49 +02:00
|
|
|
|
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
class AssociatedRoleSchema(LemurInputSchema):
|
2016-10-16 12:56:13 +02:00
|
|
|
id = fields.Int()
|
2016-05-09 20:00:16 +02:00
|
|
|
name = fields.String()
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2016-10-16 12:56:13 +02:00
|
|
|
return fetch_objects(Role, data, many=many)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AssociatedDestinationSchema(LemurInputSchema):
|
2016-10-16 12:56:13 +02:00
|
|
|
id = fields.Int()
|
2016-05-09 20:00:16 +02:00
|
|
|
name = fields.String()
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2016-10-16 12:56:13 +02:00
|
|
|
return fetch_objects(Destination, data, many=many)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AssociatedNotificationSchema(LemurInputSchema):
|
2016-10-16 12:56:13 +02:00
|
|
|
id = fields.Int()
|
|
|
|
name = fields.String()
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2016-10-16 12:56:13 +02:00
|
|
|
return fetch_objects(Notification, data, many=many)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AssociatedCertificateSchema(LemurInputSchema):
|
2016-10-16 12:56:13 +02:00
|
|
|
id = fields.Int()
|
|
|
|
name = fields.String()
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2016-10-16 12:56:13 +02:00
|
|
|
return fetch_objects(Certificate, data, many=many)
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
|
2016-05-10 23:22:22 +02:00
|
|
|
class AssociatedUserSchema(LemurInputSchema):
|
2016-10-16 12:56:13 +02:00
|
|
|
id = fields.Int()
|
|
|
|
name = fields.String()
|
2016-05-10 23:22:22 +02:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2016-10-16 12:56:13 +02:00
|
|
|
return fetch_objects(User, data, many=many)
|
2016-05-10 23:22:22 +02:00
|
|
|
|
|
|
|
|
2017-07-13 01:46:11 +02:00
|
|
|
class AssociatedRotationPolicySchema(LemurInputSchema):
|
|
|
|
id = fields.Int()
|
|
|
|
name = fields.String()
|
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
|
|
|
return fetch_objects(RotationPolicy, data, many=many)
|
|
|
|
|
|
|
|
|
2016-05-12 21:38:44 +02:00
|
|
|
class PluginInputSchema(LemurInputSchema):
|
2017-03-03 21:59:16 +01:00
|
|
|
plugin_options = fields.List(fields.Dict(), validate=validate_options)
|
2016-05-12 21:38:44 +02:00
|
|
|
slug = fields.String(required=True)
|
2016-05-10 20:27:57 +02:00
|
|
|
title = fields.String()
|
|
|
|
description = fields.String()
|
2016-05-05 21:52:08 +02:00
|
|
|
|
|
|
|
@post_load
|
|
|
|
def get_object(self, data, many=False):
|
2016-07-04 23:32:46 +02:00
|
|
|
try:
|
2019-05-16 16:57:02 +02:00
|
|
|
data["plugin_object"] = plugins.get(data["slug"])
|
2017-06-26 21:03:24 +02:00
|
|
|
|
|
|
|
# parse any sub-plugins
|
2019-05-16 16:57:02 +02:00
|
|
|
for option in data.get("plugin_options", []):
|
|
|
|
if "plugin" in option.get("type", []):
|
|
|
|
sub_data, errors = PluginInputSchema().load(option["value"])
|
|
|
|
option["value"] = sub_data
|
2017-06-26 21:03:24 +02:00
|
|
|
|
2016-07-04 23:32:46 +02:00
|
|
|
return data
|
2017-06-26 21:03:24 +02:00
|
|
|
except Exception as e:
|
2019-05-16 16:57:02 +02:00
|
|
|
raise ValidationError(
|
|
|
|
"Unable to find plugin. Slug: {0} Reason: {1}".format(data["slug"], e)
|
|
|
|
)
|
2016-05-12 21:38:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PluginOutputSchema(LemurOutputSchema):
|
|
|
|
id = fields.Integer()
|
|
|
|
label = fields.String()
|
|
|
|
description = fields.String()
|
|
|
|
active = fields.Boolean()
|
2019-05-16 16:57:02 +02:00
|
|
|
options = fields.List(fields.Dict(), dump_to="pluginOptions")
|
2016-05-12 21:38:44 +02:00
|
|
|
slug = fields.String()
|
|
|
|
title = fields.String()
|
2016-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
|
2016-05-13 23:35:38 +02:00
|
|
|
plugins_output_schema = PluginOutputSchema(many=True)
|
|
|
|
plugin_output_schema = PluginOutputSchema
|
|
|
|
|
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
class BaseExtensionSchema(LemurSchema):
|
|
|
|
@pre_load(pass_many=True)
|
|
|
|
def preprocess(self, data, many):
|
|
|
|
return self.under(data, many=many)
|
|
|
|
|
|
|
|
@post_dump(pass_many=True)
|
|
|
|
def post_process(self, data, many):
|
|
|
|
if data:
|
|
|
|
data = self.camel(data, many=many)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class AuthorityKeyIdentifierSchema(BaseExtensionSchema):
|
|
|
|
use_key_identifier = fields.Boolean()
|
2017-01-18 23:16:19 +01:00
|
|
|
use_authority_cert = fields.Boolean()
|
2016-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CertificateInfoAccessSchema(BaseExtensionSchema):
|
|
|
|
include_aia = fields.Boolean()
|
|
|
|
|
|
|
|
@post_dump
|
|
|
|
def handle_keys(self, data):
|
2019-05-16 16:57:02 +02:00
|
|
|
return {"includeAIA": data["include_aia"]}
|
2016-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
|
2018-03-23 16:51:18 +01:00
|
|
|
class CRLDistributionPointsSchema(BaseExtensionSchema):
|
|
|
|
include_crl_dp = fields.String()
|
|
|
|
|
|
|
|
@post_dump
|
|
|
|
def handle_keys(self, data):
|
2019-05-16 16:57:02 +02:00
|
|
|
return {"includeCRLDP": data["include_crl_dp"]}
|
2018-03-23 16:51:18 +01:00
|
|
|
|
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
class SubjectKeyIdentifierSchema(BaseExtensionSchema):
|
|
|
|
include_ski = fields.Boolean()
|
|
|
|
|
|
|
|
@post_dump
|
|
|
|
def handle_keys(self, data):
|
2019-05-16 16:57:02 +02:00
|
|
|
return {"includeSKI": data["include_ski"]}
|
2016-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CustomOIDSchema(BaseExtensionSchema):
|
|
|
|
oid = fields.String()
|
2016-06-09 01:41:31 +02:00
|
|
|
encoding = fields.String(validate=validators.encoding)
|
2016-05-09 20:00:16 +02:00
|
|
|
value = fields.String()
|
2017-01-18 23:20:44 +01:00
|
|
|
is_critical = fields.Boolean()
|
2016-05-09 20:00:16 +02:00
|
|
|
|
|
|
|
|
2017-01-28 06:05:25 +01:00
|
|
|
class NamesSchema(BaseExtensionSchema):
|
|
|
|
names = SubjectAlternativeNameExtension()
|
|
|
|
|
|
|
|
|
2016-05-09 20:00:16 +02:00
|
|
|
class ExtensionSchema(BaseExtensionSchema):
|
2019-05-16 16:57:02 +02:00
|
|
|
basic_constraints = (
|
|
|
|
BasicConstraintsExtension()
|
|
|
|
) # some devices balk on default basic constraints
|
2017-01-27 21:31:29 +01:00
|
|
|
key_usage = KeyUsageExtension()
|
|
|
|
extended_key_usage = ExtendedKeyUsageExtension()
|
2016-05-09 20:00:16 +02:00
|
|
|
subject_key_identifier = fields.Nested(SubjectKeyIdentifierSchema)
|
2017-01-28 06:05:25 +01:00
|
|
|
sub_alt_names = fields.Nested(NamesSchema)
|
2016-05-09 20:00:16 +02:00
|
|
|
authority_key_identifier = fields.Nested(AuthorityKeyIdentifierSchema)
|
|
|
|
certificate_info_access = fields.Nested(CertificateInfoAccessSchema)
|
2019-05-16 16:57:02 +02:00
|
|
|
crl_distribution_points = fields.Nested(
|
|
|
|
CRLDistributionPointsSchema, dump_to="cRL_distribution_points"
|
|
|
|
)
|
2017-01-27 21:31:29 +01:00
|
|
|
# FIXME: Convert custom OIDs to a custom field in fields.py like other Extensions
|
|
|
|
# FIXME: Remove support in UI for Critical custom extensions https://github.com/Netflix/lemur/issues/665
|
2016-05-09 20:00:16 +02:00
|
|
|
custom = fields.List(fields.Nested(CustomOIDSchema))
|
2016-06-27 23:40:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EndpointNestedOutputSchema(LemurOutputSchema):
|
|
|
|
__envelope__ = False
|
|
|
|
id = fields.Integer()
|
|
|
|
description = fields.String()
|
|
|
|
name = fields.String()
|
|
|
|
dnsname = fields.String()
|
|
|
|
owner = fields.Email()
|
|
|
|
type = fields.String()
|
|
|
|
active = fields.Boolean()
|