确保 Amazon Gateway API 只能从 S3 存储桶中的内容访问
Secure Amazon Gateway API to only be accessable from content in S3 bucket
我有一个 API 暴露在 public 互联网上,它是我通过亚马逊的网关 API 服务创建的。 API 调用触发查询后端数据库的 Lambda 函数。我创建的 API 被 S3 静态网页用来获取数据。
我想保护我的 API,以便只有我的 S3 存储桶 (index.html) 的内容有权访问我的 API。我想阻止人们直接查询我的 API 并使用 curl 请求等抓取数据。我已经阅读了一些关于 IAM 角色、Cognito 和 Lambda 权限模型的内容,但我我不确定如何使用所有这些不同的工具来保护我的 API。
保护我的 API 的最佳方法是什么,以便只有我的 S3 存储桶中的内容有权访问我的 API?
你不能真正授权 S3 访问 API 因为 S3 不访问你的 API - S3 交付 html (js/css 和其他静态资产)到访问您网站的用户的客户端(网络浏览器)。然后从客户端调用您的 API。
此外,Amazon 强制要求 API 网关为 public(有道理,但您不能在 VPC 后面设置 API)
我建议你仔细阅读 Security and Authorization of the FAQ
你可以做什么:
- 将请求签署到 API 网关,但它需要其他东西生成 html 代码给客户端(据我所知,S3 本身不能这样做 但我喜欢犯错)
- 亚马逊API网关可以生成客户端SSL证书来验证后端的请求
- 设置一些限制(见下文)
如果您担心滥用,也可以阅读常见问题解答
Q: How can I address or prevent API threats or abuse?
Amazon API Gateway supports throttling settings for each method in
your APIs. You can set a standard rate limit and a burst rate limit
per second for each method in your REST APIs. Further, Amazon API
Gateway automatically protects your backend systems from distributed
denial-of-service (DDoS) attacks, whether attacked with counterfeit
requests (Layer 7) or SYN floods (Layer 3).
根据您的设置,您可以尝试使用一个或多个 AWS condition keys in your API Gateway's Resource Policy。例如,您只能允许 referer 是您的 S3 静态网站的请求:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": [
"execute-api:/{{stageNameOrWildcard*}}/{{httpVerbOrWildcard*}}/{{resourcePathOrWildcard*}}"
],
"Condition": {
"StringLike": {
"aws:Referer": [
"<bucket-name>.s3-website-<AWS-region>.amazonaws.com/*"
]
}
}
}
]
}
我有一个 API 暴露在 public 互联网上,它是我通过亚马逊的网关 API 服务创建的。 API 调用触发查询后端数据库的 Lambda 函数。我创建的 API 被 S3 静态网页用来获取数据。
我想保护我的 API,以便只有我的 S3 存储桶 (index.html) 的内容有权访问我的 API。我想阻止人们直接查询我的 API 并使用 curl 请求等抓取数据。我已经阅读了一些关于 IAM 角色、Cognito 和 Lambda 权限模型的内容,但我我不确定如何使用所有这些不同的工具来保护我的 API。
保护我的 API 的最佳方法是什么,以便只有我的 S3 存储桶中的内容有权访问我的 API?
你不能真正授权 S3 访问 API 因为 S3 不访问你的 API - S3 交付 html (js/css 和其他静态资产)到访问您网站的用户的客户端(网络浏览器)。然后从客户端调用您的 API。
此外,Amazon 强制要求 API 网关为 public(有道理,但您不能在 VPC 后面设置 API)
我建议你仔细阅读 Security and Authorization of the FAQ
你可以做什么:
- 将请求签署到 API 网关,但它需要其他东西生成 html 代码给客户端(据我所知,S3 本身不能这样做 但我喜欢犯错)
- 亚马逊API网关可以生成客户端SSL证书来验证后端的请求
- 设置一些限制(见下文)
如果您担心滥用,也可以阅读常见问题解答
Q: How can I address or prevent API threats or abuse?
Amazon API Gateway supports throttling settings for each method in your APIs. You can set a standard rate limit and a burst rate limit per second for each method in your REST APIs. Further, Amazon API Gateway automatically protects your backend systems from distributed denial-of-service (DDoS) attacks, whether attacked with counterfeit requests (Layer 7) or SYN floods (Layer 3).
根据您的设置,您可以尝试使用一个或多个 AWS condition keys in your API Gateway's Resource Policy。例如,您只能允许 referer 是您的 S3 静态网站的请求:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": [
"execute-api:/{{stageNameOrWildcard*}}/{{httpVerbOrWildcard*}}/{{resourcePathOrWildcard*}}"
],
"Condition": {
"StringLike": {
"aws:Referer": [
"<bucket-name>.s3-website-<AWS-region>.amazonaws.com/*"
]
}
}
}
]
}