|
16 | 16 | import org.apache.hadoop.classification.VisibleForTesting; |
17 | 17 | import java.nio.ByteBuffer; |
18 | 18 | import java.security.SecureRandom; |
19 | | -import java.util.Collections; |
20 | | -import java.util.List; |
21 | 19 | import java.util.Properties; |
22 | 20 | import java.util.Random; |
23 | | -import javax.security.auth.login.Configuration; |
24 | 21 | import javax.servlet.ServletContext; |
25 | | -import org.apache.curator.RetryPolicy; |
26 | 22 | import org.apache.curator.framework.CuratorFramework; |
27 | | -import org.apache.curator.framework.CuratorFrameworkFactory; |
28 | | -import org.apache.curator.framework.api.ACLProvider; |
29 | | -import org.apache.curator.framework.imps.DefaultACLProvider; |
30 | | -import org.apache.curator.retry.ExponentialBackoffRetry; |
31 | 23 | import org.apache.hadoop.classification.InterfaceAudience; |
32 | 24 | import org.apache.hadoop.classification.InterfaceStability; |
33 | 25 | import org.apache.zookeeper.KeeperException; |
34 | | -import org.apache.zookeeper.ZooDefs.Perms; |
35 | | -import org.apache.zookeeper.client.ZKClientConfig; |
36 | | -import org.apache.zookeeper.data.ACL; |
37 | | -import org.apache.zookeeper.data.Id; |
38 | 26 | import org.apache.zookeeper.data.Stat; |
39 | 27 | import org.slf4j.Logger; |
40 | 28 | import org.slf4j.LoggerFactory; |
@@ -92,6 +80,16 @@ public class ZKSignerSecretProvider extends RolloverSignerSecretProvider { |
92 | 80 | public static final String ZOOKEEPER_KERBEROS_PRINCIPAL = |
93 | 81 | CONFIG_PREFIX + "kerberos.principal"; |
94 | 82 |
|
| 83 | + public static final String ZOOKEEPER_SSL_ENABLED = CONFIG_PREFIX + "ssl.enabled"; |
| 84 | + public static final String ZOOKEEPER_SSL_KEYSTORE_LOCATION = |
| 85 | + CONFIG_PREFIX + "ssl.keystore.location"; |
| 86 | + public static final String ZOOKEEPER_SSL_KEYSTORE_PASSWORD = |
| 87 | + CONFIG_PREFIX + "ssl.keystore.password"; |
| 88 | + public static final String ZOOKEEPER_SSL_TRUSTSTORE_LOCATION = |
| 89 | + CONFIG_PREFIX + "ssl.truststore.location"; |
| 90 | + public static final String ZOOKEEPER_SSL_TRUSTSTORE_PASSWORD = |
| 91 | + CONFIG_PREFIX + "ssl.truststore.password"; |
| 92 | + |
95 | 93 | /** |
96 | 94 | * Constant for the property that specifies whether or not the Curator client |
97 | 95 | * should disconnect from ZooKeeper on shutdown. The default is "true". Only |
@@ -350,80 +348,33 @@ protected byte[] generateRandomSecret() { |
350 | 348 | * This method creates the Curator client and connects to ZooKeeper. |
351 | 349 | * @param config configuration properties |
352 | 350 | * @return A Curator client |
353 | | - * @throws Exception thrown if an error occurred |
354 | 351 | */ |
355 | | - protected CuratorFramework createCuratorClient(Properties config) |
356 | | - throws Exception { |
357 | | - String connectionString = config.getProperty( |
358 | | - ZOOKEEPER_CONNECTION_STRING, "localhost:2181"); |
359 | | - |
360 | | - RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
361 | | - ACLProvider aclProvider; |
| 352 | + protected CuratorFramework createCuratorClient(Properties config) { |
| 353 | + String connectionString = config.getProperty(ZOOKEEPER_CONNECTION_STRING, "localhost:2181"); |
362 | 354 | String authType = config.getProperty(ZOOKEEPER_AUTH_TYPE, "none"); |
363 | | - if (authType.equals("sasl")) { |
364 | | - LOG.info("Connecting to ZooKeeper with SASL/Kerberos" |
365 | | - + "and using 'sasl' ACLs"); |
366 | | - String principal = setJaasConfiguration(config); |
367 | | - System.setProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, |
368 | | - JAAS_LOGIN_ENTRY_NAME); |
369 | | - System.setProperty("zookeeper.authProvider.1", |
370 | | - "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); |
371 | | - aclProvider = new SASLOwnerACLProvider(principal); |
372 | | - } else { // "none" |
373 | | - LOG.info("Connecting to ZooKeeper without authentication"); |
374 | | - aclProvider = new DefaultACLProvider(); // open to everyone |
375 | | - } |
376 | | - CuratorFramework cf = CuratorFrameworkFactory.builder() |
377 | | - .connectString(connectionString) |
378 | | - .retryPolicy(retryPolicy) |
379 | | - .aclProvider(aclProvider) |
380 | | - .build(); |
381 | | - cf.start(); |
382 | | - return cf; |
383 | | - } |
384 | | - |
385 | | - private String setJaasConfiguration(Properties config) throws Exception { |
386 | | - String keytabFile = config.getProperty(ZOOKEEPER_KERBEROS_KEYTAB).trim(); |
387 | | - if (keytabFile == null || keytabFile.length() == 0) { |
388 | | - throw new IllegalArgumentException(ZOOKEEPER_KERBEROS_KEYTAB |
389 | | - + " must be specified"); |
390 | | - } |
391 | | - String principal = config.getProperty(ZOOKEEPER_KERBEROS_PRINCIPAL) |
392 | | - .trim(); |
393 | | - if (principal == null || principal.length() == 0) { |
394 | | - throw new IllegalArgumentException(ZOOKEEPER_KERBEROS_PRINCIPAL |
395 | | - + " must be specified"); |
396 | | - } |
| 355 | + String keytab = config.getProperty(ZOOKEEPER_KERBEROS_KEYTAB, "").trim(); |
| 356 | + String principal = config.getProperty(ZOOKEEPER_KERBEROS_PRINCIPAL, "").trim(); |
397 | 357 |
|
398 | | - // This is equivalent to writing a jaas.conf file and setting the system |
399 | | - // property, "java.security.auth.login.config", to point to it |
400 | | - JaasConfiguration jConf = |
401 | | - new JaasConfiguration(JAAS_LOGIN_ENTRY_NAME, principal, keytabFile); |
402 | | - Configuration.setConfiguration(jConf); |
403 | | - return principal.split("[/@]")[0]; |
404 | | - } |
| 358 | + boolean sslEnabled = Boolean.parseBoolean(config.getProperty(ZOOKEEPER_SSL_ENABLED, "false")); |
| 359 | + String keystoreLocation = config.getProperty(ZOOKEEPER_SSL_KEYSTORE_LOCATION, ""); |
| 360 | + String keystorePassword = config.getProperty(ZOOKEEPER_SSL_KEYSTORE_PASSWORD, ""); |
| 361 | + String truststoreLocation = config.getProperty(ZOOKEEPER_SSL_TRUSTSTORE_LOCATION, ""); |
| 362 | + String truststorePassword = config.getProperty(ZOOKEEPER_SSL_TRUSTSTORE_PASSWORD, ""); |
405 | 363 |
|
406 | | - /** |
407 | | - * Simple implementation of an {@link ACLProvider} that simply returns an ACL |
408 | | - * that gives all permissions only to a single principal. |
409 | | - */ |
410 | | - private static class SASLOwnerACLProvider implements ACLProvider { |
411 | | - |
412 | | - private final List<ACL> saslACL; |
413 | | - |
414 | | - private SASLOwnerACLProvider(String principal) { |
415 | | - this.saslACL = Collections.singletonList( |
416 | | - new ACL(Perms.ALL, new Id("sasl", principal))); |
417 | | - } |
418 | | - |
419 | | - @Override |
420 | | - public List<ACL> getDefaultAcl() { |
421 | | - return saslACL; |
422 | | - } |
423 | | - |
424 | | - @Override |
425 | | - public List<ACL> getAclForPath(String path) { |
426 | | - return saslACL; |
427 | | - } |
| 364 | + CuratorFramework zkClient = |
| 365 | + ZookeeperClient.configure() |
| 366 | + .withConnectionString(connectionString) |
| 367 | + .withAuthType(authType) |
| 368 | + .withKeytab(keytab) |
| 369 | + .withPrincipal(principal) |
| 370 | + .withJaasLoginEntryName(JAAS_LOGIN_ENTRY_NAME) |
| 371 | + .enableSSL(sslEnabled) |
| 372 | + .withKeystore(keystoreLocation) |
| 373 | + .withKeystorePassword(keystorePassword) |
| 374 | + .withTruststore(truststoreLocation) |
| 375 | + .withTruststorePassword(truststorePassword) |
| 376 | + .create(); |
| 377 | + zkClient.start(); |
| 378 | + return zkClient; |
428 | 379 | } |
429 | 380 | } |
0 commit comments