@Test public void testJksProvider() throws Exception { Configuration conf = new Configuration(); final String ourUrl = JavaKeyStoreProvider.SCHEME_NAME + ": File file = new File(tmpDir, "test.jks"); file.delete(); conf.set(KeyProviderFactory.KEY_PROVIDER_PATH, ourUrl); checkSpecificProvider(conf, ourUrl); Path path = ProviderUtils.unnestUri(new URI(ourUrl)); FileSystem fs = path.getFileSystem(conf); FileStatus s = fs.getFileStatus(path); assertTrue(s.getPermission().toString().equals("rwx------")); assertTrue(file + " should exist", file.isFile()); File oldFile = new File(file.getPath() + "_OLD"); file.renameTo(oldFile); file.delete(); file.createNewFile(); assertTrue(oldFile.exists()); KeyProvider provider = KeyProviderFactory.getProviders(conf).get(0); assertTrue(file.exists()); assertTrue(oldFile + "should be deleted", !oldFile.exists()); verifyAfterReload(file, provider); assertTrue(!oldFile.exists()); File newFile = new File(file.getPath() + "_NEW"); newFile.createNewFile(); try { provider = KeyProviderFactory.getProviders(conf).get(0); Assert.fail("_NEW and current file should not exist together !!"); } catch (Exception e) { } finally { if (newFile.exists()) { newFile.delete(); } } file.renameTo(newFile); file.delete(); try { provider = KeyProviderFactory.getProviders(conf).get(0); Assert.assertFalse(newFile.exists()); Assert.assertFalse(oldFile.exists()); } catch (Exception e) { Assert.fail("JKS should load from _NEW file !!"); } verifyAfterReload(file, provider); newFile.createNewFile(); file.renameTo(oldFile); file.delete(); try { provider = KeyProviderFactory.getProviders(conf).get(0); Assert.assertFalse(newFile.exists()); Assert.assertFalse(oldFile.exists()); } catch (Exception e) { Assert.fail("JKS should load from _OLD file !!"); } finally { if (newFile.exists()) { newFile.delete(); } } verifyAfterReload(file, provider); fs.setPermission(path, new FsPermission("777")); checkPermissionRetention(conf, ourUrl, path); }
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; }