Skip to content

Commit 1c3915c

Browse files
cx-andre-pereiracx-eduardo-semanascx-artur-ribeiro
authored
fix(query): passwords and secrets improvements to "Avoiding TF resource access" allow rules (#7905)
* initial fix * improved support for index referencing --------- Co-authored-by: Eduardo Semanas <[email protected]> Co-authored-by: Artur Ribeiro <[email protected]>
1 parent 9cd68d9 commit 1c3915c

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

assets/queries/common/passwords_and_secrets/regex_rules.json

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
"allowRules": [
88
{
99
"description": "Avoiding TF resource access",
10-
"regex": "(?i)['\"]?password['\"]?\\s*=\\s*(([a-zA-z_]+(.))?[a-zA-z_]+\\s*(.)\\s*[a-zA-z_]+(.)[a-zA-z_]+)?(\\s*:\\s*null|null)$"
10+
"regex": "(?i)['\"]?password['\"]?\\s*[:=]\\s*(([a-zA-Z_]+(\\[([a-zA-Z_]+\\.[a-zA-Z_]+.*|\\d+)\\])?\\.[a-zA-Z_]+(\\[([a-zA-Z_]+\\.[a-zA-Z_]+.*|\\d+)\\])?)\\s*([\\[\\.\\)\\]\\}\\$]|(:\\s*null))|null)"
1111
},
1212
{
1313
"description": "Avoiding description field",
1414
"regex": "(?i)['\"]?description['\"]?\\s*=\\s*['\"].*['\"]"
1515
},
1616
{
1717
"description": "Avoiding Terraform 'optional' statement",
18-
"regex": "(?i)['\"]?password['\"]?\\s*=\\s*optional\\((string|number|sensitive\\(string\\)|map\\(string\\)|set\\(string\\)|any)\\)$"
18+
"regex": "(?i)['\"]?password['\"]?\\s*=\\s*optional\\((string|number|sensitive\\(string\\)|map\\(string\\)|set\\(string\\)|any)\\)$"
1919
},
2020
{
2121
"description": "Avoiding Terraform 'try' statement",
@@ -29,14 +29,6 @@
2929
"description": "Avoiding Ansible playbook update_password",
3030
"regex": "['\"]?update_password['\"]?\\s*[:=]\\s*['\"]?([A-Za-z0-9/~^_!@&%()=?*+-.]{4,})['\"]?"
3131
},
32-
{
33-
"description": "Allow passwords retrieved from Terraform data sources",
34-
"regex": "(?i)['\"]?password['\"]?\\s*=\\s*data\\.azurerm_key_vault_secret\\.[A-Za-z0-9_]+\\.value"
35-
},
36-
{
37-
"description": "Allow passwords retrieved from AWS KMS Secrets",
38-
"regex": "(?i)['\"]?password['\"]?\\s*=\\s*data\\.aws_kms_secrets\\.[A-Za-z0-9_]+\\.plaintext\\[\"[A-Za-z0-9_]+\"\\]"
39-
},
4032
{
4133
"description": "Allow placeholders",
4234
"regex": "(?i)['\"]?password['\"]?\\s*[:=]\\s*['\"](\\$\\(|\\$?\\{\\{)\\s*\\w+\\s*(\\)|\\}\\})['\"]"
@@ -70,7 +62,7 @@
7062
},
7163
{
7264
"description": "Avoiding TF resource access",
73-
"regex": "(?i)['\"]?secret[_]?(key)?['\"]?\\s*=\\s*([a-zA-z_]+(.))?[a-zA-z_]+(.)[a-zA-z_]+(.)[a-zA-z_]+"
65+
"regex": "(?i)['\"]?secret[_]?(key)?['\"]?\\s*[:=]\\s*(([a-zA-Z_]+(\\[([a-zA-Z_]+\\.[a-zA-Z_]+.*|\\d+)\\])?\\.[a-zA-Z_]+(\\[([a-zA-Z_]+\\.[a-zA-Z_]+.*|\\d+)\\])?)\\s*([\\.\\)\\]\\$]|(:\\s*null))|null)"
7466
},
7567
{
7668
"description": "Avoiding Secrets Manager arn",
@@ -83,11 +75,11 @@
8375
{
8476
"description": "Avoiding Secrets from Azure Key Vault",
8577
"regex": "(?i)['\"]?secret[_]?(key)?['\"]?\\s*(:|=)\\s*['\"]?[${]+[A-Za-z0-9/~^_!@&%()=?*+-]+}?"
86-
},
78+
},
8779
{
8880
"description": "Allow secret retrieved from ARM parameters",
8981
"regex": "(?i)['\"]?secret[_]?(key)?['\"]?\\s*[:=]\\s*['\"]?\\[\\s*parameters\\(['\"][a-zA-Z][a-zA-Z0-9_-]*['\"]\\s*\\)\\s*\\]"
90-
},
82+
},
9183
{
9284
"description": "Allow secrets retrieved from Bicep getSecret built in function",
9385
"regex": "(?i)['\"]?secret[_]?(key|value)?['\"]?\\s*(:|=)\\s*[a-zA-Z]*\\.getSecret\\(\\s*[\"']([A-Za-z0-9/~^_!@#&%(){};=?*+-<>,:;[\\]%$]+)[\"']\\)"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
resource "aws_secretsmanager_secret_version" "secret_version" {
2+
for_each = { for k, v in var.clients.scram : k => v if var.enabled && var.client_sasl_scram_enabled }
3+
4+
secret_id = aws_secretsmanager_secret.client_secret[each.key].id # use of indexes
5+
secret_string = jsonencode({ "username" : join("_", [var.product, each.key, var.environment == "dev" ? var.environment : var.stack]), "password" : random_password.client_password[each.key].result })
6+
}
7+
8+
resource "aws_secretsmanager_secret_version" "secret_version_2" {
9+
for_each = { for k, v in var.clients.scram : k => v if var.enabled && var.client_sasl_scram_enabled }
10+
11+
secret_id = aws_secretsmanager_secret.client_secret[each.key].id # use of indexes
12+
secret_string = jsonencode({ "username" : join("_", [var.product, each.key, var.environment == "dev" ? var.environment : var.stack]), "password" : random_password[each.key].client_password.result })
13+
}
14+
15+
resource "aws_secretsmanager_secret_version" "secret_version_3" {
16+
for_each = { for k, v in var.clients.scram : k => v if var.enabled && var.client_sasl_scram_enabled }
17+
18+
secret_id = aws_secretsmanager_secret.client_secret[each.key].id # use of indexes
19+
secret_string = jsonencode({ "username" : join("_", [var.product, each.key, var.environment == "dev" ? var.environment : var.stack]), "password" : random_password["index"].client_password.result })
20+
}
21+
22+
resource "aws_msk_scram_secret_association" "msk_secret_association" {
23+
count = var.enabled && var.client_sasl_scram_enabled ? 1 : 0
24+
cluster_arn = aws_msk_cluster.kafka[0].arn
25+
secret_arn_list = [for secret in aws_secretsmanager_secret.client_secret : secret.arn] # short reference
26+
}
27+
28+
resource "aws_msk_scram_secret_association" "msk_secret_association_2" {
29+
count = var.enabled && var.client_sasl_scram_enabled ? 1 : 0
30+
cluster_arn = aws_msk_cluster.kafka[0].arn
31+
secret_arn_list = [for secret in aws_secretsmanager_secret.client_secret : null] # short reference
32+
}

0 commit comments

Comments
 (0)