介绍Vault的基本用法,包括安装和数据存取
- 安装
vault是使用go语言开发的开源工具,必须先安装golang
- 准备工程目录
$ mkdir -p $GOPATH/src/github.com/hashicorp
$ cd $GOPATH/src/github.com/hashicorp
- 下载源代码
$ git clone [https://github.com/hashicorp/vault.git](https://github.com/hashicorp/vault.git)
- 编译
$ cd vault
$ make bootstrap # download dependents
$ make dev
- 验证编译结果
$ bin/vault -v
Vault v1.4.0-beta1 ('6c4508007a79567d6c8c1ca4e1424b1d54d46aa7')
- 启动
- 定义启动配置文件
这个例子中,我们使用zookeeper作为后段存储。
因为需要支持多个vault实例,不能使用本地文件系统存储;另外使用zookeeper作为存储,必须配置api_addr参数。
$ cat vault.hcl
disable_mlock = true
// Enable UI
ui = true
api_addr = "http://vaultserver:8200"
// Zookeeper storage
storage "zookeeper" {
address = "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181"
path = "/myzkroot/myzkpath/.vault/"
}
// TCP Listener using a self-signed certificate
listener "tcp" {
address = "0.0.0.0:8200"
tls_disable = "1"
}
- 启动vault
$ vault server -config ./vault.hcl
- 初始化
初始化只需要整个vault集群执行一次就行;如果有多个vault实例,只要任何一个,只需要执行一次即可。
会生成5个unseal key和一个root token;其中unseal key下面就会用到,必须存好不可丢失,而root token则是一个超级用户token,必须妥善保管,一般用它来做系统配置,然后签出用户token。
$ VAULT_ADDR=http://localhost:8200 vault operator init
Unseal Key 1: 1q26g1lJ+E6/S+BIKsiFuUtTBIaqnya6q2fNvAWHsQrb
Unseal Key 2: iV7wZ2glNvkwHEVb9Jyx8DV9XUDYST2D0rOmoAL9sLY7
Unseal Key 3: +qf01yhTEh5UfCYqC36MDZDMJMyRSOV4iUo/BEmO6HNx
Unseal Key 4: M/rO0VWc5ofxyjTAecDF7D7bE2YhUo1LZPQA2W/5OQaX
Unseal Key 5: GFS4cuKF4ifw4F6wlwJSp0fqAt0BYYo0VXqtpgVij+up
Initial Root Token: s.jLx1tMuxBGcfcFdYR7oZuPzN
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.
Rest API:
$ curl -X PUT -d '{"secret_shares":5, "secret_threshold":3}' http://127.0.0.1:8200/v1/sys/init
- 解封(unseal)
前面我们生成了5个unseal key,这是vault的缺省配置,一共生成5个unseal key,每次解封必须使用其中的三个。(这5和3可以在初始化的时候配置)
解封操作需要在每一个实例的每次启动时执行。
$ VAULT_ADDR=http://localhost:8200 vault operator unseal 1q26g1lJ+E6/S+BIKsiFuUtTBIaqnya6q2fNvAWHsQrb
$ VAULT_ADDR=http://localhost:8200 .vault operator unseal iV7wZ2glNvkwHEVb9Jyx8DV9XUDYST2D0rOmoAL9sLY7
$ VAULT_ADDR=http://localhost:8200 vault operator unseal +qf01yhTEh5UfCYqC36MDZDMJMyRSOV4iUo/BEmO6HNx
Rest API
$ curl -X PUT -d '{"key": "8221d97ab6763892a945bf643b71c6bab57d7268874cd22d0b221f6acb1046a383"}' http://127.0.0.1:8200/v1/sys/unseal
三次解封,在查看结果:
$ VAULT_ADDR=http://localhost:8200 vault status
Key Value
--- -----
Seal Type shamir
Initialized true
Sealed false
Total Shares 5
Threshold 3
Version 1.4.0-beta1
Cluster Name vault-cluster-0e976f2a
Cluster ID 66c5f95a-e046-1809-0bfe-f56a8e9a2dda
HA Enabled true
HA Cluster https://4ba315ed-3387-4de0-85e4-c717f16a9642-vault:8201
HA Mode standby
Active Node Address http://4ba315ed-3387-4de0-85e4-c717f16a9642-vault:8200
Rest API
$ curl -X GET http://127.0.0.1:8200/v1/sys/seal-status
- 数据读取
- 激活secrets引擎
查看secrets激活状态
$ VAULT_ADDR=http://localhost:8200 VAULT_TOKEN=s.azwvCg0qucGM8pTH9Z9FDb2B vault secrets list
在能够进行secret的读写操作之前,必须先激活secrets引擎。
激活操作也初始化一样,整个集群只需要第一次启动的时候执行一次就行。
$ VAULT_ADDR=http://localhost:8200 VAULT_TOKEN=s.jLx1tMuxBGcfcFdYR7oZuPzN vault secrets enable -path=secret/ kv
这里用到了TOKEN,是在vault初始化的时候生成的。
Rest API
$ curl -X POST -H "X-Vault-Token:s.azwvCg0qucGM8pTH9Z9FDb2B" -d '{"type":"kv"}' http://127.0.0.1:8200/v1/sys/mounts/secret
- 存入
$ VAULT_ADDR=http://localhost:8200 VAULT_TOKEN=s.jLx1tMuxBGcfcFdYR7oZuPzN vault kv put secret/mypath mykey=myvalue
Rest API
$ curl -X POST -H "X-Vault-Token:s.WjNV37NgVvt9aKOjmQn4NCT7" -d '{"mykey1":"myvalue1"}' http://127.0.0.1:8200/v1/secret/mypath1
- 读取
$ VAULT_ADDR=http://localhost:8200 VAULT_TOKEN=s.jLx1tMuxBGcfcFdYR7oZuPzN vault kv get -format=json secret/mypath
{
"request_id": "5c6b0048-adab-b7a1-37ed-2ecf32fcab67",
"lease_id": "",
"lease_duration": 2764800,
"renewable": false,
"data": {
"mykey": "myvalue"
},
"warnings": null
}
$ VAULT_ADDR=http://localhost:8200 VAULT_TOKEN=s.jLx1tMuxBGcfcFdYR7oZuPzN vault kv get secret/mypath
==== Data ====
Key Value
--- -----
mykey myvalue
$ VAULT_ADDR=http://localhost:8200 VAULT_TOKEN=s.jLx1tMuxBGcfcFdYR7oZuPzN vault kv get -field=mykey secret/mypath
myvalue
这个地方需要注意的是,虽然每个path(secert/mypath)下面有多个key/value对,但他们都是值,实际上只有path才是唯一的key,这就是说不能对一个path下面的单独key进行修改,vault只能覆盖整个path的内容,例如:
$ ... vault kv put secret/mypath mykey1=myvalue1 mykey2=myvalue2
$ ... vault kv put secret/mypath mykey1=myvalue3
Rest API
$ curl -X GET -H "X-Vault-Token:s.7AiCBWsFbptKnKhMHDL1Y9n9" http://127.0.0.1:8200/v1/secret/mypath1 | jq
上面这两条命令,后面一条可能是想修改mykey1的值,保留mykey2的值,但实际情况会覆盖前面一条的所有值,即mykey2会丢掉。
网友评论