/** * * @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 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 testEmptyKeystore() throws Exception { assertThat(KeystoreWrapper.newStore().build().getSecureSetting("anything"), is(nullValue())); }
@Test public void testSetValue() throws Exception { KeystoreWrapper keystoreWrapper = KeystoreWrapper.newStore().build(); keystoreWrapper.setSecureSetting("key", "swordfish"); assertThat(keystoreWrapper.getSecureSetting("key"), is("swordfish")); }
@Test public void addKey() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("blahh"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, 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.getSecureSetting("test.password.1"), equalTo("blahh")); }
@Test public void addKeyStdIn() throws Exception { BytesArray storeData = new BytesArray(128); KeystoreWrapper ks = KeystoreWrapper.newStore().build(); ks.saveKeystore(new FastByteArrayOutputStream(storeData)); TestPrompt console = new TestPrompt(); console.addInput("blahh"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", true, 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.getSecureSetting("test.password.1"), equalTo("blahh")); }
@Test public void addExistingKeyOverwrite() 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("y").addInput("blerb"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, 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.getSecureSetting("test.password.1"), equalTo("blerb")); }
@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")); }
@Test public void addExistingKeyForce() 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("blerb"); KeytoolHarness keytool = new KeytoolHarness(console, Keytool.Command.ADD, true, storeData); assertThat(keytool.run("test.password.1", false, true), 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.getSecureSetting("test.password.1"), equalTo("blerb")); }
/** * * @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; } }
/** * * @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; } }
/** * * @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; } }