繁体   English   中英

如何创建在 Python CDK 中引用自身的 API 网关资源策略?

[英]How to create API Gateway Resource Policy that references itself in the Python CDK?

我正在创建一个 API,它将接受来自 GitHub Webhook 服务器的请求,方法是将资源策略与 GitHub IP 结合使用。 我已经使用控制台并手动创建资源策略成功地完成了这项工作,但是我在使用 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()]))

我觉得我非常接近找到解决方案,但我不知道它是什么。 上面代码的问题是我在尝试部署它时收到ValidationError: Circular dependency between resources错误 - 我可以理解,资源策略正在解决它内部的资源。 但是我在 CDK 中找不到解决方法。

对于其他资源,在使用aws_iam.add_to_role_policy创建后添加 IAM 策略真的很容易,但我在 CDK 中找不到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"
                    ]
                }
            }
        }
    ]
}

有谁知道我的问题的解决方案? 提前致谢!

此外,您可以忽略空的集成响应 - 我仍在努力。

这与底层 CloudFormation 资源的工作方式有关。

由于Policy必须在AWS::ApiGateway::RestApi资源中定义,因此它不能引用自身。

文档中:

要为策略设置 ARN,请使用!Join内部函数,其中""作为分隔符,并使用"execute-api:/""*"的值。

这在您的 CDK 代码中转换为以下内容:

resources=[core.Fn.join('', ['execute-api:/', '*'])]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM