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): fix false positive for rds backup_retention_period not set #5902

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions assets/queries/ansible/aws/rds_with_backup_disabled/query.rego
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ import data.generic.common as common_lib

modules := {"community.aws.rds_instance", "rds_instance"}

CxPolicy[result] {
task := ansLib.tasks[id][t]
instance := task[modules[m]]
ansLib.checkState(instance)

not common_lib.valid_key(instance, "backup_retention_period")

result := {
"documentId": id,
"resourceType": modules[m],
"resourceName": task.name,
"searchKey": sprintf("name={{%s}}.{{%s}}", [task.name, modules[m]]),
"issueType": "MissingAttribute",
"keyExpectedValue": "rds_instance should have the property 'backup_retention_period' greater than 0",
"keyActualValue": "rds_instance has the property 'backup_retention_period' unassigned",
}
}

CxPolicy[result] {
task := ansLib.tasks[id][t]
instance := task[modules[m]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@
username: '{{ username }}'
cluster_id: ansible-test-cluster # This cluster must exist - see rds_cluster to manage it
backup_retention_period: 5
- name: create minimal aurora instance in default VPC and default subnet group2
community.aws.rds_instance:
engine: aurora
db_instance_identifier: ansible-test-aurora-db-instance
instance_type: db.t2.small
password: '{{ password }}'
username: '{{ username }}'
cluster_id: ansible-test-cluster # This cluster must exist - see rds_cluster to manage it
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,3 @@
username: "{{ username }}"
cluster_id: ansible-test-cluster # This cluster must exist - see rds_cluster to manage it
backup_retention_period: 0
- name: create minimal aurora instance in default VPC and default subnet group2
community.aws.rds_instance:
engine: aurora
db_instance_identifier: ansible-test-aurora-db-instance
instance_type: db.t2.small
password: "{{ password }}"
username: "{{ username }}"
cluster_id: ansible-test-cluster # This cluster must exist - see rds_cluster to manage it
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"queryName": "RDS With Backup Disabled",
"severity": "MEDIUM",
"line": 10
},
{
"queryName": "RDS With Backup Disabled",
"severity": "MEDIUM",
"line": 12
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,6 @@ package Cx
import data.generic.common as common_lib
import data.generic.terraform as tf_lib

Copy link
Contributor

Choose a reason for hiding this comment

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

Hello again, @patrickpichler! Thank you for another contribution!

In addition to Miguel's suggestion, do you mind also updating the Ansible query, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've adjusted the Ansible query. I will squash commits later into one, when everything is working 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you so much 🚀

CxPolicy[result] {
db := input.document[i].resource.aws_db_instance[name]

not common_lib.valid_key(db, "backup_retention_period")

result := {
"documentId": input.document[i].id,
"resourceType": "aws_db_instance",
"resourceName": tf_lib.get_resource_name(db, name),
"searchKey": sprintf("aws_db_instance[%s]", [name]),
"issueType": "MissingAttribute",
"keyExpectedValue": "'backup_retention_period' exists",
"keyActualValue": "'backup_retention_period' is missing",
"searchLine": common_lib.build_search_line(["resource", "aws_db_instance", name], []),
"remediation": "backup_retention_period = 12",
"remediationType": "addition",
}
}

CxPolicy[result] {
module := input.document[i].module[name]
keyToCheck := common_lib.get_module_equivalent_key("aws", module.source, "aws_db_instance", "backup_retention_period")

not common_lib.valid_key(module, keyToCheck)

result := {
"documentId": input.document[i].id,
"resourceType": "n/a",
"resourceName": "n/a",
"searchKey": sprintf("module[%s]", [name]),
"issueType": "MissingAttribute",
"keyExpectedValue": "'backup_retention_period' exists",
"keyActualValue": "'backup_retention_period' is missing",
"searchLine": common_lib.build_search_line(["module", name], []),
"remediation": sprintf("%s = 12",[keyToCheck]),
"remediationType": "addition",
}
}

CxPolicy[result] {
db := input.document[i].resource.aws_db_instance[name]
db.backup_retention_period == 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//some comments (used just for resource offset)

resource "aws_db_instance" "negative1" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "mydb"
username = "foo"
password = "foobarbaz"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ module "db" {
instance_class = "db.t2.large"
allocated_storage = 5
auto_minor_version_upgrade = true
backup_retention_period = 0


name = "demodb"
username = "user"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,3 @@ resource "aws_db_instance" "positive1" {
password = "foobarbaz"
backup_retention_period = 0
}


resource "aws_db_instance" "positive2" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "mydb"
username = "foo"
password = "foobarbaz"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module "db" {
instance_class = "db.t2.large"
allocated_storage = 5
auto_minor_version_upgrade = true
backup_retention_period = 0


name = "demodb"
username = "user"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
[
{
"queryName": "RDS With Backup Disabled",
"severity": "MEDIUM",
"line": 16,
"fileName": "positive1.tf"
},
{
"queryName": "RDS With Backup Disabled",
"severity": "MEDIUM",
"line": 12,
"fileName": "positive1.tf"
},
{
"queryName": "RDS With Backup Disabled",
"severity": "MEDIUM",
"line": 1,
"fileName": "positive2.tf"
},
{
"queryName": "RDS With Backup Disabled",
"severity": "MEDIUM",
"line": 12,
"fileName": "positive3.tf"
"fileName": "positive2.tf"
}
]