hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.52k stars 4.6k forks source link

Support for Azure SQL Extended Auditing Policy Action Groups #16745

Open iso514 opened 2 years ago

iso514 commented 2 years ago

Is there an existing issue for this?

Community Note

Description

There is currently no support to configure azure sql database audit-policy action groups in Terraform. By default, only these action groups are enabled :

New or Affected Resource(s)/Data Source(s)

azurerm_mssql_server_extended_auditing_policy

Potential Terraform Configuration

resource "azurerm_mssql_server_extended_auditing_policy" "example" {
  actions                                 = FAILED_DATABASE_AUTHENTICATION_GROUP
  server_id                               = azurerm_mssql_server.example.id
  storage_endpoint                        = azurerm_storage_account.example.primary_blob_endpoint
  storage_account_access_key              = azurerm_storage_account.example.primary_access_key
  storage_account_access_key_is_secondary = false
  retention_in_days                       = 6
}

References

It was part of this initial feature request but has not been implemented : https://github.com/hashicorp/terraform-provider-azurerm/issues/5929 Microsoft documentation reference :

joakimlemb commented 9 months ago

Example workaround with azapi provider for configuring SQL auditing actions/groups and logging to log analytics:

# No native support for SQL audit actions/groups in azurerm_mssql_server_extended_auditing_policy yet: 
# https://learn.microsoft.com/en-us/azure/templates/microsoft.sql/servers/auditingsettings?pivots=deployment-language-terraform
resource "azapi_update_resource" "example_audit_actions" {
  type      = "Microsoft.Sql/servers/auditingSettings@2022-05-01-preview"
  name      = "default"
  parent_id = azurerm_mssql_server.example.id
  body = jsonencode({
    properties = {
      auditActionsAndGroups = [
        "FAILED_DATABASE_AUTHENTICATION_GROUP",
        "SUCCESSFUL_DATABASE_AUTHENTICATION_GROUP"
      ]
      isAzureMonitorTargetEnabled = true
      state                       = "Enabled"
    }
  })

  depends_on = [azurerm_mssql_server.example]
}

# Will most likely fail on first create run as the master database in azurerm_mssql_server is created async and azurerm provider is missing retry/wait logic: 
# https://github.com/hashicorp/terraform-provider-azurerm/issues/22226
resource "azurerm_monitor_diagnostic_setting" "example_audit_log" {
  name                       = "SQLSecurityAuditEvents"
  target_resource_id         = "${azurerm_mssql_server.example.id}/databases/master"
  log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id

  enabled_log { category = "SQLSecurityAuditEvents" }

  lifecycle {
    ignore_changes = [metric]
  }

  depends_on = [azurerm_mssql_server.example]
}
davidkarlsen commented 5 months ago

This is now supported at server level:

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_server_extended_auditing_policy#audit_actions_and_groups

But not at database level:

azurerm_mssql_database_extended_auditing_policy