amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Best way to allow multiple users access to S3 on a per-organization or per-group basis

Open pafflu opened this issue 6 years ago • 10 comments

Note: If your question is regarding the AWS Amplify Console service, please log it in the official AWS Amplify Console forum

Which Category is your question related to? Storage and Auth

Amplify CLI Version 4.13.4

What AWS Services are you utilizing? Cognito, S3

Provide additional details e.g. code snippets I'm currently building an application with multiple users divided across multiple (and indefinitely many) organizations. Users need to be able to access data related to their organization and none other. I am avoiding Coginto User Groups as they do not seem to be made for indefinetly many groups or organizations.

In my Graphql API this was pretty straight-forward to solve, using dynamic group auth with a custom groupClaim that I populate in a pretoken lambda.

When it comes to S3, however, I haven't quite figured out how to protect resources but allow all users of one organization to access these shared files. I have only found documentation on how to do so with static Cognito User Groups or general public/private/protected access on a per-user basis instead of a per-organization basis.

Any help or ideas?

pafflu avatar Feb 25 '20 22:02 pafflu

@pafflu could you give some more details about your use case of using indefinitely many organizations? Its not clear to me why Cognito User Pools would not be a good choice here - you would still be able to add other groups with the right access levels at any time

nikhname avatar Feb 26 '20 01:02 nikhname

@nikhname Thanks for the quick response! It's a multi-tenant web-app for small enterprises, meaning users will register and belong to one company (and in larger companies sub-divided into different departments). I figured I'd rather manage this multi-tenant situation without User Groups, especially because they reach a hard limit at 500 per user pool as far as I know, and creating an entirely new pool for each company seems like a massive bother, too.

I currently use a pretoken lambda to stick the user's company id into their token that I then use when querying the API, and it works like a charm! It's just that I don't yet understand how to solve the same issue on the Storage side of things.

Any advice you can give me?

pafflu avatar Feb 26 '20 01:02 pafflu

I am facing the same problem.

ricsirigu avatar Feb 27 '20 09:02 ricsirigu

+1. Relevant comment about this that I posted in June in the @auth RFC: https://github.com/aws-amplify/amplify-cli/issues/1043#issuecomment-501462433

hisham avatar Feb 27 '20 17:02 hisham

@pafflu @hisham @ricsirigu I had the same need as what you are describing (I believe) and solved it using S3 pre-signed URLs. You can read about my problem and solution here: https://forums.aws.amazon.com/thread.jspa?threadID=317864

@hisham the @auth directive in the graphql schema is just for auth rules to the GraphQL API and doesn't have anything to do with auth to S3 objects. Remember amplify cli just stubs out CloudFormation templates for you and you can modify them (except for the api category) to fit your needs.

paulsson avatar Mar 05 '20 21:03 paulsson

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 09 '20 01:05 stale[bot]

The current built in way of accomplishing this is to leverage User Pool Groups which will set appropriate IAM policy for your bucket locations: https://docs.amplify.aws/cli/auth/groups

Will mark this as a feature request for alternative solutions in the future.

undefobj avatar May 09 '20 02:05 undefobj

The current built in way of accomplishing this is to leverage User Pool Groups which will set appropriate IAM policy for your bucket locations: https://docs.amplify.aws/cli/auth/groups

Will mark this as a feature request for alternative solutions in the future.

How does that give users from a group access to the files uploaded by other users within the group?

I'm not getting this. :) If userA private file it will be under: "/myPrivatePrefix/${cognito-identity.amazonaws.com:sub}/*". UserB does not know the sub of userA. And even if he did, how would you compose the IAM policy?

Nxtra avatar Apr 08 '21 15:04 Nxtra

cc @palpatim @renebrandel @swaminator for visibility

josefaidt avatar Mar 16 '22 18:03 josefaidt

A workaround for this use case would be to leverage Cognito’s custom attributes for managing multi-tenancy.

  1. Configure tenant custom attribute for access control on the identity pool using steps mentioned here.
  2. Next, configure the following auth role override to include a custom bucket policy based on on tenant principal tag

amplify override project

export function override(resources: AmplifyRootStackTemplate) {
    const authRole = resources.unauthRole;

    const basePolicies = Array.isArray(authRole.policies)
      ? authRole.policies
      : [authRole.policies];
  
    authRole.policies = [
      ...basePolicies,
      {
        policyName: "amplify-multi-tenant-custom-attribute-policy",
        policyDocument: {
          Version: "2012-10-17",
          Statement: [
            {
              Resource: "arn:aws:s3:::<bucketname>/${aws:PrincipalTag/tenant}/*", //resource to permission, needs to be restricted for according to scope
              Action: ['s3:ListBucket', 's3:PutObject', 's3:GetObject', 's3:DeleteObject'], //permissions
              Effect: "Allow", //action allow or deny
            },
          ],
        },
      },
    ];
    // policy to edit trust policy of authRole to allow sts:TagSession for attribute based access control
    authRole.assumeRolePolicyDocument = {
      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": "Identity pool ID"
              },
              'ForAnyValue:StringLike': {
                  "cognito-identity.amazonaws.com:amr": "authenticated"
              }
         }
  }]
}
}
  1. We then configure the default public, protected & private prefixes to empty strings so that these prefixes does not get appended based on our auth configuration -
Amplify.configure(awsconfig);
Storage.configure({
  customPrefix: {
      public: '',
      protected: '',
      private: ''
  }
  1. Lastly, while making get/put requests from front-end we can get custom attribute value using Auth.currentAuthenticatedUser() and make request - const result = await Storage.put("<tenant>/test.txt", "Hello");

sushpatg avatar Apr 22 '23 05:04 sushpatg