setup.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. # One-time setup to enable GitHub Actions OIDC -> AWS access for this repo.
  3. # Run with credentials that can manage IAM (admin, or a scoped IAM-admin role).
  4. # Re-running is safe: each step is idempotent or no-ops if already present.
  5. set -euo pipefail
  6. ACCOUNT_ID="$(aws sts get-caller-identity --query Account --output text)"
  7. ROLE_NAME="cloudbridge-github-actions"
  8. POLICY_NAME="cloudbridge-github-actions-policy"
  9. REGION="us-east-1"
  10. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  11. TRUST_POLICY="${SCRIPT_DIR}/trust-policy.json"
  12. PERMISSIONS_POLICY="${SCRIPT_DIR}/permissions-policy.json"
  13. # 1. Register GitHub's OIDC provider in IAM (no-op if it already exists).
  14. if ! aws iam get-open-id-connect-provider \
  15. --open-id-connect-provider-arn "arn:aws:iam::${ACCOUNT_ID}:oidc-provider/token.actions.githubusercontent.com" \
  16. >/dev/null 2>&1; then
  17. aws iam create-open-id-connect-provider \
  18. --url "https://token.actions.githubusercontent.com" \
  19. --client-id-list "sts.amazonaws.com" \
  20. --thumbprint-list "ffffffffffffffffffffffffffffffffffffffff"
  21. # Thumbprint above is a placeholder — GitHub OIDC uses a JWKS endpoint and
  22. # AWS now validates the JWKS chain server-side, so the thumbprint is no
  23. # longer security-critical. Set any 40-char hex value.
  24. fi
  25. # 2. Render the trust policy with the real account id.
  26. TRUST_RENDERED="$(mktemp)"
  27. sed "s/ACCOUNT_ID/${ACCOUNT_ID}/g" "${TRUST_POLICY}" > "${TRUST_RENDERED}"
  28. # 3. Create or update the role.
  29. if aws iam get-role --role-name "${ROLE_NAME}" >/dev/null 2>&1; then
  30. aws iam update-assume-role-policy \
  31. --role-name "${ROLE_NAME}" \
  32. --policy-document "file://${TRUST_RENDERED}"
  33. else
  34. aws iam create-role \
  35. --role-name "${ROLE_NAME}" \
  36. --assume-role-policy-document "file://${TRUST_RENDERED}" \
  37. --description "Assumed by GitHub Actions to run cloudbridge integration tests in ${REGION}"
  38. fi
  39. # 4. Attach an inline permissions policy (replaces on each run).
  40. aws iam put-role-policy \
  41. --role-name "${ROLE_NAME}" \
  42. --policy-name "${POLICY_NAME}" \
  43. --policy-document "file://${PERMISSIONS_POLICY}"
  44. ROLE_ARN="arn:aws:iam::${ACCOUNT_ID}:role/${ROLE_NAME}"
  45. echo
  46. echo "Role ready: ${ROLE_ARN}"
  47. echo "Set this as a repo secret named AWS_OIDC_ROLE_ARN at:"
  48. echo " https://github.com/CloudVE/cloudbridge/settings/secrets/actions"
  49. echo
  50. echo "Then remove the AWS_ACCESS_KEY and AWS_SECRET_KEY repo secrets — they are no longer used."