DFSUtil { static String getPassword(Configuration conf, String alias) { String password = null; try { char[] passchars = conf.getPassword(alias); if (passchars != null) { password = new String(passchars); } } catch (IOException ioe) { password = null; } return password; } private  DFSUtil(); static int compareBytes(byte[] left, byte[] right); static Random getRandom(); static SecureRandom getSecureRandom(); static T[] shuffle(final T[] array); static boolean isValidName(String src); static boolean isValidNameForComponent(String component); static boolean isReservedPathComponent(String component); static String bytes2String(byte[] bytes); static String bytes2String(byte[] bytes, int offset, int length); static byte[] string2Bytes(String str); static String byteArray2PathString(byte[][] pathComponents,
      int offset, int length); static String byteArray2PathString(byte[][] pathComponents); static String strings2PathString(String[] components); static byte[] byteArray2bytes(byte[][] pathComponents); static String path2String(final Object path); static byte[][] bytes2byteArray(byte[] bytes, byte separator); static byte[][] bytes2byteArray(byte[] bytes,
                                         int len,
                                         byte separator); static BlockLocation[] locatedBlocks2Locations(LocatedBlocks blocks); static BlockLocation[] locatedBlocks2Locations(List<LocatedBlock> blocks); static Collection<String> getNameServiceIds(Configuration conf); static Collection<String> getNameNodeIds(Configuration conf, String nsId); static String addKeySuffixes(String key, String... suffixes); static Map<String, InetSocketAddress> getRpcAddressesForNameserviceId(
      Configuration conf, String nsId, String defaultValue); static Set<String> getAllNnPrincipals(Configuration conf); static Map<String, Map<String, InetSocketAddress>> getHaNnRpcAddresses(
      Configuration conf); static Map<String, Map<String, InetSocketAddress>> getHaNnWebHdfsAddresses(
      Configuration conf, String scheme); static Map<String, Map<String, InetSocketAddress>> getBackupNodeAddresses(
      Configuration conf); static Map<String, Map<String, InetSocketAddress>> getSecondaryNameNodeAddresses(
      Configuration conf); static Map<String, Map<String, InetSocketAddress>> getNNServiceRpcAddresses(
      Configuration conf); static Map<String, Map<String, InetSocketAddress>> getNNServiceRpcAddressesForCluster(Configuration conf); static List<ConfiguredNNAddress> flattenAddressMap(
      Map<String, Map<String, InetSocketAddress>> map); static String addressMapToString(
      Map<String, Map<String, InetSocketAddress>> map); static String nnAddressesAsString(Configuration conf); static Collection<URI> getNsServiceRpcUris(Configuration conf); static Collection<URI> getNameServiceUris(Configuration conf,
      String... keys); static String getNameServiceIdFromAddress(final Configuration conf, 
      final InetSocketAddress address, String... keys); static URI getInfoServer(InetSocketAddress namenodeAddr,
      Configuration conf, String scheme); static URI getInfoServerWithDefaultHost(String defaultHost,
      Configuration conf, final String scheme); static String getHttpClientScheme(Configuration conf); static void setGenericConf(Configuration conf,
      String nameserviceId, String nnId, String... keys); static float getPercentUsed(long used, long capacity); static float getPercentRemaining(long remaining, long capacity); static String percent2String(double percentage); static int roundBytesToGB(long bytes); static ClientDatanodeProtocol createClientDatanodeProtocolProxy(
      DatanodeID datanodeid, Configuration conf, int socketTimeout,
      boolean connectToDnViaHostname, LocatedBlock locatedBlock); static ClientDatanodeProtocol createClientDatanodeProtocolProxy(
      DatanodeID datanodeid, Configuration conf, int socketTimeout,
      boolean connectToDnViaHostname); static ClientDatanodeProtocol createClientDatanodeProtocolProxy(
      InetSocketAddress addr, UserGroupInformation ticket, Configuration conf,
      SocketFactory factory); static String getNamenodeNameServiceId(Configuration conf); static String getBackupNameServiceId(Configuration conf); static String getSecondaryNameServiceId(Configuration conf); static URI createUri(String scheme, InetSocketAddress address); static void addPBProtocol(Configuration conf, Class<?> protocol,
      BlockingService service, RPC.Server server); static String getNamenodeServiceAddr(final Configuration conf,
      String nsId, String nnId); static String getOnlyNameServiceIdOrNull(Configuration conf); static boolean parseHelpArgument(String[] args,
      String helpDescription, PrintStream out, boolean printGenericCommandUsage); static float getInvalidateWorkPctPerIteration(Configuration conf); static int getReplWorkMultiplier(Configuration conf); static String getSpnegoKeytabKey(Configuration conf, String defaultKey); static HttpConfig.Policy getHttpPolicy(Configuration conf); static HttpServer2.Builder loadSslConfToHttpServerBuilder(HttpServer2.Builder builder,
      Configuration sslConf); static Configuration loadSslConfiguration(Configuration conf); static HttpServer2.Builder httpServerTemplateForNNAndJN(
      Configuration conf, final InetSocketAddress httpAddr,
      final InetSocketAddress httpsAddr, String name, String spnegoUserNameKey,
      String spnegoKeytabFileKey); static String dateToIso8601String(Date date); static String durationToString(long durationMs); static long parseRelativeTime(String relTime); static void assertAllResultsEqual(Collection<?> objects); static KeyProvider createKeyProvider(
      final Configuration conf); static KeyProviderCryptoExtension createKeyProviderCryptoExtension(
      final Configuration conf); static boolean isHDFSEncryptionEnabled(Configuration conf); static final Log LOG; static final byte[] EMPTY_BYTES; static final Comparator<DatanodeInfo> DECOM_COMPARATOR; static final Options helpOptions; static final Option helpOpt; }
