-
Notifications
You must be signed in to change notification settings - Fork 349
feat(query): implements "Beta - SQL DB Instance With Minimum Log Duration" #7784
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
Open
cx-andre-pereira
wants to merge
8
commits into
master
Choose a base branch
from
AST-116628_15_6.2_PostgreSQL_Database_ensure_that_the_'log_min_duration_statement'_database_flag_for_cloud_sql_postgresql_instance_is_set_to_-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f3c12b9
initial implementation
cx-andre-pereira 16c5f2b
fix 2
cx-andre-pereira 4c0a1ea
simplified query logic
cx-andre-pereira 9642ed3
added single object support
cx-andre-pereira a7034c4
Merge branch 'master' into AST-116628_15_6.2_PostgreSQL_Database_ensu…
cx-andre-pereira 04c5fd3
fixed tests
cx-andre-pereira c1d07e9
Merge branch 'master' of https://github.com/Checkmarx/kics into AST-1…
cx-andre-pereira f436fad
simId transition update
cx-andre-pereira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
assets/queries/terraform/gcp/sql_db_instance_with_minimum_log_duration/metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "id": "00335e17-674c-442e-a64c-9436e60e6efb", | ||
| "queryName": "Beta - SQL DB Instance With Minimum Log Duration", | ||
| "severity": "MEDIUM", | ||
| "category": "Observability", | ||
| "descriptionText": "No 'google_sql_database_instance' resource based on POSTGRES should set the 'log_min_duration_statement' flag to a value that is not '-1'", | ||
| "descriptionUrl": "https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance.html#settings-1", | ||
| "platform": "Terraform", | ||
| "descriptionID": "00335e17", | ||
| "cloudProvider": "gcp", | ||
| "cwe": "778", | ||
| "riskScore": "3.0", | ||
| "experimental": "true" | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
assets/queries/terraform/gcp/sql_db_instance_with_minimum_log_duration/query.rego
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package Cx | ||
|
|
||
| import data.generic.common as common_lib | ||
| import data.generic.terraform as tf_lib | ||
|
|
||
| CxPolicy[result] { | ||
| resource := input.document[i].resource.google_sql_database_instance[name] | ||
|
|
||
| contains(resource.database_version, "POSTGRES") | ||
| results := get_results(resource, name) | ||
|
|
||
| result := { | ||
| "documentId": input.document[i].id, | ||
| "resourceType": "google_sql_database_instance", | ||
| "resourceName": tf_lib.get_resource_name(resource, name), | ||
| "searchKey": results.searchKey, | ||
| "issueType": "IncorrectValue", | ||
| "keyExpectedValue": sprintf("'google_sql_database_instance[%s].settings.database_flags' should set 'log_min_duration_statement' to '-1'", [name]), | ||
| "keyActualValue": sprintf("'google_sql_database_instance[%s].settings.database_flags' sets 'log_min_duration_statement' to '%s'", [name, results.value]), | ||
| "searchLine": results.searchLine | ||
| } | ||
| } | ||
|
|
||
| get_results(resource, name) = results { # array | ||
| resource.settings.database_flags[x].name == "log_min_duration_statement" | ||
| resource.settings.database_flags[x].value != "-1" | ||
|
|
||
| results := { | ||
| "searchKey" : sprintf("google_sql_database_instance[%s].settings.database_flags[%d].name", [name, x]), | ||
| "searchLine" : common_lib.build_search_line(["resource", "google_sql_database_instance", name, "settings", "database_flags", x, "name"], []), | ||
| "value" : resource.settings.database_flags[x].value | ||
| } | ||
| } else = results { # single object | ||
| resource.settings.database_flags.name == "log_min_duration_statement" | ||
| resource.settings.database_flags.value != "-1" | ||
|
|
||
| results := { | ||
| "searchKey" : sprintf("google_sql_database_instance[%s].settings.database_flags.name", [name]), | ||
| "searchLine" : common_lib.build_search_line(["resource", "google_sql_database_instance", name, "settings", "database_flags", "name"], []), | ||
| "value" : resource.settings.database_flags.value | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
assets/queries/terraform/gcp/sql_db_instance_with_minimum_log_duration/test/negative.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| resource "google_sql_database_instance" "negative_1" { | ||
| name = "main-instance" | ||
| database_version = "MYSQL_8_0" # Is not a POSTGRES instance | ||
| region = "us-central1" | ||
|
|
||
| settings { | ||
| database_flags { | ||
| name = "log_min_duration_statement" | ||
| value = "2" | ||
| } # Flag is not set to "-1" | ||
| } | ||
| } | ||
|
|
||
| resource "google_sql_database_instance" "negative_2" { | ||
| name = "mysql-instance-without-flag" | ||
| database_version = "POSTGRES_17" | ||
| region = "us-central1" | ||
|
|
||
| # Default value is -1 | ||
| } | ||
|
|
||
| resource "google_sql_database_instance" "negative_3" { | ||
| name = "postgres-instance-without-flag" | ||
| database_version = "POSTGRES_16" | ||
| region = "us-central1" | ||
|
|
||
| settings {} # Default value is -1 | ||
| } | ||
|
|
||
| resource "google_sql_database_instance" "negative_4" { | ||
| name = "postgres-instance-without-flag" | ||
| database_version = "POSTGRES_15" | ||
| region = "us-central1" | ||
|
|
||
| settings { | ||
| database_flags { | ||
| name = "sample_flag1" | ||
| value = "off" | ||
| } | ||
| # Default value is -1 | ||
| } | ||
| } | ||
|
|
||
| resource "google_sql_database_instance" "negative_5" { | ||
| name = "mysql-instance-with-flag" | ||
| database_version = "POSTGRES_15" | ||
| region = "us-central1" | ||
|
|
||
| settings { | ||
| tier = "db-f1-micro" | ||
|
|
||
| database_flags { | ||
| name = "sample_flag1" | ||
| value = "off" | ||
| } | ||
|
|
||
| database_flags { # Has flag set to "-1" | ||
| name = "log_min_duration_statement" | ||
| value = "-1" | ||
| } | ||
|
|
||
| database_flags { | ||
| name = "sample_flag2" | ||
| value = "off" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "google_sql_database_instance" "negative_6" { # Single object support test | ||
| name = "mysql-instance-with-flag" | ||
| database_version = "POSTGRES_15" | ||
| region = "us-central1" | ||
|
|
||
| settings { | ||
| tier = "db-f1-micro" | ||
|
|
||
| database_flags { | ||
| name = "log_min_duration_statement" | ||
| value = "-1" | ||
| } # Has flag set to "-1" | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
assets/queries/terraform/gcp/sql_db_instance_with_minimum_log_duration/test/positive.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| resource "google_sql_database_instance" "positive_1" { | ||
| name = "postgres-instance-with-flag" | ||
| database_version = "POSTGRES_14" | ||
| region = "us-central1" | ||
|
|
||
| settings { | ||
| database_flags { | ||
| name = "sample_flag1" | ||
| value = "off" | ||
| } | ||
|
|
||
| database_flags { # Flag is not set to "-1" | ||
| name = "log_min_duration_statement" | ||
| value = "2" | ||
| } | ||
|
|
||
| database_flags { | ||
| name = "sample_flag2" | ||
| value = "off" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "google_sql_database_instance" "positive_2" { # Single object support test | ||
| name = "postgres-instance-with-flag" | ||
| database_version = "POSTGRES_14" | ||
| region = "us-central1" | ||
|
|
||
| settings { | ||
| database_flags { | ||
| name = "log_min_duration_statement" | ||
| value = "3" | ||
| } # Flag is not set to "-1" | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
...erraform/gcp/sql_db_instance_with_minimum_log_duration/test/positive_expected_result.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [ | ||
| { | ||
| "queryName": "Beta - SQL DB Instance With Minimum Log Duration", | ||
| "severity": "MEDIUM", | ||
| "line": 13 | ||
| }, | ||
| { | ||
| "queryName": "Beta - SQL DB Instance With Minimum Log Duration", | ||
| "severity": "MEDIUM", | ||
| "line": 31 | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't the cwe https://cwe.mitre.org/data/definitions/532.html be more appropriate here?
Following the description provided "Logging SQL statements may include sensitive information that should not be recorded in logs." I would personally put 532 here.
Let me know what you think.