loopback 3.x | ACL not working
I have a derived 'Person' model from the in-built User model. But for some reason whenever I am accessing GET: /Person
It's saying 401 | Authentication required.
Things I've done:
- Added a custom role as per the documentation, "admin"
- My Person model's ACL config is:
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$admin",
"permission": "ALLOW",
"property": "find"
}
- In Debug mode, i tried to analyze the loopback acl resolver's output:
loopback:security:role isInRole(): $everyone +20s
loopback:security:access-context ---AccessContext--- +0ms
loopback:security:access-context principals: +0ms
loopback:security:access-context principal: {"type":"USER","id":"588c0a0cb4228040ec51ed20"} +0ms
loopback:security:access-context modelName Person +1ms
loopback:security:access-context modelId undefined +0ms
loopback:security:access-context property find +0ms
loopback:security:access-context method find +0ms
loopback:security:access-context accessType READ +0ms
loopback:security:access-context accessToken: +0ms
loopback:security:access-context id "56qnbCovt2uGDwk3wXBObi9GfceZn27t254Yf07A4xllcqUbEQHFD4fYN0HSCfj0" +0ms
loopback:security:access-context ttl 1209600 +0ms
loopback:security:access-context getUserId() 588c0a0cb4228040ec51ed20 +0ms
loopback:security:access-context isAuthenticated() true +0ms
loopback:security:role Custom resolver found for role $everyone +0ms
loopback:security:role isInRole(): $admin +0ms
loopback:security:access-context ---AccessContext--- +0ms
loopback:security:access-context principals: +0ms
loopback:security:access-context principal: {"type":"USER","id":"588c0a0cb4228040ec51ed20"} +0ms
loopback:security:access-context modelName Person +0ms
loopback:security:access-context modelId undefined +1ms
loopback:security:access-context property find +0ms
loopback:security:access-context method find +0ms
loopback:security:access-context accessType READ +0ms
loopback:security:access-context accessToken: +0ms
loopback:security:access-context id "56qnbCovt2uGDwk3wXBObi9GfceZn27t254Yf07A4xllcqUbEQHFD4fYN0HSCfj0" +0ms
loopback:security:access-context ttl 1209600 +0ms
loopback:security:access-context getUserId() 588c0a0cb4228040ec51ed20 +0ms
loopback:security:access-context isAuthenticated() true +0ms
loopback:security:role Role found: {"id":"588c0a0cb4228040ec51ed21","name":"$admin","created":{},"modified":{}} +4ms
loopback:security:role Role mapping found: null +5ms
loopback:security:role isInRole() returns: null +1ms
loopback:security:acl The following ACLs were searched: +0ms
loopback:security:acl ---ACL--- +0ms
loopback:security:acl model Person +0ms
loopback:security:acl property * +0ms
loopback:security:acl principalType ROLE +0ms
loopback:security:acl principalId $everyone +0ms
loopback:security:acl accessType * +0ms
loopback:security:acl permission DENY +0ms
loopback:security:acl with score: 7495 +0ms
loopback:security:acl ---Resolved--- +0ms
loopback:security:access-context ---AccessRequest--- +0ms
loopback:security:access-context model Person +0ms
loopback:security:access-context property find +1ms
loopback:security:access-context accessType READ +0ms
loopback:security:access-context permission DENY +0ms
loopback:security:access-context isWildcard() false +0ms
loopback:security:access-context isAllowed() false +0ms
I was able to figure out that this has something to do with #2270
But even there, I couldn't find a solution to what should I do in order for loopback role resolver to give higher priority to my custom role.
same here
I think I figured this out last week I believe there was an equals some where that's comparing the role mapping principalType of 'Role' against the defined acl accessType of 'ROLE' and gets filtered out some where.
I also think there is an issue with https://github.com/strongloop/loopback/blob/master/common/models/acl.js#L114 as it's got an || ALL so for the acls that don't have a property set like https://github.com/strongloop/loopback/blob/master/common/models/user.json#L29 so both the ruleValue and requestedValue get set to ALL which adds to the score amount
I got the same problem
Any word on a fix for this or a work around?
hi, could anyone provide a sandbox reproducing the issue, it would greatly ease the troubleshooting? (do not forget to update the dependancies to make sure it matches your setup)
🖐 Looking at the logs: i can see
loopback:security:role Role found: {"id":"588c0a0cb4228040ec51ed21","name":"$admin","created":{},"modified":{}} +4ms
loopback:security:role Role mapping found: null +5ms
loopback:security:role isInRole() returns: null +1ms
As the role $admin is found, i assume a static role mapping is used here, and not a dynamic role resolver.
The log clearly states that no role mapping was found for the given Person instance and the role $admin : did you create this mapping?
The situation shown in the logs seems not related to ACL scoring as suggested in some comments, you can see the ACL is not even scored as the user is not considered to have the $admin role
@ebarault Yes, I've used a static role mapping here. But as I'm writing this, I'm thinking I should try using a dynamic role resolver, although is the the ideal way to implement this?
Meanwhile, I'm creating a sandbox of this issue and will add it in the comments for everyone to go through.
Thanks.
@akshayKrSingh Usually for an admin role a role mapping is the best solution. Let's wait for your sandbox.
@ebarault Created a repo with the issue, Sandbox. Although, the issue is no longer reproducible after upgrading the packages, unless somebody is able to reproduce this issue I'm closing this.
FYI for future reference: as @ebarault mentioned in his comments -
do not forget to update the dependancies
That should solve the issue, in case anyone is facing the same problem.
@ebarault @akshayKrSingh I'm also facing the same issue. Couldn't repro using your Sandbox. I verified that my dependencies are up to date. The only difference I can find is that I'm using MongoDB.
I could track down the issue to https://github.com/strongloop/loopback/blob/master/common/models/role.js#L445 where roleMappingModel.findOne cannot find the RoleMapping instance even though I can see it in my database. Any thoughts? Thanks
UPDATE: Changed my datasource to In Memory and custom roles works fine. Looks like the issue exists using MongoDB
@hyperd : are you able to reproduce the issue in a sandbox using mongodb as the db backend?
@ebarault Looks like my application was created with an older version of lb which doesn't automatically set strictObjectIDCoercion for RoleMapping model. After adding it to model-config.json roles started working fine. Thanks for chiming in!
@akshayKrSingh what dependencies did you update?
@akshayKrSingh figured that out. Seems instantiating the project with the loopback cli installs with very outdated versions of all required lb related dependencies.
@kodepareek yup.
I have the same issue, did someone figured out the fix?
@sisardor this workaround can help https://gist.github.com/spencermefford/bc73812f216e0e254ad1