【发布时间】:2020-03-02 22:18:25
【问题描述】:
我正在尝试缓存授权方 lambda 在首次验证 JWT 令牌时返回的 IAM 策略。我已在 API Gateway Authorizer 中启用并将 authorizerResultTtlInSeconds 设置为 3500 秒。但是,我仍然看到在缓存时间范围内发送到 Authorizer lambda 函数的请求。
我的 node.js 脚本如下:
const jwt = require('jsonwebtoken');
const jwksClient = require('jwks-rsa');
const keyClient = jwksClient({
jwksUri: process.env.JWKS_URI
})
const allow = {
"principalId": "user",
"policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": process.env.RESOURCE // RESOURCE = *
}
]
}
}
const unauthorized = {
"error": "Unauthorized",
}
//excluded verificationJWTOptions object and getSigningKey function for simplicity
function validateJWTToken(token, callback) {
jwt.verify(token, getSigningKey, verificationJWTOptions, (error) => {
if (error) {
callback(unauthorized)
} else {
callback(null, allow)
}
})
}
exports.handler = (event, context, callback) => {
const token = extractTokenFromHeader(event);
validateJWTToken(token, callback);
}
不确定我错过了什么。任何帮助将不胜感激!
【问题讨论】:
-
查看两个请求的 API Gateway 执行日志可能有助于识别授权策略或缓存密钥的任何问题
-
我检查正确,但在 CloudWatch 日志组中找不到今天日期和时间的 API 网关执行日志。我在 CloudWatch 日志中看到的 Authorizer lambda 函数只是
START RequestId、END RequestId和REPORT RequestId(没有其他信息)。 -
@nrai 尝试在授权函数中打印事件数据,并比较第一次和后续调用的日志。 docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
标签: node.js caching aws-api-gateway lambda-authorizer