@Test public void testGetPassword() throws Exception { File testDir = new File(System.getProperty("test.build.data", "target/test-dir")); Configuration conf = new Configuration(); final Path jksPath = new Path(testDir.toString(), "test.jks"); final String ourUrl = JavaKeyStoreProvider.SCHEME_NAME + ": File file = new File(testDir, "test.jks"); file.delete(); conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, ourUrl); CredentialProvider provider = CredentialProviderFactory.getProviders(conf).get(0); char[] keypass = {'k', 'e', 'y', 'p', 'a', 's', 's'}; char[] storepass = {'s', 't', 'o', 'r', 'e', 'p', 'a', 's', 's'}; char[] trustpass = {'t', 'r', 'u', 's', 't', 'p', 'a', 's', 's'}; assertEquals(null, provider.getCredentialEntry( DFS_SERVER_HTTPS_KEYPASSWORD_KEY)); assertEquals(null, provider.getCredentialEntry( DFS_SERVER_HTTPS_KEYSTORE_PASSWORD_KEY)); assertEquals(null, provider.getCredentialEntry( DFS_SERVER_HTTPS_TRUSTSTORE_PASSWORD_KEY)); try { provider.createCredentialEntry( DFS_SERVER_HTTPS_KEYPASSWORD_KEY, keypass); provider.createCredentialEntry( DFS_SERVER_HTTPS_KEYSTORE_PASSWORD_KEY, storepass); provider.createCredentialEntry( DFS_SERVER_HTTPS_TRUSTSTORE_PASSWORD_KEY, trustpass); provider.flush(); } catch (Exception e) { e.printStackTrace(); throw e; } assertArrayEquals(keypass, provider.getCredentialEntry( DFS_SERVER_HTTPS_KEYPASSWORD_KEY).getCredential()); assertArrayEquals(storepass, provider.getCredentialEntry( DFS_SERVER_HTTPS_KEYSTORE_PASSWORD_KEY).getCredential()); assertArrayEquals(trustpass, provider.getCredentialEntry( DFS_SERVER_HTTPS_TRUSTSTORE_PASSWORD_KEY).getCredential()); Assert.assertEquals("keypass", DFSUtil.getPassword(conf, DFS_SERVER_HTTPS_KEYPASSWORD_KEY)); Assert.assertEquals("storepass", DFSUtil.getPassword(conf, DFS_SERVER_HTTPS_KEYSTORE_PASSWORD_KEY)); Assert.assertEquals("trustpass", DFSUtil.getPassword(conf, DFS_SERVER_HTTPS_TRUSTSTORE_PASSWORD_KEY)); Assert.assertEquals(null, DFSUtil.getPassword(conf,"invalid-alias")); }