Skip to content

Commit bf42103

Browse files
committed
Adding check-iam check
1 parent 5191f11 commit bf42103

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
entry: check-json
4747
language: python
4848
types: [json]
49+
- id: check-iam
50+
name: check IAM
51+
description: checks terraform files for parseable IAM syntax.
52+
entry: check-iam
53+
language: python
54+
types: [tf]
4955
- id: check-shebang-scripts-are-executable
5056
name: check that scripts with shebangs are executable
5157
description: ensures that (non-binary) files with a shebang are executable.

pre_commit_hooks/check_iam.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import argparse
2+
import hcl2
3+
# from typing import Any
4+
from typing import Sequence
5+
6+
7+
def main(argv: Sequence[str] | None = None) -> int:
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
10+
args = parser.parse_args(argv)
11+
12+
retval = 0
13+
for filename in args.filenames:
14+
if filename == "iam.tf":
15+
continue
16+
with open(filename, 'rb') as f:
17+
try:
18+
dict = hcl2.load(f)
19+
except ValueError as exc:
20+
print(f'{filename}: Failed to hcl decode ({exc})')
21+
retval = 1
22+
resources = data.get('resource')
23+
if resources:
24+
for item in resources:
25+
for keys in item:
26+
if key.startswith("aws_iam"):
27+
print(f'{filename}: Has {key} resource')
28+
retval = 1
29+
resources = data.get('data')
30+
if resources:
31+
for item in resources:
32+
for keys in item:
33+
if key.startswith("aws_iam"):
34+
print(f'{filename}: Has {key} data resource')
35+
retval = 1
36+
return retval
37+
38+
39+
if __name__ == '__main__':
40+
raise SystemExit(main())

0 commit comments

Comments
 (0)