|
| 1 | +# TFSec Security Scan Evidence Example |
| 2 | + |
| 3 | +This example demonstrates how to automate TFSec security scanning for Terraform code and attach the scan results as |
| 4 | +signed evidence to the package in JFrog Artifactory using GitHub Actions and JFrog CLI. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The workflow scans Terraform code with TFSec for security issues, publishes the package to Artifactory, and |
| 9 | +attaches the TFSec scan results as evidence to the package. This enables traceability and compliance for security |
| 10 | +scanning in your CI/CD pipeline. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- JFrog CLI 2.65.0 or above (installed automatically in the workflow) |
| 15 | +- Artifactory configured as a repository |
| 16 | +- The following GitHub repository variables: |
| 17 | + - `ARTIFACTORY_URL` (Artifactory base URL) |
| 18 | + - `EVIDENCE_KEY_ALIAS` (Key alias for signing evidence) |
| 19 | +- The following GitHub repository secrets: |
| 20 | + - `ARTIFACTORY_ACCESS_TOKEN` (Artifactory access token) |
| 21 | + - `PRIVATE_KEY` (Private key for signing evidence) |
| 22 | + |
| 23 | +## Environment Variables Used |
| 24 | + |
| 25 | +- `ATTACH_OPTIONAL_CUSTOM_MARKDOWN_TO_EVIDENCE` - Whether to attach a custom markdown report to the evidence |
| 26 | + |
| 27 | +## Workflow Steps |
| 28 | + |
| 29 | +1. **Install JFrog CLI** |
| 30 | + - Installs the JFrog CLI using the official GitHub Action. |
| 31 | +2. **Checkout Repository** |
| 32 | + - Checks out the source code for the Terraform package. |
| 33 | +3. **Publish Terraform Package to Artifactory** |
| 34 | + - Publishes the Terraform package to Artifactory using JFrog CLI. |
| 35 | +4. **Run TFSec Security Scan** |
| 36 | + - Scans the Terraform code for security issues using TFSec and outputs the results in JSON format. |
| 37 | +5. **Generate Custom Markdown For TFSec Results** |
| 38 | + - (Optional) Converts the TFSec JSON scan results to markdown format for better readability using a Python script |
| 39 | + with a predefined static markdown template. |
| 40 | +6. **Attach TFSec Evidence Using JFrog CLI** |
| 41 | + - Attaches the TFSec scan results as signed evidence to the Terraform package in Artifactory. |
| 42 | + |
| 43 | +```mermaid |
| 44 | +graph TD |
| 45 | + A[Workflow Dispatch Trigger] --> B[Setup JFrog CLI] |
| 46 | + B --> C[Checkout Repository] |
| 47 | + C --> D[Publish Terraform Package to Artifactory] |
| 48 | + D --> E[Run TFSec Security Scan] |
| 49 | + E --> F{Attach Optional Custom Markdown Report?} |
| 50 | + F -->|Yes| G[Generate Custom Markdown Report] |
| 51 | + F -->|No| H[Skip Markdown Report] |
| 52 | + G --> I[Attach Evidence Using JFrog CLI] |
| 53 | + H --> I[Attach Evidence Using JFrog CLI] |
| 54 | +``` |
| 55 | + |
| 56 | +## Example Usage |
| 57 | + |
| 58 | +You can trigger the workflow manually from the GitHub Actions tab. The workflow will: |
| 59 | + |
| 60 | +- Scan the Terraform code |
| 61 | +- Publish the package to Artifactory |
| 62 | +- Attach the TFSec scan results as evidence |
| 63 | + |
| 64 | +## Key Commands Used |
| 65 | + |
| 66 | +- **Publish Terraform Package:** |
| 67 | + ```bash |
| 68 | + jf tfc --repo-deploy tf-local \ |
| 69 | + --server-id-deploy setup-jfrog-cli-server |
| 70 | + jf tf p --namespace example \ |
| 71 | + --provider aws \ |
| 72 | + --tag v0.0.${{ github.run_number }} \ |
| 73 | + --build-name my-tf-build \ |
| 74 | + --build-number ${{ github.run_number }} |
| 75 | + jf rt bp my-tf-build ${{ github.run_number }} |
| 76 | + ``` |
| 77 | +- **Run TFSec Scan:** |
| 78 | + ```yaml |
| 79 | + uses: aquasecurity/tfsec-action@v1.0.0 |
| 80 | + with: |
| 81 | + additional_args: --format json --out tfsec.json |
| 82 | + soft_fail: true |
| 83 | + ``` |
| 84 | +- **Attach Evidence:** |
| 85 | + ```bash |
| 86 | + jf evd create \ |
| 87 | + --build-name my-tf-build \ |
| 88 | + --build-number ${{ github.run_number }} \ |
| 89 | + --key "${{ secrets.PRIVATE_KEY }}" \ |
| 90 | + --key-alias "${{ vars.EVIDENCE_KEY_ALIAS }}" \ |
| 91 | + --predicate ./tfsec.json \ |
| 92 | + --predicate-type http://aquasec.com/tfsec/security-scan \ |
| 93 | + ${{ env.ATTACH_OPTIONAL_CUSTOM_MARKDOWN_TO_EVIDENCE == 'true' && '--markdown "tfsec.md"' || '' }} |
| 94 | + ``` |
| 95 | + |
| 96 | +## References |
| 97 | + |
| 98 | +- [TFSec Documentation](https://aquasecurity.github.io/tfsec/) |
| 99 | +- [JFrog Evidence Management](https://jfrog.com/help/r/jfrog-artifactory-documentation/evidence-management) |
| 100 | +- [JFrog CLI Documentation](https://jfrog.com/getcli/) |
0 commit comments