Implementing ABAC using Custom Cognito Attributes.

0

I'm trying to implement Attribute based authorization using API Gateway, DynamoDB and Cognito. For some reason the authorization works fine, but it doesn't work when I refer to the users' Custom Attribute in the mapping template of the integration request in API gateway.

Any help would be really welcome... I'm quite new to AWS, and I'm stuck with this issue for over a week now...

What I tried:

Custom Cognito user attribute

Create a custom attribute in Cognito. (organisation_id)

Custom mapping:

Cognito -> Identity pools -> User access -> Identity providers: Attributes for access control: Custom mapping:

  • Tag key: organisation_id
  • Claim: custom:organisation_id

Role settings:

Use default authenticated role

Default authenticated role:

Trust relationships:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "cognito-identity.amazonaws.com"
            },
            "Action": [
                "sts:AssumeRoleWithWebIdentity",
                "sts:TagSession"
            ],
            "Condition": {
                "StringEquals": {
                    "cognito-identity.amazonaws.com:aud": "x"
                },
                "ForAnyValue:StringLike": {
                    "cognito-identity.amazonaws.com:amr": "authenticated"
                }
            }
        }
    ]
}

Permissions:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "dynamodb:Query",
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem"
            ],
            "Resource": "arn:aws:dynamodb:eu-central-1:x:table/Organisations",
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:LeadingKeys": "${aws:PrincipalTag/organisation_id}"
                }
            }
        }
    ]
}

API Gateway API:

  • Authorizer: Cognito
    • Resources: /organisation
      • POST:
        • Method request: All empty.
        • Integration request:
          • Type: AWS service Dynamo
          • HTTP method: Post
          • Action type: use action name
          • Action name: Query
          • Execution role: (see below)
          • Credential cache: Do not add caller credentials to cache key
          • Content Handling: Passthrough
          • Request body passthrough: When there are no templates defined
          • URL path parameters: Empty
          • URL query string: empty
          • URL request headers parameters: empty
          • Mapping Template:
          • Content type: application/json
          • Template body:
{
  "TableName": "Organisations",
  "KeyConditionExpression": "PK = :orgId",
  "ExpressionAttributeValues": {
    ":orgId": { "S": "${aws:PrincipalTag/organisation_id}"}
  }
}

When I make a call to the endpoint it doesn't work Logs: Execution failed due to configuration error: Unable to transform request

When I replace the Template body with a fixed organisation_id it works as expected, and returns the data. (depending on the organisation_id of the user)

{
  "TableName": "Organisations",
  "KeyConditionExpression": "PK = :orgId",
  "ExpressionAttributeValues": {
    ":orgId": { "S": "yyy"}
  }
}

Logs: Endpoint request body after transformations: { "TableName": "Organisations", "KeyConditionExpression": "PK = :orgId", "ExpressionAttributeValues": { ":orgId": { "S": "yyy"} } }

Execution Role for API gateway:

Trust relationships:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": "apigateway.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

Permissions:

  • AmazonDynamoDBFullAccess
  • AmazonAPIGatewayPushToCloudWatchLogs

Could anyone tell me what I'm doing wrong?

1 個回答
2
已接受的答案

Using IAM role tags or variables directly in DynamoDB query parameters, such as ${aws:PrincipalTag/organisation_id}, is not supported. Instead, you must programmatically retrieve relevant values (like an organization ID) and then use them in your query.

Not Supported:

{
  "TableName": "Organisations",
  "KeyConditionExpression": "PK = :orgId",
  "ExpressionAttributeValues": {
    ":orgId": { "S": "**${aws:PrincipalTag/organisation_id}**"}
  }
}
profile picture
專家
已回答 3 個月前
profile picture
專家
已審閱 1 個月前
  • Thanks a lot Osvaldo!

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南