在策略文档中为REST API配置iPaddress条件的正确方法是正确的?

发布于 2025-02-11 22:34:39 字数 1028 浏览 2 评论 0原文

我正在尝试仅允许特定的IP地址访问我的API Gateway REST API,而无需成功。

我配置了以下资源策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:eu-west-1:my-account-id:rest-api-id/*/*/*",
            "Condition": {
                "IpAddress": {
                    "aws.SourceIp": "MY.IP.MY.IP/32"
                }
            }
        }
    ]
}

但是,当我使用Postman发送请求时,我会收到403。

如果我设置“ aws.sourceip”:“ 0.0.0.0/0”,我也会收到403。

如果我从策略中删除条件,则可以工作(我没有收到403),所以我想iPadress条件出了问题。

我有一个与REST API集成的C#lambda函数,其中我使用以下代码将sourceip登录到CloudWatch:

context.Logger.LogInformation($"SourceIp: {request.RequestContext.Identity.SourceIp}");

它记录以下内容:

2022-07-01T06:38:32.634Z 0DC80274-BBBB-494C-BA73-541F053BA5A2 IFONE INFO SOUNCE SOUNTERIP: my.ip.my.ip

我想念什么?如何正确编写该政策?

I'm trying to allow only specific IP addresses to access my API Gateway REST API without success.

I configured the following resource policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:eu-west-1:my-account-id:rest-api-id/*/*/*",
            "Condition": {
                "IpAddress": {
                    "aws.SourceIp": "MY.IP.MY.IP/32"
                }
            }
        }
    ]
}

But when I send a request using Postman I receive 403 forbidden.

If I set "aws.SourceIp": "0.0.0.0/0" I receive 403 as well.

If I remove the condition from the policy then it works (I don't receive 403), so I guess something is wrong with IpAdress condition.

I have a C# lambda function integrated with the REST API where I log the SourceIp to CloudWatch using the following code:

context.Logger.LogInformation(
quot;SourceIp: {request.RequestContext.Identity.SourceIp}");

It logs the following:

2022-07-01T06:38:32.634Z 0dc80274-bbbb-494c-ba73-541f053ba5a2 info SourceIp:
MY.IP.MY.IP

What am I missing? How to properly write the policy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2025-02-18 22:34:39

我将更改为“ aws.sourceip”“ aws:sourceip”和解决问题。对不起,错字。

I changed "aws.SourceIp" to "aws:SourceIp" and problem solved. Sorry for the typo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文