Patrick de Ruiter cfbe6cbdc4
Some checks failed
Code Quality & Security Scan / TFLint (push) Successful in 24s
Code Quality & Security Scan / Terraform Destroy (push) Has been skipped
Code Quality & Security Scan / Tfsec Security Scan (push) Successful in 29s
Code Quality & Security Scan / Checkov Security Scan (push) Successful in 44s
Code Quality & Security Scan / Terraform Tests (push) Failing after 35s
Code Quality & Security Scan / SonarQube Trigger (push) Has been skipped
Code Quality & Security Scan / Terraform Init (push) Has been skipped
Code Quality & Security Scan / Terraform Apply (push) Has been skipped
Add comprehensive Terraform testing framework
- Implemented 21 test cases across 3 test suites:
  * resource_groups.tftest.hcl (7 tests): Default behavior and validation
  * custom_configuration.tftest.hcl (6 tests): Custom configurations
  * variable_validation.tftest.hcl (8 tests): Input validation and edge cases

- Updated CI/CD pipeline (.gitea/workflows/sonarqube.yaml):
  * Added terraform-test job with format check and test execution
  * Generates and uploads test reports (30-day retention)
  * Runs after security scanning, before deployment

- Added comprehensive documentation:
  * TESTING.md: Complete testing guide with best practices
  * TEST_SUMMARY.md: Implementation summary and statistics
  * TESTING_QUICK_START.md: Quick reference for developers
  * TESTING_WORKFLOW.md: Visual workflow diagrams

- Updated existing documentation:
  * README.md: Added testing section with examples
  * CLAUDE.md: Added test commands to workflow

- Test coverage includes:
  * Resource creation and configuration validation
  * Tag category and tag management
  * Variable validation and defaults
  * Custom configurations and overrides
  * Edge cases and error handling
  * Output generation verification

Tests use mock credentials for infrastructure-independent execution.
Requires Terraform >= 1.6.0 for native testing framework.
2025-11-09 00:37:45 +01:00

353 lines
12 KiB
YAML

