consul ACL配置使用

consul自带ACL控制功能,看了很多遍官方文档,没有配置步骤
https://www.consul.io/docs/internals/acl.html 主要对各种配置参数解释,没有明确的步骤,当时唯一疑惑的是怎样生成ACL规则。看了很多相关的blog都是相似的内容,都是基础的安装测试而已,没有提到具体配置ACL,估计更多的只是实验尝试而已,没有涉及ACL配置使用。后来有辛搜到了一片文章才恍然大悟,明白ACL配置是怎么回事了,http://qiita.com/yunano/items/931448a590c7f346ed01。我之后是这样配置的:
1、对数据中心的每个server,添加acl_config.json配置:

1
2
3
4
5
{
"acl_datacenter": "datacenter-tag",
"acl_master_token": "xxxxxxxxxx9cda01",
"acl_default_policy": "deny"
}

这3个参数每个server模式的node都必须有。相关参数解释官方文档都有,https://www.consul.io/docs/agent/options.html
acl_default_policy默认值值是allow,即能够执行任何操作,这里需要关闭。
acl_master_token需要在每个server上配置,有management级别的权限,相当于一个种子token。
acl_datacenter区域的标识。
2、通过API接口 /v1/acl/create 创建一个management用户用于管理token的权限分配,这里生成这个management级别的token需要之前配置文件里面的种子token。

1
2
$ curl -H "X-Consul-Token: secret" -X PUT -d '{"Name": "datacenter-tag", "Type": "management"}' http://127.0.0.1:8500/v1/acl/create?token=xxxxxxxxxx9cda01
{"ID":"xxxxxxxxxxx-xxxx-xxxx-xxxxxxxxxxx"}

3、可以将这个management权限的token配置在ui节目管理的节点上,便于管理ACL、k/v、service等(但是我没有这么干,不然这个node的权限太大,不便于控制)。

1
2
3
4
5
6
{
"acl_datacenter": "datacenter-tag",
"acl_master_token": "xxxxxxxxxxx9cda01",
"acl_token": "b9exxxxx-xxxx-xxxx-xxx-xxxxxxxxxx291ba",
"acl_default_policy": "deny"
}

现在就可以通过ui界面管理token的权限分配了(制订ACL规则)。
consul-acl

4、合理分配token的权限(制定ACL规则),官方文档有一例分配说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Default all keys to read-only
key "" {
policy = "read"
}
key "foo/" {
policy = "write"
}
key "foo/private/" {
# Deny access to the dir "foo/private"
policy = "deny"
}
# Default all services to allow registration. Also permits all
# services to be discovered.
service "" {
policy = "write"
}
# Deny registration access to services prefixed "secure-".
# Discovery of the service is still allowed in read mode.
service "secure-" {
policy = "read"
}
# Allow firing any user event by default.
event "" {
policy = "write"
}
# Deny firing events prefixed with "destroy-".
event "destroy-" {
policy = "deny"
}
# Default prepared queries to read-only.
query "" {
policy = "read"
}
# Read-only mode for the encryption keyring by default (list only)
keyring = "read"

API注册ACL规则用JSION数据格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
"key": {
"": {
"policy": "read"
},
"foo/": {
"policy": "write"
},
"foo/private": {
"policy": "deny"
}
},
"service": {
"": {
"policy": "write"
},
"secure-": {
"policy": "read"
}
},
"event": {
"": {
"policy": "write"
},
"destroy-": {
"policy": "deny"
}
},
"query": {
"": {
"policy": "read"
}
},
"keyring": "read"
}

创建好ACL后,将生成的acl_token xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx 配置到每个agent,除了那个ui的server节点,当然这个节点也可以配置这个token,只是权限没有那么大了,管理不是很便捷。
client节点的acl_config.json配置:

1
2
3
4
{
"acl_datacenter": "datacenter-tag",
"acl_token": "xxxxxx-4bf0-xxxx-2079-xxxxxxxxx"
}

server节点的acl_config.json配置(UI的server节点除外):

1
2
3
4
5
6
{
"acl_datacenter": "datacenter-tag",
"acl_master_token": "xxxxxxxxxxxxxxxxx",
"acl_token": "xxxxxxx-xxxx-xxxx-2079-xxxxxxxxxx",
"acl_default_policy": "deny"
}

测试ACL是否生效:

1
2
3
4
5
$ curl -X PUT -d 'test' http://127.0.0.1:8500/v1/kv/web/key1
rpc error: Permission denied
$
$ curl -X PUT -d 'test' http://127.0.0.1:8500/v1/kv/foo/key1
true

虽然分享了知识但也得为安全考虑,文章中所有敏感信息均已处理,比如token都是未知或无效的等。

----------------本文结束 感谢阅读----------------