使用 WAF 验证在 API GW 设置的自定义 HTTP Header 值
在 API GW HTTP 集成方法的集成请求中注入自定义 HTTP 标头。按照Amazon API Gateway API request and response data mapping reference 中的说明使用静态值。
'STATIC_VALUE'。 STATIC_VALUE 是一个字符串文字,必须用一对单引号括起来。
与 AWS 文档的情况一样,如果我们应该使用“integration.request.header”会令人困惑。格式。如果在 AWS 控制台中设置,则无需键入“integration.request.header”。只需键入标题名称即可。确保标题值是单引号
但是,当使用 CDK 或 CFN 之类的工具时,我们需要使用“integration.request.header”。部分。
cdk_api_method: aws_apigateway.Method = cdk_api_resource.add_method(
http_method="post",
integration=aws_apigateway.HttpIntegration(
url=url,
http_method="post",
proxy=True,
options=aws_apigateway.IntegrationOptions(
request_parameters={
"integration.request.header.{}".format(HTTP_HEADER_X_VALIDATION_CLIENT_NAME): "'{}'".format(HTTP_HEADER_X_VALIDATION_CLIENT_VALUE)
}
)
)
)
设置 WAF 以验证 HTTP 标头值并将 ALB 关联到 WAF ACL。
# https://github.com/aws-samples/wafv2-json-yaml-samples/blob/master/JSON/rule-001.json
aws_wafv2.CfnWebACL.RuleProperty(
name='header-x-validation-client',
action=aws_wafv2.CfnWebACL.RuleActionProperty(
allow={}
),
statement=aws_wafv2.CfnWebACL.StatementOneProperty(
byte_match_statement=aws_wafv2.CfnWebACL.ByteMatchStatementProperty(
field_to_match=aws_wafv2.CfnWebACL.FieldToMatchProperty(
single_header={
"Name": HTTP_HEADER_X_VALIDATION_CLIENT_NAME
}
),
positional_constraint="EXACTLY",
search_string=HTTP_HEADER_X_VALIDATION_CLIENT_VALUE,
text_transformations=[
aws_wafv2.CfnWebACL.TextTransformationProperty(
priority=0,
type="NONE"
)
]
)
),
visibility_config=aws_wafv2.CfnWebACL.VisibilityConfigProperty(
sampled_requests_enabled=True,
cloud_watch_metrics_enabled=True,
metric_name='waf-rule-header-x-validation-client'
),
priority=0
)