CredentialProviderFactory { public static List<CredentialProvider> getProviders(Configuration conf ) throws IOException { List<CredentialProvider> result = new ArrayList<CredentialProvider>(); for(String path: conf.getStringCollection(CREDENTIAL_PROVIDER_PATH)) { try { URI uri = new URI(path); boolean found = false; synchronized (serviceLoader) { for (CredentialProviderFactory factory : serviceLoader) { CredentialProvider kp = factory.createProvider(uri, conf); if (kp != null) { result.add(kp); found = true; break; } } } if (!found) { throw new IOException("No CredentialProviderFactory for " + uri + " in " + CREDENTIAL_PROVIDER_PATH); } } catch (URISyntaxException error) { throw new IOException("Bad configuration of " + CREDENTIAL_PROVIDER_PATH + " at " + path, error); } } return result; }  abstract CredentialProvider createProvider(URI providerName,
                                             Configuration conf
                                             ); static List<CredentialProvider> getProviders(Configuration conf
                                               ); static final String CREDENTIAL_PROVIDER_PATH; }
@Test public void testFactory() throws Exception { Configuration conf = new Configuration(); final String userUri = UserProvider.SCHEME_NAME + ": final Path jksPath = new Path(tmpDir.toString(), "test.jks"); final String jksUri = JavaKeyStoreProvider.SCHEME_NAME + ": conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, userUri + "," + jksUri); List<CredentialProvider> providers = CredentialProviderFactory.getProviders(conf); assertEquals(2, providers.size()); assertEquals(UserProvider.class, providers.get(0).getClass()); assertEquals(JavaKeyStoreProvider.class, providers.get(1).getClass()); assertEquals(userUri, providers.get(0).toString()); assertEquals(jksUri, providers.get(1).toString()); }
@Test public void testFactoryErrors() throws Exception { Configuration conf = new Configuration(); conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, "unknown: try { List<CredentialProvider> providers = CredentialProviderFactory.getProviders(conf); assertTrue("should throw!", false); } catch (IOException e) { assertEquals("No CredentialProviderFactory for unknown: CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, e.getMessage()); } }
@Test public void testUriErrors() throws Exception { Configuration conf = new Configuration(); conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, "unkn@own:/x/y"); try { List<CredentialProvider> providers = CredentialProviderFactory.getProviders(conf); assertTrue("should throw!", false); } catch (IOException e) { assertEquals("Bad configuration of " + CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH + " at unkn@own:/x/y", e.getMessage()); } }
@Test public void testLocalBCFKSProvider() throws Exception { Configuration conf = new Configuration(); final Path ksPath = new Path(tmpDir.toString(), "test.bcfks"); final String ourUrl = LocalBouncyCastleFipsKeyStoreProvider.SCHEME_NAME + ": conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, ourUrl); exception.expect(IOException.class); exception.expectMessage("Can't create keystore"); List<CredentialProvider> providers = CredentialProviderFactory.getProviders(conf); assertTrue("BCFKS needs additional JDK setup", providers.isEmpty()); }