8000 keycloak/.github/scripts/aws at main · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
8000

Latest commit

 

History

History

aws

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

AWS access for CI

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.

How it works

  1. The job requests an OIDC token from GitHub, which needs id-token: write in the job permissions.
  2. aws-actions/configure-aws-credentials exchanges the token for temporary credentials of the role in the repository secret AWS_AURORA_ROLE_ARN.
  3. The credentials are exported as environment variables, so the aws CLI 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.

Setup in AWS

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.

  1. 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
  2. Create the role with a trust policy for that provider. Restrict the sub claim to the repository and refs that are allowed to run the job, and keep the aud condition. The maximum session duration must be at least 3 hours, as the workflow requests role-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
  3. Attach a permission policy with the actions used by aurora_create.sh, aurora_delete.sh and the aws_ec2 Ansible role. The EC2 and RDS actions are limited to us-east-1, the region used by ci.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.

  4. 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.

Running the job in a fork

The job also runs in a fork on pushes, as long as the fork has its own AWS setup:

  1. Run the setup commands above in your own AWS account with <owner> set to your GitHub user. For other branches than main, widen the sub claim, for example to repo:<user>/keycloak:ref:refs/heads/*, or to repo:<user>/keycloak:* for a throwaway account.
  2. 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.

References

0