|
| 1 | +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-certificates.md |
| 2 | +name: Check Certificates |
| 3 | + |
| 4 | +# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows |
| 5 | +on: |
| 6 | + push: |
| 7 | + paths: |
| 8 | + - ".github/workflows/check-certificates.ya?ml" |
| 9 | + pull_request: |
| 10 | + paths: |
| 11 | + - ".github/workflows/check-certificates.ya?ml" |
| 12 | + schedule: |
| 13 | + # Run every 10 hours. |
| 14 | + - cron: "0 */10 * * *" |
| 15 | + workflow_dispatch: |
| 16 | + repository_dispatch: |
| 17 | + |
| 18 | +env: |
| 19 | + # Begin notifications when there are less than this many days remaining before expiration. |
| 20 | + EXPIRATION_WARNING_PERIOD: 30 |
| 21 | + |
| 22 | +jobs: |
| 23 | + check-certificates: |
| 24 | + name: ${{ matrix.certificate.identifier }} |
| 25 | + # Only run when the workflow will have access to the certificate secrets. |
| 26 | + # TODO: Update repository name. |
| 27 | + if: > |
| 28 | + (github.event_name != 'pull_request' && github.repository == 'REPO_OWNER/REPO_NAME') || |
| 29 | + (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'REPO_OWNER/REPO_NAME') |
| 30 | + runs-on: ubuntu-latest |
| 31 | + strategy: |
| 32 | + fail-fast: false |
| 33 | + |
| 34 | + matrix: |
| 35 | + certificate: |
| 36 | + # Additional certificate definitions can be added to this list. |
| 37 | + - identifier: macOS signing certificate # Text used to identify certificate in notifications. |
| 38 | + certificate-secret: INSTALLER_CERT_MAC_P12 # Name of the secret that contains the certificate. |
| 39 | + password-secret: INSTALLER_CERT_MAC_PASSWORD # Name of the secret that contains the certificate password. |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Set certificate path environment variable |
| 43 | + run: | |
| 44 | + # See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable |
| 45 | + echo "CERTIFICATE_PATH=${{ runner.temp }}/certificate.p12" >> "$GITHUB_ENV" |
| 46 | +
|
| 47 | + - name: Decode certificate |
| 48 | + env: |
| 49 | + CERTIFICATE: ${{ secrets[matrix.certificate.certificate-secret] }} |
| 50 | + run: | |
| 51 | + echo "${{ env.CERTIFICATE }}" | base64 --decode > "${{ env.CERTIFICATE_PATH }}" |
| 52 | +
|
| 53 | + - name: Verify certificate |
| 54 | + env: |
| 55 | + CERTIFICATE_PASSWORD: ${{ secrets[matrix.certificate.password-secret] }} |
| 56 | + run: | |
| 57 | + ( |
| 58 | + openssl pkcs12 \ |
| 59 | + -in "${{ env.CERTIFICATE_PATH }}" \ |
| 60 | + -noout -passin env:CERTIFICATE_PASSWORD |
| 61 | + ) || ( |
| 62 | + echo "::error::Verification of ${{ matrix.certificate.identifier }} failed!!!" |
| 63 | + exit 1 |
| 64 | + ) |
| 65 | +
|
| 66 | + - name: Slack notification of certificate verification failure |
| 67 | + if: failure() |
| 68 | + env: |
| 69 | + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} |
| 70 | + SLACK_MESSAGE: | |
| 71 | + :warning::warning::warning::warning: |
| 72 | + WARNING: ${{ github.repository }} ${{ matrix.certificate.identifier }} verification failed!!! |
| 73 | + :warning::warning::warning::warning: |
| 74 | + SLACK_COLOR: danger |
| 75 | + MSG_MINIMAL: true |
| 76 | + uses: rtCamp/action-slack-notify@v2 |
| 77 | + |
| 78 | + - name: Get days remaining before certificate expiration date |
| 79 | + env: |
| 80 | + CERTIFICATE_PASSWORD: ${{ secrets[matrix.certificate.password-secret] }} |
| 81 | + id: get-days-before-expiration |
| 82 | + run: | |
| 83 | + EXPIRATION_DATE="$( |
| 84 | + ( |
| 85 | + openssl pkcs12 \ |
| 86 | + -in "${{ env.CERTIFICATE_PATH }}" \ |
| 87 | + -clcerts \ |
| 88 | + -nodes \ |
| 89 | + -passin env:CERTIFICATE_PASSWORD |
| 90 | + ) | ( |
| 91 | + openssl x509 \ |
| 92 | + -noout \ |
| 93 | + -enddate |
| 94 | + ) | ( |
| 95 | + grep \ |
| 96 | + --max-count=1 \ |
| 97 | + --only-matching \ |
| 98 | + --perl-regexp \ |
| 99 | + 'notAfter=(\K.*)' |
| 100 | + ) |
| 101 | + )" |
| 102 | +
|
| 103 | + DAYS_BEFORE_EXPIRATION="$((($(date --utc --date="$EXPIRATION_DATE" +%s) - $(date --utc +%s)) / 60 / 60 / 24))" |
| 104 | +
|
| 105 | + # Display the expiration information in the log. |
| 106 | + echo "Certificate expiration date: $EXPIRATION_DATE" |
| 107 | + echo "Days remaining before expiration: $DAYS_BEFORE_EXPIRATION" |
| 108 | +
|
| 109 | + echo "::set-output name=days::$DAYS_BEFORE_EXPIRATION" |
| 110 | +
|
| 111 | + - name: Check if expiration notification period has been reached |
| 112 | + id: check-expiration |
| 113 | + run: | |
| 114 | + if [[ ${{ steps.get-days-before-expiration.outputs.days }} -lt ${{ env.EXPIRATION_WARNING_PERIOD }} ]]; then |
| 115 | + echo "::error::${{ matrix.certificate.identifier }} will expire in ${{ steps.get-days-before-expiration.outputs.days }} days!!!" |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | +
|
| 119 | + - name: Slack notification of pending certificate expiration |
| 120 | + # Don't send spurious expiration notification if verification fails. |
| 121 | + if: failure() && steps.check-expiration.outcome == 'failure' |
| 122 | + env: |
| 123 | + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} |
| 124 | + SLACK_MESSAGE: | |
| 125 | + :warning::warning::warning::warning: |
| 126 | + WARNING: ${{ github.repository }} ${{ matrix.certificate.identifier }} will expire in ${{ steps.get-days-before-expiration.outputs.days }} days!!! |
| 127 | + :warning::warning::warning::warning: |
| 128 | + SLACK_COLOR: danger |
| 129 | + MSG_MINIMAL: true |
| 130 | + uses: rtCamp/action-slack-notify@v2 |
0 commit comments