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

azurerm_nginx_deployment - support NGINX App Protect WAF #27454

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
149 changes: 131 additions & 18 deletions internal/services/nginx/nginx_deployment_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ import (
)

type DeploymentDataSourceModel struct {
ResourceGroupName string `tfschema:"resource_group_name"`
Name string `tfschema:"name"`
NginxVersion string `tfschema:"nginx_version"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
Sku string `tfschema:"sku"`
ManagedResourceGroup string `tfschema:"managed_resource_group"`
Location string `tfschema:"location"`
Capacity int64 `tfschema:"capacity"`
AutoScaleProfile []AutoScaleProfile `tfschema:"auto_scale_profile"`
DiagnoseSupportEnabled bool `tfschema:"diagnose_support_enabled"`
Email string `tfschema:"email"`
IpAddress string `tfschema:"ip_address"`
LoggingStorageAccount []LoggingStorageAccount `tfschema:"logging_storage_account"`
FrontendPublic []FrontendPublic `tfschema:"frontend_public"`
FrontendPrivate []FrontendPrivate `tfschema:"frontend_private"`
NetworkInterface []NetworkInterface `tfschema:"network_interface"`
UpgradeChannel string `tfschema:"automatic_upgrade_channel"`
Tags map[string]string `tfschema:"tags"`
ResourceGroupName string `tfschema:"resource_group_name"`
Name string `tfschema:"name"`
NginxVersion string `tfschema:"nginx_version"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
Sku string `tfschema:"sku"`
ManagedResourceGroup string `tfschema:"managed_resource_group"`
Location string `tfschema:"location"`
Capacity int64 `tfschema:"capacity"`
AutoScaleProfile []AutoScaleProfile `tfschema:"auto_scale_profile"`
DiagnoseSupportEnabled bool `tfschema:"diagnose_support_enabled"`
Email string `tfschema:"email"`
IpAddress string `tfschema:"ip_address"`
LoggingStorageAccount []LoggingStorageAccount `tfschema:"logging_storage_account"`
FrontendPublic []FrontendPublic `tfschema:"frontend_public"`
FrontendPrivate []FrontendPrivate `tfschema:"frontend_private"`
NetworkInterface []NetworkInterface `tfschema:"network_interface"`
UpgradeChannel string `tfschema:"automatic_upgrade_channel"`
WebApplicationFirewallSettings []WebApplicationFirewallSettings `tfschema:"web_application_firewall_settings"`
WebApplicationFirewallStatus []WebApplicationFirewallStatus `tfschema:"web_application_firewall_status"`
Tags map[string]string `tfschema:"tags"`
}

type DeploymentDataSource struct{}
Expand Down Expand Up @@ -194,10 +196,74 @@ func (m DeploymentDataSource) Attributes() map[string]*pluginsdk.Schema {
Computed: true,
},

"web_application_firewall_settings": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"activation_state": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"web_application_firewall_status": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"attack_signatures_package": webApplicationFirewallPackageComputed(),
"bot_signatures_package": webApplicationFirewallPackageComputed(),
"threat_campaigns_package": webApplicationFirewallPackageComputed(),
"component_versions": webApplicationFirewallComponentVersionsComputed(),
},
},
},

"tags": commonschema.TagsDataSource(),
}
}

func webApplicationFirewallPackageComputed() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"revision_datetime": {
Type: pluginsdk.TypeString,
Computed: true,
},
"version": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
}
}

func webApplicationFirewallComponentVersionsComputed() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"waf_engine_version": {
Type: pluginsdk.TypeString,
Computed: true,
},
"waf_nginx_version": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
}
}

