Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,98 @@ package Cx
import data.generic.common as common_lib
import data.generic.azureresourcemanager as arm_lib

types := ["Microsoft.Sql/servers/databases/auditingSettings", "auditingSettings"]
types := ["Microsoft.Sql/servers/databases/auditingSettings", "auditingSettings", "Microsoft.Sql/servers/auditingSettings"]
dbTypes := ["Microsoft.Sql/servers/databases", "databases", "Microsoft.Sql/servers"]

CxPolicy[result] {
doc := input.document[i]

[path, value] = walk(doc)

value.type == types[_]
properties := value.properties
[val, _] := arm_lib.getDefaultValueFromParametersIfPresent(doc, properties.state)
value.type == dbTypes[_]
childrenArr := get_children(doc, value, path)

child := childrenArr[k].value[_]
child.type == types[_]

child_path := childrenArr[k].path

[val, _] := arm_lib.getDefaultValueFromParametersIfPresent(doc, child.properties.state)
lower(val) == "enabled"
not common_lib.valid_key(properties, "retentionDays")
not common_lib.valid_key(child.properties, "retentionDays")

result := {
"documentId": input.document[i].id,
"resourceType": value.type,
"resourceName": value.name,
"searchKey": sprintf("%s.name=%s.properties", [common_lib.concat_path(path), value.name]),
"resourceType": child.type,
"resourceName": child.name,
"searchKey": sprintf("%s.name={{%s}}.properties", [common_lib.concat_path(child_path), child.name]),
"issueType": "MissingAttribute",
"keyExpectedValue": "'auditingSettings.properties.retentionDays' should be defined and above 90 days",
"keyActualValue": "'auditingSettings.properties.retentionDays' is missing",
"searchLine": common_lib.build_search_line(path, ["properties"]),
"searchLine": common_lib.build_search_line(child_path, ["properties"]),
}
}

