> For the complete documentation index, see [llms.txt](https://gitbook.cdxiaodong.life/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.cdxiaodong.life/yun-an-quan/aws/api-gateway.md).

# Amazon API网关

## Amazon API Gateway 概述

Amazon API Gateway 是一项 AWS 服务，用于创建、发布、维护、监控和保护任意规模的 REST、HTTP 和 WebSocket API。API Gateway 处理接受和处理多达数十万个并发 API 调用所需的所有任务，包括流量管理、CORS 支持、授权和访问控制、限流、监控和 API 版本管理。换句话说，API Gateway 是一个可扩展的无服务器反向代理服务，可以将 HTTP/HTTPS 请求路由到不同的实现后端。

{% embed url="<https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html>" %}

### API Gateway 工作原理

以下图表解释了 API Gateway 的工作原理：

![](/files/CQFVCxgVwOI8on5H5LA4)

### Lambda 授权器

[Lambda 授权器](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html)（以前称为自定义授权器）是 API Gateway 的一个功能，它使用 lambda 函数来控制对 API 的访问。Lambda 授权器对于实现自定义授权方案非常有用，这些方案使用持有者令牌认证策略（如 OAuth 或 SAML），或者使用请求参数来确定调用者的身份。

以下图表说明了 Lambda 授权器的授权工作流程：

![](/files/S4yODBM1Qcu5toTZUBeB)

当客户端对 API 的某个方法发出请求时，API Gateway 会调用 Lambda 授权器，该授权器将调用者的身份作为输入，并返回一个 IAM 策略作为输出。[Amazon API Gateway Lambda 授权器的输出](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html)具有以下结构：

![](/files/457WI97taOlxpgPF3xVt)

`policyDocument` 属性允许 Lambda 授权器指定此用户可以访问或不能访问哪些 API 端点。`policyDocument` 对象包含一个 IAM 策略，该策略允许或拒绝对表示每个 API 端点的特殊格式的[资源](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_resource.html)执行 `execute-api` [操作](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_action.html)：

![](/files/2afeTCG5YnmpDC037g5Z)

IAM 策略中的 [ARNs](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html#identifiers-arns) 有几个不同的部分，由冒号分隔：

![](/files/JODpaFCiAYxerG7Uk2SB)

`execute-api` ARN 格式将 *API ID、API 阶段、HTTP 动词和 API 资源*（URL 路径）全部打包到 ARN 的最后一个 `resource` 部分中。它们由斜杠分隔，就像在虚拟文件系统中形成单个逻辑路径一样。

[文档](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html)声称，API ID、阶段名称和 HTTP 动词可以很容易地被 `*` 替换，表示该部分的任何值：

![](/files/nmqjEWTngXDcRSMKt9ag)

{% hint style="info" %}
通配符 `*` 是贪婪的，会在 ARN 的每个冒号分隔部分内尽可能扩展（它相当于 POSIX 正则表达式中的 `.*`）。扩展不会在斜杠处停止 - 任何 `*` 都可以自由将其匹配扩展到 [execute-api ARN](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html) 的整个最后一部分。

通配符也匹配空字符串。
{% endhint %}

{% hint style="info" %}
每个 AWS 服务决定 ARN 的资源部分匹配是否区分大小写，请参阅[链接](https://acloud.guru/forums/aws-certified-solutions-architect-associate/discussion/-KGQ_3oKHuxKEqAv5EPw/key-names-case-sensitive-or-insensitive-?answer=-KO0jdWjDzmwUANNtSz6)。
{% endhint %}

## 安全问题

### 不正确的 policyDocument 通配符配置

在 ARN 中使用通配符可能导致绕过访问控制的可能性。想象一个需要允许用户访问单个端点 `/business/op` 上的任何 HTTP 动词的 API。开发人员可能会倾向于编写如下的 `policyDocument`：

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "execute-api:Invoke",
            "Effect": "Allow",
            "Resource": "anr:aws:execute-api:us-west-1:12345678:myApiId/myStage/*/business/op"
        }
    ]
}
```

所有以下 ARN 都会被示例策略允许。但是，阅读了这部分文档的开发人员很可能不会预料到第 6 项。

![](/files/6DAvDahNrWvSTx8F7wwg)

因此，将 HTTP 动词设置为星号会使端点资源不再锚定到 URL 路径的开头。

## 参考资料

* [AWS API Gateway Lambda 授权器和 IAM 通配符扩展的安全影响](https://www.tenchisecurity.com/blog/thefaultinourstars)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gitbook.cdxiaodong.life/yun-an-quan/aws/api-gateway.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
