/** * @param props the properties to take the access credentials from. * @throws MissingValueException in case the given properties is missing values. */ public static UrlCredentials getCredentials(Dictionary<String, ?> props) throws MissingValueException { return getCredentials(props, ""); }
/** * @param truststoreFile * @param storePass * @return * @throws IOException * @throws GeneralSecurityException */ private static TrustManager[] getTrustManagerFactory(String truststoreFile, String storePass) throws IOException, GeneralSecurityException { if (truststoreFile == null) { return null; } InputStream trustInput = null; try { TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustInput = new FileInputStream(truststoreFile); trustStore.load(trustInput, storePass.toCharArray()); trustManagerFactory.init(trustStore); return trustManagerFactory.getTrustManagers(); } finally { closeSafely(trustInput); } }
private static String getStringProperty(Dictionary<String, ?> dict, String key, String defaultValue) { String value = getStringProperty(dict, key); return (value == null) ? defaultValue : value; }
String baseUrlValue = getStringProperty(props, prefix.concat(KEY_AUTH_BASE_URL)); if (baseUrlValue == null) { throw new MissingValueException(KEY_AUTH_BASE_URL); String authType = getStringProperty(props, prefix.concat(KEY_AUTH_TYPE), "none"); try { type = AuthType.valueOf(authType.toUpperCase()); creds = new Object[0]; } else if (AuthType.BASIC.equals(type)) { String userName = getStringProperty(props, prefix.concat(KEY_AUTH_USER_NAME)); if (userName == null) { throw new MissingValueException(prefix.concat(KEY_AUTH_USER_NAME)); String password = getStringProperty(props, prefix.concat(KEY_AUTH_USER_PASSWORD)); if (password == null) { throw new MissingValueException(prefix.concat(KEY_AUTH_USER_PASSWORD)); String keystoreFile = getStringProperty(props, prefix.concat(KEY_AUTH_KEYSTORE_FILE)); String keystorePass = getStringProperty(props, prefix.concat(KEY_AUTH_KEYSTORE_PASS)); if ((keystoreFile != null) && (keystorePass == null)) { throw new MissingValueException(prefix.concat(KEY_AUTH_KEYSTORE_PASS)); String truststoreFile = getStringProperty(props, prefix.concat(KEY_AUTH_TRUSTSTORE_FILE)); String truststorePass = getStringProperty(props, prefix.concat(KEY_AUTH_TRUSTSTORE_PASS)); if ((truststoreFile != null) && (truststorePass == null)) { throw new MissingValueException(prefix.concat(KEY_AUTH_TRUSTSTORE_PASS)); KeyManager[] keyManagers = getKeyManagerFactory(keystoreFile, keystorePass);
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testGetCredentialsWithNullDictionaryFail() { UrlCredentialsFactory.getCredentials(null); }
/** * @param keystoreFile * @param storePass * @return * @throws IOException * @throws GeneralSecurityException */ private static KeyManager[] getKeyManagerFactory(String keystoreFile, String storePass) throws IOException, GeneralSecurityException { if (keystoreFile == null) { return null; } InputStream keyInput = null; try { KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyInput = new FileInputStream(keystoreFile); keyStore.load(keyInput, storePass.toCharArray()); keyManagerFactory.init(keyStore, storePass.toCharArray()); return keyManagerFactory.getKeyManagers(); } finally { closeSafely(keyInput); } }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary, java.lang.String)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testGetCredentialsWithNullPrefixFail() { UrlCredentialsFactory.getCredentials(new Hashtable<String, Object>(), null); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = MissingValueException.class) public void testGetCredentialsWithDictionaryMissingBaseUrlFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "none"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testGetCredentialsWithDictionaryInvalidAuthTypeFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "nonsense"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = MissingValueException.class) public void testGetCredentialsWithDictionaryBasicTypeMissingUserNameFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "basic"); props.put(UrlCredentialsFactory.KEY_AUTH_USER_PASSWORD, "bar"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test() public void testGetCredentialsWithValidDictionaryOk() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "none"); UrlCredentialsFactory.getCredentials(props); } }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = MissingValueException.class) public void testGetCredentialsWithDictionaryBasicTypeMissingPasswordFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "basic"); props.put(UrlCredentialsFactory.KEY_AUTH_USER_NAME, "bar"); UrlCredentialsFactory.getCredentials(props); }
/** * {@inheritDoc} */ public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException { UrlCredentials creds; synchronized (m_credentialMapping) { creds = m_credentialMapping.get(pid); } try { creds = UrlCredentialsFactory.getCredentials(properties); synchronized (m_credentialMapping) { m_credentialMapping.put(pid, creds); } } catch (MissingValueException e) { throw new ConfigurationException(e.getProperty(), e.getMessage()); } }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test() public void testGetCredentialsWithDictionaryBasicTypeOk() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "basic"); props.put(UrlCredentialsFactory.KEY_AUTH_USER_NAME, "foo"); props.put(UrlCredentialsFactory.KEY_AUTH_USER_PASSWORD, "bar"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = MissingValueException.class) public void testGetCredentialsWithDictionaryClientCertTypeMissingKeystoreFileFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "client_cert"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_FILE, "bar"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_PASS, "qux"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_PASS, "foo"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = MissingValueException.class) public void testGetCredentialsWithDictionaryClientCertTypeMissingKeystorePasswordFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "client_cert"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_FILE, "bar"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_PASS, "qux"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_FILE, "foo"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = MissingValueException.class) public void testGetCredentialsWithDictionaryClientCertTypeMissingTruststoreFileFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "client_cert"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_FILE, "bar"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_PASS, "qux"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_PASS, "foo"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test(expectedExceptions = MissingValueException.class) public void testGetCredentialsWithDictionaryClientCertTypeMissingTruststorePasswordFail() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "client_cert"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_FILE, "bar"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_PASS, "qux"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_FILE, "foo"); UrlCredentialsFactory.getCredentials(props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentialsFactory#getCredentials(java.util.Dictionary)}. */ @Test() public void testGetCredentialsWithDictionaryClientCertTypeOk() { Dictionary<String, Object> props = new Hashtable<>(); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, AUTH_BASE_URL); props.put(UrlCredentialsFactory.KEY_AUTH_TYPE, "client_cert"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_FILE, "foo"); props.put(UrlCredentialsFactory.KEY_AUTH_TRUSTSTORE_PASS, "bar"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_FILE, "qux"); props.put(UrlCredentialsFactory.KEY_AUTH_KEYSTORE_PASS, "quu"); try { UrlCredentialsFactory.getCredentials(props); } catch (IllegalArgumentException e) { // Ok; expected as the implementation tries to open the files "foo" and "qux"... } }