CLOVER🍀

That was when it all began.

Valkey 8.0でACLを定義する

これは、なにをしたくて書いたもの?

前に、Valkey 8.0をインストールしました。

Redis互換のキーバリューストア、ValkeyをUbuntu Linux 22.0.4 LTSにインストールする - CLOVER🍀

この時はとりあえずビルドしてインストール、起動しただけだったので今回はACLを少し扱いたいと思います。

ValkeyのACL

ValkeyのACLに関する設定はこちらのページに載っています。

Valkey Documentation · ACL

ACLと言っていますが、要するに認証や認可の設定です。

ドキュメントを見ていると、ACLの書き方はRedisと同じようです。Redisについて見た時は、こちらのエントリーに書きました。

RedisのACLを試す - CLOVER🍀

ACLの例はこちら。ACL SETUSERコマンドで定義します。

> ACL SETUSER alice on >p1pp0 ~cached:* +get

これは、以下のACLの定義を表します。

  • ユーザーはalice
  • onは有効なユーザーを表す
  • パスワードはp1pp0
  • cached:で始まるキーにのみGETコマンドが実行できる

ACLは左から右に評価されます。

ACLs are defined using a DSL (domain specific language) that describes what a given user is allowed to do. Such rules are always implemented from the first to the last, left-to-right, because sometimes the order of the rules is important to understand what the user is really able to do.

つまりACLでユーザーと権限の定義を同時に行うことになります。ACLはコマンドだけではなく、設定ファイルもしくは別ファイルで
定義することもできます。

設定ファイルで定義する場合は、ACL SETUSERコマンドの内容をuserディレクティブに定義します。

user worker +@list +@connection ~jobs:* on >ffa9203c493aa99

ACLを別のファイルで定義する場合は、設定ファイルでaclfileディレクティブでACLが書かれたファイルのパスを指定します。

aclfile /etc/valkey/users.acl

ちなみに認証はAUTHコマンドでユーザー名、パスワードを指定して行います。

Valkey Command · AUTH

パスワードのみを指定した場合はdefaultユーザーに対する認証要求とみなされます。defaultユーザーのパスワードはrequirepass
設定しますが、defaultユーザー自体を無効にすることもできます。

内容自体は前にRedisで1度確認しているので、今回は簡単に見るくらいにしておきます。

環境

今回の環境はこちら。

$ bin/valkey-server --version
Valkey server v=8.0.1 sha=00000000:0 malloc=jemalloc-5.3.0 bits=64 build=7dac90c18a6d053e

Valkey 8.0.1でサーバーとクライアントを異なるホストで用意します。サーバー側は172.17.0.2とします。

ACLを定義する

ではACLを定義しましょう。

今回は設定ファイルで定義することにします。

conf/valkey.conf

user default off
user valkey-admin on >admin-password ~* &* +@all
user read-write-user on >password +@read +@write ~*
user read-only-user on >password +@read ~*

上から順に以下の定義になっています。

  • defaultユーザーを無効化
  • すべての操作ができるvalkey-adminユーザー
  • すべてのキーに読み書きができるread-write-userユーザー
  • すべてのキーの読み取りだけができるread-only-userユーザー

パスワードはvalkey-adminのみadmin-passwordで、他のユーザーはpasswordです。

この設定ファイルを指定して、Valkeyサーバーを起動します。

$ bin/valkey-server conf/valkey.conf

クライアントから接続してみましょう。

$ bin/valkey-cli -h 172.17.0.2

いきなりコマンドを実行しても「認証が必要だ」と怒られます。

172.17.0.2:6379> set key1 value1
(error) NOAUTH Authentication required.

ちなみに、ACLなどの設定を指定せずに単にbin/valkey-serverコマンドだけで起動したValkeyサーバーにリモートから接続すると、
プロテクションモードになっていてなにもできません。

172.17.0.2:6379> set key1 value1
(error) DENIED Running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers, you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting from the same host the server is running, however MAKE SURE it's not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Set up an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

リモート接続の場合は認証が必要です。

話を戻して。

読み書きできるユーザーで認証してみます。

172.17.0.2:6379> auth read-write-user password
OK

操作できました。

172.17.0.2:6379> set key1 value1
OK
172.17.0.2:6379> get key1
"value1"

読み取り専用ユーザーに切り替えます。

172.17.0.2:6379> auth read-only-user password
OK

読み込みはできますが、書き込みはできません。

172.17.0.2:6379> get key1
"value1"
172.17.0.2:6379> set key2 value2
(error) NOPERM User read-only-user has no permissions to run the 'set' command

読み書きできるユーザーに戻します。

172.17.0.2:6379> auth read-write-user password
OK

ACLを定義してみましょう。

172.17.0.2:6379> acl setuser alice on >p1pp0 ~cached:* +get
(error) NOPERM User read-write-user has no permissions to run the 'acl|setuser' command

これは権限がないと怒られます。

管理用ユーザーに切り替えます。

172.17.0.2:6379> auth valkey-admin admin-password
OK

管理者であればACLの定義ができます。

172.17.0.2:6379> acl setuser alice on >p1pp0 ~cached:* +get
OK

今回はこんなところでしょうか。

おわりに

Valkey 8.0でACLを定義してみました。

内容としてはRedisと変わっていないので、あっさりとした内容になりましたが。

今後はACLを定義することを前提にValkeyを使っていきたいと思います。