Skip to content

Commit

Permalink
Merge pull request #28594 from dbell-infra/f-add-wafv2-managed-rule-g…
Browse files Browse the repository at this point in the history
…roup-configs

Add Managed Rule Group Config functionality to WAFv2 resource
  • Loading branch information
johnsonaj authored Jan 4, 2023
2 parents ec2b82f + 9305b35 commit f7cf135
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/28594.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_wafv2_web_acl: Add support for ManagedRuleGroupConfig
```
148 changes: 148 additions & 0 deletions internal/service/wafv2/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,87 @@ func expandManagedRuleGroupStatement(l []interface{}) *wafv2.ManagedRuleGroupSta
if v, ok := m["version"]; ok && v != "" {
r.Version = aws.String(v.(string))
}
if v, ok := m["managed_rule_group_configs"].([]interface{}); ok && len(v) > 0 {
r.ManagedRuleGroupConfigs = expandManagedRuleGroupConfigs(v)
}

return r
}

func expandManagedRuleGroupConfigs(tfList []interface{}) []*wafv2.ManagedRuleGroupConfig {
if len(tfList) == 0 {
return nil
}

var out []*wafv2.ManagedRuleGroupConfig
for _, item := range tfList {
m, ok := item.(map[string]interface{})
if !ok {
continue
}

var r wafv2.ManagedRuleGroupConfig
if v, ok := m["aws_managed_rules_bot_rule_set"].([]interface{}); ok && len(v) > 0 {
r.AWSManagedRulesBotControlRuleSet = expandManagedRulesBotControlRuleSet(v)
}
if v, ok := m["login_path"].(string); ok && v != "" {
r.LoginPath = aws.String(v)
}
if v, ok := m["payload_type"].(string); ok && v != "" {
r.PayloadType = aws.String(v)
}
if v, ok := m["password_field"].([]interface{}); ok && len(v) > 0 {
r.PasswordField = expandPasswordField(v)
}
if v, ok := m["username_field"].([]interface{}); ok && len(v) > 0 {
r.UsernameField = expandUsernameField(v)
}

out = append(out, &r)
}

return out
}

func expandPasswordField(tfList []interface{}) *wafv2.PasswordField {
if len(tfList) == 0 || tfList[0] == nil {
return nil
}

m := tfList[0].(map[string]interface{})
out := wafv2.PasswordField{
Identifier: aws.String(m["identifier"].(string)),
}

return &out
}

func expandUsernameField(tfList []interface{}) *wafv2.UsernameField {
if len(tfList) == 0 || tfList[0] == nil {
return nil
}

m := tfList[0].(map[string]interface{})
out := wafv2.UsernameField{
Identifier: aws.String(m["identifier"].(string)),
}

return &out
}

func expandManagedRulesBotControlRuleSet(tfList []interface{}) *wafv2.AWSManagedRulesBotControlRuleSet {
if len(tfList) == 0 || tfList[0] == nil {
return nil
}

m := tfList[0].(map[string]interface{})
out := wafv2.AWSManagedRulesBotControlRuleSet{
InspectionLevel: aws.String(m["inspection_level"].(string)),
}

return &out
}

func expandRateBasedStatement(l []interface{}) *wafv2.RateBasedStatement {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down Expand Up @@ -1929,9 +2006,80 @@ func flattenManagedRuleGroupStatement(apiObject *wafv2.ManagedRuleGroupStatement
tfMap["version"] = aws.StringValue(apiObject.Version)
}

if apiObject.ManagedRuleGroupConfigs != nil {
tfMap["managed_rule_group_configs"] = flattenManagedRuleGroupConfigs(apiObject.ManagedRuleGroupConfigs)
}

return []interface{}{tfMap}
}

func flattenManagedRuleGroupConfigs(c []*wafv2.ManagedRuleGroupConfig) []interface{} {
if len(c) == 0 {
return nil
}

var out []interface{}

for _, config := range c {
m := make(map[string]interface{})
if config.AWSManagedRulesBotControlRuleSet != nil {
m["aws_managed_rules_bot_control_rule_set"] = flattenManagedRulesBotControlRuleSet(config.AWSManagedRulesBotControlRuleSet)
}
if config.LoginPath != nil {
m["login_path"] = aws.StringValue(config.LoginPath)
}
if config.PayloadType != nil {
m["payload_type"] = aws.StringValue(config.PayloadType)
}
if config.PasswordField != nil {
m["password_field"] = flattenPasswordField(config.PasswordField)
}
if config.UsernameField != nil {
m["username_field"] = flattenUsernameField(config.UsernameField)
}

out = append(out, m)
}

return out
}

func flattenPasswordField(apiObject *wafv2.PasswordField) []interface{} {
if apiObject == nil {
return nil
}

m := map[string]interface{}{
"identifier": aws.StringValue(apiObject.Identifier),
}

return []interface{}{m}
}

func flattenUsernameField(apiObject *wafv2.UsernameField) []interface{} {
if apiObject == nil {
return nil
}

m := map[string]interface{}{
"identifier": aws.StringValue(apiObject.Identifier),
}

return []interface{}{m}
}

func flattenManagedRulesBotControlRuleSet(apiObject *wafv2.AWSManagedRulesBotControlRuleSet) []interface{} {
if apiObject == nil {
return nil
}

m := map[string]interface{}{
"inspection_level": aws.StringValue(apiObject.InspectionLevel),
}

return []interface{}{m}
}

func flattenRateBasedStatement(apiObject *wafv2.RateBasedStatement) interface{} {
if apiObject == nil {
return []interface{}{}
Expand Down
77 changes: 75 additions & 2 deletions internal/service/wafv2/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,9 @@ func managedRuleGroupStatementSchema(level int) *schema.Schema {
Required: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"rule_action_override": ruleActionOverrideSchema(),
"scope_down_statement": scopeDownStatementSchema(level - 1),
"rule_action_override": ruleActionOverrideSchema(),
"managed_rule_group_configs": managedRuleGroupConfigSchema(),
"scope_down_statement": scopeDownStatementSchema(level - 1),
"vendor_name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -942,6 +943,78 @@ func ruleActionOverrideSchema() *schema.Schema {
}
}

func managedRuleGroupConfigSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aws_managed_rules_bot_control_rule_set": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"inspection_level": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(wafv2.InspectionLevel_Values(), false),
},
},
},
},
"login_path": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 256),
validation.StringMatch(regexp.MustCompile(`.*\S.*`), `must conform to pattern .*\S.* `),
),
},
"password_field": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 512),
validation.StringMatch(regexp.MustCompile(`.*\S.*`), `must conform to pattern .*\S.* `),
),
},
},
},
},
"payload_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(wafv2.PayloadType_Values(), false),
},
"username_field": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 512),
validation.StringMatch(regexp.MustCompile(`.*\S.*`), `must conform to pattern .*\S.* `),
),
},
},
},
},
},
},
}
}

func actionToUseSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Expand Down
Loading

0 comments on commit f7cf135

Please sign in to comment.