Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mapping between (team) permissions and roles more lenient. #1711

Merged
merged 7 commits into from
Aug 20, 2021

Conversation

fisx
Copy link
Contributor

@fisx fisx commented Aug 18, 2021

shouldn't change behavior for any existing users, but i found a user in our staging env that has an unknown set of permissions, and for that user is should fix https://wearezeta.atlassian.net/browse/SQSERVICES-720.

I've explained things in comments in the code, where we'll be able to find it in the future.

Checklist

  • The PR Title explains the impact of the change.
  • The PR description provides context as to why the change should occur and what the code contributes to that effect. This could also be a link to a JIRA ticket or a Github issue, if there is one.
  • If end-points have been added or changed: the endpoint / config-flag checklist (see Wire-employee only backend wiki page) has been followed.
  • If a schema migration has been added, I ran make git-add-cassandra-schema to update the cassandra schema documentation.
  • Section Unreleased of CHANGELOG.md contains the following bits of information:
    • A line with the title and number of the PR in one or more suitable sub-sections.
    • If /a: measures to be taken by instance operators.
    • If /a: list of cassandra migrations.
    • If public end-points have been changed or added: does nginz need upgrade?
    • If internal end-points have been added or changed: which services have to be deployed in a specific order?

@fisx fisx changed the title Fix mapping between (team) permissions and roles. Make mapping between (team) permissions and roles more lenient. Aug 18, 2021
@fisx fisx force-pushed the fix-permission-role-mapping branch 2 times, most recently from f8743c5 to a912a44 Compare August 18, 2021 16:43
Copy link
Contributor

@arianvp arianvp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matthias says this doesn't work

@fisx fisx force-pushed the fix-permission-role-mapping branch from a912a44 to 4e9494f Compare August 19, 2021 15:45
@fisx fisx requested a review from arianvp August 19, 2021 15:46
@fisx
Copy link
Contributor Author

fisx commented Aug 19, 2021

now it does. i think.

-- we never did use @p /= p'@ for anything, fingers crossed that it doesn't occur anywhere
-- in the wild. but if it does, this implementation prevents privilege escalation.
let p'' = Set.intersection p p'
in permissionsRole (Permissions p'' p'')
permissionsRole (Permissions p _) = permsRole p
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 152 and line 158 have overlapping patterns. Will haskell actually fall back when the guard doesn't match? or would we have to move line 158 into an | otherwise = ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does fall back, you've hit on something, this here is actually more clear and robust: f12e85c

permissionsRole (Permissions p _) = permsRole p
where
permsRole :: Set Perm -> Maybe Role
permsRole perms =
Maybe.listToMaybe
[role | role <- [minBound ..], rolePerms role == perms]
[ role
| (perms', role) <- reverse . sortBy (compare `on` (length . fst)) $ (\r -> (rolePerms r, r)) <$> [minBound ..],
Copy link
Contributor

@arianvp arianvp Aug 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sortBy (compare `on` (length . fst))   -> sortOn (length . fst)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

permissionsRole (Permissions p _) = permsRole p
where
permsRole :: Set Perm -> Maybe Role
permsRole perms =
Maybe.listToMaybe
[role | role <- [minBound ..], rolePerms role == perms]
[ role
| (perms', role) <- reverse . sortBy (compare `on` (length . fst)) $ (\r -> (rolePerms r, r)) <$> [minBound ..],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused why we're sorting on the length of the permission set? does that really do what we want? We can have two permission sets that are the same size; but we must consider one permission set strictly less permissive than another (Perhaps based on the ordering of Perm ?)

e.g. the rolePerms for RoleAdmin and RoleMember have the same length; they both contain 4 elements; but RoleMember <= RoleAdmin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we sort by the natural ordering of Role ? e.g. just iterate over [minBound ..]

So:

[ role |  role <- [minBound..],  rolePerms role `Set.isSubsetOf` perms]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, in this particular case it's the same, and the tests will show if it changes: c67cd00

@fisx fisx requested a review from arianvp August 20, 2021 11:24
@fisx fisx requested a review from arianvp August 20, 2021 11:57
@fisx fisx merged commit 5b5fe77 into develop Aug 20, 2021
@fisx fisx deleted the fix-permission-role-mapping branch August 20, 2021 12:22
@akshaymankar akshaymankar mentioned this pull request Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants