2015-06-22 22:47:27 +02:00
|
|
|
"""
|
|
|
|
.. module: lemur.certificates.models
|
|
|
|
:platform: Unix
|
2018-05-29 19:18:16 +02:00
|
|
|
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more
|
2015-06-22 22:47:27 +02:00
|
|
|
:license: Apache, see LICENSE for more details.
|
|
|
|
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
|
|
|
|
"""
|
2017-07-13 01:46:11 +02:00
|
|
|
from datetime import timedelta
|
2015-08-04 06:07:28 +02:00
|
|
|
|
2018-10-11 20:36:50 +02:00
|
|
|
import arrow
|
2017-01-27 21:31:29 +01:00
|
|
|
from cryptography import x509
|
2016-12-02 00:41:53 +01:00
|
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
2018-10-11 20:36:50 +02:00
|
|
|
from flask import current_app
|
2017-03-15 16:41:06 +01:00
|
|
|
from idna.core import InvalidCodepoint
|
2018-10-11 20:36:50 +02:00
|
|
|
from sqlalchemy import event, Integer, ForeignKey, String, PassiveDefault, func, Column, Text, Boolean, Index
|
|
|
|
from sqlalchemy.ext.hybrid import hybrid_property
|
2016-05-19 22:37:05 +02:00
|
|
|
from sqlalchemy.orm import relationship
|
2017-07-13 01:46:11 +02:00
|
|
|
from sqlalchemy.sql.expression import case, extract
|
2016-12-28 18:52:23 +01:00
|
|
|
from sqlalchemy_utils.types.arrow import ArrowType
|
2018-06-19 17:41:12 +02:00
|
|
|
from werkzeug.utils import cached_property
|
2016-12-28 18:52:23 +01:00
|
|
|
|
2018-06-20 17:42:34 +02:00
|
|
|
from lemur.common import defaults, utils, validators
|
2018-01-03 00:26:31 +01:00
|
|
|
from lemur.constants import SUCCESS_METRIC_STATUS, FAILURE_METRIC_STATUS
|
2018-10-11 20:36:50 +02:00
|
|
|
from lemur.database import db
|
|
|
|
from lemur.domains.models import Domain
|
|
|
|
from lemur.extensions import metrics
|
|
|
|
from lemur.extensions import sentry
|
2015-08-02 00:29:34 +02:00
|
|
|
from lemur.models import certificate_associations, certificate_source_associations, \
|
2015-11-24 23:53:22 +01:00
|
|
|
certificate_destination_associations, certificate_notification_associations, \
|
Async Certificate Issuing using Pending Certificates (#1037)
* Add PendingCertificate model
This change creates a DB table called pending_certificates and
associated mapping relationship tables from pending certificate to
roles, rotation policy, destination, sources, etc.
The table is generated on initialization of Lemur. A pending
certificate holds most of the information of a Certificate, while it has
not be issued so that it can later backfill the information when the CA
has issued the certificate.
Change-Id: I277c16b776a71fe5edaf0fa0e76bbedc88924db0
Tickets: PBL-36499
* Create a PendingCertificate if cert is empty
IssuePlugins should return empty cert bodies if the request failed to
complete immediately (such as Digicert). This way, we can immediately
return the certificate, or if not just place into PendingCertificates
for later processing.
+ Fix relation from Certificate to Pending Certificate, as view only.
There is no real need for anything more than that since Pending cert
only needs to know the cert to replace when it is issued later.
+ Made PendingCertificate private key be empty: UI does not allow
private key on 'Create' but only on 'Import'. For Instart, we require
the private key but upstream does not necessarily need it. Thus, if
someone at Instart wants to create a CSR / key combo, they should
manually issue the cert themselves and import later. Otherwise you
should let Lemur generate that. This keeps the workflow transparent for
upstream Lemur users.
Change-Id: Ib74722a5ed5792d4b10ca702659422739c95ae26
Tickets: PBL-36343
* Fix empty private_key when create Pending Cert
On creation of a certificate with a CSR, there is no option for private
key. In this case, we actually have a dictionary with private_key as
key, but the value is None. This fixes the strip() called on NoneType.
Change-Id: I7b265564d8095bfc83d9d4cd14ae13fea3c03199
Tickets: PBL-36499
* Source sync finds and uses pending certificate
When a source syncs certificates, it will check for a pending
certificate. If that is found via external_id (given by digicert as
order_id) then it will use the found Pending Certificate's fields to
create a new certificate. Then the pending certificate is deleted.
Tickets: PBL-36343
Change-Id: I4f7959da29275ebc47a3996741f7e98d3e2d29d9
* Add Lemur static files and views for pending certs
This adds the basic static files to view pending certificates in a
table.
Tickets: PBL-36343
Change-Id: Ia4362e6664ec730d05d280c5ef5c815a6feda0d9
* Add CLI and plugin based pending fetch
This change uses the adds a new function to issuer plugins to fetch
certificates like source, but for one order. This way, we can control
which pending certificates to try and populate instead of getting all
certificates from source.
Tickets: PBL-36343
Change-Id: Ifc1747ccdc2cba09a81f298b31ddddebfee1b1d6
* Revert source using Pending Certificate
Tickets: PBL-36343
Change-Id: I05121bc951e0530d804070afdb9c9e09baa0bc51
* Fix PendingCertificate init getting authority id
Should get authority id from authority.id instead of the authority_id
key in kwargs.
Change-Id: Ie56df1a5fb0ab2729e91050f3ad1a831853e0623
Tickets: n/a
* Add fixtures and basic test for PendingCertificate
Change-Id: I4cca34105544d40dac1cc50a87bba93d8af9ab34
Tickets: PBL-36343
* Add User to create_certificate parameters
create_certificate now takes a User, which will be used to populate the
'creator' field in certificates.service.upload(). This allows the UI
populate with the current user if the owner does not exist in Lemur.
+ Fix chain being replaced with version from pending certificate, which
may be empty (depends on plugin implementation).
Change-Id: I516027b36bc643c4978b9c4890060569e03f3049
Tickets: n/a
* Fix permalink and filters to pending certs
Fixes the permalink button to get a single pending certificate
Add argument filter parsing for the pending certificate API
Fix comment on API usage
Added get_by_name for pending_certificate (currently unused, but useful
for CLI, instead of using IDs)
Change-Id: Iaa48909c45606bec65dfb193c13d6bd0e816f6db
Tickets: PBL-36910
* Update displayed fields for Pending Certificates
There are a number of unused / unpopulated fields from Certificate UI
that does apply to Pending Certificates. Those ones were removed, and
added other useful fields:
Owner, number of attempts to fetch and date created
Change-Id: I3010a715f0357ba149cf539a19fdb5974c5ce08b
Tickets: PBL-36910
* Add common name (cn) to Pending Certificate model
Fixes the UI missing the CN for Pending Certificate, as it was
originally being parsed from the generated certificate. In the case of
pending certificate, the CN from the user generates the request, which
means a pending cert can trust the original user putting in the CN
instead of having to parse the not-yet-generated certificate. There is
no real possibility to return a certificate from a pending certificate
where the CN has changed since it was initially ordered.
Change-Id: I88a4fa28116d5d8d293e58970d9777ce73fbb2ab
Tickets: PBL-36910
* Fix missing imports for service filter
+ Removed duplicate get_by_name function from old merge
Change-Id: I04ae6852533aa42988433338de74390e2868d69b
Tickets: PBL-36910
* Add private key viewing to Pending Certificates
Add private key API for Pending Certificates, with the same
authorization as Certificates (only owner, creator or owner-roles can
view private key).
Change-Id: Ie5175154a10fe0007cc0e9f35b80c0a01ed48d5b
Tickets: PBL-36910
* Add edit capability to pending certificates
Like editing certificates, we should be able to modify some parts of a
pending certificate so the resulting certificate has the right
references, owner, etc.
+ Added API to update pending certificate
+ Fix UI to use pending certificate scope instead of reusing Certificate
+ Change pending_certificate.replaces to non-passive association, so
that updates do affect it (similar to roles/notifications/etc)
Tickets: PBL-36910
Change-Id: Ibbcb166a33f0337e1b14f426472261222f790ce6
* Add common_name parsing instead using kwargs
To fix tests where common name may not be passed in, use the CSR
generated to find the official common name.
Change-Id: I09f9258fa92c2762d095798676ce210c5d7a3da4
Tickets: PBL-36343
* Add Cancel to pending certificates and plugins
This allows pending certificates to be cancelled, which will be handled
by the issuer plugin.
Change-Id: Ibd6b5627c3977e33aca7860690cfb7f677236ca9
Tickets: PBL-36910
* Add API for Cancelling Pending Certificate
Added the DELETE handler for pending_certificates, which will cancel and
delete the pending certificate from the pending certs table on
successful cancellation via Issuer Plugin.
+ Add UT for testing cancel API
Change-Id: I11b1d87872e4284f6e4f9c366a15da4ddba38bc4
Tickets: PBL-36910
* Remove Export from Pending Certificates
Pending Certificates doesn't need an export since it should just be
fetched by Lemur via plugins, and the CSR is viewable via the UI.
Change-Id: I9a3e65ea11ac5a85316f6428e7f526c3c09178ae
Tickets: PBL-36910
* Add cancel button functionality to UI
This adds the Cancel option to the dropdown of pending certificates.
+ Adds modal window for Note (may not be required for all issuers, just
Digicert)
+ Add schema for cancel input
+ Fix Digitcert plugin for non-existant orders
When an order is actually issued, then attempting to cancel will return
a 403 from Digicert. This is a case where it should only be done once
we know the pending cert has been sitting for too long.
Change-Id: I256c81ecd142dd51dcf8e38802d2c202829887b0
Tickets: PBL-36910
* Fix test_pending_cancel UT
This change creates and injects a pending cert, which will then be used
for the ID so it can be canceled by the unit test.
Change-Id: I686e7e0fafd68cdaeb26438fb8504d79de77c346
Tickets: PBL-36343
* Fix test_digicert on non-existent order
cancelling a non-existent order is fine since we're cancelling it
Change-Id: I70c0e82ba2f4b8723a7f65b113c19e6eeff7e68c
Tickets: PBL-36343
* Add migrations for PendingCertificates
Added revision for Pending Certificates table and foreign key mapping
tables.
Change-Id: Ife8202cef1e6b99db377851264639ba540b749db
Tickets: n/a
* Fix relationship copy from Pending to Certificate
When a Pending Certificate is changed to a full Certificate, the
relationship fields are not copied via vars() function, as it's not a
column but mapped via association table. This adds an explicit copy for
these relations. Which will properly copy them to the new Certificate,
and thus also update destinations.
Change-Id: I322032ce4a9e3e67773f7cf39ee4971054c92685
Tickets: PBL-36343
* Fix renaming of certificates and unit tests
The rename flag was not used to rename certificates on creation as
expected.
Fixed unit test, instead of expunging the session, just copy the
pending_certificate so we don't have a weird reference to the object
that can't be copied via vars() function.
Change-Id: I962943272ed92386ab6eab2af4ed6d074d4cffa0
Tickets: PBL-36343
* Updated developer docs for async certs
Added blurb for implementing new issuer functions.
Change-Id: I1caed6e914bcd73214eae2d241e4784e1b8a0c4c
Tickets: n/a
2018-02-22 17:13:16 +01:00
|
|
|
certificate_replacement_associations, roles_certificates, pending_cert_replacement_associations
|
2018-10-11 20:36:50 +02:00
|
|
|
from lemur.plugins.base import plugins
|
2017-07-13 01:46:11 +02:00
|
|
|
from lemur.policies.models import RotationPolicy
|
2018-10-11 20:36:50 +02:00
|
|
|
from lemur.utils import Vault
|
2015-06-22 22:47:27 +02:00
|
|
|
|
|
|
|
|
2017-01-06 01:06:34 +01:00
|
|
|
def get_sequence(name):
|
|
|
|
if '-' not in name:
|
|
|
|
return name, None
|
|
|
|
|
|
|
|
parts = name.split('-')
|
|
|
|
|
2017-10-11 22:20:19 +02:00
|
|
|
# see if we have an int at the end of our name
|
2017-01-06 01:06:34 +01:00
|
|
|
try:
|
2017-10-11 22:20:19 +02:00
|
|
|
seq = int(parts[-1])
|
2017-01-06 01:06:34 +01:00
|
|
|
except ValueError:
|
2017-10-11 22:20:19 +02:00
|
|
|
return name, None
|
2017-01-06 01:06:34 +01:00
|
|
|
|
2017-10-11 22:20:19 +02:00
|
|
|
# we might have a date at the end of our name
|
|
|
|
if len(parts[-1]) == 8:
|
|
|
|
return name, None
|
2017-01-06 01:06:34 +01:00
|
|
|
|
2017-10-11 22:20:19 +02:00
|
|
|
root = '-'.join(parts[:-1])
|
|
|
|
return root, seq
|
2017-01-06 01:06:34 +01:00
|
|
|
|
2017-10-11 22:20:19 +02:00
|
|
|
|
|
|
|
def get_or_increase_name(name, serial):
|
2017-01-06 01:06:34 +01:00
|
|
|
certificates = Certificate.query.filter(Certificate.name.ilike('{0}%'.format(name))).all()
|
|
|
|
|
|
|
|
if not certificates:
|
|
|
|
return name
|
2015-06-22 22:47:27 +02:00
|
|
|
|
2017-10-11 22:20:19 +02:00
|
|
|
serial_name = '{0}-{1}'.format(name, hex(int(serial))[2:].upper())
|
|
|
|
certificates = Certificate.query.filter(Certificate.name.ilike('{0}%'.format(serial_name))).all()
|
|
|
|
|
|
|
|
if not certificates:
|
|
|
|
return serial_name
|
|
|
|
|
2017-01-06 01:06:34 +01:00
|
|
|
ends = [0]
|
2017-10-11 22:20:19 +02:00
|
|
|
root, end = get_sequence(serial_name)
|
2017-01-06 01:06:34 +01:00
|
|
|
for cert in certificates:
|
|
|
|
root, end = get_sequence(cert.name)
|
|
|
|
if end:
|
|
|
|
ends.append(end)
|
2015-06-22 22:47:27 +02:00
|
|
|
|
2017-01-06 01:06:34 +01:00
|
|
|
return '{0}-{1}'.format(root, max(ends) + 1)
|
2015-06-22 22:47:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Certificate(db.Model):
|
|
|
|
__tablename__ = 'certificates'
|
2018-11-05 19:29:11 +01:00
|
|
|
__table_args__ = (
|
|
|
|
Index('ix_certificates_cn', "cn",
|
|
|
|
postgresql_ops={"cn": "gin_trgm_ops"},
|
|
|
|
postgresql_using='gin'),
|
|
|
|
Index('ix_certificates_name', "name",
|
|
|
|
postgresql_ops={"name": "gin_trgm_ops"},
|
|
|
|
postgresql_using='gin'),
|
|
|
|
)
|
2015-06-22 22:47:27 +02:00
|
|
|
id = Column(Integer, primary_key=True)
|
2018-10-11 20:36:50 +02:00
|
|
|
ix = Index('ix_certificates_id_desc', id.desc(), postgresql_using='btree', unique=True)
|
2017-09-29 03:27:56 +02:00
|
|
|
external_id = Column(String(128))
|
2016-05-19 22:37:05 +02:00
|
|
|
owner = Column(String(128), nullable=False)
|
2017-10-11 22:20:19 +02:00
|
|
|
name = Column(String(256), unique=True)
|
2016-05-19 22:37:05 +02:00
|
|
|
description = Column(String(1024))
|
2016-10-15 09:12:11 +02:00
|
|
|
notify = Column(Boolean, default=True)
|
2016-05-19 22:37:05 +02:00
|
|
|
|
|
|
|
body = Column(Text(), nullable=False)
|
2015-06-22 22:47:27 +02:00
|
|
|
chain = Column(Text())
|
2018-10-12 01:29:35 +02:00
|
|
|
csr = Column(Text())
|
2016-05-19 22:37:05 +02:00
|
|
|
private_key = Column(Vault)
|
|
|
|
|
2015-06-22 22:47:27 +02:00
|
|
|
issuer = Column(String(128))
|
|
|
|
serial = Column(String(128))
|
|
|
|
cn = Column(String(128))
|
2019-02-14 11:57:27 +01:00
|
|
|
deleted = Column(Boolean, index=True, default=False)
|
2018-08-13 23:22:59 +02:00
|
|
|
dns_provider_id = Column(Integer(), ForeignKey('dns_providers.id', ondelete='CASCADE'), nullable=True)
|
2016-05-19 22:37:05 +02:00
|
|
|
|
2016-11-18 20:27:46 +01:00
|
|
|
not_before = Column(ArrowType)
|
|
|
|
not_after = Column(ArrowType)
|
|
|
|
date_created = Column(ArrowType, PassiveDefault(func.now()), nullable=False)
|
2016-05-19 22:37:05 +02:00
|
|
|
|
2015-10-06 21:51:59 +02:00
|
|
|
signing_algorithm = Column(String(128))
|
2016-05-19 22:37:05 +02:00
|
|
|
status = Column(String(128))
|
|
|
|
bits = Column(Integer())
|
|
|
|
san = Column(String(1024)) # TODO this should be migrated to boolean
|
|
|
|
|
2016-12-27 00:55:11 +01:00
|
|
|
rotation = Column(Boolean, default=False)
|
2015-06-22 22:47:27 +02:00
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
2016-05-23 20:28:25 +02:00
|
|
|
authority_id = Column(Integer, ForeignKey('authorities.id', ondelete="CASCADE"))
|
|
|
|
root_authority_id = Column(Integer, ForeignKey('authorities.id', ondelete="CASCADE"))
|
2017-07-13 01:46:11 +02:00
|
|
|
rotation_policy_id = Column(Integer, ForeignKey('rotation_policies.id'))
|
2016-05-23 20:28:25 +02:00
|
|
|
|
2016-11-28 23:13:16 +01:00
|
|
|
notifications = relationship('Notification', secondary=certificate_notification_associations, backref='certificate')
|
|
|
|
destinations = relationship('Destination', secondary=certificate_destination_associations, backref='certificate')
|
|
|
|
sources = relationship('Source', secondary=certificate_source_associations, backref='certificate')
|
|
|
|
domains = relationship('Domain', secondary=certificate_associations, backref='certificate')
|
|
|
|
roles = relationship('Role', secondary=roles_certificates, backref='certificate')
|
|
|
|
replaces = relationship('Certificate',
|
2015-11-24 23:53:22 +01:00
|
|
|
secondary=certificate_replacement_associations,
|
|
|
|
primaryjoin=id == certificate_replacement_associations.c.certificate_id, # noqa
|
|
|
|
secondaryjoin=id == certificate_replacement_associations.c.replaced_certificate_id, # noqa
|
|
|
|
backref='replaced')
|
2015-06-22 22:47:27 +02:00
|
|
|
|
Async Certificate Issuing using Pending Certificates (#1037)
* Add PendingCertificate model
This change creates a DB table called pending_certificates and
associated mapping relationship tables from pending certificate to
roles, rotation policy, destination, sources, etc.
The table is generated on initialization of Lemur. A pending
certificate holds most of the information of a Certificate, while it has
not be issued so that it can later backfill the information when the CA
has issued the certificate.
Change-Id: I277c16b776a71fe5edaf0fa0e76bbedc88924db0
Tickets: PBL-36499
* Create a PendingCertificate if cert is empty
IssuePlugins should return empty cert bodies if the request failed to
complete immediately (such as Digicert). This way, we can immediately
return the certificate, or if not just place into PendingCertificates
for later processing.
+ Fix relation from Certificate to Pending Certificate, as view only.
There is no real need for anything more than that since Pending cert
only needs to know the cert to replace when it is issued later.
+ Made PendingCertificate private key be empty: UI does not allow
private key on 'Create' but only on 'Import'. For Instart, we require
the private key but upstream does not necessarily need it. Thus, if
someone at Instart wants to create a CSR / key combo, they should
manually issue the cert themselves and import later. Otherwise you
should let Lemur generate that. This keeps the workflow transparent for
upstream Lemur users.
Change-Id: Ib74722a5ed5792d4b10ca702659422739c95ae26
Tickets: PBL-36343
* Fix empty private_key when create Pending Cert
On creation of a certificate with a CSR, there is no option for private
key. In this case, we actually have a dictionary with private_key as
key, but the value is None. This fixes the strip() called on NoneType.
Change-Id: I7b265564d8095bfc83d9d4cd14ae13fea3c03199
Tickets: PBL-36499
* Source sync finds and uses pending certificate
When a source syncs certificates, it will check for a pending
certificate. If that is found via external_id (given by digicert as
order_id) then it will use the found Pending Certificate's fields to
create a new certificate. Then the pending certificate is deleted.
Tickets: PBL-36343
Change-Id: I4f7959da29275ebc47a3996741f7e98d3e2d29d9
* Add Lemur static files and views for pending certs
This adds the basic static files to view pending certificates in a
table.
Tickets: PBL-36343
Change-Id: Ia4362e6664ec730d05d280c5ef5c815a6feda0d9
* Add CLI and plugin based pending fetch
This change uses the adds a new function to issuer plugins to fetch
certificates like source, but for one order. This way, we can control
which pending certificates to try and populate instead of getting all
certificates from source.
Tickets: PBL-36343
Change-Id: Ifc1747ccdc2cba09a81f298b31ddddebfee1b1d6
* Revert source using Pending Certificate
Tickets: PBL-36343
Change-Id: I05121bc951e0530d804070afdb9c9e09baa0bc51
* Fix PendingCertificate init getting authority id
Should get authority id from authority.id instead of the authority_id
key in kwargs.
Change-Id: Ie56df1a5fb0ab2729e91050f3ad1a831853e0623
Tickets: n/a
* Add fixtures and basic test for PendingCertificate
Change-Id: I4cca34105544d40dac1cc50a87bba93d8af9ab34
Tickets: PBL-36343
* Add User to create_certificate parameters
create_certificate now takes a User, which will be used to populate the
'creator' field in certificates.service.upload(). This allows the UI
populate with the current user if the owner does not exist in Lemur.
+ Fix chain being replaced with version from pending certificate, which
may be empty (depends on plugin implementation).
Change-Id: I516027b36bc643c4978b9c4890060569e03f3049
Tickets: n/a
* Fix permalink and filters to pending certs
Fixes the permalink button to get a single pending certificate
Add argument filter parsing for the pending certificate API
Fix comment on API usage
Added get_by_name for pending_certificate (currently unused, but useful
for CLI, instead of using IDs)
Change-Id: Iaa48909c45606bec65dfb193c13d6bd0e816f6db
Tickets: PBL-36910
* Update displayed fields for Pending Certificates
There are a number of unused / unpopulated fields from Certificate UI
that does apply to Pending Certificates. Those ones were removed, and
added other useful fields:
Owner, number of attempts to fetch and date created
Change-Id: I3010a715f0357ba149cf539a19fdb5974c5ce08b
Tickets: PBL-36910
* Add common name (cn) to Pending Certificate model
Fixes the UI missing the CN for Pending Certificate, as it was
originally being parsed from the generated certificate. In the case of
pending certificate, the CN from the user generates the request, which
means a pending cert can trust the original user putting in the CN
instead of having to parse the not-yet-generated certificate. There is
no real possibility to return a certificate from a pending certificate
where the CN has changed since it was initially ordered.
Change-Id: I88a4fa28116d5d8d293e58970d9777ce73fbb2ab
Tickets: PBL-36910
* Fix missing imports for service filter
+ Removed duplicate get_by_name function from old merge
Change-Id: I04ae6852533aa42988433338de74390e2868d69b
Tickets: PBL-36910
* Add private key viewing to Pending Certificates
Add private key API for Pending Certificates, with the same
authorization as Certificates (only owner, creator or owner-roles can
view private key).
Change-Id: Ie5175154a10fe0007cc0e9f35b80c0a01ed48d5b
Tickets: PBL-36910
* Add edit capability to pending certificates
Like editing certificates, we should be able to modify some parts of a
pending certificate so the resulting certificate has the right
references, owner, etc.
+ Added API to update pending certificate
+ Fix UI to use pending certificate scope instead of reusing Certificate
+ Change pending_certificate.replaces to non-passive association, so
that updates do affect it (similar to roles/notifications/etc)
Tickets: PBL-36910
Change-Id: Ibbcb166a33f0337e1b14f426472261222f790ce6
* Add common_name parsing instead using kwargs
To fix tests where common name may not be passed in, use the CSR
generated to find the official common name.
Change-Id: I09f9258fa92c2762d095798676ce210c5d7a3da4
Tickets: PBL-36343
* Add Cancel to pending certificates and plugins
This allows pending certificates to be cancelled, which will be handled
by the issuer plugin.
Change-Id: Ibd6b5627c3977e33aca7860690cfb7f677236ca9
Tickets: PBL-36910
* Add API for Cancelling Pending Certificate
Added the DELETE handler for pending_certificates, which will cancel and
delete the pending certificate from the pending certs table on
successful cancellation via Issuer Plugin.
+ Add UT for testing cancel API
Change-Id: I11b1d87872e4284f6e4f9c366a15da4ddba38bc4
Tickets: PBL-36910
* Remove Export from Pending Certificates
Pending Certificates doesn't need an export since it should just be
fetched by Lemur via plugins, and the CSR is viewable via the UI.
Change-Id: I9a3e65ea11ac5a85316f6428e7f526c3c09178ae
Tickets: PBL-36910
* Add cancel button functionality to UI
This adds the Cancel option to the dropdown of pending certificates.
+ Adds modal window for Note (may not be required for all issuers, just
Digicert)
+ Add schema for cancel input
+ Fix Digitcert plugin for non-existant orders
When an order is actually issued, then attempting to cancel will return
a 403 from Digicert. This is a case where it should only be done once
we know the pending cert has been sitting for too long.
Change-Id: I256c81ecd142dd51dcf8e38802d2c202829887b0
Tickets: PBL-36910
* Fix test_pending_cancel UT
This change creates and injects a pending cert, which will then be used
for the ID so it can be canceled by the unit test.
Change-Id: I686e7e0fafd68cdaeb26438fb8504d79de77c346
Tickets: PBL-36343
* Fix test_digicert on non-existent order
cancelling a non-existent order is fine since we're cancelling it
Change-Id: I70c0e82ba2f4b8723a7f65b113c19e6eeff7e68c
Tickets: PBL-36343
* Add migrations for PendingCertificates
Added revision for Pending Certificates table and foreign key mapping
tables.
Change-Id: Ife8202cef1e6b99db377851264639ba540b749db
Tickets: n/a
* Fix relationship copy from Pending to Certificate
When a Pending Certificate is changed to a full Certificate, the
relationship fields are not copied via vars() function, as it's not a
column but mapped via association table. This adds an explicit copy for
these relations. Which will properly copy them to the new Certificate,
and thus also update destinations.
Change-Id: I322032ce4a9e3e67773f7cf39ee4971054c92685
Tickets: PBL-36343
* Fix renaming of certificates and unit tests
The rename flag was not used to rename certificates on creation as
expected.
Fixed unit test, instead of expunging the session, just copy the
pending_certificate so we don't have a weird reference to the object
that can't be copied via vars() function.
Change-Id: I962943272ed92386ab6eab2af4ed6d074d4cffa0
Tickets: PBL-36343
* Updated developer docs for async certs
Added blurb for implementing new issuer functions.
Change-Id: I1caed6e914bcd73214eae2d241e4784e1b8a0c4c
Tickets: n/a
2018-02-22 17:13:16 +01:00
|
|
|
replaced_by_pending = relationship('PendingCertificate',
|
|
|
|
secondary=pending_cert_replacement_associations,
|
|
|
|
backref='pending_replace',
|
|
|
|
viewonly=True)
|
|
|
|
|
2016-11-28 23:13:16 +01:00
|
|
|
logs = relationship('Log', backref='certificate')
|
|
|
|
endpoints = relationship('Endpoint', backref='certificate')
|
2017-07-13 01:46:11 +02:00
|
|
|
rotation_policy = relationship("RotationPolicy")
|
2017-08-16 18:38:42 +02:00
|
|
|
sensitive_fields = ('private_key',)
|
|
|
|
|
2016-05-19 22:37:05 +02:00
|
|
|
def __init__(self, **kwargs):
|
2018-06-19 17:41:12 +02:00
|
|
|
self.body = kwargs['body'].strip()
|
|
|
|
cert = self.parsed_cert
|
2016-06-04 03:45:58 +02:00
|
|
|
|
|
|
|
self.issuer = defaults.issuer(cert)
|
|
|
|
self.cn = defaults.common_name(cert)
|
|
|
|
self.san = defaults.san(cert)
|
|
|
|
self.not_before = defaults.not_before(cert)
|
|
|
|
self.not_after = defaults.not_after(cert)
|
2017-10-11 22:20:19 +02:00
|
|
|
self.serial = defaults.serial(cert)
|
2016-06-04 03:45:58 +02:00
|
|
|
|
|
|
|
# when destinations are appended they require a valid name.
|
|
|
|
if kwargs.get('name'):
|
2017-10-11 22:20:19 +02:00
|
|
|
self.name = get_or_increase_name(defaults.text_to_slug(kwargs['name']), self.serial)
|
2016-06-04 03:45:58 +02:00
|
|
|
else:
|
2017-07-13 01:46:11 +02:00
|
|
|
self.name = get_or_increase_name(
|
2017-10-11 22:20:19 +02:00
|
|
|
defaults.certificate_name(self.cn, self.issuer, self.not_before, self.not_after, self.san), self.serial)
|
2016-06-04 03:45:58 +02:00
|
|
|
|
2016-05-19 22:37:05 +02:00
|
|
|
self.owner = kwargs['owner']
|
2016-07-28 22:08:24 +02:00
|
|
|
|
|
|
|
if kwargs.get('private_key'):
|
|
|
|
self.private_key = kwargs['private_key'].strip()
|
|
|
|
|
|
|
|
if kwargs.get('chain'):
|
|
|
|
self.chain = kwargs['chain'].strip()
|
|
|
|
|
2018-10-12 01:29:35 +02:00
|
|
|
if kwargs.get('csr'):
|
|
|
|
self.csr = kwargs['csr'].strip()
|
|
|
|
|
2016-11-12 18:29:42 +01:00
|
|
|
self.notify = kwargs.get('notify', True)
|
2016-05-20 18:03:34 +02:00
|
|
|
self.destinations = kwargs.get('destinations', [])
|
|
|
|
self.notifications = kwargs.get('notifications', [])
|
|
|
|
self.description = kwargs.get('description')
|
2016-06-01 00:44:54 +02:00
|
|
|
self.roles = list(set(kwargs.get('roles', [])))
|
2016-12-26 20:09:50 +01:00
|
|
|
self.replaces = kwargs.get('replaces', [])
|
2016-12-23 22:18:42 +01:00
|
|
|
self.rotation = kwargs.get('rotation')
|
2017-07-13 01:46:11 +02:00
|
|
|
self.rotation_policy = kwargs.get('rotation_policy')
|
2016-05-19 22:37:05 +02:00
|
|
|
self.signing_algorithm = defaults.signing_algorithm(cert)
|
|
|
|
self.bits = defaults.bitstrength(cert)
|
2017-09-29 03:27:56 +02:00
|
|
|
self.external_id = kwargs.get('external_id')
|
2018-05-08 23:54:10 +02:00
|
|
|
self.authority_id = kwargs.get('authority_id')
|
|
|
|
self.dns_provider_id = kwargs.get('dns_provider_id')
|
2016-05-19 22:37:05 +02:00
|
|
|
|
|
|
|
for domain in defaults.domains(cert):
|
2015-06-22 22:47:27 +02:00
|
|
|
self.domains.append(Domain(name=domain))
|
|
|
|
|
2018-06-20 17:42:34 +02:00
|
|
|
# Check integrity before saving anything into the database.
|
|
|
|
# For user-facing API calls, validation should also be done in schema validators.
|
|
|
|
self.check_integrity()
|
|
|
|
|
|
|
|
def check_integrity(self):
|
|
|
|
"""
|
2018-06-20 17:42:34 +02:00
|
|
|
Integrity checks: Does the cert have a valid chain and matching private key?
|
2018-06-20 17:42:34 +02:00
|
|
|
"""
|
|
|
|
if self.private_key:
|
|
|
|
validators.verify_private_key_match(utils.parse_private_key(self.private_key), self.parsed_cert,
|
|
|
|
error_class=AssertionError)
|
|
|
|
|
2018-06-20 17:42:34 +02:00
|
|
|
if self.chain:
|
|
|
|
chain = [self.parsed_cert] + utils.parse_cert_chain(self.chain)
|
|
|
|
validators.verify_cert_chain(chain, error_class=AssertionError)
|
|
|
|
|
2018-06-19 17:41:12 +02:00
|
|
|
@cached_property
|
|
|
|
def parsed_cert(self):
|
|
|
|
assert self.body, "Certificate body not set"
|
2018-06-27 12:56:39 +02:00
|
|
|
return utils.parse_certificate(self.body)
|
2018-06-19 17:41:12 +02:00
|
|
|
|
2016-10-15 09:12:11 +02:00
|
|
|
@property
|
|
|
|
def active(self):
|
2016-11-17 00:56:24 +01:00
|
|
|
return self.notify
|
2016-10-15 09:12:11 +02:00
|
|
|
|
2016-11-18 20:27:46 +01:00
|
|
|
@property
|
|
|
|
def organization(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
return defaults.organization(self.parsed_cert)
|
2016-11-18 20:27:46 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def organizational_unit(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
return defaults.organizational_unit(self.parsed_cert)
|
2016-11-18 20:27:46 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def country(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
return defaults.country(self.parsed_cert)
|
2016-11-18 20:27:46 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
return defaults.state(self.parsed_cert)
|
2016-11-18 20:27:46 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def location(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
return defaults.location(self.parsed_cert)
|
2016-11-18 20:27:46 +01:00
|
|
|
|
2018-01-05 12:08:07 +01:00
|
|
|
@property
|
|
|
|
def distinguished_name(self):
|
|
|
|
return self.parsed_cert.subject.rfc4514_string()
|
|
|
|
|
2016-12-02 00:41:53 +01:00
|
|
|
@property
|
|
|
|
def key_type(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
if isinstance(self.parsed_cert.public_key(), rsa.RSAPublicKey):
|
|
|
|
return 'RSA{key_size}'.format(key_size=self.parsed_cert.public_key().key_size)
|
2016-12-02 00:41:53 +01:00
|
|
|
|
2016-12-20 21:48:14 +01:00
|
|
|
@property
|
|
|
|
def validity_remaining(self):
|
|
|
|
return abs(self.not_after - arrow.utcnow())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def validity_range(self):
|
|
|
|
return self.not_after - self.not_before
|
|
|
|
|
2017-02-01 19:34:24 +01:00
|
|
|
@property
|
|
|
|
def subject(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
return self.parsed_cert.subject
|
2017-02-01 19:34:24 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def public_key(self):
|
2018-06-19 17:41:12 +02:00
|
|
|
return self.parsed_cert.public_key()
|
2017-02-01 19:34:24 +01:00
|
|
|
|
2016-06-27 23:40:46 +02:00
|
|
|
@hybrid_property
|
|
|
|
def expired(self):
|
2016-11-18 20:27:46 +01:00
|
|
|
if self.not_after <= arrow.utcnow():
|
2015-06-22 22:47:27 +02:00
|
|
|
return True
|
|
|
|
|
2016-06-27 23:40:46 +02:00
|
|
|
@expired.expression
|
|
|
|
def expired(cls):
|
|
|
|
return case(
|
|
|
|
[
|
2016-12-12 20:22:49 +01:00
|
|
|
(cls.not_after <= arrow.utcnow(), True)
|
2016-06-27 23:40:46 +02:00
|
|
|
],
|
|
|
|
else_=False
|
|
|
|
)
|
|
|
|
|
|
|
|
@hybrid_property
|
|
|
|
def revoked(self):
|
|
|
|
if 'revoked' == self.status:
|
2015-06-22 22:47:27 +02:00
|
|
|
return True
|
|
|
|
|
2016-06-27 23:40:46 +02:00
|
|
|
@revoked.expression
|
|
|
|
def revoked(cls):
|
|
|
|
return case(
|
|
|
|
[
|
|
|
|
(cls.status == 'revoked', True)
|
|
|
|
],
|
|
|
|
else_=False
|
|
|
|
)
|
2015-06-22 22:47:27 +02:00
|
|
|
|
2017-07-13 01:46:11 +02:00
|
|
|
@hybrid_property
|
|
|
|
def in_rotation_window(self):
|
|
|
|
"""
|
|
|
|
Determines if a certificate is available for rotation based
|
|
|
|
on the rotation policy associated.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
now = arrow.utcnow()
|
|
|
|
end = now + timedelta(days=self.rotation_policy.days)
|
|
|
|
|
|
|
|
if self.not_after <= end:
|
|
|
|
return True
|
|
|
|
|
|
|
|
@in_rotation_window.expression
|
|
|
|
def in_rotation_window(cls):
|
|
|
|
"""
|
|
|
|
Determines if a certificate is available for rotation based
|
|
|
|
on the rotation policy associated.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return case(
|
|
|
|
[
|
|
|
|
(extract('day', cls.not_after - func.now()) <= RotationPolicy.days, True)
|
|
|
|
],
|
|
|
|
else_=False
|
|
|
|
)
|
|
|
|
|
2016-11-18 01:19:52 +01:00
|
|
|
@property
|
|
|
|
def extensions(self):
|
2017-01-29 01:40:37 +01:00
|
|
|
# setup default values
|
|
|
|
return_extensions = {
|
|
|
|
'sub_alt_names': {'names': []}
|
|
|
|
}
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
try:
|
2018-06-19 17:41:12 +02:00
|
|
|
for extension in self.parsed_cert.extensions:
|
2017-03-15 16:41:06 +01:00
|
|
|
value = extension.value
|
|
|
|
if isinstance(value, x509.BasicConstraints):
|
|
|
|
return_extensions['basic_constraints'] = value
|
|
|
|
|
|
|
|
elif isinstance(value, x509.SubjectAlternativeName):
|
|
|
|
return_extensions['sub_alt_names']['names'] = value
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
elif isinstance(value, x509.ExtendedKeyUsage):
|
|
|
|
return_extensions['extended_key_usage'] = value
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
elif isinstance(value, x509.KeyUsage):
|
|
|
|
return_extensions['key_usage'] = value
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
elif isinstance(value, x509.SubjectKeyIdentifier):
|
|
|
|
return_extensions['subject_key_identifier'] = {'include_ski': True}
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
elif isinstance(value, x509.AuthorityInformationAccess):
|
|
|
|
return_extensions['certificate_info_access'] = {'include_aia': True}
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
elif isinstance(value, x509.AuthorityKeyIdentifier):
|
|
|
|
aki = {
|
|
|
|
'use_key_identifier': False,
|
|
|
|
'use_authority_cert': False
|
|
|
|
}
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
if value.key_identifier:
|
|
|
|
aki['use_key_identifier'] = True
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
if value.authority_cert_issuer:
|
|
|
|
aki['use_authority_cert'] = True
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
return_extensions['authority_key_identifier'] = aki
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
elif isinstance(value, x509.CRLDistributionPoints):
|
2018-03-23 16:51:18 +01:00
|
|
|
return_extensions['crl_distribution_points'] = {'include_crl_dp': value}
|
2017-01-28 06:05:25 +01:00
|
|
|
|
2017-03-15 16:41:06 +01:00
|
|
|
# TODO: Not supporting custom OIDs yet. https://github.com/Netflix/lemur/issues/665
|
|
|
|
else:
|
|
|
|
current_app.logger.warning('Custom OIDs not yet supported for clone operation.')
|
|
|
|
except InvalidCodepoint as e:
|
2017-07-13 23:49:04 +02:00
|
|
|
sentry.captureException()
|
2017-03-15 16:41:06 +01:00
|
|
|
current_app.logger.warning('Unable to parse extensions due to underscore in dns name')
|
2017-04-28 02:45:33 +02:00
|
|
|
except ValueError as e:
|
2017-07-13 23:49:04 +02:00
|
|
|
sentry.captureException()
|
2017-04-28 02:45:33 +02:00
|
|
|
current_app.logger.warning('Unable to parse')
|
|
|
|
current_app.logger.exception(e)
|
2017-01-27 21:31:29 +01:00
|
|
|
|
|
|
|
return return_extensions
|
2016-11-18 01:19:52 +01:00
|
|
|
|
2016-11-16 18:30:54 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return "Certificate(name={name})".format(name=self.name)
|
|
|
|
|
2015-07-30 02:13:06 +02:00
|
|
|
|
2017-01-06 01:06:34 +01:00
|
|
|
@event.listens_for(Certificate.destinations, 'append')
|
2015-07-30 02:13:06 +02:00
|
|
|
def update_destinations(target, value, initiator):
|
2015-11-24 23:53:22 +01:00
|
|
|
"""
|
2016-12-28 18:52:23 +01:00
|
|
|
Attempt to upload certificate to the new destination
|
2015-11-24 23:53:22 +01:00
|
|
|
|
|
|
|
:param target:
|
|
|
|
:param value:
|
|
|
|
:param initiator:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-07-30 02:13:06 +02:00
|
|
|
destination_plugin = plugins.get(value.plugin_name)
|
2018-01-03 00:26:31 +01:00
|
|
|
status = FAILURE_METRIC_STATUS
|
2016-05-20 18:03:34 +02:00
|
|
|
try:
|
2018-11-12 18:52:11 +01:00
|
|
|
if target.private_key or not destination_plugin.requires_key:
|
2017-01-06 01:06:34 +01:00
|
|
|
destination_plugin.upload(target.name, target.body, target.private_key, target.chain, value.options)
|
2018-01-03 00:26:31 +01:00
|
|
|
status = SUCCESS_METRIC_STATUS
|
2016-05-20 18:03:34 +02:00
|
|
|
except Exception as e:
|
2017-10-27 00:21:13 +02:00
|
|
|
sentry.captureException()
|
2018-10-22 23:51:13 +02:00
|
|
|
raise
|
2018-01-03 00:26:31 +01:00
|
|
|
|
|
|
|
metrics.send('destination_upload', 'counter', 1,
|
|
|
|
metric_tags={'status': status, 'certificate': target.name, 'destination': value.label})
|
2015-11-24 23:53:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@event.listens_for(Certificate.replaces, 'append')
|
|
|
|
def update_replacement(target, value, initiator):
|
|
|
|
"""
|
2016-11-16 18:31:02 +01:00
|
|
|
When a certificate is marked as 'replaced' we should not notify.
|
2015-11-24 23:53:22 +01:00
|
|
|
|
|
|
|
:param target:
|
|
|
|
:param value:
|
|
|
|
:param initiator:
|
|
|
|
:return:
|
|
|
|
"""
|
2016-11-16 18:31:02 +01:00
|
|
|
value.notify = False
|