Vault Secrets Engine

环境信息

  • Kubernetes 1.24
  • Vault 1.14.0

kv

Key/Value 机密引擎是一个通用的键值存储,用于在 Vault 使用的物理存储中存储任意秘密。该后端可以以两种模式之一运行 [1]

  • kv v1 - 可以将其配置为存储密钥的单个值,只有最近写入的值会被保存下来
  • kv v2 - 开启版本控制并存储每个键的一定数量版本的值。默认保留 10 个版本的值。

kv Version 1

Version 1 的 KV Secret Engine 相比 v2 版本,有以下限制:

  • 不能使用 vault kvmetadatapatch 命令
  • 使用 vault kv put 写入的值会覆盖之前的内容,即只保存了最后一次写入的值。

启用 version 1 的 kv 存储,没有 -version 选项时默认开启 version 1 版本的 kv:

$ vault secrets enable -version=1 kv

与其他 Secret Engine 不同,kv 机密引擎不会强制执行 TTL 过期。即使设置了 ttl,kv Secret Engine 也不会自行删除数据。 [1]

写入数据

$ vault kv put kv/mycorp/mydepartment/myproject/myapp/myapp-api/config db_type=mysql
Success! Data written to: kv/mycorp/mydepartment/myproject/myapp/myapp-api/config

$ vault kv put kv/mycorp/mydepartment/myproject/myapp/myapp-api/config db_host=127.0.0.1
Success! Data written to: kv/mycorp/mydepartment/myproject/myapp/myapp-api/config

$ vault kv put kv/mycorp/mydepartment/myproject/myapp/myapp-api/config db_port=3306
Success! Data written to: kv/mycorp/mydepartment/myproject/myapp/myapp-api/config

列出键

$ vault secrets list
Path Type Accessor Description
---- ---- -------- -----------
cubbyhole/ cubbyhole cubbyhole_e5c17df6 per-token private secret storage
identity/ identity identity_f0404cf8 identity store
kv/ kv kv_618be90b n/a
sys/ system system_053aea79 system endpoints used for control, policy and debugging
transit/ transit transit_aaaaf63d n/a


$ vault kv list kv
Keys
----
mycorp/

$ vault kv list kv/mycorp
Keys
----
mydepartment/

$ vault kv list kv/mycorp/mydepartment
Keys
----
myproject/

$ vault kv list kv/mycorp/mydepartment/myproject/myapp/myapp-api
Keys
----
config

$ vault kv list kv/mycorp/mydepartment/myproject/myapp/myapp-api/config
No value found at kv/mycorp/mydepartment/myproject/myapp/myapp-api/config

读取键值

$ vault kv get kv/mycorp/mydepartment/myproject/myapp/myapp-api/config
===== Data =====
Key Value
--- -----
db_port 3306

以上输出中,键 kv/mycorp/mydepartment/myproject/myapp/myapp-api/config 的内容为 db_port=3306,之前写入的其他数据被覆盖,只保留有最后一个写入

删除键

$ vault kv delete kv/mycorp/mydepartment/myproject/myapp/myapp-api/config
Success! Data deleted (if it existed) at: kv/mycorp/mydepartment/myproject/myapp/myapp-api/config

$ vault kv get kv/mycorp/mydepartment/myproject/myapp/myapp-api/config
No value found at kv/mycorp/mydepartment/myproject/myapp/myapp-api/config

kv Version 2

kv Version 2 相比 Version 1,有以下改进

  • version 2 支持多版本管理,默认保留 10 个版本
  • version 2 支持 patch 操作,可以更新 key,而不是像 version 1 一样直接覆盖
  • version 2 支持数据回滚操作
  • version 2 保护了 metadata 元数据信息,里面可以看到多个版本的信息及其数据

使用以下命令启用 version 2 版本的 kv,挂载路径为 kv2

$ vault secrets enable -path=kv2 -version=2 kv
Success! Enabled the kv secrets engine at: kv2/