on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
name: Code Quality & Security Scan
jobs:
tflint:
name: TFLint
runs-on: ubuntu-latest
steps:
- name: Checking out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup TFLint
uses: terraform-linters/setup-tflint@v4
with:
tflint_version: latest
- name: Initialize TFLint
run: tflint --init
- name: Run TFLint
run: tflint --format compact
tfsec:
name: Tfsec Security Scan
runs-on: ubuntu-latest
needs: tflint
steps:
- name: Checking out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Tfsec
uses: aquasecurity/tfsec-action@v1.0.3
with:
format: default
soft_fail: false
checkov:
name: Checkov Security Scan
runs-on: ubuntu-latest
needs: tfsec
steps:
- name: Checking out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Checkov
uses: bridgecrewio/checkov-action@v12
with:
directory: .
framework: terraform
output_format: cli
soft_fail: false
terraform-test:
name: Terraform Tests
runs-on: ubuntu-latest
needs: checkov
steps:
- name: Checking out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: latest
- name: Terraform Format Check
run: terraform fmt -check -recursive
- name: Run Terraform Tests
run: terraform test -verbose
env:
TF_VAR_role_id: "test-role-id"
TF_VAR_secret_id: "test-secret-id"
- name: Generate Test Report
if: always()
run: |
echo "# Terraform Test Results" > test-report.md
echo "" >> test-report.md
echo "Test execution completed at $(date)" >> test-report.md
- name: Upload Test Report
if: always()
uses: actions/upload-artifact@v4
with:
name: terraform-test-report
path: test-report.md
retention-days: 30
sonarqube:
name: SonarQube Trigger
runs-on: ubuntu-latest
needs: terraform-test
steps:
- name: Checking out
uses: actions/checkout@v4
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@v6
env:
SONAR_HOST_URL: ${{ secrets.SONARQUBE_HOST }}
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
terraform-init:
name: Terraform Init
runs-on: ubuntu-latest
needs: sonarqube
steps:
- name: Checking out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: latest
- name: Terraform Init
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
TF_BACKEND_ENDPOINT: ${{ secrets.MINIO_ENDPOINT }}
TF_BACKEND_BUCKET: ${{ secrets.MINIO_BUCKET }}
TF_BACKEND_KEY: ${{ secrets.MINIO_STATE_KEY }}
TF_BACKEND_REGION: "main"
TF_VAR_role_id: ${{ secrets.VAULT_ROLE_ID }}
TF_VAR_secret_id: ${{ secrets.VAULT_SECRET_ID }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
run: |
terraform init \
-backend-config="endpoints={s3=\"${TF_BACKEND_ENDPOINT}\"}" \
-backend-config="bucket=${TF_BACKEND_BUCKET}" \
-backend-config="key=${TF_BACKEND_KEY}" \
-backend-config="region=${TF_BACKEND_REGION}" \
-backend-config="skip_credentials_validation=true" \
-backend-config="skip_metadata_api_check=true" \
-backend-config="skip_requesting_account_id=true" \
-backend-config="skip_region_validation=true" \
-backend-config="use_path_style=true"
- name: Terraform Plan
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
TF_VAR_role_id: ${{ secrets.VAULT_ROLE_ID }}
TF_VAR_secret_id: ${{ secrets.VAULT_SECRET_ID }}
TF_VAR_datacenter: ${{ secrets.VSPHERE_DATACENTER }}
TF_VAR_cluster_name: ${{ secrets.VSPHERE_CLUSTER }}
TF_VAR_environment: ${{ secrets.ENVIRONMENT }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
run: |
terraform plan -out=tfplan
terraform show -no-color tfplan > tfplan.txt
- name: Upload Terraform Plan to MinIO
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
MINIO_ENDPOINT: ${{ secrets.MINIO_ENDPOINT }}
MINIO_BUCKET: ${{ secrets.MINIO_BUCKET }}
run: |
# Install AWS CLI for S3-compatible operations
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -q awscliv2.zip
sudo ./aws/install
# Upload plan files to MinIO
PLAN_PATH="terraform-plans/${{ github.repository }}/${{ github.run_number }}"
aws s3 cp tfplan "s3://${MINIO_BUCKET}/${PLAN_PATH}/tfplan" \
--endpoint-url="${MINIO_ENDPOINT}"
aws s3 cp tfplan.txt "s3://${MINIO_BUCKET}/${PLAN_PATH}/tfplan.txt" \
--endpoint-url="${MINIO_ENDPOINT}"
echo "Plan uploaded to: s3://${MINIO_BUCKET}/${PLAN_PATH}/"
terraform-apply:
name: Terraform Apply
runs-on: ubuntu-latest
needs: terraform-init
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
environment:
name: production
steps:
- name: Checking out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: latest
- name: Install AWS CLI
run: |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -q awscliv2.zip
sudo ./aws/install
- name: Terraform Init
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
TF_BACKEND_ENDPOINT: ${{ secrets.MINIO_ENDPOINT }}
TF_BACKEND_BUCKET: ${{ secrets.MINIO_BUCKET }}
TF_BACKEND_KEY: ${{ secrets.MINIO_STATE_KEY }}
TF_BACKEND_REGION: "main"
TF_VAR_role_id: ${{ secrets.VAULT_ROLE_ID }}
TF_VAR_secret_id: ${{ secrets.VAULT_SECRET_ID }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
run: |
terraform init \
-backend-config="endpoints={s3=\"${TF_BACKEND_ENDPOINT}\"}" \
-backend-config="bucket=${TF_BACKEND_BUCKET}" \
-backend-config="key=${TF_BACKEND_KEY}" \
-backend-config="region=${TF_BACKEND_REGION}" \
-backend-config="skip_credentials_validation=true" \
-backend-config="skip_metadata_api_check=true" \
-backend-config="skip_requesting_account_id=true" \
-backend-config="skip_region_validation=true" \
-backend-config="use_path_style=true"
- name: Download Terraform Plan from MinIO
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
MINIO_ENDPOINT: ${{ secrets.MINIO_ENDPOINT }}
MINIO_BUCKET: ${{ secrets.MINIO_BUCKET }}
run: |
PLAN_PATH="terraform-plans/${{ github.repository }}/${{ github.run_number }}"
aws s3 cp "s3://${MINIO_BUCKET}/${PLAN_PATH}/tfplan" tfplan \
--endpoint-url="${MINIO_ENDPOINT}"
echo "Plan downloaded from: s3://${MINIO_BUCKET}/${PLAN_PATH}/tfplan"
- name: Terraform Apply
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
TF_VAR_role_id: ${{ secrets.VAULT_ROLE_ID }}
TF_VAR_secret_id: ${{ secrets.VAULT_SECRET_ID }}
TF_VAR_datacenter: ${{ secrets.VSPHERE_DATACENTER }}
TF_VAR_cluster_name: ${{ secrets.VSPHERE_CLUSTER }}
TF_VAR_environment: ${{ secrets.ENVIRONMENT }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
run: terraform apply -auto-approve tfplan
terraform-destroy:
name: Terraform Destroy
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'destroy')
environment:
name: destroy-approval
steps:
- name: Verify Destroy Authorization
run: |
echo "⚠️ CRITICAL: INFRASTRUCTURE DESTRUCTION REQUESTED"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "PR: ${{ github.event.pull_request.html_url }}"
echo "Requested by: ${{ github.actor }}"
echo "Repository: ${{ github.repository }}"
echo "Branch: ${{ github.head_ref }}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "This action will PERMANENTLY DESTROY all infrastructure"
echo "managed by this Terraform configuration."
echo ""
echo "Waiting for manual approval via environment protection rules..."
- name: Checking out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: latest
- name: Terraform Init (Fresh - No Cache)
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
TF_BACKEND_ENDPOINT: ${{ secrets.MINIO_ENDPOINT }}
TF_BACKEND_BUCKET: ${{ secrets.MINIO_BUCKET }}
TF_BACKEND_KEY: ${{ secrets.MINIO_STATE_KEY }}
TF_BACKEND_REGION: "main"
TF_VAR_role_id: ${{ secrets.VAULT_ROLE_ID }}
TF_VAR_secret_id: ${{ secrets.VAULT_SECRET_ID }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
run: |
echo "Performing fresh terraform init (no cache for safety)..."
terraform init \
-backend-config="endpoints={s3=\"${TF_BACKEND_ENDPOINT}\"}" \
-backend-config="bucket=${TF_BACKEND_BUCKET}" \
-backend-config="key=${TF_BACKEND_KEY}" \
-backend-config="region=${TF_BACKEND_REGION}" \
-backend-config="skip_credentials_validation=true" \
-backend-config="skip_metadata_api_check=true" \
-backend-config="skip_requesting_account_id=true" \
-backend-config="skip_region_validation=true" \
-backend-config="use_path_style=true"
- name: Terraform Destroy Plan
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
TF_VAR_role_id: ${{ secrets.VAULT_ROLE_ID }}
TF_VAR_secret_id: ${{ secrets.VAULT_SECRET_ID }}
TF_VAR_datacenter: ${{ secrets.VSPHERE_DATACENTER }}
TF_VAR_cluster_name: ${{ secrets.VSPHERE_CLUSTER }}
TF_VAR_environment: ${{ secrets.ENVIRONMENT }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
run: |
echo "Generating destroy plan..."
terraform plan -destroy -out=destroy.tfplan
echo ""
echo "Destroy plan generated. Review the plan above carefully."
- name: Terraform Destroy Execute
env:
AWS_ACCESS_KEY_ID: ${{ secrets.MINIO_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINIO_SECRET_KEY }}
TF_VAR_role_id: ${{ secrets.VAULT_ROLE_ID }}
TF_VAR_secret_id: ${{ secrets.VAULT_SECRET_ID }}
TF_VAR_datacenter: ${{ secrets.VSPHERE_DATACENTER }}
TF_VAR_cluster_name: ${{ secrets.VSPHERE_CLUSTER }}
TF_VAR_environment: ${{ secrets.ENVIRONMENT }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
run: |
echo "🔥 DESTROYING INFRASTRUCTURE..."
echo "This cannot be undone!"
echo ""
terraform apply -auto-approve destroy.tfplan
echo ""
echo "✅ Infrastructure has been destroyed"
echo "State file updated in MinIO: ${{ secrets.MINIO_STATE_KEY }}"