From 80ca2ba3c63b8e99a5c2c0d3d95d620fc95cce68 Mon Sep 17 00:00:00 2001 From: John Franklin Date: Wed, 27 Jan 2021 17:58:08 -0500 Subject: [PATCH 1/4] Issue #32: Add support for SSL certificate management. --- acapi2/resources/environment.py | 94 +++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/acapi2/resources/environment.py b/acapi2/resources/environment.py index bc5cb4b..4ee53fd 100644 --- a/acapi2/resources/environment.py +++ b/acapi2/resources/environment.py @@ -345,3 +345,97 @@ def enable_cron(self, cron_id: str) -> Session: response = self.request(uri=uri, method="POST", data="") return response + + def get_ssl_settings(self) -> dict: + """ + Return the SSL settings for the environment. + """ + uri = f"{self.uri}/ssl" + + response = self.request(uri=uri) + return response.json() + + def get_ssl_certs(self) -> dict: + """ + Return a list of SSL certificates. + """ + uri = f"{self.uri}/ssl/certificates" + response = self.request(uri=uri) + + return response.json() + + def get_ssl_cert(self, cert_id) -> dict: + """ + Return an SSL cert. + """ + uri = f"{self.uri}/ssl/certificates/{cert_id}" + response = self.request(uri=uri) + + return response.json() + + def install_ssl_cert( + self, + label: str, + certificate: str, + private_key: str, + ca_certificates: str = None, + legacy: bool = False, + csr_id: int = None, + ) -> Session: + """ + Add a new SSL cert to the environment. + :param: label: Human-friendly identifier for the cert. + :param: certificate: The certificate in PEM format. + :param: private_key: The private keyfor the cert in PEM format. + :param: ca_certificates: Any chain certificates, in PEM format. + Defaults to None. + :param: legacy: Legacy in the sense of Acquia's legacy architecture, + not an old version of the SSL or TLS standards. See Acquia's docs + for more details. Defaults to False. + :param: csr_id: Associate with an existing installed CSR. Defaults + to None. + """ + uri = f"{self.uri}/ssl/certificates" + data = { + "legacy": legacy, + "label": label, + "certificate": certificate, + "private_key": private_key, + } + if csr_id is not None: + data['csr_id'] = csr_id + if ca_certificates is not None: + data['ca_certificates'] = ca_certificates + response = self.request(uri=uri, method="POST", data=data) + + return response + + def delete_ssl_cert(self, cert_id) -> Session: + """ + Remove an SSL cert. + :param: cert_id: The Acquia certificate ID. + """ + uri = f"{self.uri}/ssl/certificates/{cert_id}" + response = self.request(uri=uri, method="DELETE") + + return response + + def activate_ssl_cert(self, cert_id) -> Session: + """ + Activate a previously installed SSL cert. + :param: cert_id: The Acquia certificate ID. + """ + uri = f"{self.uri}/ssl/certificates/{cert_id}/actions/activate" + response = self.request(uri=uri, method="POST") + + return response + + def deactivate_ssl_cert(self, cert_id) -> Session: + """ + Deactivate a previously installed SSL cert. + :param: cert_id: The Acquia certificate ID. + """ + uri = f"{self.uri}/ssl/certificates/{cert_id}/actions/deactivate" + response = self.request(uri=uri, method="POST") + + return response From 01d50580698550b0d550c00531db12c5dae150b4 Mon Sep 17 00:00:00 2001 From: John Franklin Date: Thu, 28 Jan 2021 11:35:12 -0500 Subject: [PATCH 2/4] Issue #32: POST requires data, even if empty; Return only the certs in get_ssl_certs(), not the raw response. --- acapi2/resources/environment.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/acapi2/resources/environment.py b/acapi2/resources/environment.py index 4ee53fd..b97e004 100644 --- a/acapi2/resources/environment.py +++ b/acapi2/resources/environment.py @@ -351,9 +351,9 @@ def get_ssl_settings(self) -> dict: Return the SSL settings for the environment. """ uri = f"{self.uri}/ssl" - response = self.request(uri=uri) - return response.json() + + return response.json().get('_embedded', {}).get('items') def get_ssl_certs(self) -> dict: """ @@ -426,7 +426,7 @@ def activate_ssl_cert(self, cert_id) -> Session: :param: cert_id: The Acquia certificate ID. """ uri = f"{self.uri}/ssl/certificates/{cert_id}/actions/activate" - response = self.request(uri=uri, method="POST") + response = self.request(uri=uri, method="POST", data={}) return response @@ -436,6 +436,6 @@ def deactivate_ssl_cert(self, cert_id) -> Session: :param: cert_id: The Acquia certificate ID. """ uri = f"{self.uri}/ssl/certificates/{cert_id}/actions/deactivate" - response = self.request(uri=uri, method="POST") + response = self.request(uri=uri, method="POST", data={}) return response From 9a7540468439133f441ea0f7bcb184630dea0cbf Mon Sep 17 00:00:00 2001 From: John Franklin Date: Thu, 28 Jan 2021 17:16:49 -0500 Subject: [PATCH 3/4] Issue #32: Apply .get() to return certs to the right function. --- acapi2/resources/environment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acapi2/resources/environment.py b/acapi2/resources/environment.py index b97e004..b264060 100644 --- a/acapi2/resources/environment.py +++ b/acapi2/resources/environment.py @@ -353,7 +353,7 @@ def get_ssl_settings(self) -> dict: uri = f"{self.uri}/ssl" response = self.request(uri=uri) - return response.json().get('_embedded', {}).get('items') + return response.json() def get_ssl_certs(self) -> dict: """ @@ -362,7 +362,7 @@ def get_ssl_certs(self) -> dict: uri = f"{self.uri}/ssl/certificates" response = self.request(uri=uri) - return response.json() + return response.json().get('_embedded', {}).get('items') def get_ssl_cert(self, cert_id) -> dict: """ From 7d16faca5ffa12d23fb8cb61f77732fc8f3600ed Mon Sep 17 00:00:00 2001 From: John Franklin Date: Mon, 1 Feb 2021 08:58:07 -0500 Subject: [PATCH 4/4] Issue #32: Add type hinting. --- acapi2/resources/environment.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/acapi2/resources/environment.py b/acapi2/resources/environment.py index b264060..c3848b5 100644 --- a/acapi2/resources/environment.py +++ b/acapi2/resources/environment.py @@ -364,7 +364,7 @@ def get_ssl_certs(self) -> dict: return response.json().get('_embedded', {}).get('items') - def get_ssl_cert(self, cert_id) -> dict: + def get_ssl_cert(self, cert_id: str) -> dict: """ Return an SSL cert. """ @@ -410,7 +410,7 @@ def install_ssl_cert( return response - def delete_ssl_cert(self, cert_id) -> Session: + def delete_ssl_cert(self, cert_id: str) -> Session: """ Remove an SSL cert. :param: cert_id: The Acquia certificate ID. @@ -420,7 +420,7 @@ def delete_ssl_cert(self, cert_id) -> Session: return response - def activate_ssl_cert(self, cert_id) -> Session: + def activate_ssl_cert(self, cert_id: str) -> Session: """ Activate a previously installed SSL cert. :param: cert_id: The Acquia certificate ID. @@ -430,7 +430,7 @@ def activate_ssl_cert(self, cert_id) -> Session: return response - def deactivate_ssl_cert(self, cert_id) -> Session: + def deactivate_ssl_cert(self, cert_id: str) -> Session: """ Deactivate a previously installed SSL cert. :param: cert_id: The Acquia certificate ID.