func (m DeploymentDataSource) ModelObject() interface{} {
return &DeploymentDataSourceModel{}
}
Expand Down Expand Up @@ -313,6 +379,53 @@ func (m DeploymentDataSource) Read() sdk.ResourceFunc {
if props.AutoUpgradeProfile != nil {
output.UpgradeChannel = props.AutoUpgradeProfile.UpgradeChannel
}

if props.NginxAppProtect != nil {
if props.NginxAppProtect.WebApplicationFirewallSettings.ActivationState != nil {
output.WebApplicationFirewallSettings = []WebApplicationFirewallSettings{
{
string(*props.NginxAppProtect.WebApplicationFirewallSettings.ActivationState),
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus != nil {
wafStatus := WebApplicationFirewallStatus{}
if props.NginxAppProtect.WebApplicationFirewallStatus.AttackSignaturesPackage != nil {
wafStatus.AttackSignaturesPackage = []WebApplicationFirewallPackage{
{
RevisionDatetime: props.NginxAppProtect.WebApplicationFirewallStatus.AttackSignaturesPackage.RevisionDatetime,
Version: props.NginxAppProtect.WebApplicationFirewallStatus.AttackSignaturesPackage.Version,
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus.BotSignaturesPackage != nil {
wafStatus.BotSignaturesPackage = []WebApplicationFirewallPackage{
{
RevisionDatetime: props.NginxAppProtect.WebApplicationFirewallStatus.BotSignaturesPackage.RevisionDatetime,
Version: props.NginxAppProtect.WebApplicationFirewallStatus.BotSignaturesPackage.Version,
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus.ThreatCampaignsPackage != nil {
wafStatus.ThreatCampaignsPackage = []WebApplicationFirewallPackage{
{
RevisionDatetime: props.NginxAppProtect.WebApplicationFirewallStatus.ThreatCampaignsPackage.RevisionDatetime,
Version: props.NginxAppProtect.WebApplicationFirewallStatus.ThreatCampaignsPackage.Version,
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus.ComponentVersions != nil {
wafStatus.ComponentVersions = []WebApplicationFirewallComponentVersions{
{
WafEngineVersion: props.NginxAppProtect.WebApplicationFirewallStatus.ComponentVersions.WafEngineVersion,
WafNginxVersion: props.NginxAppProtect.WebApplicationFirewallStatus.ComponentVersions.WafNginxVersion,
},
}
}
output.WebApplicationFirewallStatus = []WebApplicationFirewallStatus{wafStatus}
}
}

}
}

Expand Down
24 changes: 24 additions & 0 deletions internal/services/nginx/nginx_deployment_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,27 @@ func TestAccNginxDeploymentDataSource_autoscaling(t *testing.T) {
},
})
}

func (d NginxDeploymentDataSource) basicNginxAppProtect(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_nginx_deployment" "test" {
name = azurerm_nginx_deployment.test.name
resource_group_name = azurerm_nginx_deployment.test.resource_group_name
}
`, DeploymentResource{}.basicNginxAppProtect(data))
}

func TestAccNginxDeploymentDataSource_nginxappprotect(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_nginx_deployment", "test")
r := NginxDeploymentDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basicNginxAppProtect(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("web_application_firewall_settings.0.activation_state").HasValue("Enabled"),
),
},
})
}
152 changes: 135 additions & 17 deletions internal/services/nginx/nginx_deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,47 @@ type AutoScaleProfile struct {
Max int64 `tfschema:"max_capacity"`
}

type WebApplicationFirewallSettings struct {
ActivationState string `tfschema:"activation_state"`
}

type WebApplicationFirewallPackage struct {
RevisionDatetime string `tfschema:"revision_datetime"`
Version string `tfschema:"version"`
}

type WebApplicationFirewallComponentVersions struct {
WafEngineVersion string `tfschema:"waf_engine_version"`
WafNginxVersion string `tfschema:"waf_nginx_version"`
}

type WebApplicationFirewallStatus struct {
AttackSignaturesPackage []WebApplicationFirewallPackage `tfschema:"attack_signatures_package"`
BotSignaturesPackage []WebApplicationFirewallPackage `tfschema:"bot_signatures_package"`
ComponentVersions []WebApplicationFirewallComponentVersions `tfschema:"component_versions"`
ThreatCampaignsPackage []WebApplicationFirewallPackage `tfschema:"threat_campaigns_package"`
}

type DeploymentModel struct {
ResourceGroupName string `tfschema:"resource_group_name"`
Name string `tfschema:"name"`
NginxVersion string `tfschema:"nginx_version"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
Sku string `tfschema:"sku"`
ManagedResourceGroup string `tfschema:"managed_resource_group"`
Location string `tfschema:"location"`
Capacity int64 `tfschema:"capacity"`
AutoScaleProfile []AutoScaleProfile `tfschema:"auto_scale_profile"`
DiagnoseSupportEnabled bool `tfschema:"diagnose_support_enabled"`
Email string `tfschema:"email"`
IpAddress string `tfschema:"ip_address"`
LoggingStorageAccount []LoggingStorageAccount `tfschema:"logging_storage_account"`
FrontendPublic []FrontendPublic `tfschema:"frontend_public"`
FrontendPrivate []FrontendPrivate `tfschema:"frontend_private"`
NetworkInterface []NetworkInterface `tfschema:"network_interface"`
UpgradeChannel string `tfschema:"automatic_upgrade_channel"`
ResourceGroupName string `tfschema:"resource_group_name"`
Name string `tfschema:"name"`
NginxVersion string `tfschema:"nginx_version"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
Sku string `tfschema:"sku"`
ManagedResourceGroup string `tfschema:"managed_resource_group"`
Location string `tfschema:"location"`
Capacity int64 `tfschema:"capacity"`
AutoScaleProfile []AutoScaleProfile `tfschema:"auto_scale_profile"`
DiagnoseSupportEnabled bool `tfschema:"diagnose_support_enabled"`
Email string `tfschema:"email"`
IpAddress string `tfschema:"ip_address"`
LoggingStorageAccount []LoggingStorageAccount `tfschema:"logging_storage_account"`
FrontendPublic []FrontendPublic `tfschema:"frontend_public"`
FrontendPrivate []FrontendPrivate `tfschema:"frontend_private"`
NetworkInterface []NetworkInterface `tfschema:"network_interface"`
UpgradeChannel string `tfschema:"automatic_upgrade_channel"`
WebApplicationFirewallSettings []WebApplicationFirewallSettings `tfschema:"web_application_firewall_settings"`
WebApplicationFirewallStatus []WebApplicationFirewallStatus `tfschema:"web_application_firewall_status"`
// Deprecated: remove in next major version
Configuration []Configuration `tfschema:"configuration,removedInNextMajorVersion"`
Tags map[string]string `tfschema:"tags"`
Expand Down Expand Up @@ -262,6 +285,24 @@ func (m DeploymentResource) Arguments() map[string]*pluginsdk.Schema {
}, false),
},

"web_application_firewall_settings": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"activation_state": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(
[]string{
"Enabled",
"Disabled",
}, false),
},
},
},
},

"tags": commonschema.Tags(),
}

Expand Down Expand Up @@ -351,6 +392,19 @@ func (m DeploymentResource) Attributes() map[string]*pluginsdk.Schema {
Type: pluginsdk.TypeString,
Computed: true,
},

"web_application_firewall_status": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"attack_signatures_package": webApplicationFirewallPackageComputed(),
"bot_signatures_package": webApplicationFirewallPackageComputed(),
"threat_campaigns_package": webApplicationFirewallPackageComputed(),
"component_versions": webApplicationFirewallComponentVersionsComputed(),
},
},
},
}
}

Expand Down Expand Up @@ -496,6 +550,15 @@ func (m DeploymentResource) Create() sdk.ResourceFunc {
}
}

if len(model.WebApplicationFirewallSettings) > 0 {
activationState := nginxdeployment.ActivationState(model.WebApplicationFirewallSettings[0].ActivationState)
prop.NginxAppProtect = &nginxdeployment.NginxDeploymentPropertiesNginxAppProtect{
WebApplicationFirewallSettings: nginxdeployment.WebApplicationFirewallSettings{
ActivationState: &activationState,
},
}
}

req.Properties = prop

req.Identity, err = identity.ExpandSystemAndUserAssignedMapFromModel(model.Identity)
Expand Down Expand Up @@ -627,6 +690,52 @@ func (m DeploymentResource) Read() sdk.ResourceFunc {
output.UpgradeChannel = props.AutoUpgradeProfile.UpgradeChannel
}

if props.NginxAppProtect != nil {
if props.NginxAppProtect.WebApplicationFirewallSettings.ActivationState != nil {
output.WebApplicationFirewallSettings = []WebApplicationFirewallSettings{
{
string(*props.NginxAppProtect.WebApplicationFirewallSettings.ActivationState),
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus != nil {
wafStatus := WebApplicationFirewallStatus{}
if props.NginxAppProtect.WebApplicationFirewallStatus.AttackSignaturesPackage != nil {
wafStatus.AttackSignaturesPackage = []WebApplicationFirewallPackage{
{
RevisionDatetime: props.NginxAppProtect.WebApplicationFirewallStatus.AttackSignaturesPackage.RevisionDatetime,
Version: props.NginxAppProtect.WebApplicationFirewallStatus.AttackSignaturesPackage.Version,
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus.BotSignaturesPackage != nil {
wafStatus.BotSignaturesPackage = []WebApplicationFirewallPackage{
{
RevisionDatetime: props.NginxAppProtect.WebApplicationFirewallStatus.BotSignaturesPackage.RevisionDatetime,
Version: props.NginxAppProtect.WebApplicationFirewallStatus.BotSignaturesPackage.Version,
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus.ThreatCampaignsPackage != nil {
wafStatus.ThreatCampaignsPackage = []WebApplicationFirewallPackage{
{
RevisionDatetime: props.NginxAppProtect.WebApplicationFirewallStatus.ThreatCampaignsPackage.RevisionDatetime,
Version: props.NginxAppProtect.WebApplicationFirewallStatus.ThreatCampaignsPackage.Version,
},
}
}
if props.NginxAppProtect.WebApplicationFirewallStatus.ComponentVersions != nil {
wafStatus.ComponentVersions = []WebApplicationFirewallComponentVersions{
{
WafEngineVersion: props.NginxAppProtect.WebApplicationFirewallStatus.ComponentVersions.WafEngineVersion,
WafNginxVersion: props.NginxAppProtect.WebApplicationFirewallStatus.ComponentVersions.WafNginxVersion,
},
}
}
output.WebApplicationFirewallStatus = []WebApplicationFirewallStatus{wafStatus}
}
}

flattenedIdentity, err := identity.FlattenSystemAndUserAssignedMapToModel(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %v", err)
Expand Down Expand Up @@ -766,6 +875,15 @@ func (m DeploymentResource) Update() sdk.ResourceFunc {
return fmt.Errorf("basic SKUs are incompatible with `capacity` or `auto_scale_profiles`")
}

if meta.ResourceData.HasChange("web_application_firewall_settings") {
activationState := nginxdeployment.ActivationState(model.WebApplicationFirewallSettings[0].ActivationState)
req.Properties.NginxAppProtect = &nginxdeployment.NginxDeploymentUpdatePropertiesNginxAppProtect{
WebApplicationFirewallSettings: &nginxdeployment.WebApplicationFirewallSettings{
ActivationState: &activationState,
},
}
}

if err := client.DeploymentsUpdateThenPoll(ctx, *id, req); err != nil {
return fmt.Errorf("updating %s: %v", id, err)
}
Expand Down
Loading
Loading