之前有介紹介紹過Consul K/V功能,但在儲存在Consul的資料只有使用base64來做加密,與Consul不同的是,Vault有自己的加密方式來保護資料,此外Vault K/V有兩種版本v1,v2, 最大的差異在v2有支援版控的功能。
Vault KV version 1是沒有版本控制的功能,只會保留最新寫入的資料,所以無須額外儲存其他metadata或是歷史數據,儲存空間更小,效能更高;若之後要升級成version 2, 可以透過enable-versioning
來升級。
啟用kv時可以選擇要使用哪一個版本。
$ vault secrets enable -path="kv-v1" -version=1 kv
Success! Enabled the kv secrets engine at: kv-v1/
$ vault kv put kv-v1/fb/account name=abc123
Success! Data written to: kv-v1/fb/accoutn
$ vault kv get kv-v1/fb/account
==== Data ====
Key Value
--- -----
name abc123
$ vault kv list kv-v1/fb/
Keys
----
account
刪除後再查詢,
$ vault kv delete kv-v1/fb/account
Success! Data deleted (if it existed) at: kv-v1/fb/account
KV Version 2的版本控制預設是10個版本,在執行delete
時,並不會真的刪除數據,只會標示為刪除,真的要刪除須使用destroy
.
圖片來源: https://learn.hashicorp.com/tutorials/vault/versioned-kv
啟用kv時可以選擇要使用哪一個版本。
$ vault secrets enable -path="kv-v2" -version=2 kv
Success! Enabled the kv secrets engine at: kv-v2/
查詢使用的K/V是哪一版本
$ vault secrets list -detailed
新增一筆資料
$ vault kv put /kv-v2/crm/info name="aaa"
Key Value
--- -----
created_time 2020-09-27T06:49:42.802240643Z
deletion_time n/a
destroyed false
version 1
再加入email資訊,會看到version變為2
$ vault kv put /kv-v2/crm/info name="aaa" email="aaa@ithome.com"
Key Value
--- -----
created_time 2020-09-27T06:49:55.345969316Z
deletion_time n/a
destroyed false
version 2
直接使用get
取值會取到最新版的資訊。
$ vault kv get /kv-v2/crm/info
====== Metadata ======
Key Value
--- -----
created_time 2020-09-27T06:49:55.345969316Z
deletion_time n/a
destroyed false
version 2
==== Data ====
Key Value
--- -----
email aaa@ithome.com
name aaa
如果要指定版本可以加入version
$ vault kv get -version=1 /kv-v2/crm/info
====== Metadata ======
Key Value
--- -----
created_time 2020-09-27T06:49:42.802240643Z
deletion_time n/a
destroyed false
version 1
==== Data ====
Key Value
--- -----
name aaa
直接使用delete
會刪除最新版的資訊。(只標示刪除)
$ vault kv delete /kv-v2/crm/info
Success! Data deleted (if it existed) at: kv-v2/crm/info
可以查看metadata,version 2有刪除時間。
$ vault kv metadata get kv-v2/crm/info
========== Metadata ==========
Key Value
--- -----
cas_required false
created_time 2020-09-27T06:49:42.802240643Z
current_version 2
delete_version_after 0s
max_versions 0
oldest_version 0
updated_time 2020-09-27T06:49:55.345969316Z
====== Version 1 ======
Key Value
--- -----
created_time 2020-09-27T06:49:42.802240643Z
deletion_time n/a
destroyed false
====== Version 2 ======
Key Value
--- -----
created_time 2020-09-27T06:49:55.345969316Z
deletion_time 2020-09-27T06:55:52.262019273Z
destroyed false
恢復刪除需指定版本號
$ vault kv undelete -versions=2 /kv-v2/crm/info
Success! Data written to: kv-v2/undelete/crm/info
$ vault kv destroy -versions=1 /kv-v2/crm/info
Success! Data written to: kv-v2/destroy/crm/info
可以看到版本1的destoryed
被標示成true
$ vault kv metadata get kv-v2/crm/info
========== Metadata ==========
Key Value
--- -----
cas_required false
created_time 2020-09-27T06:49:42.802240643Z
current_version 2
delete_version_after 0s
max_versions 0
oldest_version 0
updated_time 2020-09-27T06:49:55.345969316Z
====== Version 1 ======
Key Value
--- -----
created_time 2020-09-27T06:49:42.802240643Z
deletion_time n/a
destroyed true
====== Version 2 ======
Key Value
--- -----
created_time 2020-09-27T06:49:55.345969316Z
deletion_time n/a
destroyed false