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

Implement permission policies in the API #22384

Draft
wants to merge 224 commits into
base: auditus
Choose a base branch
from
Draft

Conversation

rijkvanzanten
Copy link
Member

@rijkvanzanten rijkvanzanten commented May 3, 2024

Scope

What's changed:

  • Implements policy-system based permissions handling on the API
  • Replaces AuthorizationService with new set of functions in the api/src/permissions folder
  • Allows roles to be nested
  • Adds new roles flag to accountability object. This is an ordered array of all the parent roles of the current user
  • Cleans up get-ast-from-query by splitting it into multiple files
  • Permissions are now injected into the AST through cases and whenCase. This allows us to dynamically generate the case/when SQL to have dynamic field output per item.
  • Cleans up run-ast by splitting it up into smaller files

Potential Risks / Drawbacks

  • The risks are very high. This replaces the full permissions system, and thus needs a lot of testing

Review Notes / Questions

  • This PR now compiles, but doesn't run yet.
  • There was some weird logic happening in the users controller for TFA enable/disable that I'm not sure we need to keep. Needs a bit more testing

Todos

  • Add permissions processing for $CURRENT_USER etc flags
    • Introduce $CURRENT_POLICIES and $CURRENT_ROLES for permissions
    • Decide if $CURRENT_POLICIES and $CURRENT_ROLES should be available in presets as well
  • Add permissions merging when you're accessing from a share
  • Add caching to:
    • Fetching Policies
    • Fetching permissions
    • Fetching the roles tree
    • Fetching the field map
    • Fetching allowed fields
    • Fetching allowed collections
  • Enable validation for admin users for wrong paths
  • Figure out what we want to do with Presets
  • Use whenCases in run-ast
  • Use applyCases in Meta Service for permissions aware counts
  • Handle admin-checks in roles and users services1 -> Handled in Rework user integrity checks for Auditus #22737
  • Add unit testing for clear method in memory/cache
  • Make sure graphql gracefully handles optional fields when you have different fields for the same collection in multiple permission sets
  • Add changeset
  • Unbork /permissions endpoint
  • Invalidate cache on permission changes
    • On directus_access changes
    • On directus_roles changes
    • On directus_permissions changes
    • On directus_policies changes
  • Bring back check-ip middlewares?
  • Fix down migration
  • Fix telemetry user counting
  • Fix user limit checking
  • Reduce accountability used in the withCache to the known keys
  • Account for user IP in the global request cache
  • Make sure that the globalAccess for a public user always has app_access and admin_access false
  • Add new collections to GraphQL
  • Add new collections to websockets
  • Add new endpoints + types to SDK
  • Before merging this one in main, either retarget Add roles and permissions to the app #22654 to main, or merge it in here

Closes #21778, closes #21765, closes #22163, closes #21769, closes #21768, closes #21767, closes #21766

Footnotes

  1. Eg check to make sure there's still >=1 admin left after the mutation is done

Comment on lines +12 to +14
throw new ForbiddenError({
reason: `You don't have permission to access collection "${collection}" or it does not exist. Queried in ${pathSuffix}.`,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though this, and the other errors are really helpful, they do expose that the collection + field exists, as it differs from the standard ForbiddenError in case of a non-existent collection.

hanneskuettner and others added 7 commits June 6, 2024 17:14
…us-21765

# Conflicts:
#	api/src/services/users.ts
#	api/src/telemetry/lib/get-report.test.ts
#	api/src/telemetry/lib/get-report.ts
#	api/src/telemetry/utils/get-user-count.test.ts
#	api/src/telemetry/utils/get-user-count.ts
# Conflicts:
#	api/src/services/users.ts
api/src/app.test.ts Show resolved Hide resolved
api/src/cli/commands/init/index.ts Outdated Show resolved Hide resolved
router.search('/', validateBatch('read'), readHandler, respond);

router.get(
'/me/flags',
Copy link
Member

Choose a reason for hiding this comment

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

Would just /me work? 👀

Comment on lines 86 to 88
const query = { ...req.sanitizedQuery, limit: -1 };

const roles = await service.readMany(req.accountability.roles, query);
Copy link
Member

Choose a reason for hiding this comment

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

Should we add the same comment as per other usages of this limit: -1 query?

Suggested change
const query = { ...req.sanitizedQuery, limit: -1 };
const roles = await service.readMany(req.accountability.roles, query);
// TODO fix this at the service level
const temporaryQuery = { ...req.sanitizedQuery, limit: -1 };
const roles = await service.readMany(req.accountability.roles, temporaryQuery);

if (this.accountability?.admin) {
return mapValues(this.schema.collections, () =>
Object.fromEntries(
['create', 'read', 'update', 'delete', 'share'].map((action) => [
Copy link
Member

Choose a reason for hiding this comment

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

Could we extract this array as a constant? This array will be used in the App as well.

Comment on lines +86 to +89
const query = { ...req.sanitizedQuery, limit: -1 };

try {
const roles = await service.readMany(req.accountability.roles, query);
Copy link
Member

Choose a reason for hiding this comment

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

Should we include this comment about it being temporary to be consistent with other usages?

Suggested change
const query = { ...req.sanitizedQuery, limit: -1 };
try {
const roles = await service.readMany(req.accountability.roles, query);
// TODO fix this at the service level
const temporaryQuery = { ...req.sanitizedQuery, limit: -1 };
try {
const roles = await service.readMany(req.accountability.roles, temporaryQuery);

hanneskuettner and others added 2 commits June 10, 2024 13:26
Co-authored-by: Hannes Küttner <4376726+hanneskuettner@users.noreply.github.com>
Copy link
Member

Choose a reason for hiding this comment

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

Should this file be run-ast.ts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🏗 In progress
Development

Successfully merging this pull request may close these issues.

None yet

7 participants