Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openssl_certificate, fixed has_expired to check the cert expiration date #53168

Merged
merged 9 commits into from
Mar 1, 2019
2 changes: 2 additions & 0 deletions changelogs/fragments/openssl_certificate_fix_has_expired.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- openssl_certificate - ``has_expired`` correctly checks if the certificate is expired or not
20 changes: 14 additions & 6 deletions lib/ansible/modules/crypto/openssl_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@

has_expired:
description:
- Checks if the certificate is expired/not expired at the time the module is executed.
- Checks if the certificate is expired/not expired at the time the module is executed. This only applies to
the C(assertonly) provider.
type: bool
default: no

Expand Down Expand Up @@ -830,11 +831,18 @@ def _validate_issuer():
)

def _validate_has_expired():
if self.has_expired:
if self.has_expired != self.cert.has_expired():
self.message.append(
'Certificate expiration check failed (certificate expiration is %s, expected %s)' % (self.cert.has_expired(), self.has_expired)
)
# The following 3 lines are the same as the current PyOpenSSL code for cert.has_expired().
# Older version of PyOpenSSL have a buggy implementation,
# to avoid issues with those we added the code from a more recent release here.

time_string = to_native(self.cert.get_notAfter())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment here that this is similar to the PyOpenSSL code for cert.has_expired(), but that older versions have a buggy implementation and we thus do it manually here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments, hopefully makes sense :)

not_after = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%SZ")
cert_expired = not_after < datetime.datetime.utcnow()

if self.has_expired != cert_expired:
self.message.append(
'Certificate expiration check failed (certificate expiration is %s, expected %s)' % (cert_expired, self.has_expired)
)

def _validate_version():
if self.version:
Expand Down
39 changes: 39 additions & 0 deletions test/integration/targets/openssl_certificate/tasks/expired.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
- name: Generate privatekey
openssl_privatekey:
path: '{{ output_dir }}/has_expired_privatekey.pem'

- name: Generate CSR
openssl_csr:
path: '{{ output_dir }}/has_expired_csr.csr'
privatekey_path: '{{ output_dir }}/has_expired_privatekey.pem'
subject:
commonName: www.example.com

- name: Generate expired selfsigned certificate
openssl_certificate:
path: '{{ output_dir }}/has_expired_cert.pem'
csr_path: '{{ output_dir }}/has_expired_csr.csr'
privatekey_path: '{{ output_dir }}/has_expired_privatekey.pem'
provider: selfsigned
selfsigned_digest: sha256
selfsigned_not_after: "-1s"

- name: "Check task fails because cert is expired (has_expired: false)"
openssl_certificate:
provider: assertonly
path: "{{ output_dir }}/has_expired_cert.pem"
has_expired: false
ignore_errors: true
register: expired_cert_check

- name: Ensure previous task failed
assert:
that: expired_cert_check is failed

- name: "Check expired cert check is ignored (has_expired: true)"
openssl_certificate:
provider: assertonly
path: "{{ output_dir }}/has_expired_cert.pem"
has_expired: true
register: expired_cert_skip
2 changes: 2 additions & 0 deletions test/integration/targets/openssl_certificate/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
- block:

- import_tasks: expired.yml

- import_tasks: selfsigned.yml

- import_tasks: ownca.yml
Expand Down