Jam*_*mie 7 python aws-cloudformation amazon-iam aws-api-gateway aws-cdk
我正在创建一个 API,它仅接受通过使用具有 GitHub IP 的资源策略从 GitHub Webhook 服务器发出的请求。我已经使用控制台并手动创建资源策略成功完成了此操作,但在使用 CDK 时遇到了问题。
这是我的代码:
delete_trigger_integration = aws_apigateway.LambdaIntegration(
trigger_step_lambda, proxy=False, integration_responses=[])
api_policy_document = aws_iam.PolicyDocument()
api = aws_apigateway.RestApi(
self,
"GithubWebhookApi",
rest_api_name=PROJECT_NAME + "-apigateway-trigger-delete",
default_integration=delete_trigger_integration,
policy=api_policy_document)
delete_execution_resource = api.root.add_resource("execution")
delete_execution_method = delete_execution_resource.add_method(
"POST", delete_trigger_integration)
delete_execution_resource.add_cors_preflight(allow_origins=["*"])
create_repo_lambda.add_environment("API_URL",
delete_execution_resource.url)
api_policy_document.add_statements(
aws_iam.PolicyStatement(
effect=aws_iam.Effect.ALLOW,
principals=[aws_iam.AnyPrincipal()],
actions=["execute-api:Invoke"],
resources=[api.arn_for_execute_api()]))
api_policy_document.add_statements(
aws_iam.PolicyStatement(
effect=aws_iam.Effect.DENY,
actions=["execute-api:Invoke"],
conditions={
"NotIpAddress": {
"aws:SourceIp": [
"192.30.252.0/22", "185.199.108.0/22",
"140.82.112.0/20"
]
}
},
principals=[aws_iam.AnyPrincipal()],
resources=[api.arn_for_execute_api()]))
Run Code Online (Sandbox Code Playgroud)
我觉得我已经非常接近找到解决方案,但我不知道它是什么。上面代码的问题是我ValidationError: Circular dependency between resources在尝试部署它时收到错误 - 我可以理解,资源策略正在解决它内部的资源。但我在 CDK 中找不到解决方法。
对于其他资源,在创建后使用 IAM 策略进行广告确实很容易,但我在 CDK 中aws_iam.add_to_role_policy找不到该类的等效项。RestApi看来你必须PolicyDocument在声明时添加RestApi.
另外,这是我尝试在 CDK 中重新创建的资源策略(正是这些资源 ARN 导致了问题):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:eu-west-1:000000000000:aaaaaaaaaa/*/*/*"
},
{
"Effect": "Deny",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:eu-west-1:000000000000:aaaaaaaaaa/*/*/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"192.30.252.0/22",
"185.199.108.0/22",
"140.82.112.0/20"
]
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我的问题的解决方案?提前致谢!
另外,您可以忽略空的集成响应 - 我仍在努力解决这个问题。
这与底层 CloudFormation 资源的工作方式有关。
由于Policy必须在资源内定义AWS::ApiGateway::RestApi,因此它不能引用自身。
从文档中:
要设置策略的 ARN,请使用
!Join内部函数 和作为分隔符以及和""的值。"execute-api:/""*"
这会在您的 CDK 代码中转化为以下内容:
resources=[core.Fn.join('', ['execute-api:/', '*'])]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6410 次 |
| 最近记录: |