Learning center

Is a checkov finding on a variable default real if every environment overrides it?

Usually yes: the default is the value that ships whenever a caller omits the variable, so changing the default is the cheap fix. Dismiss the finding only with evidence that every caller of the module sets the variable, and collect that evidence by scanning each root module with its own variable files.

Find where the failing value came from#

The fixture is constructed. A shared module, db/, declares one variable and passes it to an RDS instance:

variable "publicly_accessible" {
  type    = bool
  default = true
}

resource "aws_db_instance" "main" {
  identifier          = "orders"
  engine              = "postgres"
  instance_class      = "db.t4g.micro"
  allocated_storage   = 20
  username            = "app"
  manage_master_user_password = true
  publicly_accessible = var.publicly_accessible
}

Scanning the module directory with checkov 3.4.11 (installed from PyPI, run with --skip-download because the container has no route to the vendor's API) fails CKV_AWS_17:

$ checkov --skip-download --compact --framework terraform --check CKV_AWS_17 -d db
terraform scan results:
Passed checks: 0, Failed checks: 1, Skipped checks: 0
Check: CKV_AWS_17: "Ensure all data stored in RDS is not publicly accessible"
	FAILED for resource: aws_db_instance.main
	File: /main.tf:6-14

The value came from the default. checkov's documentation describes the mechanism: "Checkov collects the default values of variables and assigns them to their corresponding references in Terraform expressions." (Handling Variables, checkov 3.4.11) The same page shows the CLI printing a line such as Variable acl (of /variables.tf) evaluated to value ... under the result. This run printed no such line, and the JSON output's evaluations field for the result was null, so the finding did not say that a default produced it. Read the flagged attribute: if it is var.something, open the variable's declaration.

Scan each environment with its own values#

The repository has two environments that call the module and set the variable from a variable file:

$ find . -path ./venv -prune -o -type f -print | sort
./db/main.tf
./envs/prod/main.tf
./envs/prod/prod.tfvars
./envs/reporting/main.tf
./envs/staging/main.tf
./envs/staging/staging.tfvars

envs/prod/main.tf and envs/staging/main.tf each declare variable "publicly_accessible" { type = bool } with no default and pass it to module "db". Each .tfvars file contains publicly_accessible = false. A scan of a root with its file passes:

$ checkov --skip-download --quiet --compact --framework terraform --check CKV_AWS_17 \
    -d envs/prod --var-file envs/prod/prod.tfvars
Passed checks: 1, Failed checks: 0, Skipped checks: 0

The same scan without --var-file printed no result at all, and its JSON summary reported "passed": 0, "failed": 0, "skipped": 0 and "resource_count": 0. The variable has no default there, so the attribute still holds an unresolved reference, and the check's base class returns CheckResult.UNKNOWN when the inspected value still contains a reference (base_resource_negative_value_check.py, checkov 3.4.11). An unknown result is not reported as a pass or a failure. A clean scan of a root module with no variable values is therefore no evidence for that environment.

checkov reads some variable files without being told. The CLI reference describes --var-file as "Variable files to load in addition to the default files" (CLI Command Reference, checkov 3.4.11). Copying prod.tfvars to envs/prod/terraform.tfvars made the plain -d envs/prod scan pass. Terraform itself loads terraform.tfvars and files ending in .auto.tfvars automatically (Input Variables, Terraform v1.9.8); files with other names, such as prod.tfvars, apply only when the pipeline passes them with -var-file. Check which files your pipeline passes, and scan with the same ones.

The disconfirmation: a caller that omits the variable#

The third root, envs/reporting/main.tf, was added later and does not set the variable:

module "db" {
  source = "../../db"
}

It fails, and checkov names both the module file and the calling file:

$ checkov --skip-download --quiet --compact --framework terraform --check CKV_AWS_17 -d envs/reporting
Passed checks: 0, Failed checks: 1, Skipped checks: 0
	FAILED for resource: module.db.aws_db_instance.main
	File: /../../db/main.tf:6-14
	Calling File: /main.tf:1-3

A scan of the whole repository reports the same single failure, with Calling File: /envs/reporting/main.tf:1-3, and nothing for prod or staging. That line is the evidence the finding needed: the module's default reached a real caller. Terraform's documentation states the rule: with a default, "the variable is considered to be optional and the default value will be used if no value is set when calling the module or running Terraform." The module's default also replaces the provider's own, since the AWS provider documents publicly_accessible with "Default is false." (aws_db_instance, terraform-provider-aws v5.80.0)

Listing the callers that omit the input is a one-line check in a repository that references the module by path:

$ grep -rl 'source *= *"../../db"' envs | sort | xargs grep -L publicly_accessible
envs/reporting/main.tf

A module consumed from a registry or another repository has callers this search cannot see, and then a dismissal cannot rest on a search.

The fix is one line#

In a copy of the repository under alt/, changing the module's default to false made the reporting root pass with no other change:

$ diff db/main.tf alt/db/main.tf
3c3
<   default = true
---
>   default = false
$ checkov --skip-download --quiet --compact --framework terraform --check CKV_AWS_17 -d alt/envs/reporting
Passed checks: 1, Failed checks: 0, Skipped checks: 0

Callers that need a public instance now have to say so in their own configuration, where a reviewer sees it. Removing the default makes the variable required instead, so every caller must choose; scans of roots without variable values then return unknown, as above, rather than a pass.

Decide by caller#

What the finding saysEvidence to collectDecision
Insecure value, attribute is a variable with an insecure defaultThe variable declaration, and whether the module has callers outside this repositoryChange the default; callers that need the other value set it explicitly
Same, every caller is in this repositoryA scan of each root with the variable files its pipeline passes; a search for callers that omit the inputDismiss only if every root passes and none omits it; record the scans
A root scan shows no result for the checkThe scan's JSON summary, and whether the variable has a value in that scanRescan with the pipeline's variable files; an empty result is not a pass
A failure with Calling File: pointing at a rootThat root's module blockFix it: the default reaches a real deployment

A default that differs between the code and what each environment supplies is the same kind of problem the guide to configuration changes incompatible with production covers for application settings. If you do dismiss the finding, record the per-root scans as the guide to dismissed findings describes, so a new caller reopens the question.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading