terrascan: Unexpected exit code of 4 when running a scan on an empty directory
- terrascan version: v1.10.0
- Operating System: Ubuntu Ubuntu 20.04.3 (Docker)
Description
I am running a terrascan scan on an empty directory and it is failing with an exit code of 4. When I provide secure terraform, it provides the exact same output, but exits 0.
What I Did
Empty directory:
easy_infra@983eec90355e:/iac$ ls
easy_infra@983eec90355e:/iac$ terrascan scan -i terraform -t all -d .
2021-09-29T23:54:46.577Z info downloader/module-download.go:80 Error building terraform credentials /home/easy_infra/.terraformrc: error converting credential map to host map
Scan Summary -
File/Folder : /iac
IaC Type : terraform
Scanned At : 2021-09-29 23:54:47.8368166 +0000 UTC
Policies Validated : 783
Violated Policies : 0
Low : 0
Medium : 0
High : 0
easy_infra@983eec90355e:/iac$ echo $?
4
Valid, secure .tf file
easy_infra@3b11d8ef3552:/iac$ ls
secure.tf
easy_infra@3b11d8ef3552:/iac$ terrascan scan -i terraform -t all -d .
2021-09-29T23:54:04.064Z info downloader/module-download.go:80 Error building terraform credentials /home/easy_infra/.terraformrc: error converting credential map to host map
Scan Summary -
File/Folder : /iac
IaC Type : terraform
Scanned At : 2021-09-29 23:54:05.4489646 +0000 UTC
Policies Validated : 0
Violated Policies : 0
Low : 0
Medium : 0
High : 0
easy_infra@3b11d8ef3552:/iac$ echo $?
0
Valid, insecure .tf file:
easy_infra@e10573fd76b1:/iac$ ls
insecure.tf
easy_infra@e10573fd76b1:/iac$ terrascan scan -i terraform -t all -d .
2021-09-29T23:57:20.663Z info downloader/module-download.go:80 Error building terraform credentials /home/easy_infra/.terraformrc: err
or converting credential map to host map
Violation Details -
Description : Ensure that detailed monitoring is enabled for EC2 instances.
File : insecure.tf
Module Name : root
Plan Root : ./
Line : 17
Severity : HIGH
-----------------------------------------------------------------------
Description : Ensure that your AWS application is not deployed within the default Virtual Private Cloud in order to follow security best practices
File : insecure.tf
Module Name : root
Plan Root : ./
Line : 17
Severity : MEDIUM
-----------------------------------------------------------------------
Description : EC2 instances should disable IMDS or require IMDSv2 as this can be related to the weaponization phase of kill chain
File : insecure.tf
Module Name : root
Plan Root : ./
Line : 17
Severity : MEDIUM
-----------------------------------------------------------------------
Scan Summary -
File/Folder : /iac
IaC Type : terraform
Scanned At : 2021-09-29 23:57:21.8560275 +0000 UTC
Policies Validated : 5
Violated Policies : 3
Low : 0
Medium : 2
High : 1
easy_infra@e10573fd76b1:/iac$ echo $?
3
My ~/.terraformrc file is:
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 13
- Comments: 15 (4 by maintainers)
I get this same
exit code 4when there are no violated policies (due to skip-rules configuration).Note: I am using the Docker container method:
TerraScan TOML file:
dumbest of dumb suggestions but: I dropped an
empty.tfin all the empty directories to quiet terrascan.Any updates?
I looked deeper into my problem. Essentially I got the exit code 4, because I had directories in my project, which did not contain any Terraform files. It was not due to the skip-rules configuration. After fixing my initial problem also the skip-rules return an exit code 0. I think the problem is that even in verbose mode there are not a lot of pointers on what is going wrong. I had to add some additional logging to
run.goto see what’s going on and then run it again with the customized binary. The fix was to add empty Terraform configuration files into these directories, which is probably not perfect.As it was mentioned somewhere that we’re simulating
terraform planwith the exit codes:terraform planreturns an exit code 0 if a sub-directory does not have a Terraform configuration file in it.Running into the same problem (with the skip-rules configuration), which actually let’s the terrascan-action for GitHub fail. It could probably be related to the changes in #994 where the exit code behavior was changed.