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

[AC-2084] Include Collection permissions for admin endpoints #3793

Merged

Conversation

shane-melton
Copy link
Member

Type of change

- [ ] Bug fix
- [X] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

We’ve run into multiple places where we need the current user’s collection permissions after fetching them from the admin endpoints. Those responses do not include the manage, readonly, hidePasswords flags so we’ve been forced to merge the permissions from the collection sync data with the admin endpoint response multiple times.

This PR adds those permission details to those admin endpoint responses. Doing so required creating new SQL queries to fetch collections and permission information even if there is no relationship (all existing queries that included permission info automatically filter out collections that have no existing User/Group relationship).

Code changes

New CollectionAdminDetails Model

Add a new Domain model that represents collection details that are intended to be used for collection management/administrative purposes. The model includes permission details for a given user, a flag for if that user is explicitly assigned to the collection, and optional lists for User/Group access relationships.

ICollectionRepository.cs

Add additional documentation to existing methods and add the two new *WithPermissions queries. By their inherent nature, these queries do not perform our normal database level authorization checks, so care should be taken to ensure the requesting user has access to the organization. (Our existing queries quietly remove any collection resources the user does not have access to.)

  • GetManyByOrganizationIdWithPermissionsAsync()
    • Fetches all of collections that belong to an organization, with permission details for a provided userId. Can also optionally include User/Group access relationships.
  • GetByIdWithPermissionsAsync()
    • Fetches a single collection by Id with permission details for the provided userId. Can also optionally include User/Group access relationships.

Before you submit

  • Please check for formatting errors (dotnet format --verify-no-changes) (required)
  • If making database changes - make sure you also update Entity Framework queries and/or migrations
  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team

@shane-melton shane-melton requested review from a team as code owners February 13, 2024 01:30
@shane-melton shane-melton changed the title Vault/ac 2084/include permissions for admin endpoints [AC-2084] Include Collection permissions for admin endpoints Feb 13, 2024
@shane-melton shane-melton marked this pull request as draft February 13, 2024 02:16
@bitwarden-bot

This comment was marked as off-topic.

- vNext endpoints should now always return CollectionDetailsResponse models
- Update constructors in CollectionDetailsResponseModel to be more explicit and add named static constructors for additional clarity
Copy link

codecov bot commented Feb 15, 2024

Codecov Report

Attention: Patch coverage is 7.05521% with 303 lines in your changes are missing coverage. Please review.

Project coverage is 38.79%. Comparing base (c045739) to head (05e070b).

Files Patch % Lines
...tityFramework/Repositories/CollectionRepository.cs 0.00% 175 Missing ⚠️
...epositories/Queries/CollectionAdminDetailsQuery.cs 0.00% 62 Missing ⚠️
...ucture.Dapper/Repositories/CollectionRepository.cs 0.00% 53 Missing ⚠️
src/Api/Controllers/CollectionsController.cs 42.10% 9 Missing and 2 partials ⚠️
src/Api/Models/Response/CollectionResponseModel.cs 85.71% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3793      +/-   ##
==========================================
- Coverage   38.95%   38.79%   -0.17%     
==========================================
  Files        1214     1216       +2     
  Lines       58874    59169     +295     
  Branches     5632     5646      +14     
==========================================
+ Hits        22937    22955      +18     
- Misses      34882    35157     +275     
- Partials     1055     1057       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@shane-melton shane-melton marked this pull request as ready for review February 15, 2024 01:45
Copy link
Member

@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

Started looking at this today just to get my head around it. Will continue tomorrow.

Copy link
Member

@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

I've reviewed the SQL sprocs, models, and controller methods to make sure they're in line with our discussions and AC Team's requirements, but I'll leave the rest (mostly EF queries and test logic I think) to the Vault reviewer as codeowner.

Thank you again for tackling this!

src/Core/Repositories/ICollectionRepository.cs Outdated Show resolved Hide resolved
src/Api/Models/Response/CollectionResponseModel.cs Outdated Show resolved Hide resolved
src/Api/Controllers/CollectionsController.cs Outdated Show resolved Hide resolved
src/Api/Controllers/CollectionsController.cs Show resolved Hide resolved
src/Api/Controllers/CollectionsController.cs Outdated Show resolved Hide resolved
src/Api/Controllers/CollectionsController.cs Outdated Show resolved Hide resolved
@shane-melton shane-melton dismissed stale reviews from eliykat and Jingo88 via 8b1be46 April 25, 2024 08:32
eliykat
eliykat previously approved these changes Apr 26, 2024
Copy link
Member

@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

Love the tests, a couple of comments below but nothing blocking.

CreationDate = collectionGroup.Key.CreationDate,
RevisionDate = collectionGroup.Key.RevisionDate,
ExternalId = collectionGroup.Key.ExternalId,
ReadOnly = Convert.ToBoolean(collectionGroup.Min(c => Convert.ToInt32(c.ReadOnly))),
Copy link
Member

Choose a reason for hiding this comment

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

The conversions are unnecessary:

  • collectionGroup.All(c => c.ReadOnly) (equivalent of MIN, it will return false if there is any ReadOnly = false)
  • collectionGroup.Any(c => c.Manage) (equivalent of MAX, it will return true if there is any Manage = true)

However I can see this is how we do it in existing queries, and that does more closely track the SQL equivalent, so I'll leave it up to you as to which you prefer.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a fair point. I had gone with what already existed in the other queries, as I figured there was some underlying implementation detail forcing the extra conversions (e.g. maybe one provider doesn't behave properly with Any or All).

I think I'll opt to leave it alone for now since we know it works as expected and it keeps consistent with the other queries.


CollectionAdminDetails collectionDetails;

// SQLite does not support the GROUP BY clause
Copy link
Member

Choose a reason for hiding this comment

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

Boo :( If this is the case, should we just have the single implementation? I guess the Sqlite version here is arguably less performant because it doesn't use delayed execution. But in practice I'm not sure it would make a significant difference, and it avoids having divergent EF implementations for different databases. Would be interested in what @justindbaur thinks.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not too attached either way (a single implementation or split). We do have a few other repository calls here that have similar behavior, so its not entirely new, if still pretty ugly.

I'm also not too familiar with the magnitude of the performance difference, but I do expect this to be called fairly frequently in the AC until we have better caching there, so its something to keep in mind.

@shane-melton shane-melton enabled auto-merge (squash) May 2, 2024 22:13
@shane-melton shane-melton merged commit d965166 into main May 3, 2024
52 checks passed
@shane-melton shane-melton deleted the vault/ac-2084/include-permissions-for-admin-endpoints branch May 3, 2024 13:33
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

7 participants