Skip to content

Commit 5daffe8

Browse files
authored
docs: Initial docs improvement (antonbabenko#218)
1 parent c7d6d00 commit 5daffe8

File tree

6 files changed

+153
-53
lines changed

6 files changed

+153
-53
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*
22
!.dockerignore
3-
!Dockerfile
3+
!Dockerfile

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{*.{sh,py,md},Dockerfile}]
11+
indent_size = 4
12+
13+
[*.md]
14+
trim_trailing_whitespace = false

.pre-commit-config.yaml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
repos:
22
- repo: git://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.4.0
3+
rev: v4.0.1
44
hooks:
5-
- id: check-yaml
5+
# Git style
6+
- id: check-added-large-files
7+
- id: check-merge-conflict
8+
- id: check-vcs-permalinks
9+
- id: forbid-new-submodules
10+
- id: no-commit-to-branch
11+
12+
# Common errors
613
- id: end-of-file-fixer
714
- id: trailing-whitespace
8-
- id: check-case-conflict
15+
args: [--markdown-linebreak-ext=md]
16+
- id: check-yaml
917
- id: check-merge-conflict
1018
- id: check-executables-have-shebangs
19+
20+
# Cross platform
21+
- id: check-case-conflict
22+
23+
# Security
24+
- id: detect-aws-credentials
25+
args: ['--allow-missing-credentials']
26+
- id: detect-private-key
27+
28+
1129
- repo: git://github.com/jumanjihouse/pre-commit-hooks
1230
rev: 2.1.5
1331
hooks:

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ All notable changes to this project will be documented in this file.
151151

152152
- fix: Change terraform_validate hook functionality for subdirectories with terraform files ([#100](https://github.com/antonbabenko/pre-commit-terraform/issues/100))
153153

154-
###
154+
###
155155

156156
configuration for the appropriate working directory.
157157

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM ubuntu:18.04
22

33
ARG PRE_COMMIT_VERSION="2.11.1"
44
ARG TERRAFORM_VERSION="0.15.0"
5-
ARG TFSEC_VERSION="v0.39.21"
5+
ARG TFSEC_VERSION="v0.39.21"
66
ARG TERRAFORM_DOCS_VERSION="v0.12.0"
77
ARG TFLINT_VERSION="v0.27.0"
88
ARG CHECKOV_VERSION="1.0.838"
@@ -33,4 +33,4 @@ RUN tflint --help
3333
RUN tfsec --help
3434
RUN checkov --help
3535

36-
ENTRYPOINT [ "pre-commit" ]
36+
ENTRYPOINT [ "pre-commit" ]

README.md

Lines changed: 114 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
# Collection of git hooks for Terraform to be used with [pre-commit framework](http://pre-commit.com/)
22

3-
[![Github tag](https://img.shields.io/github/tag/antonbabenko/pre-commit-terraform.svg)](https://github.com/antonbabenko/pre-commit-terraform/releases) ![](https://img.shields.io/maintenance/yes/2021.svg) [![Help Contribute to Open Source](https://www.codetriage.com/antonbabenko/pre-commit-terraform/badges/users.svg)](https://www.codetriage.com/antonbabenko/pre-commit-terraform)
3+
[![Github tag](https://img.shields.io/github/tag/antonbabenko/pre-commit-terraform.svg)](https://github.com/antonbabenko/pre-commit-terraform/releases) ![maintenance status](https://img.shields.io/maintenance/yes/2021.svg) [![Help Contribute to Open Source](https://www.codetriage.com/antonbabenko/pre-commit-terraform/badges/users.svg)](https://www.codetriage.com/antonbabenko/pre-commit-terraform)
4+
5+
* [How to install](#how-to-install)
6+
* [1. Install dependencies](#1-install-dependencies)
7+
* [MacOS](#macos)
8+
* [Ubuntu 18.04](#ubuntu-1804)
9+
* [Ubuntu 20.04](#ubuntu-2004)
10+
* [2. Install the pre-commit hook globally](#2-install-the-pre-commit-hook-globally)
11+
* [3. Add configs and hooks](#3-add-configs-and-hooks)
12+
* [4. Run](#4-run)
13+
* [Available Hooks](#available-hooks)
14+
* [Hooks notes](#hooks-notes)
15+
* [terraform_docs](#terraform_docs)
16+
* [terraform_tflint](#terraform_tflint)
17+
* [terraform_tfsec](#terraform_tfsec)
18+
* [terraform_validate](#terraform_validate)
19+
* [Notes for contributors](#notes-for-contributors)
20+
* [Run and debug hooks locally](#run-and-debug-hooks-locally)
21+
* [Authors](#authors)
22+
* [License](#license)
423

524
## How to install
625

@@ -16,28 +35,46 @@
1635

1736
or build and use the Docker image locally as mentioned below in the `Run` section.
1837

19-
##### MacOS
38+
#### MacOS
2039

2140
```bash
2241
brew install pre-commit gawk terraform-docs tflint tfsec coreutils checkov terrascan
2342
```
2443

25-
##### Ubuntu 18.04
44+
#### Ubuntu 18.04
2645

2746
```bash
2847
sudo apt update
2948
sudo apt install -y gawk unzip software-properties-common
3049
sudo add-apt-repository ppa:deadsnakes/ppa
3150
sudo apt install -y python3.7 python3-pip
51+
python3 -m pip install --upgrade pip
3252
pip3 install pre-commit
3353
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-linux-amd64.tar.gz")" > terraform-docs.tgz && tar xzf terraform-docs.tgz && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
3454
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
35-
curl -L "$(curl -s https://api.github.com/repos/tfsec/tfsec/releases/latest | grep -o -E "https://.+?tfsec-linux-amd64")" > tfsec && chmod +x tfsec && sudo mv tfsec /usr/bin/
55+
curl -L "$(curl -s https://api.github.com/repos/aquasecurity/tfsec/releases/latest | grep -o -E "https://.+?tfsec-linux-amd64" | head -n 1)" > tfsec && chmod +x tfsec && sudo mv tfsec /usr/bin/
3656
curl -L "$(curl -s https://api.github.com/repos/accurics/terrascan/releases/latest | grep -o -E "https://.+?_Linux_x86_64.tar.gz")" > terrascan.tar.gz && tar -xf terrascan.tar.gz terrascan && rm terrascan.tar.gz && sudo mv terrascan /usr/bin/
3757
python3.7 -m pip install -U checkov
3858
```
3959

60+
##### Ubuntu 20.04
61+
62+
```bash
63+
sudo apt update
64+
sudo apt install -y gawk unzip software-properties-common
65+
sudo apt install -y python3 python3-pip
66+
python3 -m pip install --upgrade pip
67+
pip3 install pre-commit
68+
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-linux-amd64.tar.gz")" > terraform-docs.tgz && tar -xzf terraform-docs.tgz terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
69+
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
70+
curl -L "$(curl -s https://api.github.com/repos/aquasecurity/tfsec/releases/latest | grep -o -E "https://.+?tfsec-linux-amd64" | head -n 1)" > tfsec && chmod +x tfsec && sudo mv tfsec /usr/bin/
71+
curl -L "$(curl -s https://api.github.com/repos/accurics/terrascan/releases/latest | grep -o -E "https://.+?_Linux_x86_64.tar.gz")" > terrascan.tar.gz && tar -xf terrascan.tar.gz terrascan && rm terrascan.tar.gz && sudo mv terrascan /usr/bin/
72+
pip3 install -U checkov
73+
```
74+
75+
4076
### 2. Install the pre-commit hook globally
77+
4178
Note: not needed if you use the Docker image
4279

4380
```bash
@@ -71,6 +108,7 @@ pre-commit run -a
71108
```
72109

73110
or you can also build and use the provided Docker container, which wraps all dependencies by
111+
74112
```bash
75113
# first building it
76114
docker build -t pre-commit .
@@ -83,80 +121,88 @@ docker run -v $(pwd):/lint -w /lint pre-commit run -a
83121

84122
There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) and Terragrunt configurations (`*.hcl`) in a good shape:
85123

86-
| Hook name | Description |
87-
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
88-
| `terraform_fmt` | Rewrites all Terraform configuration files to a canonical format. |
89-
| `terraform_validate` | Validates all Terraform configuration files. |
90-
| `terraform_docs` | Inserts input and output documentation into `README.md`. Recommended. |
91-
| `terraform_docs_without_aggregate_type_defaults` | Inserts input and output documentation into `README.md` without aggregate type defaults. |
92-
| `terraform_docs_replace` | Runs `terraform-docs` and pipes the output directly to README.md (requires terraform-docs v0.10.0 or later) |
93-
| `terraform_tflint` | Validates all Terraform configuration files with [TFLint](https://github.com/terraform-linters/tflint). |
94-
| `terragrunt_fmt` | Rewrites all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. |
95-
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) |
96-
| `terraform_tfsec` | [TFSec](https://github.com/liamg/tfsec) static analysis of terraform templates to spot potential security issues. |
97-
| `checkov` | [checkov](https://github.com/bridgecrewio/checkov) static analysis of terraform templates to spot potential security issues. |
98-
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. |
124+
| Hook name | Description |
125+
| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------ |
126+
| `terraform_fmt` | Rewrites all Terraform configuration files to a canonical format. [Hook notes](#terraform_docs) |
127+
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) |
128+
| `terraform_docs` | Inserts input and output documentation into `README.md`. Recommended. |
129+
| `terraform_docs_without_aggregate_type_defaults` | Inserts input and output documentation into `README.md` without aggregate type defaults. |
130+
| `terraform_docs_replace` | Runs `terraform-docs` and pipes the output directly to README.md |
131+
| `terraform_tflint` | Validates all Terraform configuration files with [TFLint](https://github.com/terraform-linters/tflint). [Hook notes](#terraform_tflint). |
132+
| `terragrunt_fmt` | Rewrites all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. |
133+
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) |
134+
| `terraform_tfsec` | [TFSec](https://github.com/liamg/tfsec) static analysis of terraform templates to spot potential security issues. [Hook notes](#terraform_tfsec) |
135+
| `checkov` | [checkov](https://github.com/bridgecrewio/checkov) static analysis of terraform templates to spot potential security issues. |
136+
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. |
99137

100138
Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook.
101139

102-
## Notes about terraform_docs hooks
140+
## Hooks notes
141+
142+
### terraform_docs
103143

104144
1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/terraform-docs/terraform-docs) framed by markers:
105-
```txt
106-
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
107145

108-
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
109-
```
110-
if they are present in `README.md`.
146+
```txt
147+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
148+
149+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
150+
```
151+
152+
if they are present in `README.md`.
111153
112-
1. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets you change the name of the file that gets created/modified. This hook requires terraform-docs v0.10.0 or later.
154+
2. `terraform_docs_replace` replaces the entire README.md rather than doing string replacement between markers. Put your additional documentation at the top of your `main.tf` for it to be pulled in. The optional `--dest` argument lets you change the name of the file that gets created/modified.
155+
156+
Example:
113157
114-
1. Example:
115158
```yaml
116159
hooks:
117160
- id: terraform_docs_replace
118161
args: ['--sort-by-required', '--dest=TEST.md']
119162
```
120163
121-
1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing.
164+
3. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing.
122165
123-
## Notes about terraform_tflint hooks
166+
### terraform_tflint
124167
125168
1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode etc.
126169
127-
1. Example:
170+
Example:
171+
128172
```yaml
129173
hooks:
130174
- id: terraform_tflint
131175
args: ['--args=--deep']
132176
```
133177
134178
In order to pass multiple args, try the following:
179+
135180
```yaml
136181
- id: terraform_tflint
137182
args:
138183
- '--args=--deep'
139184
- '--args=--enable-rule=terraform_documented_variables'
140185
```
141186
142-
1. When you have multiple directories and want to run `tflint` in all of them and share single config file it is impractical to hard-code the path to `.tflint.hcl` file. The solution is to use `__GIT_WORKING_DIR__` placeholder which will be replaced by `terraform_tflint` hooks with Git working directory (repo root) at run time. For example:
187+
3. When you have multiple directories and want to run `tflint` in all of them and share single config file it is impractical to hard-code the path to `.tflint.hcl` file. The solution is to use `__GIT_WORKING_DIR__` placeholder which will be replaced by `terraform_tflint` hooks with Git working directory (repo root) at run time. For example:
143188
144-
```yaml
145-
hooks:
146-
- id: terraform_tflint
147-
args:
148-
- '--args=--config=__GIT_WORKING_DIR__/.tflint.hcl'
149-
```
189+
```yaml
190+
hooks:
191+
- id: terraform_tflint
192+
args:
193+
- '--args=--config=__GIT_WORKING_DIR__/.tflint.hcl'
194+
```
150195
151196
152-
## Notes about terraform_tfsec hooks
197+
### terraform_tfsec
153198
154199
1. `terraform_tfsec` will consume modified files that pre-commit
155200
passes to it, so you can perform whitelisting of directories
156201
or files to run against via [files](https://pre-commit.com/#config-files)
157202
pre-commit flag
158203
159-
1. Example:
204+
Example:
205+
160206
```yaml
161207
hooks:
162208
- id: terraform_tfsec
@@ -167,44 +213,51 @@ if they are present in `README.md`.
167213
only such that the underlying `tfsec` tool can run against changed files in this
168214
directory, ignoring any other folders at the root level
169215
170-
1. To ignore specific warnings, follow the convention from the
216+
2. To ignore specific warnings, follow the convention from the
171217
[documentation](https://github.com/liamg/tfsec#ignoring-warnings).
172-
1. Example:
218+
219+
Example:
220+
173221
```hcl
174222
resource "aws_security_group_rule" "my-rule" {
175223
type = "ingress"
176224
cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:AWS006
177225
}
178226
```
179227
180-
## Notes about terraform_validate hooks
228+
### terraform_validate
181229
182230
1. `terraform_validate` supports custom arguments so you can pass supported no-color or json flags.
183231
184-
1. Example:
232+
Example:
233+
185234
```yaml
186235
hooks:
187236
- id: terraform_validate
188237
args: ['--args=-json']
189238
```
190239
191240
In order to pass multiple args, try the following:
241+
192242
```yaml
193243
- id: terraform_validate
194244
args:
195245
- '--args=-json'
196246
- '--args=-no-color'
197247
```
198-
1. `terraform_validate` also supports custom environment variables passed to the pre-commit runtime
199248
200-
1. Example:
249+
2. `terraform_validate` also supports custom environment variables passed to the pre-commit runtime
250+
251+
Example:
252+
201253
```yaml
202254
hooks:
203255
- id: terraform_validate
204256
args: ['--envs=AWS_DEFAULT_REGION="us-west-2"']
205257
```
206258
207259
In order to pass multiple args, try the following:
260+
208261
```yaml
209262
- id: terraform_validate
210263
args:
@@ -213,22 +266,37 @@ if they are present in `README.md`.
213266
- '--envs=AWS_SECRET_ACCESS_KEY="asecretkey"'
214267
```
215268
216-
1. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc). To solve this problem you can find and delete all `.terraform` directories in your repository using this command:
269+
3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc). To solve this problem you can find and delete all `.terraform` directories in your repository using this command:
217270
218271
```shell
219272
find . -type d -name ".terraform" -print0 | xargs -0 rm -r
220273
```
221274
222275
`terraform_validate` hook will try to reinitialize them before running `terraform validate` command.
223276
224-
## Notes for developers
277+
## Notes for contributors
225278
226279
1. Python hooks are supported now too. All you have to do is:
227280
1. add a line to the `console_scripts` array in `entry_points` in `setup.py`
228-
1. Put your python script in the `pre_commit_hooks` folder
281+
2. Put your python script in the `pre_commit_hooks` folder
229282
230283
Enjoy the clean, valid, and documented code!
231284
285+
### Run and debug hooks locally
286+
287+
```bash
288+
pre-commit try-repo {-a} /path/to/local/pre-commit-terraform/repo {hook_name}
289+
```
290+
291+
I.e.
292+
293+
```bash
294+
pre-commit try-repo /mnt/c/Users/tf/pre-commit-terraform terraform_fmt # Run only `terraform_fmt` check
295+
pre-commit try-repo -a ~/pre-commit-terraform # run all existing checks from repo
296+
```
297+
298+
Running `pre-commit` with `try-repo` ignores all arguments specified in `.pre-commit-config.yaml`.
299+
232300
## Authors
233301

234302
This repository is managed by [Anton Babenko](https://github.com/antonbabenko) with help from [these awesome contributors](https://github.com/antonbabenko/pre-commit-terraform/graphs/contributors).

0 commit comments

Comments
 (0)