基于token的身份验证概述
Pulsar支持基于JSON Web Tokens (RFC-7519) 的security tokens 来对客户端(clients)进行身份认证。
你可以使用tokens来标识Pulsar client,将其与允许执行某些操作(如: 向topic发布消息或消费消息)的 “principal 主体”(或“role 角色”)关联。
作为用户,你通常从管理员(或某个自动化服务)那里获取token字符串。
一个已签名(signed)的JWT字符串看起来像下面这样:
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY
应用程序在创建pulsar client实例时要指定所用的token。另一种方法是提供一个token supplier参数(该参数是一个函数,当客户端需要token时就调用该函数实时地获取一个token)。
注意:为了更安全地进行token传递,客户端与服务端的通信最好启用TLS传输加密。
Secret vs Public/Private keys (基于密钥的key VS 基于公钥/私钥的key)
JWT支持2种不同类型的key,它们都可以用来生成和验证token。
Symmetric(对称的) :
只有一个单一的 Secret key,既用于token生成也用于token校验。
Asymmetric(非对称的): 有一对keys。
Private key(私钥) 用于生成token。
Public key(公钥用于) 用于校验token。
本文只介绍Secret Keys的使用,Public/Private Keys的使用请参考https://pulsar.apache.org/docs/en/security-token-admin/。
Secret Key (密钥)
基于密钥的方式,管理员需要先创建密钥,再使用密钥来生成客户端token。
密钥也需要配置给brokers,brokers在对客户端进行验证时需要它们。
创建密钥
以下命令默认将密钥创建在pulsar安装目录下,你可以指定别的绝对路径。
bin/pulsar tokens create-secret-key \--output my-secret.key
如果你需要base64编码的密钥:
bin/pulsar tokens create-secret-key \--output /opt/my-secret.key --base64
生成tokens
token是与用户关联的凭据。关联是通过“主体 principal”或“角色 role”来完成的。对于JWT令牌,这个字段通常被称为subject,尽管它是完全相同的概念。
因此 生成token时需要指定subject字段:
bin/pulsar tokens create \--secret-key file:///path/to/my-secret.key \--subject test-user
该命令会把token字符串打印到控制台。
我们也可以为token指定有效时间,时间一到token将自动失效:
bin/pulsar tokens create \--secret-key file:///path/to/my-secret.key \--subject test-user--expiry-time 1y
授权(Authorization)
token本身没有包含任何权限信息。权限是由授权引擎决定的。创建完token后,你可以给token授权,允许它执行某些操作。
bin/pulsar-admin namespaces grant-permission \my-tenant/my-namespace \--role test-user \--actions produce,consume
启用token验证
生成完token,设置好权限后,还需要在服务端启用token验证,它们才会生效。
Broker端配置
要让broker端对client端进行身份验证,在broker.conf中加入以下配置:
# Configuration to enable authentication and authorizationauthenticationEnabled=trueauthorizationEnabled=trueauthenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderToken# If using secret keytokenSecretKey=file:///path/to/secret.key# The key can also be passed inline:# tokenSecretKey=data:base64,FLFyW0oLJ2Fi22KKCm21J18mbAdztfSHN/lAT5ucEKU=# If using public/private# tokenPublicKey=file:///path/to/public.key# operations and publish/consume from all topicssuperUserRoles=admin# Authentication settings of the broker itself. Used when the broker connects to other brokers, either in same or other clustersbrokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTokenbrokerClientAuthenticationParameters=token:eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.9OHgE9ZUDeBTZs7nSMEFIuGNEX18FLR3qvy8mqxSxXw# Or, alternatively, read token from file# brokerClientAuthenticationParameters=file:///path/to/admin-token.txt
注意: 上面19行的token,是你为 admin 账户另外生成的。
Proxy 端配置
这一项一般用不到,但以防你需要,下面是配置(位于proxy.conf)。
proxy在和brokers通信时,需要它自己的token(即,下面第9行中的token,是你为proxy另外生成的)。
# For clients connecting to the proxyauthenticationEnabled=trueauthorizationEnabled=trueauthenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderTokentokenSecretKey=file:///path/to/secret.key# For the proxy to connect to brokersbrokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTokenbrokerClientAuthenticationParameters=token:eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.9OHgE9ZUDeBTZs7nSMEFIuGNEX18FLR3qvy8mqxSxXw# Or, alternatively, read token from file# brokerClientAuthenticationParameters=file:///path/to/proxy-token.txt
CLI 工具
一些命令行工具,如 pulsar-admin、pulsar-perf、pulsar-client,用的是pulsar安装路径下的 conf/client.conf 配置文件。启用token验证后,也需要为它们配置可用的token。
# For clients connecting to the proxyauthenticationEnabled=trueauthorizationEnabled=trueauthenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderTokentokenSecretKey=file:///path/to/secret.key# For the proxy to connect to brokersbrokerClientAuthenticationPlugin=org.apache.pulsar.client.impl.auth.AuthenticationToken# 需要另外生成,一般是admin角色的tokenbrokerClientAuthenticationParameters=token:eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.9OHgE9ZUDeBTZs7nSMEFIuGNEX18FLR3qvy8mqxSxXw# Or, alternatively, read token from file# brokerClientAuthenticationParameters=file:///path/to/proxy-token.txt
如何在pulsar client中指定token
以node.js为例,只需要在实例化client对象时,传入authentication参数:
const token='eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ5b3Vtb2...';const client = new Pulsar.Client({serviceUrl: 'pulsar://localhost:6650',authentication: new Pulsar.AuthenticationToken({token})});
参考:
https://pulsar.apache.org/docs/en/next/security-jwt/
https://pulsar.apache.org/docs/en/security-token-admin/
https://github.com/apache/pulsar/issues/4560




