我正在尝试创建一个只接受POST请求的WAF规则。通过UI,这是直截了当的,然而,试图实现与CDK相同。我想我已经完成了大部分,但是方法给我带来了问题。我本以为应该是HttpMethod.Post
,但这行不通。
我有:
Amazon.CDK.AWS.WAFv2.CfnWebACL cfnWebACL2 = new Amazon.CDK.AWS.WAFv2.CfnWebACL(this, "MyCfnWebACL", new Amazon.CDK.AWS.WAFv2.CfnWebACLProps {
DefaultAction = new Amazon.CDK.AWS.WAFv2.CfnWebACL.DefaultActionProperty {
Block = true
},
Name = "Allow_Post",
Rules = new [] { new Amazon.CDK.AWS.WAFv2.CfnWebACL.RuleProperty {
Name = "Allow_Post",
Priority = 1,
Statement = new Amazon.CDK.AWS.WAFv2.CfnWebACL.StatementProperty {
ByteMatchStatement = new Amazon.CDK.AWS.WAFv2.CfnWebACL.ByteMatchStatementProperty {
FieldToMatch = new Amazon.CDK.AWS.WAFv2.CfnWebACL.FieldToMatchProperty {
Method = HttpMethod.Post
},
PositionalConstraint = "EXACTLY",
SearchString = "POST",
TextTransformations = new [] { new Amazon.CDK.AWS.WAFv2.CfnWebACL.TextTransformationProperty {
Priority = 1,
Type = "NONE"
} },
}
},
VisibilityConfig = new Amazon.CDK.AWS.WAFv2.CfnWebACL.VisibilityConfigProperty {
CloudWatchMetricsEnabled = false,
MetricName = "metricName",
SampledRequestsEnabled = false
}}},
VisibilityConfig = new Amazon.CDK.AWS.WAFv2.CfnWebACL.VisibilityConfigProperty {
CloudWatchMetricsEnabled = false,
MetricName = "metricName",
SampledRequestsEnabled = false
},
Scope = "REGIONAL",
});
终于成功了:
Amazon.CDK.AWS.WAFv2.CfnWebACL cfnWebACL = new Amazon.CDK.AWS.WAFv2.CfnWebACL(this, "MyCfnWebACLw", new Amazon.CDK.AWS.WAFv2.CfnWebACLProps {
DefaultAction = new Amazon.CDK.AWS.WAFv2.CfnWebACL.DefaultActionProperty {
Block = new Amazon.CDK.AWS.WAFv2.CfnWebACL.BlockActionProperty {
CustomResponse = new Amazon.CDK.AWS.WAFv2.CfnWebACL.CustomResponseProperty {
ResponseCode = 403,
}
}
},
Scope = "REGIONAL",
VisibilityConfig = new Amazon.CDK.AWS.WAFv2.CfnWebACL.VisibilityConfigProperty {
MetricName = "test",
SampledRequestsEnabled = false,
CloudWatchMetricsEnabled = false
},
Rules = new [] { new Amazon.CDK.AWS.WAFv2.CfnWebACL.RuleProperty {
Name = "myRule",
Priority = 0,
Statement = new Amazon.CDK.AWS.WAFv2.CfnWebACL.StatementProperty {
ByteMatchStatement = new Amazon.CDK.AWS.WAFv2.CfnWebACL.ByteMatchStatementProperty {
PositionalConstraint = "EXACTLY",
SearchString = "POST",
TextTransformations = new [] {new Amazon.CDK.AWS.WAFv2.CfnWebACL.TextTransformationProperty {
Priority = 0,
Type = "NONE"
}},
FieldToMatch = new Amazon.CDK.AWS.WAFv2.CfnWebACL.FieldToMatchProperty {
Method = new Dictionary<string, object> {{ "name", "Post" }}
}
}
},
VisibilityConfig = new Amazon.CDK.AWS.WAFv2.CfnWebACL.VisibilityConfigProperty {
MetricName = "myMEtric",
SampledRequestsEnabled = false,
CloudWatchMetricsEnabled = false
},
Action = new Amazon.CDK.AWS.WAFv2.CfnWebACL.RuleActionProperty {
Allow = new Amazon.CDK.AWS.WAFv2.CfnWebACL.AllowActionProperty {
CustomRequestHandling = new Amazon.CDK.AWS.WAFv2.CfnWebACL.CustomRequestHandlingProperty {
InsertHeaders = new [] { new Amazon.CDK.AWS.WAFv2.CfnWebACL.CustomHTTPHeaderProperty {
Name = "name",
Value = "value"
} }
}
}
}
}}
});