Skip to content

Commit 1c31712

Browse files
authored
Merge pull request #184 from dflook/test-load
Try loading hcl files in another process
2 parents 62ade84 + b0d2672 commit 1c31712

File tree

4 files changed

+106
-11
lines changed

4 files changed

+106
-11
lines changed

.github/workflows/test-validate.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,29 @@ jobs:
100100
with:
101101
path: tests/workflows/test-validate/workspace_eval_remote
102102
workspace: prod
103+
104+
validate_unterminated_string:
105+
runs-on: ubuntu-latest
106+
name: Validate with unterminated string
107+
steps:
108+
- name: Checkout
109+
uses: actions/checkout@v2
110+
111+
- name: validate
112+
uses: ./terraform-validate
113+
with:
114+
path: tests/workflows/test-validate/unterminated-string
115+
id: validate
116+
continue-on-error: true
117+
118+
- name: Check invalid
119+
run: |
120+
if [[ "${{ steps.validate.outcome }}" != "failure" ]]; then
121+
echo "Validate did not fail correctly"
122+
exit 1
123+
fi
124+
125+
if [[ "${{ steps.validate.outputs.failure-reason }}" != "validate-failed" ]]; then
126+
echo "::error:: failure-reason not set correctly"
127+
exit 1
128+
fi

image/src/terraform/hcl.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Wraps python-hcl
3+
"""
4+
5+
import hcl2 # type: ignore
6+
import sys
7+
import subprocess
8+
from pathlib import Path
9+
10+
from github_actions.debug import debug
11+
12+
13+
def try_load(path: Path) -> dict:
14+
try:
15+
with open(path) as f:
16+
return hcl2.load(f)
17+
except:
18+
return {}
19+
20+
21+
def is_loadable(path: Path) -> bool:
22+
try:
23+
subprocess.run(
24+
[sys.executable, '-m', 'terraform.hcl', path],
25+
timeout=10
26+
)
27+
except subprocess.TimeoutExpired:
28+
debug('TimeoutExpired')
29+
# We found a file that won't parse :(
30+
return False
31+
except:
32+
# If we get an exception, we can still try and load it.
33+
return True
34+
35+
return True
36+
37+
38+
def load(path: Path) -> dict:
39+
if is_loadable(path):
40+
return try_load(path)
41+
42+
debug(f'Unable to load {path}')
43+
raise ValueError(f'Unable to load {path}')
44+
45+
46+
def loads(hcl: str) -> dict:
47+
tmp_path = Path('/tmp/load_test.hcl')
48+
49+
with open(tmp_path, 'w') as f:
50+
f.write(hcl)
51+
52+
if is_loadable(tmp_path):
53+
return hcl2.loads(hcl)
54+
55+
debug(f'Unable to load hcl')
56+
raise ValueError(f'Unable to load hcl')
57+
58+
59+
if __name__ == '__main__':
60+
try_load(Path(sys.argv[1]))

image/src/terraform/module.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
from typing import Any, cast, NewType, Optional, TYPE_CHECKING, TypedDict
77

8-
import hcl2 # type: ignore
8+
import terraform.hcl
99

1010
from github_actions.debug import debug
1111
from terraform.versions import Constraint
@@ -66,22 +66,21 @@ def load_module(path: Path) -> TerraformModule:
6666
if not file.endswith('.tf'):
6767
continue
6868

69-
with open(os.path.join(path, file)) as f:
70-
try:
71-
module = merge(module, cast(TerraformModule, hcl2.load(f)))
72-
except Exception as e:
73-
# ignore tf files that don't parse
74-
debug(f'Failed to parse {file}')
75-
debug(str(e))
69+
try:
70+
tf_file = cast(TerraformModule, terraform.hcl.load(os.path.join(path, file)))
71+
module = merge(module, tf_file)
72+
except Exception as e:
73+
# ignore tf files that don't parse
74+
debug(f'Failed to parse {file}')
75+
debug(str(e))
7676

7777
return module
7878

7979

8080
def load_backend_config_file(path: Path) -> TerraformModule:
8181
"""Load a backend config file."""
8282

83-
with open(path) as f:
84-
return cast(TerraformModule, hcl2.load(f))
83+
return cast(TerraformModule, terraform.hcl.load(path))
8584

8685

8786
def read_cli_config(config: str) -> dict[str, str]:
@@ -93,7 +92,7 @@ def read_cli_config(config: str) -> dict[str, str]:
9392

9493
hosts = {}
9594

96-
config_hcl = hcl2.loads(config)
95+
config_hcl = terraform.hcl.loads(config)
9796

9897
for credential in config_hcl.get('credentials', {}):
9998
for cred_hostname, cred_conf in credential.items():
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module "enforce_mfa" {
2+
source = "terraform-module/enforce-mfa/aws"
3+
version = "0.13.0”
4+
policy_name = "managed-mfa-enforce"
5+
account_id = data.aws_caller_identity.current.id
6+
groups = [aws_iam_group.console_group.name]
7+
manage_own_signing_certificates = true
8+
manage_own_ssh_public_keys = true
9+
manage_own_git_credentials = true
10+
}

0 commit comments

Comments
 (0)