无服务器框架AWS细粒度访问控制

时间:2018-12-15 22:37:20

标签: amazon-web-services aws-lambda amazon-dynamodb amazon-cognito serverless-framework

我正在尝试保护我的AWS API,以便通过在我的无服务器框架配置(serverless.yml)中实现细粒度的访问控制,只有经过身份验证的Cognito用户才能访问DynamoDB行

查看我在AWS Documentation中尝试的示例

我尝试将Cloudformation语法转换为Serverless,但没有成功;当我在政策声明中尝试类似以下表达式的内容时:

Condition:
  ForAllValues:StringEquals:
    dynamodb:LeadingKeys: ["${cognito-identity.amazonaws.com:sub}"]

然后我得到一个错误:

  

变量cognito-identity.amazonaws.com:sub的变量参考语法无效。您只能引用环境变量,选项和文件。您可以查看我们的文档以获取更多信息。

在Serverless中甚至有可能吗?还是仅Cloudformation和SAM?

1 个答案:

答案 0 :(得分:0)

在无服务器中是可能的。如果您是我,我将使用AWS Lambda验证发送给用户的id_token。 在这种情况下,您应该首先使用Api Gateway或其他方法将密钥转移到AWS Lambda函数。然后按照this guide验证令牌。可以在以下位置找到该代码:https://github.com/awslabs/aws-support-tools/tree/master/Cognito/decode-verify-jwt

验证后,您可以在此处添加代码:

 ...... 
if claims['aud'] != app_client_id:
    print('Token was not issued for this audience')
    return False
# now we can use the claims

# add your code here #

print(claims)
return claims