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

Dynamic Group Authorization for subscriptions

Open simholte opened this issue 6 years ago • 7 comments

Is your feature request related to a problem? Please describe. Currently subscriptions only support static group authorization. I'd like to be able to extend that to use Dynamic Groups as well.

Describe the solution you'd like I'd like to use the Dynamic Group authorization for subscriptions so that I can subscribe to my data using the same auth directives as query and mutate. For example the below statement should authorize groups in that column for queries, mutations and subscriptions. { allow: groups, groupsField: "userGroups"}

Describe alternatives you've considered Turning authorization off or manually overriding the resolvers is how I've gotten around the problem so far.

Additional context It looks like a simple change to add it, I'm assuming there must have been a reason to omit Dynamic Groups?

simholte avatar Feb 15 '20 19:02 simholte

Hi @simholte , I'm also stuck with this. How do you override the resolvers to get around this problem?

ricsirigu avatar Feb 22 '20 16:02 ricsirigu

Hey @ricsirigu , I haven't implemented it yet as my apps not yet in production and I can live with no authentication but I think I can create custom subscriptions with my own resolvers. Planning on using the cli to generate the resolvers, move them to the /resolvers folder as my new custom name, modify them to check dynamic groups and do something like this https://aws-amplify.github.io/docs/cli-toolchain/graphql?sdk=js#add-a-custom-resolver-that-targets-a-dynamodb-table-from-model to get the new subscriptions into the stack. It's basically creating new subscriptions with custom resolvers that you can control. I think that should work :)

simholte avatar Feb 22 '20 19:02 simholte

@simholte mmmh I don't think it's possible. I'm writing a custom resolver but there is no $ctx.result at subscription time, so the following assignments #set( $allowedGroups = $util.defaultIfNull($ctx.result.groupCanAccess, []) ) results in an empty list.

I'm stuck right now :(

EDIT: I think that passing groupCanAccess as a parameter to the subscription request should do the trick

onUpdateAlert(owner: String, groupCanAccess: String): Alert @aws_subscribe(mutations: ["updateAlert"])

ricsirigu avatar Feb 27 '20 15:02 ricsirigu

@ricsirigu I finally got a chance to look into this, from what I can tell from how the other subscriptions work dynamic authorization isn't possible. For example when you use static groups those things are known at the time the socket connection is established, if the user isn't in one of those static groups the resolver returns unauthorized. So basically they're saying that in order to establish connection to the socket you must be authorized to receive all that data. In order to change that they would have to allow all user to connect and only send data they should have. Not sure if that's good or bad... I think the direction they are pushing and how I solved it is to use subscription filters https://docs.aws.amazon.com/appsync/latest/devguide/real-time-data.html. This link really helped me a lot as well https://github.com/aws-amplify/amplify-cli/issues/3047

I have a table named Asset, customers own assets (Asset table has a column named customerId) and I need to limit subscriptions to the assets that customer owns. So in my schema.graphql file I added: type Subscription { onUpdateAsset(customerId: ID!): Asset @aws_subscribe(mutations: ["updateAsset"]) ... }

I call it with API.graphql(graphqlOperation(subscriptions.onCreateAsset, {customerId: customerId})).subscribe

It worked right out of the box with no custom resolvers or cloudformation updates. I plan on adding a custom resolver that validates that the user who's making the request is a member of the appropriate group to close the security hole and validate the user can perform this action. Looks like you were on a similar path as I ended up being on.

simholte avatar Mar 21 '20 01:03 simholte

@ricsirigu I finally got a chance to look into this, from what I can tell from how the other subscriptions work dynamic authorization isn't possible. For example when you use static groups those things are known at the time the socket connection is established, if the user isn't in one of those static groups the resolver returns unauthorized. So basically they're saying that in order to establish connection to the socket you must be authorized to receive all that data. In order to change that they would have to allow all user to connect and only send data they should have. Not sure if that's good or bad... I think the direction they are pushing and how I solved it is to use subscription filters https://docs.aws.amazon.com/appsync/latest/devguide/real-time-data.html. This link really helped me a lot as well https://github.com/aws-amplify/amplify-cli/issues/3047

In case anyone else stumbles across this for example: I have a table named Asset, customers own assets (Asset table has a column named customerId) and I need to limit subscriptions to the assets that customer owns. So in my schema.graphql file I added: type Subscription { onUpdateAsset(customerId: ID!): Asset @aws_subscribe(mutations: ["updateAsset"]) ... }

I call it with API.graphql(graphqlOperation(subscriptions.onCreateAsset, {customerId: customerId})).subscribe

It worked right out of the box with no custom resolvers or cloudformation updates. I plan on adding a custom resolver that validates that the user who's making the request is a member of the appropriate group to close the security hole and validate the user can perform this action. Looks like you were on a similar path as I ended up being on.

Closing this as filtering appears to be the preferred way.

simholte avatar Mar 22 '20 13:03 simholte

Hey @simholte , I'm curently looking into dynamic group auth for subscriptions and your solutions seems to be the only one that could really work. I was wondering whether you've found a good way to make a custom resolver for your filtered subscriptions and if you can provide some guidance on it?

pafflu avatar Apr 01 '21 12:04 pafflu

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 24 '21 21:05 github-actions[bot]