Closed loganpowell closed 2 years ago
@loganpowell Facing the same issue (even after migration to v2)
Here is a reduced schema that reproduces the issue:
input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!
type Todo @model {
id: ID! @index(name: "gsi", queryField: "gsiQueryField", sortKeyFields: ["createdAt", "foobar"])
createdAt: AWSDateTime
foobar: ID
}
The createTodo
mutation will fail with the error mentioned in the OP if the foobar
field is provided, but createdAt
is not. The reason is because the Mutation.createTodo.preAuth.1.req.vtl
file only checks the inputs, and not any default/auto-computed values.
Marking as a bug. Thanks for reporting this.
Might be related: composite keys key1#key2
also cause models with field-level auth to fail interactions. Facing this after a migration from V1 to V2
E.g.
type StrippedExample @model
@auth(rules: [
{allow: owner, operations: [create, update, read, delete]},
{allow: owner, identityClaim: "custom:teamId", ownerField: "groupOwner", operations: [create, update, read, delete]},
{allow: private, provider: iam, operations: [create, update, read, delete]}
])
{
id: ID! @primaryKey
startTime: AWSDateTime! @index(name: "ByStartTime")
status: Status! @index(name: "ByStatus", queryField: "ByStatus", sortKeyFields: ["vmId", "startTime"]) @index(name: "ByStatusUpdatedAt", queryField: "ByStatusUpdatedAt", sortKeyFields: ["updatedAt"])
vmId: String!
runnerId: ID @auth(rules: [
{allow: owner, operations: [read]},
{allow: owner, identityClaim: "custom:teamId", ownerField: "groupOwner", operations: [read]},
{allow: private, provider: iam, operations: [create, update, read, delete]}
])
updatedAt: AWSDateTime!
owner: String @index(name: "ByOwner")
groupOwner: String @index(name: "ByGroupOwner", queryField: "ByGroupOwner")
}
Configures the composite field in Mutation.createStrippedExample.preAuth.2.req.vtl
...
#if( $hasSeenSomeKeyArg )
$util.qr($ctx.args.input.put("vmId#startTime","${mergedValues.vmId}#${mergedValues.startTime}"))
#end
...
But doesn't include it in the allowedFields In Mutation.createStrippedExample.auth.1.req.vtl
#set( $inputFields = $util.parseJson($util.toJson($ctx.args.input.keySet())) )
...
#set( $ownerAllowedFields0 = ["id","startTime","status","vmId","updatedAt","owner","groupOwner","_version","_deleted","_lastChangedAt"] )
Which then causes the graphql interaction to fail:
message: "Unauthorized on [vmId#startTime]"
From the later bit of Mutation.createStrippedExample.auth.1.req.vtl
...
#set( $deniedFields = $util.list.copyAndRemoveAll($inputFields, $allowedFields) )
#if( $deniedFields.size() > 0 )
$util.error("Unauthorized on ${deniedFields}", "Unauthorized")
#end
...
I suspect both issues result from composite fields being mishandled in some way; the pipeline resolver seems to add them to $ctx
but forget them later.
Man this is still an issue occuring now !
Before opening, please confirm:
How did you install the Amplify CLI?
npm
If applicable, what version of Node.js are you using?
14.18.1
Amplify CLI Version
7.6.14
What operating system are you using?
Windows 10
Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.
No manual changes made
Amplify Categories
api
Amplify Commands
Not applicable
Describe the bug
When I have a composite index made with two sort keys, specifically
createdAt
, though this should be automatically handled by a PR made last year by Amplify, when I try to run a mutation, I'm required to add thecreatedAt
field to the query.I've set up a simple example repo that seems to work without having to require the
createdAt
key, but doesn't work in my project, which I'm currently trying to update to the v2 of the transformer.... perhaps that's the problem, IDK :(I'm sorry I can't seem to reproduce the but using a simpler example.
Expected behavior
When using a composite sort key with
createdAt
, running a graphql mutation doesn't require it (being autogenerated by the appsync resolvers).Reproduction steps
I'm sorry, but I tried to reproduce this with a simple Todo example, but it works there...
GraphQL schema(s)
Log output
Additional information
No response