如何通过API网关设置Cognito放大细粒度的DynamoDB访问

时间:2019-03-09 00:32:58

标签: amazon-web-services amazon-dynamodb aws-api-gateway amazon-cognito aws-amplify

我使用Cognito身份池使用Cognito联合身份,并使用Amplify进行设置。

我有一个通过IAM授权的API网关端点。 Cognito Auth角色允许调用此端点。

API网关端点资源与DynamoDB(PutItem)集成在一起,并承担执行角色,该角色允许访问特定的DynamoDB表和dynamodb:PutItem。

这一切都顺畅进行,经过验证的用户可以将物品放在桌子上。

我现在希望启用细粒度访问,以便Cognito用户只能读取/写入DynamoDB表中由其标识ID标识的项目。

我正在使用S3存储模块来工作。

我将此条件添加到API网关假定的ExecutionRole中:

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

请求主体包含用户的身份令牌,作为该项目的哈希/分区键。

Auth.currentAuthenticatedUser({ bypassCache: false  })
      .then(user => {

        // Set the credentials necessary for making API calls
        Auth.currentCredentials()
          .then(creds => {
            state.identityId = credentials.identityId
            state.accessKey = credentials.accessKeyId
            state.secretKey = credentials.secretAccessKey
            state.sessionToken = credentials.sessionToken
        })

let body = {
    userid: state.identityId,
    name: username,
    load_date: now
  }

要清楚,在我将条件添加到IAM策略之前,一切都会按预期进行。

我已经关注了这篇文章,但是它缺少API网关组件,而是将IAM策略直接放在Cognito Auth角色上,然后直接调用Dynamo。

https://aws.amazon.com/blogs/mobile/building-fine-grained-authorization-using-amazon-cognito-user-pools-groups/

为什么我的设置不起作用?

编辑: 我开始认为这是不可能的,我需要在两者之间使用Lambda函数,或直接进入DynamoDB。.然而,来自BobK的评论:

  

如果直接使用以下命令调用DynamoDB,则细粒度访问控制有效   您的Cognito凭证,或者如果您将AWS Service代理设置为   启用了“使用调用方凭据”的DynamoDB

https://forums.aws.amazon.com/thread.jspa?messageID=687420&#687420

似乎表明我正在尝试做的事情...在“集成请求”面板的任何位置都找不到“用户呼叫者凭据”选项。

这是我的集成映射:

"TableName": "TABLE_NAME",
    "Item": {
        "userid": {
                "S": "$context.identity.cognitoIdentityId"
            },

当我关闭IAM角色条件时,将按预期填充表...主分区键是userid ...

0 个答案:

没有答案