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

feat(kics_ar): add remediation for terraform alicloud security queries #5600

Merged
merged 29 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
47d30ab
(kics auto remediation): first approach
rafaela-soares Jul 5, 2022
773d0b5
adding tests
rafaela-soares Jul 6, 2022
b3aa98b
replacement approach change
rafaela-soares Jul 7, 2022
610dd6d
alicloud
cxMiguelSilva Jul 7, 2022
31b0342
Merge branch 'feat/kics_auto_remediation' into kics_auto_remediation/…
cxMiguelSilva Jul 7, 2022
dd76d59
QUERIES THAT VERIFY A FIELD SET TO FALSE
cxMiguelSilva Jul 7, 2022
54799d5
UNRECOMMENDED VALUE
cxMiguelSilva Jul 7, 2022
6f6c27e
added E2E tests
rafaela-soares Jul 8, 2022
7c486aa
fixing unit test + improving
rafaela-soares Jul 8, 2022
181f989
fix errors
rafaela-soares Jul 8, 2022
af7b10e
fix
rafaela-soares Jul 8, 2022
48a94c4
correcting f.Close
rafaela-soares Jul 8, 2022
52dd94f
improving
rafaela-soares Jul 8, 2022
17143e7
Merge branch 'feat/kics_auto_remediation' into kics_auto_remediation/…
cxMiguelSilva Jul 8, 2022
65ce776
improving
rafaela-soares Jul 8, 2022
0c09dd8
fixing E2E
rafaela-soares Jul 11, 2022
970c2ad
test
rafaela-soares Jul 11, 2022
b1de60f
Merge branch 'feat/kics_auto_remediation' into kics_auto_remediation/…
cxMiguelSilva Jul 11, 2022
ae57ab8
adding more tests
rafaela-soares Jul 11, 2022
2a2c8d9
Merge branch 'feat/kics_auto_remediation' into kics_auto_remediation/…
cxMiguelSilva Jul 12, 2022
4e69405
fixing codacy issue
rafaela-soares Jul 12, 2022
f672e20
improving tests
rafaela-soares Jul 12, 2022
94aaaba
testing permissions on Dockerfile.ubi8
rafaela-soares Jul 12, 2022
cd8c793
Merge branch 'feat/kics_auto_remediation' into kics_auto_remediation/…
cxMiguelSilva Jul 12, 2022
1a7dde2
merge
cxMiguelSilva Jul 14, 2022
adc3ba8
Merge branch 'kics_auto_remediation/terraform_alic
cxMiguelSilva Jul 14, 2022
fa3cfd9
Merge branch 'master' into kics_auto_remediation/terraform_alicloud
cxMiguelSilva Jul 14, 2022
e94041f
remove changes
cxMiguelSilva Jul 14, 2022
682fe99
delete newline at file end
cxMiguelSilva Jul 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,26 @@ import data.generic.terraform as tf_lib
CxPolicy[result] {
some i
resource := input.document[i].resource.alicloud_actiontrail_trail[name]
not common_lib.valid_key(resource, "oss_bucket_name")

possibilities := {"event_rw", "oss_bucket_name", "trail_region"}

result := {
"documentId": input.document[i].id,
"resourceType": "alicloud_actiontrail_trail",
"resourceName": tf_lib.get_specific_resource_name(resource, "alicloud_actiontrail_trail", name),
"searchKey": sprintf("alicloud_actiontrail_trail[%s]", [name]),
"issueType": "MissingAttribute",
"keyExpectedValue": "oss_bucket_name is set.",
"keyActualValue": "oss_bucket_name is not set.",
"searchLine": common_lib.build_search_line(["resource", "alicloud_actiontrail_trail", name], []),
}
}

CxPolicy[result] {
some i
resource := input.document[i].resource.alicloud_actiontrail_trail[name]

possibilities := {"event_rw", "trail_region"}
not common_lib.valid_key(resource, possibilities[p])


Expand All @@ -20,6 +38,8 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("'%s' is set.",[possibilities[p]]),
"keyActualValue": sprintf("'%s' is not set.",[possibilities[p]]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_actiontrail_trail", name], []),
"remediation": sprintf("%s= \"ALL\"", [p]),
"remediationType": "addition",
}
}

