Vault初めてみる : Secretの基礎
目次
- 初めに
- 環境情報
- vault serverの起動
- Secretの保存
- Secret Engineについて触れておく
- 終わりに
1. 初めに
Vaultの勉強でHashiCorp Learnをやってみました。その時にやった内容や補足で調べたことなどを備忘録として残していきたいと思います。
また、HashiCorp Learnのサイトは下記リンクから確認できます。コマンド等は本家のサイトとほぼ、同様になっています。
Vault Curriculum - HashiCorp Learn
このブログよりも細かい説明もHashiCorp Learnに記載されていますので、気になる方は合わせてご確認ください。
今回はVault事態の説明は省いていますので、気になる方は下記サイトなどを参考にしてください。
HashiCorp Vaultの基礎知識と導入 | Developers.IO
2. 環境情報
3. Vault Serverの起動
まずはVault Serverを起動させてみます。HashiCorp Learnでは dev modeでServerを起動させていますので、サンプルにそって実行させてみましょう。 Serverはセキュアなものではないので本番での利用は推奨されてませんので気を付けましょう。
$vault server -dev ==> 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: inmem Version: Vault v1.3.0 Version Sha: 4083c36aa0630faafb7c04be62c4940299880bc9+CHANGES WARNING! dev mode is enabled! In this mode, Vault runs entirely in-memory and starts unsealed with a single unseal key. The root token is already authenticated to the CLI, so you can immediately begin using Vault. You may need to set the following environment variable: $ export VAULT_ADDR='http://127.0.0.1:8200' The unseal key and root token are displayed below in case you want to seal/unseal the Vault or re-authenticate. Unseal Key: UoL9YBIsRrIxBFbfO+cfpmEHyUyzL8DH57CXWm7aI4E= Root Token: s.VGnMIQk4ogkD2Yn42O4GT0Zr Development mode should NOT be used in production installations!
Serverが実行されるとフォワグラウンドで実行されます。ですので別のターミナルを使って続きをやっていきましょう。 ここでServerを起動したときに出力されたServerのエンドポイントとRoot Token情報新しく立ち上げたターミナルの環境変数として登録します。
export VAULT_ADDR='http://127.0.0.1:8200' export VAULT_DEV_ROOT_TOKEN_ID="s.VGnMIQk4ogkD2Yn42O4GT0Zr"
VAULT_ADDRは出力された文字内に環境変数の設定まできれいに記載されています。
VAULT_DEV_ROOT_TOKEN_IDは Root Token
の値になります。
環境変数を設定した後にステータスを確認してみると下記のような結果になります。
$ vault status Key Value --- ----- Seal Type shamir Initialized true Sealed false Total Shares 1 Threshold 1 Version 1.3.0 Cluster Name vault-cluster-9bdecbfc Cluster ID e619f604-2e79-b963-55e0-c3f73e1e860c HA Enabled false
これでCLIでVault Serverを操作する準備ができました。では次にSecretを保存してみましょう。
4. Secretの保存
まずSecretとはどういうものかというと、APIキーやユーザ/パスワードなどの機密情報のことです。
そしてこのSecretを管理するのがSecrets Engineになります。
HashiCorp Learnでは kv
っというSecrets Engineを使っていますので、まずはその挙動を見てみましょう。
$ vault kv put secret/hello foo=world Key Value --- ----- created_time 2019-12-22T08:38:13.005825481Z deletion_time n/a destroyed false version 1
ここでは foo=world
っというペアで secret/hello
っというパスに保存をしました。パスっというのは各Secrets Engineで独自のパスとプロパティを定義されています。
dev modeで起動しているServerでは kv
はデフォルトでenableになっており、パスは /secret
を使うようになっています。
ちなみに、 kv
はversion 1とversion 2があり、デフォルトでenableになっているのはversion 2のほうになります。
サンプルにそってもう一つ、ペアを登録してみましょう。
$ vault kv put secret/hello foo=world excited=yes Key Value --- ----- created_time 2019-12-22T08:47:51.195186928Z deletion_time n/a destroyed false version 2
では、登録したSecretを確認してみましょう。
$ vault kv get secret/hello ====== Metadata ====== Key Value --- ----- created_time 2019-12-22T08:47:51.195186928Z deletion_time n/a destroyed false version 2 ===== Data ===== Key Value --- ----- excited yes foo world
無事に登録されているのが確認できました。
ちなみに vault kv put
処理は追加ではなく、上書となるので注意が必要です。
kv
で保存されたSecretで特定のKeyのものだけを取り出すこともできます。
$ vault kv get -field=excited secret/hello yes
最後に、不要となったSecretを削除する処理は下記のようになります。
$ vault kv delete secret/hello Success! Data deleted (if it existed) at: secret/hello
ここでの delete
処理は secret/hello
の削除ではなく、 secret/hello
の中にあるSecretのみ削除となります。
ですので下記のように secret/hello
をgetしたときに情報が返ってきます。
$ vault kv get secret/hello ====== Metadata ====== Key Value --- ----- created_time 2019-12-22T10:24:36.600075751Z deletion_time 2019-12-22T10:32:10.159743478Z destroyed false version 2
5. Secret Engineについて触れておく
先ほどの secret/hello
はSecrets Engine独自のパスとプロパティっていう記載しました。
もう少し掘り下げると、「パスによってSecrets EngineがなにかをVaultへ指示している」っということになります。
リクエストのパスによってSecrets Engineが特定されるようになるので、各Secrets Engineによって独自のパスになるということです。
パスとSecrets Engineの対応を見る方法は下記のコマンドで確認できます。
$ vault secrets list Path Type Accessor Description ---- ---- -------- ----------- cubbyhole/ cubbyhole cubbyhole_18f07d22 per-token private secret storage identity/ identity identity_5170f551 identity store secret/ kv kv_2fd4ecf6 key/value secret storage sys/ system system_4a94a9f3 system endpoints used for control, policy and debugging
これで kv
のパスが secret/
になっているのが確認できます。
では kv
のSecrets Engineを別のパスで一つ追加してみたいと思います。
$ vault secrets enable -path=kv kv Success! Enabled the kv secrets engine at: kv/ $ vault secrets list Path Type Accessor Description ---- ---- -------- ----------- cubbyhole/ cubbyhole cubbyhole_18f07d22 per-token private secret storage identity/ identity identity_5170f551 identity store kv/ kv kv_30fe82bc n/a secret/ kv kv_2fd4ecf6 key/value secret storage sys/ system system_4a94a9f3 system endpoints used for control, policy and debugging
kv/
っというパスで kv
Secret Engineが追加されました。
ではSecretを追加していきたいと思います。
$ vault write kv/my-secret value="s3c(eT" Success! Data written to: kv/my-secret $ vault write kv/hello target=world Success! Data written to: kv/hello $ vault write kv/airplane type=boeing class=787 Success! Data written to: kv/airplane $ vault list kv Keys ---- airplane hello my-secret
最後にSecrets Engineを無効にします。
$ vault secrets disable kv/
6. 終わりに
HashiCorp Learnを参考にVault Serverを立ち上げてSecretの保存、Secrets Engineの概要について勉強してきました。 引き続きHashiCorp Learnを続けていきたいと思います。