SecurityUtil { public static List<ZKUtil.ZKAuthInfo> getZKAuthInfos(Configuration conf, String configKey) throws IOException { char[] zkAuthChars = conf.getPassword(configKey); String zkAuthConf = zkAuthChars != null ? String.valueOf(zkAuthChars) : null; try { zkAuthConf = ZKUtil.resolveConfIndirection(zkAuthConf); if (zkAuthConf != null) { return ZKUtil.parseAuth(zkAuthConf); } else { return Collections.emptyList(); } } catch (IOException | ZKUtil.BadAuthFormatException e) { LOG.error("Couldn't read Auth based on {}", configKey); throw e; } } protected  SecurityUtil(); @InterfaceAudience.Public @InterfaceStability.Evolving static void setConfiguration(Configuration conf); @InterfaceAudience.Private @VisibleForTesting static void setTokenServiceUseIp(boolean flag); @InterfaceAudience.Public @InterfaceStability.Evolving static String getServerPrincipal(String principalConfig,
      String hostname); @InterfaceAudience.Public @InterfaceStability.Evolving static String getServerPrincipal(String principalConfig,
      InetAddress addr); @InterfaceAudience.Public @InterfaceStability.Evolving static void login(final Configuration conf,
      final String keytabFileKey, final String userNameKey); @InterfaceAudience.Public @InterfaceStability.Evolving static void login(final Configuration conf,
      final String keytabFileKey, final String userNameKey, String hostname); static String buildDTServiceName(URI uri, int defPort); static String getHostFromPrincipal(String principalName); @InterfaceAudience.Private static void setSecurityInfoProviders(SecurityInfo... providers); static KerberosInfo getKerberosInfo(Class<?> protocol, Configuration conf); static TokenInfo getTokenInfo(Class<?> protocol, Configuration conf); static InetSocketAddress getTokenServiceAddr(Token<?> token); static void setTokenService(Token<?> token, InetSocketAddress addr); static Text buildTokenService(InetSocketAddress addr); static Text buildTokenService(URI uri); static T doAsLoginUserOrFatal(PrivilegedAction<T> action); static T doAsLoginUser(PrivilegedExceptionAction<T> action); static T doAsCurrentUser(PrivilegedExceptionAction<T> action); @InterfaceAudience.Private static InetAddress getByName(String hostname); static AuthenticationMethod getAuthenticationMethod(Configuration conf); static void setAuthenticationMethod(
      AuthenticationMethod authenticationMethod, Configuration conf); static boolean isPrivilegedPort(final int port); static List<ZKUtil.ZKAuthInfo> getZKAuthInfos(Configuration conf,
      String configKey); static final Logger LOG; static final String HOSTNAME_PATTERN; static final String FAILED_TO_GET_UGI_MSG_HEADER; }
@Test public void testAuthPlainPasswordProperty() throws Exception { Configuration conf = new Configuration(); conf.set(CommonConfigurationKeys.ZK_AUTH, ZK_AUTH_VALUE); List<ZKAuthInfo> zkAuths = SecurityUtil.getZKAuthInfos(conf, CommonConfigurationKeys.ZK_AUTH); assertEquals(1, zkAuths.size()); ZKAuthInfo zkAuthInfo = zkAuths.get(0); assertEquals("a_scheme", zkAuthInfo.getScheme()); assertArrayEquals("a_password".getBytes(), zkAuthInfo.getAuth()); }
@Test public void testAuthPlainTextFile() throws Exception { Configuration conf = new Configuration(); File passwordTxtFile = File.createTempFile( getClass().getSimpleName() + ".testAuthAtPathNotation-", ".txt"); Files.write(ZK_AUTH_VALUE, passwordTxtFile, StandardCharsets.UTF_8); try { conf.set(CommonConfigurationKeys.ZK_AUTH, "@" + passwordTxtFile.getAbsolutePath()); List<ZKAuthInfo> zkAuths = SecurityUtil.getZKAuthInfos(conf, CommonConfigurationKeys.ZK_AUTH); assertEquals(1, zkAuths.size()); ZKAuthInfo zkAuthInfo = zkAuths.get(0); assertEquals("a_scheme", zkAuthInfo.getScheme()); assertArrayEquals("a_password".getBytes(), zkAuthInfo.getAuth()); } finally { boolean deleted = passwordTxtFile.delete(); assertTrue(deleted); } }
@Test public void testAuthLocalJceks() throws Exception { File localJceksFile = File.createTempFile( getClass().getSimpleName() +".testAuthLocalJceks-", ".localjceks"); populateLocalJceksTestFile(localJceksFile.getAbsolutePath()); try { String localJceksUri = "localjceks: localJceksFile.getAbsolutePath(); Configuration conf = new Configuration(); conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, localJceksUri); List<ZKAuthInfo> zkAuths = SecurityUtil.getZKAuthInfos(conf, CommonConfigurationKeys.ZK_AUTH); assertEquals(1, zkAuths.size()); ZKAuthInfo zkAuthInfo = zkAuths.get(0); assertEquals("a_scheme", zkAuthInfo.getScheme()); assertArrayEquals("a_password".getBytes(), zkAuthInfo.getAuth()); } finally { boolean deleted = localJceksFile.delete(); assertTrue(deleted); } }