当前位置:   article > 正文

Zookeeper_sasl configuration failed

sasl configuration failed

Zookeeper客户端使用:

  1. 使用DefaultZookeeperFactory创建默认的Zookeeper;
  1. DefaultZookeeperFactory factory = new DefaultZookeeperFactory();
  2. factory.newZooKeeper(connectString, sessionTimeout, null, false);

    2. 直接使用Zookeeper的构造函数;

ZooKeeper zookeeper = new ZooKeeper(connectString, sessionTimeout, (event)->{}, clientConfig);

 如果所有采用默认的配置,在创建Zookeeper后建立连接时,Zookeeper的SendThread在建立连接时,startConnect()函数,先判断是否采用SASL连接,默认情况下回使用SASL连接,即创建ZooKeeperSaslClient对象。如果未配置SASL,此过程会阻塞正常与Zookeeper的连接创建,导致Zookeeper对象创建后不能立即建立与Zookeeper的连接,因此为了加快建立与Zookeeper的连接,需要将相关参数修改。

  1. ZKClientConfig clientConfig = new ZKClientConfig();
  2. clientConfig.setProperty("zookeeper.sasl.client", "false");
  3. ZooKeeper zooKeeper =new ZooKeeper(connectString, sessionTimeout, (event)->{}, clientConfig);

    ClientCnxn类中SendThread的startConnect方法:

  1. private void startConnect(InetSocketAddress addr) throws IOException {
  2. // initializing it for new connection
  3. saslLoginFailed = false;
  4. if (!isFirstConnect) {
  5. try {
  6. Thread.sleep(r.nextInt(1000));
  7. } catch (InterruptedException e) {
  8. LOG.warn("Unexpected exception", e);
  9. }
  10. }
  11. state = States.CONNECTING;
  12. String hostPort = addr.getHostString() + ":" + addr.getPort();
  13. MDC.put("myid", hostPort);
  14. setName(getName().replaceAll("\\(.*\\)", "(" + hostPort + ")"));
  15. if (clientConfig.isSaslClientEnabled()) {
  16. try {
  17. if (zooKeeperSaslClient != null) {
  18. zooKeeperSaslClient.shutdown();
  19. }
  20. zooKeeperSaslClient = new ZooKeeperSaslClient(SaslServerPrincipal.getServerPrincipal(addr, clientConfig), clientConfig);
  21. } catch (LoginException e) {
  22. // An authentication error occurred when the SASL client tried to initialize:
  23. // for Kerberos this means that the client failed to authenticate with the KDC.
  24. // This is different from an authentication error that occurs during communication
  25. // with the Zookeeper server, which is handled below.
  26. LOG.warn(
  27. "SASL configuration failed. "
  28. + "Will continue connection to Zookeeper server without "
  29. + "SASL authentication, if Zookeeper server allows it.", e);
  30. eventThread.queueEvent(new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.AuthFailed, null));
  31. saslLoginFailed = true;
  32. }
  33. }
  34. logStartConnect(addr);
  35. clientCnxnSocket.connect(addr);
  36. }

对比zoookeeper3.4.X版本与3.5.X版本:

3.5.X版本:

  1. if (clientConfig.isSaslClientEnabled()) {
  2. try {
  3. if (zooKeeperSaslClient != null) {
  4. zooKeeperSaslClient.shutdown();
  5. }
  6. zooKeeperSaslClient = new ZooKeeperSaslClient(getServerPrincipal(addr), clientConfig);
  7. } catch (LoginException e) {
  8. private String getServerPrincipal(InetSocketAddress addr) {
  9. String principalUserName = clientConfig.getProperty(ZKClientConfig.ZK_SASL_CLIENT_USERNAME,
  10. ZKClientConfig.ZK_SASL_CLIENT_USERNAME_DEFAULT);
  11. String serverPrincipal = principalUserName + "/" + addr.getHostString();
  12. return serverPrincipal;
  13. }

3.4.X版本:

  1. if (ZooKeeperSaslClient.isEnabled()) {
  2. try {
  3. zooKeeperSaslClient = new ZooKeeperSaslClient(SaslServerPrincipal.getServerPrincipal(addr));
  4. } catch (LoginException e) {
  5. // An authentication error occurred when the SASL client tried to initialize:
  6. // for Kerberos this means that the client failed to authenticate with the KDC.
  7. // This is different from an authentication error that occurs during communication
  8. // with the Zookeeper server, which is handled below.
  9. LOG.warn("SASL configuration failed: " + e + " Will continue connection to Zookeeper server without "
  10. + "SASL authentication, if Zookeeper server allows it.");
  11. eventThread.queueEvent(new WatchedEvent(
  12. Watcher.Event.EventType.None,
  13. Watcher.Event.KeeperState.AuthFailed, null));
  14. saslLoginFailed = true;
  15. }
  16. }
  1. static String getServerPrincipal(WrapperInetSocketAddress addr) {
  2. String principalUserName = System.getProperty(ZK_SASL_CLIENT_USERNAME, "zookeeper");
  3. String hostName = addr.getHostName();
  4. boolean canonicalize = true;
  5. String canonicalizeText = System.getProperty(ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, "true");
  6. try {
  7. canonicalize = Boolean.parseBoolean(canonicalizeText);
  8. } catch (IllegalArgumentException ea) {
  9. LOG.warn("Could not parse config {} \"{}\" into a boolean using default {}",
  10. ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, canonicalizeText, canonicalize);
  11. }
  12. if (canonicalize) {
  13. WrapperInetAddress ia = addr.getAddress();
  14. if (ia == null) {
  15. throw new IllegalArgumentException("Unable to canonicalize address " + addr + " because it's not resolvable");
  16. }
  17. String canonicalHostName = ia.getCanonicalHostName();
  18. //avoid using literal IP address when security check fails
  19. if (!canonicalHostName.equals(ia.getHostAddress())) {
  20. hostName = canonicalHostName;
  21. }
  22. if (LOG.isDebugEnabled()) {
  23. LOG.debug("Canonicalized address to {}", hostName);
  24. }
  25. }
  26. String serverPrincipal = principalUserName + "/" + hostName;
  27. return serverPrincipal;
  28. }

两个版本的主要区别在与:3.4.X版本会去解析域名,导致创建了Zookeeper时,延迟创建连接。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/763558
推荐阅读
相关标签
  

闽ICP备14008679号