validate_secrets.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. name: 1. Validate Secrets
  2. run-name: Validate Secrets (${{ github.ref_name }})
  3. on: [workflow_call, workflow_dispatch]
  4. jobs:
  5. validate-access-token:
  6. name: Access
  7. runs-on: ubuntu-latest
  8. env:
  9. GH_PAT: ${{ secrets.GH_PAT }}
  10. GH_TOKEN: ${{ secrets.GH_PAT }}
  11. outputs:
  12. HAS_WORKFLOW_PERMISSION: ${{ steps.access-token.outputs.has_workflow_permission }}
  13. steps:
  14. - name: Validate Access Token
  15. id: access-token
  16. run: |
  17. # Validate Access Token
  18. # Ensure that gh exit codes are handled when output is piped.
  19. set -o pipefail
  20. # Define patterns to validate the access token (GH_PAT) and distinguish between classic and fine-grained tokens.
  21. GH_PAT_CLASSIC_PATTERN='^ghp_[a-zA-Z0-9]{36}$'
  22. GH_PAT_FINE_GRAINED_PATTERN='^github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59}$'
  23. # Validate Access Token (GH_PAT)
  24. if [ -z "$GH_PAT" ]; then
  25. failed=true
  26. echo "::error::The GH_PAT secret is unset or empty. Set it and try again."
  27. else
  28. if [[ $GH_PAT =~ $GH_PAT_CLASSIC_PATTERN ]]; then
  29. provides_scopes=true
  30. echo "The GH_PAT secret is a structurally valid classic token."
  31. elif [[ $GH_PAT =~ $GH_PAT_FINE_GRAINED_PATTERN ]]; then
  32. echo "The GH_PAT secret is a structurally valid fine-grained token."
  33. else
  34. unknown_format=true
  35. echo "The GH_PAT secret does not have a known token format."
  36. fi
  37. # Attempt to capture the x-oauth-scopes scopes of the token.
  38. if ! scopes=$(curl -sS -f -I -H "Authorization: token $GH_PAT" https://api.github.com | { grep -i '^x-oauth-scopes:' || true; } | cut -d ' ' -f2- | tr -d '\r'); then
  39. failed=true
  40. if [ $unknown_format ]; then
  41. echo "::error::Unable to connect to GitHub using the GH_PAT secret. Verify that it is set correctly (including the 'ghp_' or 'github_pat_' prefix) and try again."
  42. else
  43. echo "::error::Unable to connect to GitHub using the GH_PAT secret. Verify that the token exists and has not expired at https://github.com/settings/tokens. If necessary, regenerate or create a new token (and update the secret), then try again."
  44. fi
  45. elif [[ $scopes =~ workflow ]]; then
  46. echo "The GH_PAT secret has repo and workflow permissions."
  47. echo "has_workflow_permission=true" >> $GITHUB_OUTPUT
  48. elif [[ $scopes =~ repo ]]; then
  49. echo "The GH_PAT secret has repo (but not workflow) permissions."
  50. elif [ $provides_scopes ]; then
  51. failed=true
  52. if [ -z "$scopes" ]; then
  53. echo "The GH_PAT secret is valid and can be used to connect to GitHub, but it does not provide any permission scopes."
  54. else
  55. echo "The GH_PAT secret is valid and can be used to connect to GitHub, but it only provides the following permission scopes: $scopes"
  56. fi
  57. echo "::error::The GH_PAT secret is lacking at least the 'repo' permission scope required to access the Match-Secrets repository. Update the token permissions at https://github.com/settings/tokens (to include the 'repo' and 'workflow' scopes) and try again."
  58. else
  59. echo "The GH_PAT secret is valid and can be used to connect to GitHub, but it does not provide inspectable scopes. Assuming that the 'repo' and 'workflow' permission scopes required to access the Match-Secrets repository and perform automations are present."
  60. echo "has_workflow_permission=true" >> $GITHUB_OUTPUT
  61. fi
  62. fi
  63. # Exit unsuccessfully if secret validation failed.
  64. if [ $failed ]; then
  65. exit 2
  66. fi
  67. - name: Validate Match-Secrets
  68. run: |
  69. # Validate Match-Secrets
  70. # Ensure that gh exit codes are handled when output is piped.
  71. set -o pipefail
  72. # If a Match-Secrets repository does not exist, attempt to create one.
  73. if ! visibility=$(gh repo view ${{ github.repository_owner }}/Match-Secrets --json visibility | jq --raw-output '.visibility | ascii_downcase'); then
  74. echo "A '${{ github.repository_owner }}/Match-Secrets' repository could not be found using the GH_PAT secret. Attempting to create one..."
  75. # Create a private Match-Secrets repository and verify that it exists and that it is private.
  76. if gh repo create ${{ github.repository_owner }}/Match-Secrets --private >/dev/null && [ "$(gh repo view ${{ github.repository_owner }}/Match-Secrets --json visibility | jq --raw-output '.visibility | ascii_downcase')" == "private" ]; then
  77. echo "Created a private '${{ github.repository_owner }}/Match-Secrets' repository."
  78. else
  79. failed=true
  80. echo "::error::Unable to create a private '${{ github.repository_owner }}/Match-Secrets' repository. Create a private 'Match-Secrets' repository manually and try again. If a private 'Match-Secrets' repository already exists, verify that the token permissions of the GH_PAT are set correctly (or update them) at https://github.com/settings/tokens and try again."
  81. fi
  82. # Otherwise, if a Match-Secrets repository exists, but it is public, cause validation to fail.
  83. elif [[ "$visibility" == "public" ]]; then
  84. failed=true
  85. echo "::error::A '${{ github.repository_owner }}/Match-Secrets' repository was found, but it is public. Change the repository visibility to private (or delete it) and try again. If necessary, a private repository will be created for you."
  86. else
  87. echo "Found a private '${{ github.repository_owner }}/Match-Secrets' repository to use."
  88. fi
  89. # Exit unsuccessfully if secret validation failed.
  90. if [ $failed ]; then
  91. exit 2
  92. fi
  93. validate-fastlane-secrets:
  94. name: Fastlane
  95. needs: [validate-access-token]
  96. runs-on: macos-15
  97. env:
  98. GH_PAT: ${{ secrets.GH_PAT }}
  99. GH_TOKEN: ${{ secrets.GH_PAT }}
  100. FASTLANE_ISSUER_ID: ${{ secrets.FASTLANE_ISSUER_ID }}
  101. FASTLANE_KEY_ID: ${{ secrets.FASTLANE_KEY_ID }}
  102. FASTLANE_KEY: ${{ secrets.FASTLANE_KEY }}
  103. MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
  104. TEAMID: ${{ secrets.TEAMID }}
  105. steps:
  106. - name: Checkout Repo
  107. uses: actions/checkout@v5
  108. # Sync the GitHub runner clock with the Windows time server (workaround as suggested in https://github.com/actions/runner/issues/2996)
  109. - name: Sync clock
  110. run: sudo sntp -sS time.windows.com
  111. - name: Validate Fastlane Secrets
  112. run: |
  113. # Validate Fastlane Secrets
  114. # Validate TEAMID
  115. if [ -z "$TEAMID" ]; then
  116. failed=true
  117. echo "::error::The TEAMID secret is unset or empty. Set it and try again."
  118. elif [ ${#TEAMID} -ne 10 ]; then
  119. failed=true
  120. echo "::error::The TEAMID secret is set but has wrong length. Verify that it is set correctly and try again."
  121. elif ! [[ $TEAMID =~ ^[A-Z0-9]+$ ]]; then
  122. failed=true
  123. echo "::error::The TEAMID secret is set but invalid. Verify that it is set correctly (only uppercase letters and numbers) and try again."
  124. fi
  125. # Validate MATCH_PASSWORD
  126. if [ -z "$MATCH_PASSWORD" ]; then
  127. failed=true
  128. echo "::error::The MATCH_PASSWORD secret is unset or empty. Set it and try again."
  129. fi
  130. # Ensure that fastlane exit codes are handled when output is piped.
  131. set -o pipefail
  132. # Validate FASTLANE_ISSUER_ID, FASTLANE_KEY_ID, and FASTLANE_KEY
  133. FASTLANE_KEY_ID_PATTERN='^[A-Z0-9]+$'
  134. FASTLANE_ISSUER_ID_PATTERN='^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$'
  135. if [ -z "$FASTLANE_ISSUER_ID" ] || [ -z "$FASTLANE_KEY_ID" ] || [ -z "$FASTLANE_KEY" ]; then
  136. failed=true
  137. [ -z "$FASTLANE_ISSUER_ID" ] && echo "::error::The FASTLANE_ISSUER_ID secret is unset or empty. Set it and try again."
  138. [ -z "$FASTLANE_KEY_ID" ] && echo "::error::The FASTLANE_KEY_ID secret is unset or empty. Set it and try again."
  139. [ -z "$FASTLANE_KEY" ] && echo "::error::The FASTLANE_KEY secret is unset or empty. Set it and try again."
  140. elif [ ${#FASTLANE_KEY_ID} -ne 10 ]; then
  141. failed=true
  142. echo "::error::The FASTLANE_KEY_ID secret is set but has wrong length. Verify that you copied it correctly from the 'Keys' tab at https://appstoreconnect.apple.com/integrations/api and try again."
  143. elif ! [[ $FASTLANE_KEY_ID =~ $FASTLANE_KEY_ID_PATTERN ]]; then
  144. failed=true
  145. echo "::error::The FASTLANE_KEY_ID secret is set but invalid. Verify that you copied it correctly from the 'Keys' tab at https://appstoreconnect.apple.com/integrations/api and try again."
  146. elif ! [[ $FASTLANE_ISSUER_ID =~ $FASTLANE_ISSUER_ID_PATTERN ]]; then
  147. failed=true
  148. echo "::error::The FASTLANE_ISSUER_ID secret is set but invalid. Verify that you copied it correctly from the 'Keys' tab at https://appstoreconnect.apple.com/integrations/api and try again."
  149. elif ! echo "$FASTLANE_KEY" | openssl pkcs8 -nocrypt >/dev/null; then
  150. failed=true
  151. echo "::error::The FASTLANE_KEY secret is set but invalid. Verify that you copied it correctly from the API Key file (*.p8) you downloaded and try again."
  152. elif ! bundle exec fastlane validate_secrets 2>&1 | tee fastlane.log; then
  153. if grep -q "Couldn't decrypt the repo" fastlane.log; then
  154. failed=true
  155. echo "::error::Unable to decrypt the Match-Secrets repository using the MATCH_PASSWORD secret. Verify that it is set correctly and try again."
  156. elif grep -q -e "required agreement" -e "license agreement" fastlane.log; then
  157. failed=true
  158. echo "::error::❗️ Verify that the latest developer program license agreement has been accepted at https://developer.apple.com/account (review and accept any updated agreement), then wait a few minutes for changes to take effect and try again."
  159. elif grep -q "Your certificate .* is not valid" fastlane.log; then
  160. echo "::notice::Your Distribution certificate is invalid or expired. Automated renewal of the certificate will be attempted."
  161. fi
  162. fi
  163. # Exit unsuccessfully if secret validation failed.
  164. if [ $failed ]; then
  165. exit 2
  166. fi