Windows環境でAzure Redis CacheにSSL接続

Windows環境にでAzure Redis Cache上のRedisにCLIから接続する場合、公式のドキュメントではRedis command-line tools for Windowsstunnelの組み合わせで接続する方法が示されています。

docs.microsoft.com

しかしRedis command-line tools for WindowsのレポジトリーはGitHub上のMicrosoftArchive 上に移されており、今日メンテナンスされていません。

本エントリーでは、Windows Subsystems for Linux(WSL)のredis-toolsとstunnelを使用して、Azure Redis Cache上のRedisに接続する方法を検証します。WSL上のLinuxUbuntu 18.04 LTSです。

ツールの導入

redis-toolsとstunnelをインストールします。

sudo apt install redis-tools
sudo apt install stunnel

証明書の作成

stunnelで使用する証明書と秘密鍵を作成します。*1 証明書の項目は適宜うめます。

sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/stunnel/redis-server.key -out /etc/stunnel/redis-server.crt
[sudo] password for azusa:
Generating a 2048 bit RSA private key
.......................................+++
.................................................................................................................................+++
writing new private key to '/etc/stunnel/redis-server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JA
State or Province Name (full name) [Some-State]:Kanagawa
Locality Name (eg, city) []:Yokohama
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:redis.fieldnotes.jp
Email Address []:hiroyuki@fieldnotes.jp

stunnelの設定

/etc/stunnel配下に設定ファイルを作成し、Redisへの接続情報を設定します。

sudo vi /etc/stunnel/redis.conf
pid = /var/run/stunnel-redis.pid

[redis-server]
cert = /etc/stunnel/redis-server.crt
key = /etc/stunnel/redis-server.key
connect = fieldnotes-redis.redis.cache.windows.net:6380
accept = 127.0.0.1:6379

stunnlを再起動します。

sudo /etc/init.d/stunnel4 restart

接続確認

[]:$ redis-cli                                                                             
127.0.0.1:6379> AUTH (アクセスキー)
OK
127.0.0.1:6379> keys *
1) "spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:user"
2) "spring:session:sessions:expires:066c4dc3-e446-4bce-b6e5-5bc98784a07a"
3) "spring:session:sessions:066c4dc3-e446-4bce-b6e5-5bc98784a07a"
4) "spring:session:expirations:1532154060000"
(2.37s)
127.0.0.1:6379>

redis-cliの実行時にアクセスキーを指定する場合は、以下の通りとなります。

[]:$ redis-cli  -a (アクセスキー)
127.0.0.1:6379> keys *
1) "spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:user"
2) "spring:session:sessions:expires:066c4dc3-e446-4bce-b6e5-5bc98784a07a"
3) "spring:session:sessions:066c4dc3-e446-4bce-b6e5-5bc98784a07a"
4) "spring:session:expirations:1532154060000"

Azureポータル上のRedisコンソール

なお、Azureポータル上にプレビュー機能としてRedisのコンソールがあり、Azure Redis Cache上のRedisに接続できるのですが、現時点ではJIS配列のキーとの相性に問題があるのかShiftキーの入力を受け付けてくれないため、残念ながら実用に供する事はできません。

f:id:setoazusa:20180721152922p:plain

f:id:setoazusa:20180721153007p:plain

参考エントリー

www.digitalocean.com

*1:クライアント証明のためなのでRedisに接続するには不要なのですが、設定上必要