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

Allow public IAM @auth on `@function` outside of `@model`

Open patspam opened this issue 7 years ago • 8 comments

Describe the bug Allow public IAM @auth on @function outside of @model

Amplify CLI Version 3.17.0

To Reproduce

  • Use IAM as default auth provider for GraphQL
  • Attempt to make a @function public outside of a @model, eg.
type Query {
  getStripeClientSecret: String
    @function(name: "foo-${env}")
    @auth(rules: [{ allow: public, provider: iam }])
}
  • Try to query:
query {
  getStripeClientSecret
}

Gives error:

{
  "data": {
    "getStripeClientSecret": null
  },
  "errors": [
    {
      "path": [
        "getStripeClientSecret"
      ],
      "data": null,
      "errorType": "Unauthorized",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Not Authorized to access getStripeClientSecret on type Query"
    }
  ]
}

If I edit the resolver, I see this stanza:

## [Start] Throw if unauthorized **
#if( !($getStripeClientSecret_isStaticGroupAuthorized == true || $isDynamicGroupAuthorized == true || $isOwnerAuthorized == true) )
  $util.unauthorized()
#end
## [End] Throw if unauthorized **

Commenting out $util.unauthorized() causes the query to succeed.

Expected behavior Query succeeds.

Additional context

  • I have been using API_KEY during development, but now want to lock down access to my API (I'm a week away from launch).
  • An unrelated Amplify CLI bug is preventing me from switching to Cognito (#2076) - that bug hasn't moved since August so I'm trying to find a way to get unblocked in time for launch (hence IAM)

patspam avatar Nov 11 '19 02:11 patspam

@patspam, https://github.com/aws-amplify/amplify-cli/pull/2607 enabled this scenario, but it seems to lack the support for multiple authorization types, we'll take a look, thanks for reporting!

attilah avatar Nov 11 '19 10:11 attilah

I may be mistaken, but could you add to your API's CustomResources.json like so:

	"Parameters": {
		"authRoleName": {
			"Type": "String"
		},
		"unauthRoleName": {
			"Type": "String"
		},
       ....
       },
       ....
	"Resources": {
		"AuthRolePolicy": {
			 "Type": "AWS::IAM::Policy",
			 "Properties": {
					 "PolicyName": "appsync-authrole-policy-custom",
					 "Roles": [
							 {
									 "Ref": "authRoleName"
							 }
					 ],
					 "PolicyDocument": {
							 "Version": "2012-10-17",
							 "Statement": [
									 {
											 "Effect": "Allow",
											 "Action": ["appsync:GraphQL"],
											 "Resource": [
													 {
															 "Fn::Sub": [
																	 "arn:aws:appsync:${AWS::Region}:${AWS::AccountId}:apis/${apiId}/types/${typeName}/fields/${fieldName}",
																	 {
																			 "apiId": {
																					 "Ref": "AppSyncApiId"
																			 },
																			 "typeName": "Query",
																			 "fieldName": "getStripeClientSecret"
																	 }
															 ]
													 },
...

and

	"Resources": {
		"UnauthRolePolicy": {
			 "Type": "AWS::IAM::Policy",
			 "Properties": {
					 "PolicyName": "appsync-unauthrole-policy-custom",
					 "Roles": [
							 {
									 "Ref": "unauthRoleName"
							 }
					 ],
					 "PolicyDocument": {
							 "Version": "2012-10-17",
							 "Statement": [
									 {
											 "Effect": "Allow",
											 "Action": ["appsync:GraphQL"],
											 "Resource": [
													 {
															 "Fn::Sub": [
																	 "arn:aws:appsync:${AWS::Region}:${AWS::AccountId}:apis/${apiId}/types/${typeName}/fields/${fieldName}",
																	 {
																			 "apiId": {
																					 "Ref": "AppSyncApiId"
																			 },
																			 "typeName": "Query",
																			 "fieldName": "getStripeClientSecret"
																	 }
															 ]
													 },
...

warrenmcquinn avatar Nov 11 '19 15:11 warrenmcquinn

I’ve hit more or less the exact same scenario and I’m also hoping to see improvements to this soon. I did try adding the configuration in CustomResources.json but I did not add it for both unAuthRole and authRole as it was not described in that issue comment, so it didn’t work.

houmark avatar Nov 12 '19 06:11 houmark

I have the same problem, any news about this?

jorge-riberi avatar Dec 02 '19 17:12 jorge-riberi

+1 same for private IAM and API Key.

dtelaroli avatar Feb 14 '20 23:02 dtelaroli

any news?

MontoyaAndres avatar Jun 24 '20 19:06 MontoyaAndres

Thanks for the fix @attilah. 🙏 Trying it out I have a question.

What I did

I implemented a very simple query on root level.

# schema.graphql

type Greeting @aws_iam {
  greeting: String!
}

type Query {
  getGreeting: Greeting!
    @function(name: "graphQLResolversFunction-${env}")
    @auth(rules: [{ allow: public, provider: iam }])
}

BTW: The returned string is "Pura Vida!". 😉

CLI Behaviour

When I did an amplify push using the CLI I get a warning.

Be careful when using @auth directives on a field in a root type. @auth directives on field definitions use the source object to perform authorization logic and the source will be an empty object for fields on root types. Static group authorization should perform as expected.

My questions

  • Am I doing something wrong?
  • Should I do it differently?
  • Or can I safely ignore the warning? (But at what risk?)

tobiasriemenschneider avatar May 15 '21 05:05 tobiasriemenschneider

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 Jun 14 '21 06:06 github-actions[bot]