Vault: 基础教程之策略

六、策略

策略可以控制用户可以访问的内容,授权和策略都采用相同的格式,所有的认证方法都会将认证映射到策略上。

策略格式及编写

策略时以HCL编写的,但它与json兼容:

# Normal servers have version 1 of KV mounted by default, so will need these
# paths:
path "secret/*" {
  capabilities = ["create"]
}
path "secret/foo" {
  capabilities = ["read"]
}

# Dev servers have version 2 of KV mounted by default, so will need these
# paths:
path "secret/data/*" {
  capabilities = ["create"]
}
path "secret/data/foo" {
  capabilities = ["read"]
}

在这个策略下,用户可以向secret/写入任何的密码,除了 secret/foo

将这个例子保存到my-policy.hcl

使用vault policy fmt my-policy.hcl来自动格式化策略。

现在我们可以上传策略文件:

$ vault policy write my-policy my-policy.hcl

也可以使用下面这种方式在命令行中编写策略:

vault policy write my-policy -<# Normal servers have version 1 of KV mounted by default, so will need these
# paths:
path "secret/*" {
  capabilities = ["create"]
}
path "secret/foo" {
  capabilities = ["read"]
}

# Dev servers have version 2 of KV mounted by default, so will need these
# paths:
path "secret/data/*" {
  capabilities = ["create"]
}
path "secret/data/foo" {
  capabilities = ["read"]
}
EOF

我们还可以使用vault policy list查看策略列表,并使用vault policy read my-policy来读取策略的内容。

测试这些策略

创建token并将它赋给策略:

$ vault token create -policy=my-policy
Key                  Value
---                  -----
token                8203710c-5e40-480f-dff0-751ff7198220
token_accessor       829f0db4-c958-08a6-e445-9a57c0845122
token_duration       768h
token_renewable      true
token_policies       ["default" "my-policy"]
identity_policies    []
policies             ["default" "my-policy"]

然后登录并测试。

将策略映射到认证方法

vault里是单一策略权限的,而auth则可以启用多个身份认证方法,任何身份认证方法都必须映射到这些策略上。
使用$ vault write auth/github/map/teams/default value=my-policy将策略写入。

你可能感兴趣的:(vault)