我有一个 lambda 函数,它将一些数据写入我也通过 AWS 设置的 Elasticsearch 域。目前我的域的访问策略只是允许我自己的 IP 地址与该域一起使用
{"Version": "2012-10-17", "Statement": [{
"Effect": "Allow", "Principal": {"AWS": "*"},
"Action": "es:*",
"Resource": "arn:aws:es:us-east-1:$ACCOUNT:domain/DOMAIN/*",
"Condition": { "IpAddress": { "aws:SourceIp": $MYIP } }
}]}
我找到了aws4
library用于签署 http 请求。我这样使用它:
axios(aws4.sign({
host: process.env.ES_ENDPOINT,
method: "post",
url: `https://${process.env.ES_ENDPOINT}/foobot/foobot`,
data,
}))
这实际上在没有 aws4.sign
片段的情况下工作,因为我完全开放了 ES 域,但现在我应用了上面的 IP 地址策略。
现在,我不断收到这样的错误响应:
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
我还需要做些什么才能正确签署请求吗?
最佳答案
这实际上与两个库有关,axios
和 aws4
。 aws4
将基于普通 NodeJS http
请求进行签名,并且在带有正文的 POST 请求中需要正文才能正确签署请求.
通过传入 body
和 path
即可轻松解决此问题
axios(aws4.sign({
host: process.env.ES_ENDPOINT,
method: "POST",
url: `https://${process.env.ES_ENDPOINT}/foobot/foobot`,
data,
body: JSON.stringify(data),
path: "/foobot/foobot",
}))
关于node.js - 通过 http 对 aws 资源的请求进行正确签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599786/