1.什么是Delegation Token?
DeleagtionToken(委托令牌)是Keberos认证提出的一种轻量级认证机制,采用委托令牌的方式直接进行权限的精细控制。
委托令牌的特点:
短期有效,无需暴露用户凭证
2.有什么应用场景?
因为委托令牌具有短期有效,且用户凭证不透明的特点,可以应用于以下场景:
代理操作,临时授权,审计和跟踪。
3.如何使用Delegation Token?
这里主要列举Kafka中DelegationToken的使用过程。因为DelegationToken在Kafka中采用SCRAM方式实现验证,所以需要首先对Kafka配置SCRAM。
3.1 配置SCRAM
3.1.1 添加SCRAM用户
因为SCRAM使用Zookeeper实现,所以需要先用Kafka脚本,创建SCRAM账户。
[root]# ./bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'SCRAM-SHA-256=[password=admin-sec],SCRAM-SHA-512=[password=admin-sec]' --entity-type users --entity-name admin
Completed Updating config for entity: user-principal 'admin'.
3.1.2 配置Kafka的Broker
listeners=SASL_PLAINTEXT://CentOS:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256
sasl.enabled.mechanisms=SCRAM-SHA-256
allow.everyone.if.no.acl.found=false
super.users=User:admin
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
...
zookeeper.connect=localhost:2181
3.1.3 配置Broker的安全文件
创建文件server.jaas。内容如下:
KafkaServer {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="admin"
password="admin-sec";
};
3.1.4 设置Broker启动时加载安全文件
修改kafka启动脚本bin/kafka-server-start.sh,如图所示:
# JVM performance options
if [ -z "$KAFKA_JVM_PERFORMANCE_OPTS" ]; then
KAFKA_JVM_PERFORMANCE_OPTS="-server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -Djava.awt.headless=true -Dzookeeper.sasl.client=false -Djava.security.auth.login.config=/usr/kafka_2.11-2.2.0/config/server.jaas $KAFKA_JAAS"
fi
配置完成之后,broker这边的工作就完成了。
3.1.5 设置客户端的安全文件
创建client.jaas文件,内容如下:
security.protocol=SASL_PLAINTEXT
sasl.mechanism=SCRAM-SHA-256
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="admin-sec";
3.2 DelegationToken的管理
安全环境搭建完毕后,就可以开始DelegationToken的操作了。
3.2.1 启用DelegationToken
编辑config/server.properties。添加关于DelegationToken的配置:
delegation.token.master.key=yoursec
#这里填写你自己的任意加密字符串,作为token生成的基础
#还可以配置有效期之类的,根据自己需要配置
3.2.2 创建DelegationToken
调用kafka提供的脚本,生成token。
#bin/kafka-delegation-tokens.sh --bootstrap-server localhost:9092 --create --max-life-time-period -1 --command-config client.jaas--renewer-principal User:admin
kafka提供了完善的管理脚本可以对token进行过期或者更新操作。
#更新token
> bin/kafka-delegation-tokens.sh --bootstrap-server localhost:9092 --renew --renew-time-period -1 --command-config client.properties --hmac ABCDEFGHIJK
#过期token
> bin/kafka-delegation-tokens.sh --bootstrap-server localhost:9092 --expire --expiry-time-period -1 --command-config client.properties --hmac ABCDEFGHIJK
#查看token
> bin/kafka-delegation-tokens.sh --bootstrap-server localhost:9092 --describe --command-config client.properties --owner-principal User:user1
3.3 客户端使用方法
使用的方式和client.jass类似,新建一个安全文件token.config。内容如下:
security.protocol=SASL_PLAINTEXT
sasl.mechanism=SCRAM-SHA-256
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="5TLa6I*******7bQ" password="nfUZGw*****************lMRg0WCzqXUrVQ25OAVNVXOMDk8G+IX6hPJNqNncSg==" tokenauth="true";
#username使用token id,在使用脚本创建的时候会有这个信息。
#password使用token的HMAC信息,这个创建的时候也会有。
#需要设置tokenauth=true
然后客户端访问的时候,使用对应的配置参数就可以了。