Allow public IAM @auth on `@function` outside of `@model`
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
@functionpublic 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, 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!
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"
}
]
},
...
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.
I have the same problem, any news about this?
+1 same for private IAM and API Key.
any news?
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?)
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.