@Test public void testJksProviderPasswordViaConfig() throws Exception { Configuration conf = new Configuration(); final String ourUrl = JavaKeyStoreProvider.SCHEME_NAME + ": File file = new File(tmpDir, "test.jks"); file.delete(); try { conf.set(KeyProviderFactory.KEY_PROVIDER_PATH, ourUrl); conf.set(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY, "javakeystoreprovider.password"); KeyProvider provider = KeyProviderFactory.getProviders(conf).get(0); provider.createKey("key3", new byte[16], KeyProvider.options(conf)); provider.flush(); } catch (Exception ex) { Assert.fail("could not create keystore with password file"); } KeyProvider provider = KeyProviderFactory.getProviders(conf).get(0); Assert.assertNotNull(provider.getCurrentKey("key3")); try { conf.set(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY, "bar"); KeyProviderFactory.getProviders(conf).get(0); Assert.fail("using non existing password file, it should fail"); } catch (IOException ex) { } try { conf.set(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY, "core-site.xml"); KeyProviderFactory.getProviders(conf).get(0); Assert.fail("using different password file, it should fail"); } catch (IOException ex) { } try { conf.unset(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY); KeyProviderFactory.getProviders(conf).get(0); Assert.fail("No password file property, env not set, it should fail"); } catch (IOException ex) { } }
public static List<KeyProvider> getProviders(Configuration conf ) throws IOException { List<KeyProvider> result = new ArrayList<KeyProvider>(); for(String path: conf.getStringCollection(KEY_PROVIDER_PATH)) { try { URI uri = new URI(path); boolean found = false; for(KeyProviderFactory factory: serviceLoader) { KeyProvider kp = factory.createProvider(uri, conf); if (kp != null) { result.add(kp); found = true; break; } } if (!found) { throw new IOException("No KeyProviderFactory for " + uri + " in " + KEY_PROVIDER_PATH); } } catch (URISyntaxException error) { throw new IOException("Bad configuration of " + KEY_PROVIDER_PATH + " at " + path, error); } } return result; }
KeyProviderFactory { public static List<KeyProvider> getProviders(Configuration conf ) throws IOException { List<KeyProvider> result = new ArrayList<KeyProvider>(); for(String path: conf.getStringCollection(KEY_PROVIDER_PATH)) { try { URI uri = new URI(path); boolean found = false; for(KeyProviderFactory factory: serviceLoader) { KeyProvider kp = factory.createProvider(uri, conf); if (kp != null) { result.add(kp); found = true; break; } } if (!found) { throw new IOException("No KeyProviderFactory for " + uri + " in " + KEY_PROVIDER_PATH); } } catch (URISyntaxException error) { throw new IOException("Bad configuration of " + KEY_PROVIDER_PATH + " at " + path, error); } } return result; } }
KeyProviderFactory { public static List<KeyProvider> getProviders(Configuration conf ) throws IOException { List<KeyProvider> result = new ArrayList<KeyProvider>(); for(String path: conf.getStringCollection(KEY_PROVIDER_PATH)) { try { URI uri = new URI(path); boolean found = false; for(KeyProviderFactory factory: serviceLoader) { KeyProvider kp = factory.createProvider(uri, conf); if (kp != null) { result.add(kp); found = true; break; } } if (!found) { throw new IOException("No KeyProviderFactory for " + uri + " in " + KEY_PROVIDER_PATH); } } catch (URISyntaxException error) { throw new IOException("Bad configuration of " + KEY_PROVIDER_PATH + " at " + path, error); } } return result; }  }
KeyProviderFactory { public static List<KeyProvider> getProviders(Configuration conf ) throws IOException { List<KeyProvider> result = new ArrayList<KeyProvider>(); for(String path: conf.getStringCollection(KEY_PROVIDER_PATH)) { try { URI uri = new URI(path); boolean found = false; for(KeyProviderFactory factory: serviceLoader) { KeyProvider kp = factory.createProvider(uri, conf); if (kp != null) { result.add(kp); found = true; break; } } if (!found) { throw new IOException("No KeyProviderFactory for " + uri + " in " + KEY_PROVIDER_PATH); } } catch (URISyntaxException error) { throw new IOException("Bad configuration of " + KEY_PROVIDER_PATH + " at " + path, error); } } return result; }  abstract KeyProvider createProvider(URI providerName,
                                             Configuration conf
                                             ); static List<KeyProvider> getProviders(Configuration conf
                                               ); }
KeyProviderFactory { public static List<KeyProvider> getProviders(Configuration conf ) throws IOException { List<KeyProvider> result = new ArrayList<KeyProvider>(); for(String path: conf.getStringCollection(KEY_PROVIDER_PATH)) { try { URI uri = new URI(path); boolean found = false; for(KeyProviderFactory factory: serviceLoader) { KeyProvider kp = factory.createProvider(uri, conf); if (kp != null) { result.add(kp); found = true; break; } } if (!found) { throw new IOException("No KeyProviderFactory for " + uri + " in " + KEY_PROVIDER_PATH); } } catch (URISyntaxException error) { throw new IOException("Bad configuration of " + KEY_PROVIDER_PATH + " at " + path, error); } } return result; }  abstract KeyProvider createProvider(URI providerName,
                                             Configuration conf
                                             ); static List<KeyProvider> getProviders(Configuration conf
                                               ); static final String KEY_PROVIDER_PATH; }