@Test public void removeKey() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.setSecureSetting("test.password.1", "bar"); ks.setSecureSetting("test.password.2", "foo"); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.REMOVE, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); ks = KeystoreWrapper.loadStore(new FastByteArrayInputStream(keytool.fileBytes)).build(); assertThat(ks.containsEntry("test.password.1"), is(false)); assertThat(ks.getSecureSetting("test.password.2"), equalTo("foo")); }
public KeystoreWrapper build() throws EsHadoopSecurityException, IOException { if (StringUtils.hasText(path)) { try { keystoreFile = IOUtils.open(path); if (keystoreFile == null) { throw new EsHadoopIllegalArgumentException(String.format("Could not locate [%s] on classpath", path)); } } catch (Exception e) { throw new EsHadoopIllegalArgumentException(String.format("Expected to find keystore file at [%s] but " + "was unable to. Make sure that it is available on the classpath, or if not, that you have " + "specified a valid file URI.", path)); } } if (!StringUtils.hasText(type)) { type = PKCS12; } if (!StringUtils.hasText(password)) { password = DEFAULT_PASS; } return new KeystoreWrapper(keystoreFile, type, password); } }
keystoreWrapper = KeystoreWrapper.newStore().build(); outputStream = openWrite(); keystoreWrapper.saveKeystore(outputStream); return 0; case LIST: keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build(); for (String entry : keystoreWrapper.listEntries()) { prompt.println(entry); keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build(); if (keystoreWrapper.containsEntry(arg) && force == false) { boolean proceed = promptYesNo("Setting %s already exists. Overwrite? [y/N]", arg); if (proceed == false) { keystoreWrapper.setSecureSetting(arg, data); } else { char[] data = prompt.readPassword("Enter value for %s:", arg); keystoreWrapper.setSecureSetting(arg, new String(data)); Arrays.fill(data, (char) 0); keystoreWrapper.saveKeystore(outputStream); return 0; case REMOVE: keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build(); if (keystoreWrapper.containsEntry(arg) == false) { prompt.printf("ERROR: Setting [%s] does not exist in the keystore.%n", arg); return 6;
@Test public void testStoreLoad() throws Exception { KeystoreWrapper keystoreWrapper = KeystoreWrapper.newStore().build(); keystoreWrapper.setSecureSetting("key", "swordfish"); assertThat(keystoreWrapper.getSecureSetting("key"), is("swordfish")); ByteArrayOutputStream stream = new ByteArrayOutputStream(1024); keystoreWrapper.saveKeystore(stream); byte[] data = stream.toByteArray(); ByteArrayInputStream inputStream = new ByteArrayInputStream(data); KeystoreWrapper loaded = KeystoreWrapper.loadStore(inputStream).build(); assertThat(loaded.getSecureSetting("key"), is("swordfish")); } }
@Test public void testSetValue() throws Exception { KeystoreWrapper keystoreWrapper = KeystoreWrapper.newStore().build(); keystoreWrapper.setSecureSetting("key", "swordfish"); assertThat(keystoreWrapper.getSecureSetting("key"), is("swordfish")); }
@Test public void testEmptyKeystore() throws Exception { assertThat(KeystoreWrapper.newStore().build().getSecureSetting("anything"), is(nullValue())); }
@Test public void listKeystore() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.setSecureSetting("test.password.1", "blah"); ks.setSecureSetting("test.password.2", "blah"); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.LIST, true, storeData); assertThat(keytool.run(null, false, false), equalTo(0)); assertThat( console.getOutput(), equalTo( "test.password.1\n" + "test.password.2\n" ) ); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
@Test public void listKeystoreEmpty() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper.newStore().build().saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.LIST, true, storeData); assertThat(keytool.run(null, false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
public void saveKeystore(String path) throws EsHadoopSecurityException, IOException { OutputStream stream = null; try { stream = new FileOutputStream(new File(path)); saveKeystore(stream); } finally { if (stream != null) { stream.close(); } } }
public SecureSettings(Settings settings) { this.settings = settings; String keystoreLocation = settings.getProperty(ConfigurationOptions.ES_KEYSTORE_LOCATION); if (keystoreLocation != null) { KeystoreBuilder builder = KeystoreWrapper.loadStore(keystoreLocation); try { this.keystoreWrapper = builder.build(); } catch (EsHadoopSecurityException e) { throw new EsHadoopException("Could not load keystore", e); } catch (IOException e) { throw new EsHadoopException("Could not load keystore", e); } } else { this.keystoreWrapper = null; } }
/** * * @param key property name * @return secure property value or null */ public String getSecureProperty(String key) { String value = null; if (keystoreWrapper != null) { try { value = keystoreWrapper.getSecureSetting(key); } catch (EsHadoopSecurityException e) { throw new EsHadoopException("Could not read secure setting [" + key + "]", e); } } if (value == null) { value = settings.getProperty(key); } return value; } }
@Test public void addExistingKeyCancel() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.setSecureSetting("test.password.1", "blah"); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("n"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(0)); assertThat(console.getOutput(), equalTo("Exiting without modifying keystore\n")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
@Test public void removeMissingKey() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.REMOVE, true, storeData); assertThat(keytool.run("test.password.1", false, false), equalTo(6)); assertThat(console.getOutput(), equalTo("ERROR: Setting [test.password.1] does not exist in the keystore.\n")); assertThat(console.hasInputLeft(), is(false)); assertThat(keytool.ksExists(), is(true)); assertThat(keytool.fileBytes.length(), is(not(0))); }
public void saveKeystore(String path) throws EsHadoopSecurityException, IOException { OutputStream stream = null; try { stream = new FileOutputStream(new File(path)); saveKeystore(stream); } finally { if (stream != null) { stream.close(); } } }
public SecureSettings(Settings settings) { this.settings = settings; String keystoreLocation = settings.getProperty(ConfigurationOptions.ES_KEYSTORE_LOCATION); if (keystoreLocation != null) { KeystoreBuilder builder = KeystoreWrapper.loadStore(keystoreLocation); try { this.keystoreWrapper = builder.build(); } catch (EsHadoopSecurityException e) { throw new EsHadoopException("Could not load keystore", e); } catch (IOException e) { throw new EsHadoopException("Could not load keystore", e); } } else { this.keystoreWrapper = null; } }
/** * * @param key property name * @return secure property value or null */ public String getSecureProperty(String key) { String value = null; if (keystoreWrapper != null) { try { value = keystoreWrapper.getSecureSetting(key); } catch (EsHadoopSecurityException e) { throw new EsHadoopException("Could not read secure setting [" + key + "]", e); } } if (value == null) { value = settings.getProperty(key); } return value; } }
keystoreWrapper = KeystoreWrapper.newStore().build(); outputStream = openWrite(); keystoreWrapper.saveKeystore(outputStream); return 0; case LIST: keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build(); for (String entry : keystoreWrapper.listEntries()) { prompt.println(entry); keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build(); if (keystoreWrapper.containsEntry(arg) && force == false) { boolean proceed = promptYesNo("Setting %s already exists. Overwrite? [y/N]", arg); if (proceed == false) { keystoreWrapper.setSecureSetting(arg, data); } else { char[] data = prompt.readPassword("Enter value for %s:", arg); keystoreWrapper.setSecureSetting(arg, new String(data)); Arrays.fill(data, (char) 0); keystoreWrapper.saveKeystore(outputStream); return 0; case REMOVE: keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build(); if (keystoreWrapper.containsEntry(arg) == false) { prompt.printf("ERROR: Setting [%s] does not exist in the keystore.%n", arg); return 6;
public void saveKeystore(String path) throws EsHadoopSecurityException, IOException { OutputStream stream = null; try { stream = new FileOutputStream(new File(path)); saveKeystore(stream); } finally { if (stream != null) { stream.close(); } } }
public SecureSettings(Settings settings) { this.settings = settings; String keystoreLocation = settings.getProperty(ConfigurationOptions.ES_KEYSTORE_LOCATION); if (keystoreLocation != null) { KeystoreBuilder builder = KeystoreWrapper.loadStore(keystoreLocation); try { this.keystoreWrapper = builder.build(); } catch (EsHadoopSecurityException e) { throw new EsHadoopException("Could not load keystore", e); } catch (IOException e) { throw new EsHadoopException("Could not load keystore", e); } } else { this.keystoreWrapper = null; } }
/** * * @param key property name * @return secure property value or null */ public String getSecureProperty(String key) { String value = null; if (keystoreWrapper != null) { try { value = keystoreWrapper.getSecureSetting(key); } catch (EsHadoopSecurityException e) { throw new EsHadoopException("Could not read secure setting [" + key + "]", e); } } if (value == null) { value = settings.getProperty(key); } return value; } }