Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query): be able to check default_tags on multiple providers #4839

Conversation

jycamier
Copy link
Contributor

@jycamier jycamier commented Feb 15, 2022

Context

In the query aws/resource_not_using_tags, the query contains the following function

check_default_tags {
	common_lib.valid_key(input.document[_].provider[_].default_tags, "tags")
} else = false {
	true
}

This function is only able to test if there's a single provider.

provider "aws" {
  region  = "eu-west-3"
  alias   = "foobar"
  profile = "foobar"
  default_tags {
    tags = {
      foo = "bar"
    }
  }
}

the payload

{
	"document": [
		{
			"file": "/Users/jycamier/workspace/terraform/00_network_tgw/toto/provider.tf",
			"id": "012c8e85-a9ee-405b-9294-2b589e389330",
			"provider": {
				"aws": {
					"alias": "foobar",
					"default_tags": {
						"tags": {
							"foo": "bar"
						}
					},
					"profile": "foobar",
					"region": "eu-west-3"
				}
			}
		}
	]
}

input.document.provider.aws is a struct so all is fine.

If you're using more than one provider :

provider "aws" {
  region  = "eu-west-3"
  alias   = "foobar"
  profile = "foobar"
  default_tags {
    tags = {
      foo = "bar"
    }
  }
}

provider "aws" {
  region  = "eu-west-3"
  alias   = "foobar2"
  profile = "foobar2"
  default_tags {
    tags = {
      foo = "foobar2"
    }
  }
}
{
	"document": [
		{
			"file": "/Users/jycamier/workspace/terraform/00_network_tgw/toto/provider.tf",
			"id": "796949ef-7c5b-479d-a99c-eb660eedd5bc",
			"provider": {
				"aws": [
					{
						"alias": "foobar",
						"default_tags": {
							"tags": {
								"foo": "bar"
							}
						},
						"profile": "foobar",
						"region": "eu-west-3"
					},
					{
						"alias": "foobar2",
						"default_tags": {
							"tags": {
								"foo": "foobar2"
							}
						},
						"profile": "foobar2",
						"region": "eu-west-3"
					}
				]
			}
		}
	]
}

input.document.provider.aws become an array of struct in this case and the rego function isn't able to access to default_tags.

Proposed Changes

  • we can use with the input.document.provider.aws as our rules is impact only aws resources
  • we check default_tags if we've more than one aws providers

I submit this contribution under the Apache-2.0 license.

@kicsbot
Copy link
Contributor

kicsbot commented Feb 15, 2022

Scan submitted to Checkmarx

@kicsbot
Copy link
Contributor

kicsbot commented Feb 15, 2022

Logo
Checkmarx SAST - Scan Summary & Details

Cx-SAST Summary

Total of 5 vulnerabilities
High 0 High
Medium 0 Medium
Low 5 Low
Info 0 Info

Violation Summary

No policy violation found

Copy link
Contributor

@rafaela-soares rafaela-soares left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great observation, @jycamier 🙂 Thank you so much for contributing!

LGTM 🚀

@joaoReigota1 joaoReigota1 merged commit 7429db3 into Checkmarx:master Feb 15, 2022
@jycamier jycamier deleted the query/fix_aws_resource_not_using_tags_with_multiple_providers branch February 15, 2022 19:45
@rafaela-soares rafaela-soares added the community Community contribution label Mar 16, 2022
@rafaela-soares rafaela-soares added the query New query feature label Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Community contribution query New query feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants