Skip to content

Database-level access control #2309

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

Open
wants to merge 3 commits into
base: unstable
Choose a base branch
from

Conversation

JoBeR007
Copy link

@JoBeR007 JoBeR007 commented Jul 4, 2025

This PR is open for review. What is already done:

  • Implemented database permissions using db=<id>, alldbs, resetdbs ACL rules, for multiple dbids comma , is separating multiple IDs
  • Added SELECTOR_FLAG_ALLDBS flag
  • Updated SELECT, MOVE, SWAPDB, FLUSHDB commands with CMD_CROSS_DB flag and functions for getting database ID args, updated FLUSHALL with CMD_ALL_DBS flag
  • Extended ACL checks with database access verification
  • Added ACL_DENIED_DB error type for database permission violations
  • Added ACL_NOT_IMPLEMENTED error type for situation when command has CMD_CROSS_DB but has no function to get its database ID args.
  • MULTI/EXEC tracks SELECT logic and correctly determines access for each command while forming queue
  • Maintained backward compatibility with default access to all databases
  • Some tests (including MULTI/EXEC) are added but more tests are needed
  • Performance impact is minimal, profiling showed that only 0.03% of CPU time was spent for evaluating database level access thanks to intset's O(log(n)) search and ACL cache

Here are topics that need discussion:

  • [VM_API] Is it possible to expand expected params in VM_ACLCheckCommandPermissions to include dbid for proper permission checking?
  • [VM_API] Should we add db level check in VM_ACLCheckKeyPermissions and ACLUserCheckKeyPerm() respectively?

Resolved topics:

  • How to handle different positions of dbid arguments for CROSS_DB and ALL_DBS commands:
    1. Extended serverCommand with get_dbid_args and made ACL checks based on IDs provided by get_dbid_args.
  • [VM_API] A new API for checking if the user can access a DB will be added with New flags for CrossDB/All DBs.

Syntax examples:

ACL SETUSER test1 on +@all ~* db=0,1 nopass
ACL SETUSER test3 on (db=0,1 +@write +select ~*) (db=2,3 +@read +select ~*) nopass 

Closes: #1336

- Implemented database permissions using `db=<id>`, `db!=<id>`, `alldbs`, `resetdbs` ACL rules
- Added SELECTOR_FLAG_ALLDBS and SELECTOR_FLAG_DBLIST_NEGATED flags
- Updated SELECT, MOVE, SWAPDB, FLUSHDB commands with CMD_CROSS_DB flag, FLUSHALL - CMD_ALL_DBS
- Extended ACL checks with database access verification
- Added ACL_DENIED_DB error type for database permission violations
- Maintained backward compatibility with default access to all databases

Signed-off-by: Daniil Kashapov <daniil.kashapov.ykt@gmail.com>
@JoBeR007
Copy link
Author

JoBeR007 commented Jul 4, 2025

Hello! I would greatly appreciate any feedback from ACL experts)
@xbasel @hpatro @madolson

@hwware hwware added the major-decision-pending Major decision pending by TSC team label Jul 4, 2025
@hwware
Copy link
Member

hwware commented Jul 4, 2025

I would like to first discuss this new feature in the weekly meeting. Thanks

Signed-off-by: Daniil Kashapov <daniil.kashapov.ykt@gmail.com>
src/module.c Outdated
@@ -6527,7 +6527,10 @@ ValkeyModuleCallReply *VM_Call(ValkeyModuleCtx *ctx, const char *cmdname, const
int acl_errpos;
int acl_retval;

acl_retval = ACLCheckAllUserCommandPerm(user, c->cmd, c->argv, c->argc, &acl_errpos);
int dbid = (c->flag.multi) ?
Copy link
Member

Choose a reason for hiding this comment

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

Additionally for modules, we'll eventually probably want:

  1. A new API for checking if the user can access a DB
  2. New flags for CrossDB/All DBs. Might be able to just skip this for now.

Copy link
Author

Choose a reason for hiding this comment

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

Both would be useful. Seems like API and flags won't be a lot of code, should we do this in this PR or do as a follow-up with another Issue?

@@ -97,6 +98,15 @@ void queueMultiCommand(client *c, uint64_t cmd_flags) {
mc->argv = c->argv;
mc->argv_len = c->argv_len;
mc->slot = c->slot;

if (mc->cmd->proc == selectCommand && mc->argc > 1) {
Copy link
Member

Choose a reason for hiding this comment

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

We have some other code like this in cluster.c, we probably should write helpers for each command that accesses DBs and provide a helper function. Basically all functions can access up to two DBs, so we should be able to construct an interface around that.

Copy link
Author

@JoBeR007 JoBeR007 Jul 14, 2025

Choose a reason for hiding this comment

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

I wrote an implementation that extends serverCommand with hepler function that extracts db args from command, is that what you meant and what we should use in that situation? Or you meant something else that could potentially also resolve same hardcoded commands in cluster.c?

Signed-off-by: Daniil Kashapov <daniil.kashapov.ykt@gmail.com>
@JoBeR007 JoBeR007 marked this pull request as ready for review July 14, 2025 14:22
@madolson madolson requested a review from hpatro July 14, 2025 14:54
@madolson
Copy link
Member

Just discussed in the weekly meeting. It seems like we are all aligned to add more database features. We don't want folks to use database instead of true multi-tenancy, like running multiple containers, but there are still plenty of workloads that could benefit from the having access control on databases for namespacing. So we'll move forward with this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
major-decision-pending Major decision pending by TSC team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[NEW] Support database level ACL
3 participants