-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4971 from ministryofjustice/certificates_renewal_…
…migration Certificates renewal migration
- Loading branch information
Showing
6 changed files
with
299 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Certificate Expiry Test Run | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
email: | ||
description: What is the email address of the recipient? | ||
jobs: | ||
certificate-expiry-check-test-run: | ||
name: Run certificate expiry script in test mode | ||
runs-on: ubuntu-latest | ||
permissions: | ||
id-token: write | ||
contents: read | ||
steps: | ||
- name: checkout repo content | ||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@486457dc46e82b9a740ca0ef1dac6a38a3fc272d # v4.0.2 | ||
with: | ||
role-to-assume: ${{secrets.AWS_CERTIFICATE_EMAIL_ARN}} | ||
aws-region: eu-west-2 | ||
- name: Python Setup | ||
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0 | ||
with: | ||
python-version: '3.11' | ||
- name: Install Pipenv | ||
run: | | ||
pip install pipenv | ||
pipenv install | ||
- run: pipenv run python3 -m bin.check_certificate_expiry --test ${{ github.event.inputs.email }} | ||
env: | ||
GANDI_CERTIFICATES_TOKEN: ${{ secrets.GANDI_CERTIFICATES_TOKEN }} | ||
NOTIFY_PROD_API_KEY: ${{ secrets.NOTIFY_PROD_API_KEY }} | ||
S3_CERT_BUCKET_NAME: ${{ secrets.S3_CERT_BUCKET_NAME }} | ||
S3_CERT_OBJECT_NAME: ${{ secrets.S3_CERT_OBJECT_NAME}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import os | ||
import sys | ||
import logging | ||
|
||
from services.gandi_service import GandiService | ||
from services.notify_service import NotifyService | ||
from services.s3_service import S3Service | ||
|
||
from config.constants import MINISTRY_OF_JUSTICE | ||
|
||
|
||
logging.basicConfig( | ||
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | ||
) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
cert_config = { | ||
"CERT_REPLY_EMAIL": "certificates@digital.justice.gov.uk", | ||
"CERT_EXPIRY_THRESHOLDS": [30], | ||
"CERT_URL_EXTENSION": "v5/certificate/issued-certs", | ||
"CERT_REPORT_TEMPLATE_ID": "04b6ca6c-2945-4a0d-a267-53fb61b370ef", | ||
"CERT_EXPIRY_TEMPALATE_ID": "06abd028-0a8f-43d9-a122-90a92f9b62ee" | ||
} | ||
|
||
|
||
def get_environment_variables() -> tuple: | ||
gandi_token = os.environ.get("GANDI_CERTIFICATES_TOKEN") | ||
if not gandi_token: | ||
raise ValueError("No GANDI_CERTIFICATES_TOKEN environment variable set") | ||
|
||
notify_api_key = os.environ.get("NOTIFY_PROD_API_KEY") | ||
if not notify_api_key: | ||
raise ValueError("No NOTIFY_PROD_API_KEY environment variable set") | ||
|
||
s3_bucket_name = os.environ.get("S3_CERT_BUCKET_NAME") | ||
if not s3_bucket_name: | ||
raise ValueError("S3_CERT_BUCKET_NAME environment variable set") | ||
|
||
s3_object_name = os.environ.get("S3_CERT_OBJECT_NAME") | ||
if not s3_object_name: | ||
raise ValueError("S3_CERT_OBJECT_NAME environment variable set") | ||
|
||
return gandi_token, notify_api_key, s3_bucket_name, s3_object_name | ||
|
||
|
||
def main(testrun: bool = False, test_email: str = ""): | ||
|
||
gandi_token, notify_api_key, s3_bucket_name, s3_object_name = get_environment_variables() | ||
logger.info("Instantiating services...") | ||
gandi_service = GandiService(gandi_token, cert_config["CERT_URL_EXTENSION"]) | ||
notify_service = NotifyService(cert_config, notify_api_key, MINISTRY_OF_JUSTICE) | ||
s3_service = S3Service(s3_bucket_name, MINISTRY_OF_JUSTICE,) | ||
|
||
logger.info("Extracting email map from S3") | ||
email_mappings = s3_service.get_json_file(s3_object_name, s3_object_name) | ||
|
||
logger.info("Extracting certificate list from Gandi...") | ||
certificate_list = gandi_service.get_certificate_list() | ||
valid_certificate_list = gandi_service.get_certificates_in_valid_state( | ||
certificate_list, email_mappings) | ||
if expired_certificate_list := gandi_service.get_expired_certificates_from_valid_certificate_list( | ||
valid_certificate_list, email_mappings, cert_config["CERT_EXPIRY_THRESHOLDS"] | ||
): | ||
|
||
print("Building parameters to send emails...") | ||
email_parameter_list = notify_service.build_email_parameter_list_crs( | ||
expired_certificate_list) | ||
|
||
if testrun: | ||
logger.info("Sending test email to {test_email}...") | ||
notify_service.send_test_email_from_parameters_crs( | ||
email_parameter_list, test_email) | ||
logger.info("Building main report...") | ||
report = notify_service.build_main_report_string_crs( | ||
email_parameter_list) | ||
logger.info("Sending test report to %s...", test_email) | ||
notify_service.send_report_email_crs( | ||
report, cert_config["CERT_REPORT_TEMPLATE_ID"], test_email) | ||
|
||
else: | ||
logger.info("Sending live emails...") | ||
notify_service.send_emails_from_parameters_crs(email_parameter_list) | ||
print("Building live report...") | ||
report = notify_service.build_main_report_string_crs( | ||
email_parameter_list) | ||
print("Sending live report to Operations Engineering...") | ||
notify_service.send_report_email_crs( | ||
report, cert_config["CERT_REPORT_TEMPLATE_ID"], cert_config["CERT_REPLY_EMAIL"]) | ||
else: | ||
logger.info("No expiring certificates found.") | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 1 and sys.argv[1] == '--test': | ||
if len(sys.argv) > 2: | ||
main(True, sys.argv[2]) | ||
else: | ||
raise SystemExit('Email address of recipient expected.') | ||
else: | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters