skip to content

How would you write Redis `ACL SETUSER` rules so a checkout service, a read-only reporting job and a cache warmer each get least privilege?

level: middleimportance: must knowfreq 55%

answer

  1. one user per service
  2. commands, keys and channels are separate grants
  3. rules read left to right
  4. read-only and write-only key patterns
  5. PING is not in the read category

basics

~10 s

Give each service its own user: commands through +@read, +@write and -@dangerous, keys through ~ and %R~ or %W~ patterns, channels through &. Rules apply left to right, and anything not granted returns NOPERM.

solid answer

~40 s

Create one user per service and start each definition with `reset`, so the rules apply to a clean slate, left to right. Checkout gets `+@read +@write -@dangerous +ping ~checkout:*`. Reporting gets `+@read -@dangerous +ping %R~orders:* %R~report:*`, which limits it to read commands and read access on those keys. The warmer gets `+set +unlink +ping %W~cache:*`, plus `+publish &cache:invalidate` if it announces refreshes. `+` rules and categories decide commands, `~`, `%R~` and `%W~` decide keys, and `&` decides channels, and a command must pass every check that applies to it. A refused call returns `NOPERM`. If one set of keys needs different commands, a selector in parentheses adds a separate rule set.

code

plaintext · 6 lines
plaintext
> ACL SETUSER checkout reset on >CHECKOUT_SECRET +@read +@write -@dangerous +ping ~checkout:*
OK
> ACL SETUSER reporting reset on >REPORTING_SECRET +@read -@dangerous +ping %R~orders:* %R~report:*
OK
> ACL SETUSER warmer reset on >WARMER_SECRET +set +unlink +ping %W~cache:* +publish &cache:invalidate
OK

go deeper

for a junior

Recall the three grants a user needs: commands, keys and channels. Know that a new user starts off with nothing, and that it needs on and a password before anyone can log in.

for a middle

Write the rules for a concrete service without looking them up, say what the reply to a refused call is, and explain which key permission LPUSH, LPOP and plain SET each need.

for a senior

Explain what these rules still leave open, such as key names visible through SCAN, and why a named command list ages differently from a category grant when commands are added.

for a principal

Weigh categories against explicit command lists as a default for many services, and decide where selectors earn their complexity against simply giving a job a second user.

## The rule language `ACL SETUSER <name> <rule> ...` creates the user if it is missing, then applies the rules **left to right**. A new user is `off`, has no passwords, no commands and no keys. Since Redis 7.0, `acl-pubsub-default` defaults to `resetchannels`, so it normally has no channels either. Later calls add to the existing user rather than replacing it, so the `reset` rule at the start of a definition is what makes it repeatable. | Family | Rules | What they control | |---|---|---| | State | `on`, `off` | Whether anyone can log in as the user | | Passwords | `>secret`, `#<sha256>`, `resetpass` | Which secrets the user accepts | | Commands | `+get`, `-get`, `+@read`, `-@dangerous` | Which commands may be called | | Keys | `~p`, `%R~p`, `%W~p`, `allkeys`, `resetkeys` | Which keys may be named, and how | | Channels | `&p`, `allchannels`, `resetchannels` | Which Pub/Sub channels may be used | | Selectors | `(rules)`, `clearselectors` | Extra independent rule sets | ## Command categories Listing commands one at a time is tedious, so Redis groups them into categories. `ACL CAT` lists the categories and `ACL CAT read` lists the commands inside one. A command can sit in several, and that is why order matters: - `@read` and `@write` cover commands that read or change keys. - `@dangerous` holds commands to treat with care, such as `FLUSHALL`, `KEYS`, `SORT`, `INFO` and `CONFIG`. - `@admin` holds administrative commands such as `ACL`, `CONFIG` and `SHUTDOWN`. - `@connection` holds `PING`, `SELECT` and `CLIENT`, which also brings in `CLIENT KILL` unless a later `-@dangerous` removes it. Since Redis 7.0, a rule can also name one subcommand, as `+config|get` or `-config|set` do. `+@read` does not include `PING`, so a read-only client with a health check needs `+ping` as well. `AUTH` and `HELLO` need no rule. Redis 8.0 added categories such as `@search` and `@json`, and its release notes say those commands also join `@read` and `@write`. ## Key permissions `~p` allows reading and writing keys that match the glob `p`. Since Redis 7.0, `%R~p` allows reading only and `%W~p` writing only. The permission a command needs comes from what it does to the key: - `LPUSH` changes a key and returns only its new size, so write permission is enough. - `LPOP` changes a key and returns data from it, so it needs both. - Plain `SET` needs write permission, while `SET` with the `GET` option needs read as well. - `EXISTS` needs either permission. A write-only pattern does not make a key invisible, as write commands can still return metadata about it. If nothing about a key may leak, grant no pattern for it. ## The three users 1. **Checkout** reads and writes its own keys: `+@read +@write -@dangerous +ping ~checkout:*`. Add `+@transaction` if it uses `MULTI`. 2. **Reporting** is read-only twice over with `+@read -@dangerous +ping %R~orders:* %R~report:*`: `+@read` grants no write command, and `%R~` would refuse a write on those keys even if one were added later. 3. **The warmer** gets named commands rather than a category, with `+set +unlink +ping %W~cache:*`, so it cannot read cached values back. With `+publish &cache:invalidate` it also gets one channel: `&` patterns match the names used by `PUBLISH` and `SUBSCRIBE`, while `PSUBSCRIBE` needs a literal match with the allowed pattern. The replies are predictable. As `reporting`, `GET orders:1` returns the value, `SET report:1 x` gets `NOPERM User reporting has no permissions to run the 'set' command`, and `GET session:1` gets `NOPERM No permissions to access a key`. `ACL DRYRUN reporting SET report:1 x` predicts the same refusal without running the command, which makes it a cheap test to keep beside the rules. ## Selectors Since Redis 7.0, rules in parentheses form a **selector**, a separate rule set. A command is allowed if the root rules or any one selector match it **in full**. Checkout could add `(+get %R~flags:*)` to read feature flags with `GET` alone, where a root `%R~flags:*` would open those keys to every read command. With `+GET ~key1 (+SET ~key2)`, `GET key2` is refused, as no single set covers it. Selectors cannot be edited after they are added: `clearselectors` removes them all.

  • With the rule `&news:*`, why is `PSUBSCRIBE news:eu:*` refused when `SUBSCRIBE news:eu` works?
    `SUBSCRIBE` and `PUBLISH` name a channel, and the name is matched against the glob. `PSUBSCRIBE` names a pattern, and it needs a literal match with a pattern the user holds, so only `PSUBSCRIBE news:*` passes. The narrower pattern is refused even though every channel it could match is allowed.
  • A user holds `+@all ~app1:* (+@read ~app2:*)`. Why does `COPY app2:user app1:user` fail, and what fixes it?
    `COPY` must be fully allowed by the root rules or by one selector. The root rules do not cover `app2:user`, and the selector neither allows `COPY` nor covers `app1:user`. `+@all ~app1:* %R~app2:*` works, because one rule set then holds write access on the destination and read access on the source.
  • Does `+@read` stop a reporting user from seeing key names outside its patterns?
    No. `+@read` includes `SCAN` and `RANDOMKEY`, which take no key argument, so key patterns do not limit them, and `-@dangerous` removes `KEYS` but not those two. Add `-scan -randomkey` if the names themselves are sensitive.

An ACL user is a keycard. Command rules say which kinds of door it opens, and key patterns say which floors it works on. A door on a named floor checks both, while a door that belongs to no floor checks only the first.

  • A new ACL user can run read commands until you take that away
  • %R~ on a pattern is enough to make a user read-only everywhere
  • A later ACL SETUSER call replaces the rules given in the earlier one
  • +@read includes PING, so health checks need no extra rule
  • A command may pass with root rules for one key and a selector for another

Assumes Redis 8.10; key permissions and selectors date from 7.0, and categories such as @json and @search from 8.0.