Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions .github/workflows/grc-gate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# .github/workflows/grc-gate.yml
name: grc-gate

on:
pull_request:
branches: [main]
workflow_dispatch:

permissions:
id-token: write # required for AWS OIDC + Cosign keyless (Lab 4.4)
contents: read
pull-requests: write # required to comment on the PR

env:
AWS_REGION: us-east-1
TF_WORKING_DIR: terraform/primitives/compliant-s3

jobs:
grc-gate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.6.6
terraform_wrapper: false

- name: Install Conftest
run: |
curl -fsSL https://github.com/open-policy-agent/conftest/releases/download/v0.50.0/conftest_0.50.0_Linux_x86_64.tar.gz \
| tar -xz -C /usr/local/bin conftest

- name: Install tfsec
run: |
curl -fsSL https://github.com/aquasecurity/tfsec/releases/download/v1.28.14/tfsec-linux-amd64 \
-o /usr/local/bin/tfsec && chmod +x /usr/local/bin/tfsec

- name: Terraform plan
working-directory: ${{ env.TF_WORKING_DIR }}
run: |
terraform init -input=false
terraform validate
# Same vars as Lab 2.3 / 3.4. -input=false means missing vars fail the job
# instead of hanging on an interactive prompt the runner can't answer.
terraform plan -out=tfplan -input=false -no-color \
-var="project_name=cgep-lab" -var="environment=dev" | tee plan.txt
terraform show -json tfplan > plan.json

- name: Conftest policy gate
id: conftest
run: |
mkdir -p evidence/lab-4-3
EXIT=0
{
echo "["
FIRST=1
# AWS namespaces only (same set as scripts/policy-gate.sh from Lab 3.4).
for ns in compliance.sc28_aws compliance.ac3_aws compliance.cm6_aws ; do
[[ $FIRST -eq 1 ]] && FIRST=0 || printf ","
set +e
OUT=$(conftest test --policy policies --namespace "$ns" --output=json "$TF_WORKING_DIR/plan.json")
STATUS=$?
set -e
[[ $STATUS -eq 0 ]] || EXIT=1
printf '%s' "$OUT"
done
echo
echo "]"
} > evidence/lab-4-3/conftest-results.json
if [[ $EXIT -eq 0 ]]; then echo "conftest: PASS"; else echo "conftest: FAIL"; exit 1; fi

- name: tfsec scan
id: tfsec
if: always()
run: |
# Write SARIF for the artifact, then re-run with a severity floor so
# tfsec's own exit code is the gate (no JSON parsing required).
echo "****** BREAK 1 ******"
tfsec "$TF_WORKING_DIR" --format sarif --out evidence/lab-4-3/tfsec.sarif || true
echo "****** BREAK 2 ******"
tfsec "$TF_WORKING_DIR" --minimum-severity HIGH

- name: Copy plan into evidence
if: always()
run: |
cp "$TF_WORKING_DIR"/plan.json evidence/lab-4-3/plan.json
cp "$TF_WORKING_DIR"/plan.txt evidence/lab-4-3/plan.txt

- name: Upload evidence artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: grc-evidence-${{ github.run_id }}
path: evidence/lab-4-3/
retention-days: 90
47 changes: 47 additions & 0 deletions terraform/primitives/oidc-trust/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# terraform/primitives/oidc-trust/main.tf
terraform {
required_version = ">= 1.6"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
}

provider "aws" { region = "us-east-1" }

variable "github_org" { type = string }
variable "github_repo" { type = string }

resource "aws_iam_openid_connect_provider" "github" {
url = "https://token.actions.githubusercontent.com"
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = ["6938fd4d98bab03faadb97b34396831e3780aea1"]
}

resource "aws_iam_role" "grc_gate" {
name = "cgep-grc-gate"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Federated = aws_iam_openid_connect_provider.github.arn }
Action = "sts:AssumeRoleWithWebIdentity"
Condition = {
StringEquals = { "token.actions.githubusercontent.com:aud" = "sts.amazonaws.com" }
StringLike = {
"token.actions.githubusercontent.com:sub" = [
"repo:${var.github_org}/${var.github_repo}:*", # repos created before Jul 15 2026
"repo:${var.github_org}@*/${var.github_repo}@*:*", # repos created after: GitHub adds owner + repo IDs
]
}
}
}]
})
}

resource "aws_iam_role_policy_attachment" "readonly" {
role = aws_iam_role.grc_gate.name
policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"
}

output "role_arn" { value = aws_iam_role.grc_gate.arn }
Loading