认证
在开启TLS之后,客户端就可以验证连接的服务端的合法性,同时可以保证中间数据处于加密状态,但服务端无法验证客户的合法性,所以认证机制提供了一个服务端可以对可信的客户端进行验证的功能。
认证功能还提供了另一个功能,它可以赋予每个客户端一个角色名,hstream在这个基础上实现授权等功能。
hstream目前只支持基于TLS的认证功能,这是TLS的一个扩展, 为了启用TLS认证功能,你需要为一个角色创建密钥和证书, 然后把这个角色的密钥和证书交给可信的客户端, 这些客户端就可以通过这个绑定了相应角色的密钥和证书连接服务端。
创建一个可信角色
生成密钥:
1. openssl genrsa -out role01.key.pem 2048
把密钥转换成PKCS 8格式(Java客户端需要这种格式的密钥)
1. openssl pkcs8 -topk8 -inform PEM -outform PEM \
2. -in role01.key.pem -out role01.key-pk8.pem -nocrypt
生成证书请求(填写Common Name时,需要填写角色名):
1. openssl req -config openssl.cnf \
2. -key role01.key.pem -new -sha256 -out role01.csr.pem
生成签名证书:
1. openssl ca -config openssl.cnf -extensions usr_cert \
2. -days 1000 -notext -md sha256 \
3. -in role01.csr.pem -out signed.role01.cert.pem
配置
对于hstream服务端,你可以通过设置tls-ca-path来开启TLS认证功能,如:
1. # TLS options
2. #
3. # enable tls, which requires tls-key-path and tls-cert-path options
4. enable-tls: true
5. #
6. # key file path for tls, can be generated by openssl
7. tls-key-path: /path/to/the/server.key.pem
8. #
9. # the signed certificate by CA for the key(tls-key-path)
10. tls-cert-path: /path/to/the/signed.server.cert.pem
11. #
12. # optional for tls, if tls-ca-path is not empty, then enable TLS authentication,
13. # in the handshake phase,
14. # the server will request and verify the client's certificate.
15. tls-ca-path: /path/to/the/ca.cert.pem
Java客户端示例:
1. HStreamClient.builder()
2. .serviceUrl(serviceUrl)
3. // enable tls
4. .enableTLS()
5. .tlsCaPath("/path/to/ca.pem")
7. // for authentication
8. .enableTlsAuthentication()
9. .tlsKeyPath("path/to/role01.key-pk8.pem")
10. .tlsCertPath("path/to/signed.role01.cert.pem")
12. .build()
