|
16 | 16 | */ |
17 | 17 | package org.apache.kafka.common.security; |
18 | 18 |
|
19 | | -import org.apache.kafka.common.KafkaException; |
20 | | - |
21 | | -import org.slf4j.Logger; |
22 | | -import org.slf4j.LoggerFactory; |
23 | | - |
24 | | -import javax.security.auth.login.Configuration; |
25 | | - |
26 | 19 | public final class JaasUtils { |
27 | | - private static final Logger LOG = LoggerFactory.getLogger(JaasUtils.class); |
28 | 20 | public static final String JAVA_LOGIN_CONFIG_PARAM = "java.security.auth.login.config"; |
29 | 21 | public static final String DISALLOWED_LOGIN_MODULES_CONFIG = "org.apache.kafka.disallowed.login.modules"; |
30 | 22 | public static final String DISALLOWED_LOGIN_MODULES_DEFAULT = "com.sun.security.auth.module.JndiLoginModule"; |
31 | 23 | public static final String SERVICE_NAME = "serviceName"; |
32 | 24 |
|
33 | | - public static final String ZK_SASL_CLIENT = "zookeeper.sasl.client"; |
34 | | - public static final String ZK_LOGIN_CONTEXT_NAME_KEY = "zookeeper.sasl.clientconfig"; |
35 | | - |
36 | | - private static final String DEFAULT_ZK_LOGIN_CONTEXT_NAME = "Client"; |
37 | | - private static final String DEFAULT_ZK_SASL_CLIENT = "true"; |
38 | | - |
39 | 25 | private JaasUtils() {} |
40 | 26 |
|
41 | | - public static String zkSecuritySysConfigString() { |
42 | | - String loginConfig = System.getProperty(JAVA_LOGIN_CONFIG_PARAM); |
43 | | - String clientEnabled = System.getProperty(ZK_SASL_CLIENT, "default:" + DEFAULT_ZK_SASL_CLIENT); |
44 | | - String contextName = System.getProperty(ZK_LOGIN_CONTEXT_NAME_KEY, "default:" + DEFAULT_ZK_LOGIN_CONTEXT_NAME); |
45 | | - return "[" + |
46 | | - JAVA_LOGIN_CONFIG_PARAM + "=" + loginConfig + |
47 | | - ", " + |
48 | | - ZK_SASL_CLIENT + "=" + clientEnabled + |
49 | | - ", " + |
50 | | - ZK_LOGIN_CONTEXT_NAME_KEY + "=" + contextName + |
51 | | - "]"; |
52 | | - } |
53 | | - |
54 | | - public static boolean isZkSaslEnabled() { |
55 | | - // Technically a client must also check if TLS mutual authentication has been configured, |
56 | | - // but we will leave that up to the client code to determine since direct connectivity to ZooKeeper |
57 | | - // has been deprecated in many clients and we don't wish to re-introduce a ZooKeeper jar dependency here. |
58 | | - boolean zkSaslEnabled = Boolean.parseBoolean(System.getProperty(ZK_SASL_CLIENT, DEFAULT_ZK_SASL_CLIENT)); |
59 | | - String zkLoginContextName = System.getProperty(ZK_LOGIN_CONTEXT_NAME_KEY, DEFAULT_ZK_LOGIN_CONTEXT_NAME); |
60 | | - |
61 | | - LOG.debug("Checking login config for Zookeeper JAAS context {}", zkSecuritySysConfigString()); |
62 | | - |
63 | | - boolean foundLoginConfigEntry; |
64 | | - try { |
65 | | - Configuration loginConf = Configuration.getConfiguration(); |
66 | | - foundLoginConfigEntry = loginConf.getAppConfigurationEntry(zkLoginContextName) != null; |
67 | | - } catch (Exception e) { |
68 | | - throw new KafkaException("Exception while loading Zookeeper JAAS login context " + |
69 | | - zkSecuritySysConfigString(), e); |
70 | | - } |
71 | | - |
72 | | - if (foundLoginConfigEntry && !zkSaslEnabled) { |
73 | | - LOG.error("JAAS configuration is present, but system property " + |
74 | | - ZK_SASL_CLIENT + " is set to false, which disables " + |
75 | | - "SASL in the ZooKeeper client"); |
76 | | - throw new KafkaException("Exception while determining if ZooKeeper is secure " + |
77 | | - zkSecuritySysConfigString()); |
78 | | - } |
79 | | - |
80 | | - return foundLoginConfigEntry; |
81 | | - } |
82 | 27 | } |
0 commit comments