Skip to content

Commit 024fb2b

Browse files
committed
add tfsec
1 parent 99967bd commit 024fb2b

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# tfsec For Terraform Security Scanning
2+
3+
## 🎯 Purpose
4+
In this lab, you'll learn how to use tfsec, a static analysis security scanner for your Terraform code, and integrate it into your GitHub Actions workflow for automated security checks.
5+
6+
## 🛠️ Install and Run tfsec
7+
8+
### Prerequisites
9+
- [ ] Basic understanding of Terraform
10+
- [ ] GitHub repository with Terraform code
11+
- [ ] Permissions to update GitHub Actions workflows
12+
13+
### Steps
14+
15+
1. **Install tfsec Locally**
16+
17+
Install tfsec using one of the following methods:
18+
19+
**Homebrew (macOS/Linux)**:
20+
```bash
21+
brew install tfsec
22+
```
23+
24+
**Docker**:
25+
```bash
26+
docker run --rm -it -v "$(pwd):/src" aquasec/tfsec /src
27+
```
28+
29+
**Go**:
30+
```bash
31+
go install github.com/aquasecurity/tfsec/cmd/tfsec@latest
32+
```
33+
34+
**Chocolatey (Windows)**:
35+
```bash
36+
choco install tfsec
37+
```
38+
39+
2. **Run tfsec Locally**
40+
41+
Run tfsec against your Terraform code:
42+
```bash
43+
tfsec /path/to/terraform/code
44+
```
45+
46+
For example:
47+
```bash
48+
tfsec DevOps-The-Hard-Way-Azure/2-Terraform-Azure-services-creation/4-aks
49+
```
50+
51+
3. **Add tfsec to GitHub Actions Workflow**
52+
53+
Open your GitHub Actions workflow file (`.github/workflows/main.yml`) and add the tfsec action:
54+
55+
```yaml
56+
- name: tfsec
57+
uses: aquasecurity/[email protected]
58+
with:
59+
tfsec_args: --soft-fail
60+
github_token: ${{ github.token }}
61+
```
62+
63+
The `--soft-fail` argument ensures the workflow doesn't fail when security issues are found, but still reports them as comments on your PR.
64+
65+
4. **Understanding tfsec Results**
66+
67+
tfsec checks include:
68+
- [ ] Insecure security group rules
69+
- [ ] Unencrypted resources
70+
- [ ] Public exposure of sensitive resources
71+
- [ ] Missing logging configurations
72+
- [ ] IAM misconfigurations
73+
- [ ] Azure-specific security best practices
74+
75+
## 🔍 Verification
76+
77+
To ensure tfsec is working correctly:
78+
1. Run tfsec locally to see immediate results
79+
2. Create a pull request with Terraform code changes
80+
3. Verify that tfsec is adding security-related comments to your PR
81+
4. Review and address the issues identified
82+
83+
Example tfsec output:
84+
85+
```
86+
Results:
87+
HIGH: Resource 'azurerm_storage_account.storage' uses unencrypted storage for account 'mystorageaccount'
88+
Impact: Data could be read if compromised
89+
Resolution: Enable encryption for storage accounts
90+
More info: https://aquasecurity.github.io/tfsec/v1.28.0/checks/azure/storage/encrypt-in-transit/
91+
File: ./storage.tf:Line:1:Column:1
92+
```
93+
94+
## 🧠 Knowledge Check
95+
96+
After integrating tfsec, consider these questions:
97+
1. How does tfsec differ from Checkov in its approach to security scanning?
98+
2. What are the benefits of having security checks integrated directly into the PR process?
99+
3. How would you handle false positives in tfsec findings?
100+
4. What is the significance of the `--soft-fail` flag in the GitHub Action?
101+
102+
## 💡 Pro Tips
103+
104+
1. **Customise Checks with .tfsec.yml**
105+
106+
Create a `.tfsec.yml` file in your repository root to customise which checks to include or exclude:
107+
108+
```yaml
109+
exclude:
110+
# Exclude a specific check
111+
- azure-storage-use-secure-tls-policy
112+
113+
# Set minimum severity level
114+
minimum_severity: MEDIUM
115+
```
116+
117+
2. **Generate a Baseline**
118+
119+
If you have existing issues that you want to ignore temporarily:
120+
121+
```bash
122+
tfsec --soft-fail --out=tfsec.baseline ./path/to/code
123+
```
124+
125+
Then use the baseline in future scans:
126+
127+
```bash
128+
tfsec --baseline tfsec.baseline ./path/to/code
129+
```
130+
131+
3. **Output Formats**
132+
133+
tfsec supports multiple output formats for CI/CD integration:
134+
135+
```bash
136+
# JSON output
137+
tfsec --format=json ./path/to/code
138+
139+
# SARIF format (for GitHub Code Scanning)
140+
tfsec --format=sarif ./path/to/code
141+
142+
# JUnit format (for test reporting)
143+
tfsec --format=junit ./path/to/code
144+
```

0 commit comments

Comments
 (0)