The AuroraDB IT job in ci.yml and the aurora-delete.yml workflow create and delete AWS resources.
They authenticate with GitHub OIDC federation and assume an IAM role.
No long-lived AWS access keys are stored in the repository secrets.
- The job requests an OIDC token from GitHub, which needs
id-token: writein the job permissions. aws-actions/configure-aws-credentialsexchanges the token for temporary credentials of the role in the repository secretAWS_AURORA_ROLE_ARN.- The credentials are exported as environment variables, so the
awsCLI and the Ansible/boto3 scripts use them without further configuration.
If AWS_AURORA_ROLE_ARN is not set, for example in forks, the Aurora job is skipped.
The job is also skipped for pull requests.
The setup can be done with the AWS CLI and the GitHub CLI.
Replace <owner> with the owner of the repository, which is keycloak for the upstream repository.
-
Create the IAM OIDC identity provider once per AWS account:
aws iam create-open-id-connect-provider \ --url https://token.actions.githubusercontent.com \ --client-id-list sts.amazonaws.com
-
Create the role with a trust policy for that provider. Restrict the
subclaim to the repository and refs that are allowed to run the job, and keep theaudcondition. The maximum session duration must be at least 3 hours, as the workflow requestsrole-duration-seconds: 10800.ACCOUNT=$(aws sts get-caller-identity --query Account --output text) cat > trust.json <<EOF { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": {"Federated": "arn:aws:iam::${ACCOUNT}:oidc-provider/token.actions.githubusercontent.com"}, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": {"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"}, "StringLike": {"token.actions.githubusercontent.com:sub": [ "repo:<owner>/keycloak:ref:refs/heads/main", "repo:<owner>/keycloak:ref:refs/heads/release/*", "repo:<owner>/keycloak:ref:refs/heads/quarkus-next" ]} } }] } EOF aws iam create-role --role-name keycloak-ci-aurora \ --assume-role-policy-document file://trust.json \ --max-session-duration 10800
-
Attach a permission policy with the actions used by
aurora_create.sh,aurora_delete.shand theaws_ec2Ansible role. The EC2 and RDS actions are limited tous-east-1, the region used byci.yml. Most EC2 and RDS actions can't be restricted to specific resources, as the resources are created with generated names during the run.cat > policy.json <<'EOF' { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Resource": "*", "Condition": {"StringEquals": {"aws:RequestedRegion": "us-east-1"}}, "Action": [ "ec2:AssociateRouteTable", "ec2:AttachInternetGateway", "ec2:AuthorizeSecurityGroupIngress", "ec2:CreateInternetGateway", "ec2:CreateKeyPair", "ec2:CreateRoute", "ec2:CreateSecurityGroup", "ec2:CreateSubnet", "ec2:CreateTags", "ec2:CreateVpc", "ec2:DeleteInternetGateway", "ec2:DeleteKeyPair", "ec2:DeleteSecurityGroup", "ec2:DeleteSubnet", "ec2:DeleteVpc", "ec2:Describe*", "ec2:DetachInternetGateway", "ec2:ModifyVpcAttribute", "ec2:RunInstances", "ec2:StartInstances", "ec2:StopInstances", "ec2:TerminateInstances", "rds:AddTagsToResource", "rds:CreateDBCluster", "rds:CreateDBInstance", "rds:CreateDBSubnetGroup", "rds:DeleteDBCluster", "rds:DeleteDBInstance", "rds:DeleteDBSubnetGroup", "rds:DescribeDBClusters", "rds:DescribeDBInstances" ] }, { "Effect": "Allow", "Resource": "*", "Action": "iam:CreateServiceLinkedRole" }] } EOF aws iam put-role-policy --role-name keycloak-ci-aurora \ --policy-name aurora-ci --policy-document file://policy.json
If a run fails with an access denied error, add the missing action.
-
Set the repository secret to the ARN of the role. The ARN is a secret so that the AWS account ID is masked in the logs of this public repository:
gh secret set AWS_AURORA_ROLE_ARN -R <owner>/keycloak \ --body "arn:aws:iam::${ACCOUNT}:role/keycloak-ci-aurora"
aurora-delete.yml is started with gh workflow run on the ref of the original run.
The trust policy therefore needs to allow the sub claim for that ref as well.
The job also runs in a fork on pushes, as long as the fork has its own AWS setup:
- Run the setup commands above in your own AWS account with
<owner>set to your GitHub user. For other branches thanmain, widen thesubclaim, for example torepo:<user>/keycloak:ref:refs/heads/*, or torepo:<user>/keycloak:*for a throwaway account. - Enable GitHub Actions in the fork and push a branch.
The resources are created in your AWS account and incur costs.
Check that aurora-delete.yml ran after the job, and delete leftover resources manually otherwise.