Vault を Deployしてみる

目次

  1. 初めに
  2. 動作環境
  3. Deploy用の設定
  4. Vaultの初期化
  5. 終わりに

1. 初めに

今までVaultを dev modeで動作させていろいろとしてきました。今回はVaultを実際に運用することを見据えた環境設定をしていこうと思います。 また今回もHashiCorp Learnを参考に進めていきます。

Deploy Vault | Vault - HashiCorp Learn

2. 動作環境

3. Deploy用の設定

まずはどんなことよりもVaultをDeployする必要がありますね。そしてDeployするにあたってVaultの起動設定が必要になってきます。 下記がVaultの起動設定になります。

storage "consul" {
  address = "127.0.0.1:8500"
  path    = "vault/"
}

listener "tcp" {
 address     = "127.0.0.1:8200"
 tls_disable = 1
}

この設定項目だけでVaultは起動します。

storage は Vaultで利用するbackendを指定します。backendは暗号化されたストレージ部分になります。ちなみにdev modeの場合はインメモリで動作してたため、停止して再度起動させたらSecret情報などがすべて消えてしまいます。

listener はVaultでAPI Requestをリッスンする設定になります。

HashiCorp LearnではstorageをConsulを使っているのでConsulを動かしてみたいと思います。

$ consul agent -dev

次にVaultを実行してみたいと思います。上記の設定を config.hcl ファイルに記述し実行してみます。

$ vault server -config=config/config.hcl
==> Vault server configuration:

             Api Address: http://127.0.0.1:8200
                     Cgo: disabled
         Cluster Address: https://127.0.0.1:8201
              Listener 1: tcp (addr: "127.0.0.1:8200", cluster address: "127.0.0.1:8201", max_request_duration: "1m30s", max_request_size: "33554432", tls: "disabled")
               Log Level: info
                   Mlock: supported: true, enabled: false
           Recovery Mode: false
                 Storage: consul (HA available)
                 Version: Vault v1.3.0
             Version Sha: 4083c36aa0630faafb7c04be62c4940299880bc9+CHANGES

==> Vault server started! Log data will stream in below:

ただ、実際に実行してみると下記のようなエラーができました。

Error initializing core: Failed to lock memory: cannot allocate memory

This usually means that the mlock syscall is not available.
Vault uses mlock to prevent memory from being swapped to
disk. This requires root privileges as well as a machine
that supports mlock. Please enable mlock on your system or
disable Vault from using it. To disable Vault from using it,
set the `disable_mlock` configuration option in your configuration
file.

今回実行してるのがroot権限がないため、mlockを呼べない状態でした。 ですのでconfigに下記の設定を追加しました。

disable_mlock=true

詳しい説明は下記のドキュメントに記載されています。

Server Configuration - Vault by HashiCorp

4. Vaultの初期化

次にVaultの初期化処理を行います。初期化処理はconfigで指定したbackendに対してVaultが初めて実行したときに行う処理になります。 初期化処理で暗号化キーとunseal キーを作成し、Root Tokenの作成を行います。

初期化処理は vault operator init コマンドで実行できます。

$ vault operator init
Unseal Key 1: fVKpSklNiVPwRjhVE+WnVRJuO8FYWfYaecOJ5eOQg0Zy
Unseal Key 2: 4W9KvNuDFoz/icFfEYuT1GbklpiFq11AHxspNYIi+07X
Unseal Key 3: Fy0URxcq4fHsJbShgOu0JkBNP0BkLX7sg++iaW35O1RP
Unseal Key 4: amZCQR3PPQIcmkd6GkgqEhdplkHaO/CxK0rwMLq1W+gt
Unseal Key 5: zjn7oOGYi9fsKoGqt5dwj1PhENtYRfVXflQJf/2BBPvn

Initial Root Token: s.Vjlc2B2UGFvd7c3Wjc87ZQmF

Vault initialized with 5 key shares and a key threshold of 3. Please securely
distribute the key shares printed above. When the Vault is re-sealed,
restarted, or stopped, you must supply at least 3 of these keys to unseal it
before it can start servicing requests.

Vault does not store the generated master key. Without at least 3 key to
reconstruct the master key, Vault will remain permanently sealed!

It is possible to generate new unseal keys, provided you have a quorum of
existing unseal keys shares. See "vault operator rekey" for more information.

出力内容で重要となるのは Unseal Key の一覧と Root Token になりますので、メモを取っておきましょう。 初期化した後のVaultは Sealed 状態となっています。これはSecretなどの情報をBackendから取り出した時に復号できない状態になっています。 ではどうやって復号する方法を手に入れるかというと、マスターキーを作成する必要があります。

Vaultはデータを暗号化するため、暗号化キーを利用します。そしてその暗号化キーを読み取るためにマスターキーを利用する構成になっています。

ではマスターキーを作成するにはどうすればいいか、それは vault operator unseal コマンドを実行し初期化処理のときに出力された Unseal Keyを3つ利用します。3つ利用するということで vault operator unseal コマンドを3回実行すれば完了となります。

まずは1回目実行してみます。

$ vault operator unseal
Unseal Key (will be hidden):
Key                Value
---                -----
Seal Type          shamir
Initialized        true
Sealed             true
Total Shares       5
Threshold          3
Unseal Progress    1/3
Unseal Nonce       0edf2205-083e-b4fd-fd71-69db2ddb779f
Version            1.3.0
HA Enabled         true

2回目

$ vault operator unseal
Unseal Key (will be hidden):
Key                Value
---                -----
Seal Type          shamir
Initialized        true
Sealed             true
Total Shares       5
Threshold          3
Unseal Progress    2/3
Unseal Nonce       0edf2205-083e-b4fd-fd71-69db2ddb779f
Version            1.3.0
HA Enabled         true

最後に3回目

$ vault operator unseal
Unseal Key (will be hidden):
Key                    Value
---                    -----
Seal Type              shamir
Initialized            true
Sealed                 false
Total Shares           5
Threshold              3
Version                1.3.0
Cluster Name           vault-cluster-99ad46ab
Cluster ID             af61b226-4599-8e7e-2272-0d332b88f058
HA Enabled             true
HA Cluster             n/a
HA Mode                standby
Active Node Address    <none>

Sealedfalse になったら準備完了になります。

では、Root Tokenでログインしてみます。

$ vault login s.Vjlc2B2UGFvd7c3Wjc87ZQmF
Success! You are now authenticated. The token information displayed below
is already stored in the token helper. You do NOT need to run "vault login"
again. Future Vault requests will automatically use this token.

Key                  Value
---                  -----
token                s.Vjlc2B2UGFvd7c3Wjc87ZQmF
token_accessor       nF4cBXxkvNWDRmO4YDTQjQct
token_duration       ∞
token_renewable      false
token_policies       ["root"]
identity_policies    []
policies             ["root"]

ログインが完了しました。

また、unseal状態となりましたが seal状態に戻すこともできます。

$ vault operator seal
Success! Vault is sealed.

5. 終わりに

実際に本番で扱うような感じでVaultを動かしてみました。鍵を使っての暗号化、復号化についてはAWS のKMSやGCPのKMSを使ってもできるそうなので実際に運用するとなるとクラウドサービスを使ったほうがいいかと思います。これもどこかのタイミングで試してみて記事にしたいと思います。