Expand All @@ -30,7 +50,7 @@ CxPolicy[result] {
p := {"event_rw", "trail_region"}
resource[p[f]] != "All"


remediation := {"before":resource[p[f]] , "after": "All" }
result := {
"documentId": input.document[i].id,
"resourceType": "alicloud_actiontrail_trail",
Expand All @@ -40,5 +60,7 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("'%s' is set to All", [p[f]]),
"keyActualValue": sprintf("'%s' is not set to All", [p[f]]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_actiontrail_trail", name, p[f]], []),
"remediation": json.marshal(remediation),
"remediationType": "replacement",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ CxPolicy[result] {

result := {
"documentId": input.document[i].id,
"resourceType": "alicloud_actiontrail_trail",
"resourceName": tf_lib.get_specific_resource_name(actiontrail, "alicloud_actiontrail_trail", name),
"searchKey": sprintf("alicloud_actiontrail_trail[%s].oss_bucket_name", [name]),
"resourceType": "alicloud_oss_bucket",
"resourceName": tf_lib.get_specific_resource_name(actiontrail, "alicloud_oss_bucket", name),
"searchKey": sprintf("alicloud_oss_bucket[%s].acl", [name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("'alicloud_actiontrail_trail[%s].oss_bucket_name' is private", [name]),
"keyActualValue": sprintf("'alicloud_actiontrail_trail[%s].oss_bucket_name' is %s", [name, possibilities[p]]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_actiontrail_trail", name, "oss_bucket_name"], []),
"keyExpectedValue": sprintf("'alicloud_oss_bucket[%s].oss_bucket_name' is private", [name]),
"keyActualValue": sprintf("'alicloud_oss_bucket[%s].oss_bucket_name' is %s", [name, possibilities[p]]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_oss_bucket", name, "acl"], []),
"remediation": json.marshal({
"before": p,
"after": "private"
}),
"remediationType": "replacement",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
"queryName": "ActionTrail Trail OSS Bucket is Publicly Accessible",
"severity": "HIGH",
"line": 9,
"line": 3,
"fileName": "positive2.tf"
},
{
"queryName": "ActionTrail Trail OSS Bucket is Publicly Accessible",
"severity": "HIGH",
"line": 9,
"line": 3,
"fileName": "positive1.tf"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ CxPolicy[result] {
"keyExpectedValue": "'alicloud_alb_listener[%s].listener_protocol' should not be 'HTTP'",
"keyActualValue": "'alicloud_alb_listener[%s].listener_protocol' is 'HTTP'",
"searchLine": common_lib.build_search_line(["resource", "alicloud_alb_listener", name, "listener_protocol"], []),
"remediation": json.marshal({
"before": "HTTP",
"after": "HTTPS"
}),
"remediationType": "replacement"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CxPolicy[result] {

auto_repair := resource.management.auto_repair
auto_repair == false

result := {
"documentId": input.document[i].id,
"resourceType": "alicloud_cs_kubernetes_node_pool",
Expand All @@ -19,6 +19,11 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("For the resource alicloud_cs_kubernetes_node_pool[%s] to have 'auto_repair' set to true.", [name]),
"keyActualValue": sprintf("The resource alicloud_cs_kubernetes_node_pool[%s] has 'auto_repair' set to false.", [name]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_cs_kubernetes_node_pool", name, "management", "auto_repair"], []),
"remediation": json.marshal({
"before": "false",
"after": "true"
}),
"remediationType": "replacement",
}
}

Expand Down Expand Up @@ -53,5 +58,7 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("For the resource alicloud_cs_kubernetes_node_pool[%s] to have a 'management' block containing 'auto_repair' set to true.", [name]),
"keyActualValue": sprintf("The resource alicloud_cs_kubernetes_node_pool[%s] has a 'management' block but it doesn't contain 'auto_repair' ", [name]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_cs_kubernetes_node_pool", name, "management"], []),
"remediation": "auto_repair = true",
"remediationType": "addition",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import data.generic.terraform as tf_lib
CxPolicy[result] {

resource := input.document[i].resource.alicloud_disk[name]
resource.encrypted == false

resource.encrypted == false

result := {
"documentId": input.document[i].id,
Expand All @@ -18,6 +17,11 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("[%s] has encryption set to true", [name]),
"keyActualValue": sprintf("[%s] has encryption set to false", [name]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_disk", name, "encrypted"], []),
"remediation": json.marshal({
"before": "false",
"after": "true"
}),
"remediationType": "replacement",
}
}

Expand All @@ -26,7 +30,6 @@ CxPolicy[result] {
resource := input.document[i].resource.alicloud_disk[name]
not common_lib.valid_key(resource, "encrypted")
not common_lib.valid_key(resource, "snapshot_id")


result := {
"documentId": input.document[i].id,
Expand All @@ -37,6 +40,8 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("[%s] has encryption enabled",[name]),
"keyActualValue": sprintf("[%s] does not have encryption enabled",[name]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_disk", name], []),
"remediation": "encrypted = true",
"remediationType": "addition",
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ CxPolicy[result] {
"keyExpectedValue": "'rotation_interval' value should not be higher than a year",
"keyActualValue": "'rotation_interval' value is higher than a year",
"searchLine": common_lib.build_search_line(["resource", "alicloud_kms_key", name, "rotation_interval"], []),
"remediation": json.marshal({
"before": resource.rotation_interval,
"after": "365d"
}),
"remediationType": "replacement",
}
}

Expand All @@ -35,6 +40,8 @@ CxPolicy[result] {
"keyExpectedValue": "'automatic_rotation' should be defined and set to Enabled",
"keyActualValue": "'automatic_rotation' is not defined",
"searchLine": common_lib.build_search_line(["resource", "alicloud_kms_key", name], []),
"remediation": "automatic_rotation = \"Enabled\"",
"remediationType": "addition",
}
}

Expand All @@ -52,6 +59,11 @@ CxPolicy[result] {
"keyExpectedValue": "'automatic_rotation' should be set to Enabled",
"keyActualValue": "'automatic_rotation' is set to Disabled",
"searchLine": common_lib.build_search_line(["resource", "alicloud_kms_key", name, "automatic_rotation"], []),
"remediation": json.marshal({
"before": "Disabled",
"after": "Enabled"
}),
"remediationType": "replacement",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("alicloud_launch_template[%s].encrypted to be true", [name]),
"keyActualValue": sprintf("alicloud_launch_template[%s].encrypted is false", [name]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_launch_template", name, "encrypted"], []),
"remediation": json.marshal({
"before": "false",
"after": "true"
}),
"remediationType": "replacement",
}
}

Expand All @@ -32,5 +37,7 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("alicloud_launch_template[%s] 'encrypted' should be defined and set to true", [name]),
"keyActualValue": sprintf("alicloud_launch_template[%s] 'encrypted' argument is not defined", [name]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_launch_template", name], []),
"remediation": "encrypted = true",
"remediationType": "addition",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ CxPolicy[result] {
"keyExpectedValue": "For attribute 'retention_period' to be set and over 90 days.",
"keyActualValue": "The attribute 'retention_period' is undefined. The default duration when undefined is 30 days, which is too short.",
"searchLine": common_lib.build_search_line(["resource", "alicloud_log_store", name], []),
"remediation": "retention_period = 100",
"remediationType": "addition",
}
}

Expand All @@ -35,5 +37,10 @@ CxPolicy[result] {
"keyExpectedValue": "For the attribite 'retention_period' to be set to 90+ days",
"keyActualValue": "The attribute 'retention_period' is not set to 90+ days",
"searchLine": common_lib.build_search_line(["resource", "alicloud_log_store", name, "retention_period"], []),
"remediation": json.marshal({
"before": sprintf("%d", [rperiod]),
"after": "100"
}),
"remediationType": "replacement",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' should not be 0", [name]),
"keyActualValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' is 0", [name]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_nas_file_system", name, "encrypt_type"], []),
"remediation": json.marshal({
"before": "0",
"after": "2"
}),
"remediationType": "replacement",
}
}

Expand All @@ -32,5 +37,7 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' should be defined and the value different from 0 ", [name]),
"keyActualValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' is undefined", [name]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_nas_file_system", name], []),
"remediation": "encrypt_type = \"2\"",
"remediationType": "addition",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' should be defined and set to 2'", [name]),
"keyActualValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' is not defined", [name]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_nas_file_system", name], []),
"remediation": "encrypt_type = \"2\"",
"remediationType": "addition",
}
}

Expand All @@ -32,5 +34,10 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' should be set to 2'", [name]),
"keyActualValue": sprintf("alicloud_nas_file_system[%s].encrypt_type' is not set to 2 ", [name]),
"searchLine": common_lib.build_search_line(["resource", "alicloud_nas_file_system", name, "encrypt_type"], []),
"remediation": json.marshal({
"before": resource.encrypt_type,
"after": "2"
}),
"remediationType": "replacement",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ CxPolicy[result] {
"keyExpectedValue": "'lifecycle_rule' is set and enabled",
"keyActualValue": "'lifecycle_rule' is set but disabled",
"searchLine":common_lib.build_search_line(["resource", "alicloud_oss_bucket", name, "lifecycle_rule", "enabled"], []),
"remediation": json.marshal({
"before": "false",
"after": "true"
}),
"remediationType": "replacement",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ CxPolicy[result] {
"keyExpectedValue": sprintf("%s 'logging_isenable' argument should be set to true",[name]),
"keyActualValue": sprintf("%s 'logging_isenable' argument is set to false",[name]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_oss_bucket", name, "logging_isenable"], []),
"remediation": json.marshal({
"before": "false",
"after": "true"
}),
"remediationType": "replacement",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ CxPolicy[result] {
"keyExpectedValue": "'acl' is set to private or not set",
"keyActualValue": sprintf("'acl' is %s", [possibilities[p]]),
"searchLine":common_lib.build_search_line(["resource", "alicloud_oss_bucket", name, "acl"], []),
"remediation": json.marshal({
"before": p,
"after": "private"
}),
"remediationType": "replacement",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ CxPolicy[result] {
"keyExpectedValue": "'transfer_acceleration.enabled' is defined and set to true",
"keyActualValue": "'transfer_acceleration.enabled' is false",
"searchLine": common_lib.build_search_line(["resource", "alicloud_oss_bucket", name, "transfer_acceleration", "enabled"], []),
"remediation": json.marshal({
"before": "false",
"after": "true"
}),
"remediationType": "replacement",
}
}

Expand All @@ -36,5 +41,7 @@ CxPolicy[result] {
"keyExpectedValue": "'transfer_acceleration.enabled' is defined and set to true",
"keyActualValue": "'transfer_acceleration' is missing",
"searchLine": common_lib.build_search_line(["resource", "alicloud_oss_bucket", name], []),
"remediation": "transfer_acceleration{\n\t\tenabled = true\n\t}",
"remediationType": "addition",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ CxPolicy[result] {
"keyExpectedValue": "'versioning.status' is enabled",
"keyActualValue": "'versioning.status' is suspended",
"searchLine": common_lib.build_search_line(["resource", "alicloud_oss_bucket", name, "versioning", "status"], []),
"remediation": json.marshal({
"before": "Suspended",
"after": "Enabled"
}),
"remediationType": "replacement",
}
}

Expand All @@ -36,5 +41,7 @@ CxPolicy[result] {
"keyExpectedValue": "'versioning.status' is defined and set to enabled",
"keyActualValue": "'versioning' is missing",
"searchLine": common_lib.build_search_line(["resource", "alicloud_oss_bucket", name], []),
"remediation": "versioning {\n\t\tstatus = \"Enabled\"\n\t}",
"remediationType": "addition",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ CxPolicy[result] {
"keyExpectedValue": "'max_login_attempts' is set to 5 or less",
"keyActualValue": "'max_login_attempts' is above than 5",
"searchLine": common_lib.build_search_line(["resource", "alicloud_ram_account_password_policy", name, "max_login_attempts"], []),

"remediation": json.marshal({
"before": sprintf("%d", [resource.max_login_attempts]),
"after": "5"
}),
"remediationType": "replacement",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ CxPolicy[result] {
"keyExpectedValue": "'max_password_age' should be higher than 0 and lower than 91",
"keyActualValue": "'max_password_age' is higher than 90",
"searchLine": common_lib.build_search_line(["resource", "alicloud_ram_account_password_policy", name, "max_password_age"], []),
"remediation": json.marshal({
"before": sprintf("%d", [resource.max_password_age]),
"after": "12"
}),
"remediationType": "replacement",

}
}
Expand All @@ -54,6 +59,10 @@ CxPolicy[result] {
"keyExpectedValue": "'max_password_age' should be higher than 0 and lower than 91",
"keyActualValue": "'max_password_age' is equal to 0",
"searchLine": common_lib.build_search_line(["resource", "alicloud_ram_account_password_policy", name, "max_password_age"], []),

"remediation": json.marshal({
"before": sprintf("%d", [resource.max_password_age]),
"after": "12"
}),
"remediationType": "replacement",
}
}
Loading