SystemPropertyTlsKeyManagersProvider extends AbstractFileTlsKeyManagersProvider { @Override public KeyManager[] getKeyManagers() { String keyStorePath = getKeyStore(); if (keyStorePath == null) { return null; } String type = getKeyStoreType(); String password = getKeyStorePassword(); char[] passwordChars = null; if (password != null) { passwordChars = password.toCharArray(); } try { return createKeyManagers(new File(keyStorePath), type, passwordChars); } catch (Exception e) { log.warn("Unable to load KeyManager from system properties", e); return null; } }  @Override KeyManager[] getKeyManagers();  }
@Test public void validProperties_loadsKeyManager() { System.setProperty(KEY_STORE_PROPERTY, clientKeyStore.getAbsolutePath()); System.setProperty(KEY_STORE_TYPE_PROPERTY, "pkcs12"); System.setProperty(KEY_STORE_PASSWORD_PROPERTY, "password"); assertThat(provider.getKeyManagers().length, equalTo(1)); }
@Test public void invalidKeyStoreType_returnsNull() { System.setProperty(KEY_STORE_PROPERTY, clientKeyStore.getAbsolutePath()); System.setProperty(KEY_STORE_TYPE_PROPERTY, "some-unknown-type"); System.setProperty(KEY_STORE_PASSWORD_PROPERTY, "password"); assertThat(provider.getKeyManagers(), nullValue()); }
@Test public void certInvalid_returnsNull() { System.setProperty(KEY_STORE_PROPERTY, badCertPath.getAbsolutePath()); assertThat(provider.getKeyManagers(), nullValue()); }
@Test public void passwordIncorrect_returnsNull() { System.setProperty(KEY_STORE_PROPERTY, clientKeyStore.getAbsolutePath()); System.setProperty(KEY_STORE_TYPE_PROPERTY, "pkcs12"); System.setProperty(KEY_STORE_PASSWORD_PROPERTY, "foo"); assertThat(provider.getKeyManagers(), nullValue()); }
@Test public void systemPropertiesNotSet_returnsNull() { assertThat(provider.getKeyManagers(), nullValue()); }
@Test public void storeDoesNotExist_returnsNull() { System.setProperty(KEY_STORE_PROPERTY, "/does/not/exist"); assertThat(provider.getKeyManagers(), nullValue()); }
@Test public void customKmfAlgorithmSetInProperty_usesAlgorithm() { System.setProperty(KEY_STORE_PROPERTY, clientKeyStore.getAbsolutePath()); System.setProperty(KEY_STORE_TYPE_PROPERTY, CLIENT_STORE_TYPE); System.setProperty(KEY_STORE_PASSWORD_PROPERTY, STORE_PASSWORD); assertThat(provider.getKeyManagers(), notNullValue()); String property = "ssl.KeyManagerFactory.algorithm"; String previousValue = Security.getProperty(property); Security.setProperty(property, "some-bogus-value"); try { assertThat(provider.getKeyManagers(), nullValue()); } finally { Security.setProperty(property, previousValue); } }