CxPolicy[result] {
doc := input.document[i]

[path, value] = walk(doc)

value.type == types[_]
properties := value.properties
[val, _] := arm_lib.getDefaultValueFromParametersIfPresent(doc, properties.state)
value.type == dbTypes[_]
childrenArr := get_children(doc, value, path)

child := childrenArr[k].value[_]
child.type == types[_]

child_path := childrenArr[k].path

[val, _] := arm_lib.getDefaultValueFromParametersIfPresent(doc, child.properties.state)
lower(val) == "enabled"
[val_rd, val_rd_type] := arm_lib.getDefaultValueFromParametersIfPresent(doc, properties.retentionDays)
val_rd < 90
[val_rd, val_rd_type] := arm_lib.getDefaultValueFromParametersIfPresent(doc, child.properties.retentionDays)

val_rd < 90

result := {
"documentId": input.document[i].id,
"resourceType": value.type,
"resourceName": value.name,
"searchKey": sprintf("%s.name={{%s}}.properties.retentionDays", [common_lib.concat_path(path), value.name]),
"resourceType": child.type,
"resourceName": child.name,
"searchKey": sprintf("%s.name={{%s}}.properties.retentionDays", [common_lib.concat_path(child_path), child.name]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("'auditingSettings.properties.retentionDays' %s should be defined and above 90 days", [val_rd_type]),
"keyActualValue": sprintf("'auditingSettings.properties.retentionDays' %s is %d", [val_rd_type, properties.retentionDays]),
"searchLine": common_lib.build_search_line(path, ["properties", "retentionDays"]),
"keyActualValue": sprintf("'auditingSettings.properties.retentionDays' %s is %d", [val_rd_type, child.properties.retentionDays]),
"searchLine": common_lib.build_search_line(child_path, ["properties", "retentionDays"]),
}
}

get_children(doc, parent, path) = childArr {
common_lib.valid_key(parent, "resources")
childArr := [{"value": parent.resources, "path": array.concat(path, ["resources"])}]
} else = childArr {
not common_lib.valid_key(parent, "resources")
not common_lib.valid_key(parent, "dependsOn")
values := [x |
[path_child, value_child] := walk(doc)
value_child.name != parent.name
not common_lib.valid_key(value_child, "dependsOn")
startswith(value_child.name, parent.name)
x := {"value": [value_child], "path": path_child}
]
count(values) > 0
unique := {y | y := values[_]}
childArr := [y | y := unique[_]]
} else = childArr {
not common_lib.valid_key(parent, "resources")
values := [x |
[path_child, value_child] := walk(doc)
value_child.name != parent.name
common_lib.valid_key(value_child, "dependsOn")
d := value_child.dependsOn[_]
search_values := {"type": parent.type, "name": parent.name}
contains(d, search_values.type)
contains(d, search_values.name)
x := {"value": [value_child], "path": path_child}
]
count(values) > 0
unique := {y | y := values[_]}
childArr := [y | y := unique[_]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource sqlServer1 'Microsoft.Sql/servers@2021-02-01-preview' = {
name: 'sqlServer1'
location: resourceGroup().location
tags: {
displayName: 'sqlServer1'
}
properties: {
administratorLogin: 'adminUsername'
administratorLoginPassword: 'adminPassword'
}
}

resource auditingSetting 'Microsoft.Sql/servers/auditingSettings@2022-05-01-preview' = {
name: 'default'
parent: sqlServer1
properties: {
state: 'Enabled'
isAzureMonitorTargetEnabled: true
retentionDays: 91
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.37.4.10188",
"templateHash": "7243181032366168335"
}
},
"resources": [
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2021-02-01-preview",
"name": "sqlServer1",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "sqlServer1"
},
"properties": {
"administratorLogin": "adminUsername",
"administratorLoginPassword": "adminPassword"
}
},
{
"type": "Microsoft.Sql/servers/auditingSettings",
"apiVersion": "2022-05-01-preview",
"name": "sqlServer1/default",
"properties": {
"state": "Enabled",
"isAzureMonitorTargetEnabled": true,
"retentionDays": 91
},
"dependsOn": [
"[resourceId('Microsoft.Sql/servers', 'sqlServer1')]"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource sqlServer1 'Microsoft.Sql/servers@2021-02-01-preview' = {
name: 'sqlServer1'
location: resourceGroup().location
tags: {
displayName: 'sqlServer1'
}
properties: {
administratorLogin: 'adminUsername'
administratorLoginPassword: 'adminPassword'
}
}

resource auditingSetting 'Microsoft.Sql/servers/auditingSettings@2022-05-01-preview' = {
name: 'default'
parent: sqlServer1
properties: {
state: 'Enabled'
isAzureMonitorTargetEnabled: true
retentionDays: 89
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.37.4.10188",
"templateHash": "7243181032366168335"
}
},
"resources": [
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2021-02-01-preview",
"name": "sqlServer1",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "sqlServer1"
},
"properties": {
"administratorLogin": "adminUsername",
"administratorLoginPassword": "adminPassword"
}
},
{
"type": "Microsoft.Sql/servers/auditingSettings",
"apiVersion": "2022-05-01-preview",
"name": "sqlServer1/default",
"properties": {
"state": "Enabled",
"isAzureMonitorTargetEnabled": true,
"retentionDays": 89
},
"dependsOn": [
"[resourceId('Microsoft.Sql/servers', 'sqlServer1')]"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource sqlServer1 'Microsoft.Sql/servers@2021-02-01-preview' = {
name: 'sqlServer1'
location: resourceGroup().location
tags: {
displayName: 'sqlServer1'
}
properties: {
administratorLogin: 'adminUsername'
administratorLoginPassword: 'adminPassword'
}
}

resource auditingSetting 'Microsoft.Sql/servers/auditingSettings@2022-05-01-preview' = {
name: 'default'
parent: sqlServer1
properties: {
state: 'Enabled'
isAzureMonitorTargetEnabled: true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.37.4.10188",
"templateHash": "17022336641796501793"
}
},
"resources": [
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2021-02-01-preview",
"name": "sqlServer1",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "sqlServer1"
},
"properties": {
"administratorLogin": "adminUsername",
"administratorLoginPassword": "adminPassword"
}
},
{
"type": "Microsoft.Sql/servers/auditingSettings",
"apiVersion": "2022-05-01-preview",
"name": "[format('{0}/{1}', 'sqlServer1', 'default')]",
"properties": {
"state": "Enabled",
"isAzureMonitorTargetEnabled": true
},
"dependsOn": [
"[resourceId('Microsoft.Sql/servers', 'sqlServer1')]"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
"line": 45,
"filename": "positive4.json"
},
{
"queryName": "SQL Server Database With Unrecommended Retention Days",
"severity": "LOW",
"line": 32,
"filename": "positive5.json"
},
{
"queryName": "SQL Server Database With Unrecommended Retention Days",
"severity": "LOW",
"line": 29,
"filename": "positive6.json"
},
{
"queryName": "SQL Server Database With Unrecommended Retention Days",
"severity": "LOW",
Expand All @@ -46,5 +58,17 @@
"severity": "LOW",
"line": 31,
"filename": "positive4.bicep"
},
{
"queryName": "SQL Server Database With Unrecommended Retention Days",
"severity": "LOW",
"line": 19,
"filename": "positive5.bicep"
},
{
"queryName": "SQL Server Database With Unrecommended Retention Days",
"severity": "LOW",
"line": 16,
"filename": "positive6.bicep"
}
]
Loading