2
2
resource_group_name = element (coalescelist (data. azurerm_resource_group . rgrp . * . name , azurerm_resource_group. rg . * . name , [" " ]), 0 )
3
3
location = element (coalescelist (data. azurerm_resource_group . rgrp . * . location , azurerm_resource_group. rg . * . location , [" " ]), 0 )
4
4
if_threat_detection_policy_enabled = var. enable_threat_detection_policy ? [{}] : []
5
- if_extended_auditing_policy_enabled = var. enable_auditing_policy ? [{}] : []
5
+ if_extended_auditing_policy_enabled = var. enable_extended_auditing_policy ? [{}] : []
6
6
}
7
7
8
8
# ---------------------------------------------------------
@@ -21,13 +21,15 @@ resource "azurerm_resource_group" "rg" {
21
21
tags = merge ({ " Name" = format (" %s" , var. resource_group_name ) }, var. tags , )
22
22
}
23
23
24
+ data "azurerm_client_config" "current" {}
25
+
24
26
# ---------------------------------------------------------
25
27
# Storage Account to keep Audit logs - Default is "false"
26
28
# ----------------------------------------------------------
27
29
28
30
resource "azurerm_storage_account" "storeacc" {
29
- count = var. enable_threat_detection_policy || var. enable_auditing_policy ? 1 : 0
30
- name = " stsqlauditlogs"
31
+ count = var. enable_threat_detection_policy || var. enable_extended_auditing_policy ? 1 : 0
32
+ name = var . storage_account_name == null ? " stsqlauditlogs" : var . storage_account_name
31
33
resource_group_name = local. resource_group_name
32
34
location = local. location
33
35
account_kind = " StorageV2"
@@ -38,6 +40,12 @@ resource "azurerm_storage_account" "storeacc" {
38
40
tags = merge ({ " Name" = format (" %s" , " stsqlauditlogs" ) }, var. tags , )
39
41
}
40
42
43
+ resource "azurerm_storage_container" "storcont" {
44
+ name = " vulnerability-assessment"
45
+ storage_account_name = azurerm_storage_account. storeacc . 0 . name
46
+ container_access_type = " private"
47
+ }
48
+
41
49
# -------------------------------------------------------------
42
50
# SQL servers - Secondary server is depends_on Failover Group
43
51
# -------------------------------------------------------------
@@ -63,26 +71,23 @@ resource "azurerm_sql_server" "primary" {
63
71
administrator_login_password = random_password. main . result
64
72
tags = merge ({ " Name" = format (" %s-primary" , var. sqlserver_name ) }, var. tags , )
65
73
66
- /* dynamic "extended_auditing_policy " {
67
- for_each = local.if_extended_auditing_policy_enabled
74
+ dynamic "identity " {
75
+ for_each = var . identity == true ? [ 1 ] : [ 0 ]
68
76
content {
69
- storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key
70
- storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint
71
- retention_in_days = var.log_retention_days
77
+ type = " SystemAssigned"
72
78
}
73
- } */
79
+ }
74
80
}
75
81
76
- resource "azurerm_mssql_server_extended_auditing_policy" "main " {
77
- count = var. enable_auditing_policy ? 1 : 0
82
+ resource "azurerm_mssql_server_extended_auditing_policy" "primary " {
83
+ count = var. enable_extended_auditing_policy ? 1 : 0
78
84
server_id = azurerm_sql_server. primary . id
79
85
storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
80
86
storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
81
87
storage_account_access_key_is_secondary = false
82
88
retention_in_days = var. log_retention_days
83
89
}
84
90
85
-
86
91
resource "azurerm_sql_server" "secondary" {
87
92
count = var. enable_failover_group ? 1 : 0
88
93
name = format (" %s-secondary" , var. sqlserver_name )
@@ -93,16 +98,24 @@ resource "azurerm_sql_server" "secondary" {
93
98
administrator_login_password = random_password. main . result
94
99
tags = merge ({ " Name" = format (" %s-secondary" , var. sqlserver_name ) }, var. tags , )
95
100
96
- dynamic "extended_auditing_policy " {
97
- for_each = local . if_extended_auditing_policy_enabled
101
+ dynamic "identity " {
102
+ for_each = var . identity == true ? [ 1 ] : [ 0 ]
98
103
content {
99
- storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
100
- storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
101
- retention_in_days = var. log_retention_days
104
+ type = " SystemAssigned"
102
105
}
103
106
}
104
107
}
105
108
109
+ resource "azurerm_mssql_server_extended_auditing_policy" "secondary" {
110
+ count = var. enable_failover_group && var. enable_extended_auditing_policy ? 1 : 0
111
+ server_id = azurerm_sql_server. secondary . 0 . id
112
+ storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
113
+ storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
114
+ storage_account_access_key_is_secondary = false
115
+ retention_in_days = var. log_retention_days
116
+ }
117
+
118
+
106
119
# --------------------------------------------------------------------
107
120
# SQL Database creation - Default edition:"Standard" and objective:"S1"
108
121
# --------------------------------------------------------------------
@@ -123,17 +136,73 @@ resource "azurerm_sql_database" "db" {
123
136
storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
124
137
storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
125
138
retention_days = var. log_retention_days
126
- email_addresses = var. email_addresses_for_alerts
139
+ email_addresses = var. sql_admin_email_addresses
127
140
}
128
141
}
142
+ }
129
143
130
- dynamic "extended_auditing_policy" {
131
- for_each = local. if_extended_auditing_policy_enabled
132
- content {
133
- storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
134
- storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
135
- retention_in_days = var. log_retention_days
136
- }
144
+ resource "azurerm_mssql_database_extended_auditing_policy" "primary" {
145
+ count = var. enable_extended_auditing_policy ? 1 : 0
146
+ database_id = azurerm_sql_database. db . id
147
+ storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
148
+ storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
149
+ storage_account_access_key_is_secondary = false
150
+ retention_in_days = var. log_retention_days
151
+ }
152
+
153
+ # -----------------------------------------------------------------------------------------------
154
+ # SQL ServerVulnerability assessment and alert to admin team - Default is "false"
155
+ # -----------------------------------------------------------------------------------------------
156
+
157
+ resource "azurerm_mssql_server_security_alert_policy" "sap_primary" {
158
+ count = var. enable_vulnerability_assessment ? 1 : 0
159
+ resource_group_name = local. resource_group_name
160
+ server_name = azurerm_sql_server. primary . name
161
+ state = " Enabled"
162
+ email_account_admins = true
163
+ email_addresses = var. sql_admin_email_addresses
164
+ retention_days = var. threat_detection_audit_logs_retention_days
165
+ disabled_alerts = var. disabled_alerts
166
+ storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
167
+ storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
168
+ }
169
+
170
+ resource "azurerm_mssql_server_security_alert_policy" "sap_secondary" {
171
+ count = var. enable_vulnerability_assessment && var. enable_failover_group ? 1 : 0
172
+ resource_group_name = local. resource_group_name
173
+ server_name = azurerm_sql_server. secondary . 0 . name
174
+ state = " Enabled"
175
+ email_account_admins = true
176
+ email_addresses = var. sql_admin_email_addresses
177
+ retention_days = var. threat_detection_audit_logs_retention_days
178
+ disabled_alerts = var. disabled_alerts
179
+ storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
180
+ storage_endpoint = azurerm_storage_account. storeacc . 0 . primary_blob_endpoint
181
+ }
182
+
183
+ resource "azurerm_mssql_server_vulnerability_assessment" "va_primary" {
184
+ count = var. enable_vulnerability_assessment ? 1 : 0
185
+ server_security_alert_policy_id = azurerm_mssql_server_security_alert_policy. sap_primary . 0 . id
186
+ storage_container_path = " ${ azurerm_storage_account . storeacc . 0 . primary_blob_endpoint } ${ azurerm_storage_container . storcont . name } /"
187
+ storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
188
+
189
+ recurring_scans {
190
+ enabled = true
191
+ email_subscription_admins = true
192
+ emails = var. sql_admin_email_addresses
193
+ }
194
+ }
195
+
196
+ resource "azurerm_mssql_server_vulnerability_assessment" "va_secondary" {
197
+ count = var. enable_vulnerability_assessment && var. enable_failover_group == true ? 1 : 0
198
+ server_security_alert_policy_id = azurerm_mssql_server_security_alert_policy. sap_secondary . 0 . id
199
+ storage_container_path = " ${ azurerm_storage_account . storeacc . 0 . primary_blob_endpoint } ${ azurerm_storage_container . storcont . name } /"
200
+ storage_account_access_key = azurerm_storage_account. storeacc . 0 . primary_access_key
201
+
202
+ recurring_scans {
203
+ enabled = true
204
+ email_subscription_admins = true
205
+ emails = var. sql_admin_email_addresses
137
206
}
138
207
}
139
208
@@ -152,8 +221,6 @@ resource "null_resource" "create_sql" {
152
221
# Adding AD Admin to SQL Server - Secondary server depend on Failover Group - Default is "false"
153
222
# -----------------------------------------------------------------------------------------------
154
223
155
- data "azurerm_client_config" "current" {}
156
-
157
224
resource "azurerm_sql_active_directory_administrator" "aduser1" {
158
225
count = var. enable_sql_ad_admin ? 1 : 0
159
226
server_name = azurerm_sql_server. primary . name
0 commit comments