$ vault secrets list
Path Type Accessor Description
---- ---- -------- -----------
cubbyhole/ cubbyhole cubbyhole_e5c17df6 per-token private secret storage
identity/ identity identity_f0404cf8 identity store
kv/ kv kv_618be90b n/a
kv2/ kv kv_2b51a6d6 n/a
sys/ system system_053aea79 system endpoints used for control, policy and debugging
transit/ transit transit_aaaaf63d n/a

写入数据

写入数据的语法和 version 1 相同

$ vault kv put kv2/corp/department/project/app/api/config db_type=mysql
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:13:57.086632928Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 1

version 2 相比 version 1 重要的是多了多版本管理的功能,初次生成 key 并写入数据后,其 version1

更新数据

version 2 相比 version 1 的另一个重要功能是 patch 操作,可以更新原有的 key 内容,而不完全覆盖,这个功能是通过 vault kv patch 命令实现

$ vault kv get kv2/corp/department/project/app/api/config 
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:13:57.086632928Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 1

===== Data =====
Key Value
--- -----
db_type mysql

$ vault kv patch kv2/corp/department/project/app/api/config db_host=lcoalhost
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:15:18.525867051Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 2


$ vault kv get kv2/corp/department/project/app/api/config
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:15:18.525867051Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 2

===== Data =====
Key Value
--- -----
db_host lcoalhost
db_type mysql

vault kv patch 后,数据的 version 变为了 2,因为 kv version 2 的多版本管理功能,旧的版本数据也被存储(默认保留 10 个版本),可以查看旧版本的数据

$ vault kv get -version=1 kv2/corp/department/project/app/api/config
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:13:57.086632928Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 1

===== Data =====
Key Value
--- -----
db_type mysql

回滚操作

kv verions 2 因为有版本管理的功能,因此可以基于保存的多版本数据,进行数据版本回滚操作,此功能通过 vault kv rollback 命令实现

以下命令演示了数据的版本回滚操作,可以实现基于以前的版本恢复数据。需要注意的是,执行 rollback 后的 version 值不是回滚的目标版本,而是一个新的版本,只是其数据内容和要回滚的目标版本的数据一致。比如以下示例中,当前 version2,执行了 vault kv rollback -version=1 后,数据版本变成了 version 3,而不是 version 1,但是 version 3version 1 的数据内容一模一样

$ vault kv get kv2/corp/department/project/app/api/config
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:15:18.525867051Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 2

===== Data =====
Key Value
--- -----
db_host lcoalhost
db_type mysql


$ vault kv rollback -version=1 kv2/corp/department/project/app/api/config
Key Value
--- -----
created_time 2023-07-14T02:18:34.86076686Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 3


$ vault kv get kv2/corp/department/project/app/api/config
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:18:34.86076686Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 3

===== Data =====
Key Value
--- -----
db_type mysql


$ vault kv get -version=1 kv2/corp/department/project/app/api/config
================= Secret Path =================
kv2/data/corp/department/project/app/api/config

======= Metadata =======
Key Value
--- -----
created_time 2023-07-14T02:13:57.086632928Z
custom_metadata <nil>
deletion_time n/a
destroyed false
version 1

===== Data =====
Key Value
--- -----
db_type mysql

查看 metadata

查看 Key 的 metadata 数据,可以看到多个版本的数据

$ vault kv metadata get kv2/corp/department/project/app/api/config
================== Metadata Path ==================
kv2/metadata/corp/department/project/app/api/config

========== Metadata ==========
Key Value
--- -----
cas_required false
created_time 2023-07-14T02:13:57.086632928Z
current_version 4
custom_metadata <nil>
delete_version_after 0s
max_versions 0
oldest_version 0
updated_time 2023-07-14T02:24:42.054033452Z

====== Version 1 ======
Key Value
--- -----
created_time 2023-07-14T02:13:57.086632928Z
deletion_time n/a
destroyed false

====== Version 2 ======
Key Value
--- -----
created_time 2023-07-14T02:15:18.525867051Z
deletion_time n/a
destroyed false

====== Version 3 ======
Key Value
--- -----
created_time 2023-07-14T02:18:34.86076686Z
deletion_time n/a
destroyed false

====== Version 4 ======
Key Value
--- -----
created_time 2023-07-14T02:24:42.054033452Z
deletion_time n/a
destroyed false

参考链接

Key / Value

脚注