A developer is building a mobile application that allows external users to authenticate using an external OpenID Connect (OIDC) identity provider. Once authenticated, users must be able to upload log files directly to a private Amazon S3 bucket. Each user's uploads must be restricted to an S3 folder named after their unique OIDC user identifier (the `sub` claim). The application also needs to write metadata for each upload to an Amazon DynamoDB table, using the same OIDC `sub` value as the partition key. Which solution meets these requirements with the least development effort and adheres to the principle of least privilege?
- AConfigure an Amazon Cognito User Pool with the OIDC provider. Configure an Amazon Cognito Identity Pool using the User Pool as an authentication provider. In the authenticated IAM role, define the S3 prefix and DynamoDB partition key using the policy variable `${cognito-identity.amazonaws.com:sub}`.
- BCreate an Amazon API Gateway endpoint with a custom Lambda Authorizer. Have the Lambda Authorizer validate the OIDC token, retrieve the `sub` claim, and call `sts:AssumeRole` to generate temporary AWS credentials with an inline IAM policy scoped to the user's OIDC `sub` prefix. Pass these credentials back to the client to perform S3 and DynamoDB operations.
- Configure an Amazon Cognito User Pool with the OIDC provider as an identity provider, mapping the OIDC `sub` claim to a custom attribute. Configure an Amazon Cognito Identity Pool with the User Pool as an authentication provider, enabling 'Attributes for access control' to map the custom attribute to a principal tag. Reference the mapped tag using `${aws:PrincipalTag/...}` in the IAM policy for the authenticated role to authorize S3 and DynamoDB actions.Answer
- DFederate the OIDC provider directly with an Amazon Cognito Identity Pool. Associate an IAM role with the Identity Pool, and reference `${amazon.cognito.auth.externalIdp:sub}` in the IAM policy to restrict S3 bucket and DynamoDB access.
Answer
The correct solution is to configure the Amazon Cognito User Pool to map the OIDC `sub` claim to a custom attribute, map that attribute to a principal tag in the Identity Pool using 'Attributes for access control', and reference the tag via the policy variable in the IAM policy.
Mapping the OIDC `sub` claim to a Cognito User Pool custom attribute, exposing it as a Principal Tag via the Identity Pool's 'Attributes for access control', and utilizing the principal tag policy variable in the IAM policy is the most secure and operationally efficient way to implement attribute-based access control (ABAC) for federated users.
Step-by-Step Solution
Key Concept
Attribute-Based Access Control (ABAC) with Amazon Cognito Identity Pools and federated OIDC providers.