NiFiConnectionMgr { static public NiFiClient getNiFiClient(String serviceName, Map<String, String> configs) throws Exception { final String url = configs.get(NiFiConfigs.NIFI_URL); validateNotBlank(url, "NiFi URL is required for " + serviceName); validateUrl(url); final String authTypeStr = configs.get(NiFiConfigs.NIFI_AUTHENTICATION_TYPE); validateNotBlank(authTypeStr, "Authentication Type is required for " + serviceName); final NiFiAuthType authType = NiFiAuthType.valueOf(authTypeStr); LOG.debug("NiFiAuthType is " + authType.name()); SSLContext sslContext = null; if (authType == NiFiAuthType.SSL) { if (!url.startsWith("https")) { throw new IllegalArgumentException("Authentication Type of SSL requires an https URL"); } final String keystore = configs.get(NiFiConfigs.NIFI_SSL_KEYSTORE); final String keystoreType = configs.get(NiFiConfigs.NIFI_SSL_KEYSTORE_TYPE); final String keystorePassword = configs.get(NiFiConfigs.NIFI_SSL_KEYSTORE_PASSWORD); final String truststore = configs.get(NiFiConfigs.NIFI_SSL_TRUSTSTORE); final String truststoreType = configs.get(NiFiConfigs.NIFI_SSL_TRUSTSTORE_TYPE); final String truststorePassword = configs.get(NiFiConfigs.NIFI_SSL_TRUSTSTORE_PASSWORD); final String useDefaultSSLContext = configs.get(NiFiConfigs.NIFI_SSL_USER_DEFAULT_CONTEXT); if (!StringUtils.isBlank(useDefaultSSLContext) && "true".equalsIgnoreCase(useDefaultSSLContext)) { if (!StringUtils.isBlank(keystore) || !StringUtils.isBlank(keystoreType) || !StringUtils.isBlank(keystorePassword) || !StringUtils.isBlank(truststore) || !StringUtils.isBlank(truststoreType) || !StringUtils.isBlank(truststorePassword)) { throw new IllegalArgumentException("Keystore and Truststore configuration cannot be provided when using default SSL context"); } sslContext = SSLContext.getDefault(); } else { validateNotBlank(keystore, "Keystore is required for " + serviceName + " with Authentication Type of SSL"); validateNotBlank(keystoreType, "Keystore Type is required for " + serviceName + " with Authentication Type of SSL"); validateNotBlank(keystorePassword, "Keystore Password is required for " + serviceName + " with Authentication Type of SSL"); validateNotBlank(truststore, "Truststore is required for " + serviceName + " with Authentication Type of SSL"); validateNotBlank(truststoreType, "Truststore Type is required for " + serviceName + " with Authentication Type of SSL"); validateNotBlank(truststorePassword, "Truststore Password is required for " + serviceName + " with Authentication Type of SSL"); LOG.debug("Creating SSLContext for NiFi connection"); sslContext = createSslContext( keystore.trim(), keystorePassword.trim().toCharArray(), keystoreType.trim(), truststore.trim(), truststorePassword.trim().toCharArray(), truststoreType.trim(), "TLS"); } } return new NiFiClient(url.trim(), sslContext); }  static NiFiClient getNiFiClient(String serviceName, Map<String, String> configs); static HashMap<String, Object> connectionTest(String serviceName, Map<String, String> configs);  }
@Test (expected = IllegalArgumentException.class) public void testValidURLWithWrongEndPoint() throws Exception { final String nifiUrl = "http: Map<String,String> configs = new HashMap<>(); configs.put(NiFiConfigs.NIFI_URL, nifiUrl); configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, NiFiAuthType.NONE.name()); NiFiConnectionMgr.getNiFiClient("nifi", configs); }
@Test (expected = IllegalArgumentException.class) public void testInvalidURL() throws Exception { final String nifiUrl = "not a url"; Map<String,String> configs = new HashMap<>(); configs.put(NiFiConfigs.NIFI_URL, nifiUrl); configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, NiFiAuthType.NONE.name()); NiFiConnectionMgr.getNiFiClient("nifi", configs); }
@Test public void testAuthTypeNone() throws Exception { final String nifiUrl = "http: Map<String,String> configs = new HashMap<>(); configs.put(NiFiConfigs.NIFI_URL, nifiUrl); configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, NiFiAuthType.NONE.name()); NiFiClient client = NiFiConnectionMgr.getNiFiClient("nifi", configs); Assert.assertNotNull(client); Assert.assertEquals(nifiUrl, client.getUrl()); Assert.assertNull(client.getSslContext()); }
@Test(expected = IllegalArgumentException.class) public void testAuthTypeNoneMissingURL() throws Exception { Map<String,String> configs = new HashMap<>(); configs.put(NiFiConfigs.NIFI_URL, null); configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, NiFiAuthType.NONE.name()); NiFiConnectionMgr.getNiFiClient("nifi", configs); }
@Test(expected = FileNotFoundException.class) public void testAuthTypeSSL() throws Exception { final String nifiUrl = "https: Map<String,String> configs = new HashMap<>(); configs.put(NiFiConfigs.NIFI_URL, nifiUrl); configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, NiFiAuthType.SSL.name()); configs.put(NiFiConfigs.NIFI_SSL_KEYSTORE, "src/test/resources/missing.jks"); configs.put(NiFiConfigs.NIFI_SSL_KEYSTORE_PASSWORD, "password"); configs.put(NiFiConfigs.NIFI_SSL_KEYSTORE_TYPE, "JKS"); configs.put(NiFiConfigs.NIFI_SSL_TRUSTSTORE, "src/test/resources/missing.jks"); configs.put(NiFiConfigs.NIFI_SSL_TRUSTSTORE_PASSWORD, "password"); configs.put(NiFiConfigs.NIFI_SSL_TRUSTSTORE_TYPE, "JKS"); NiFiConnectionMgr.getNiFiClient("nifi", configs); }
@Test(expected = IllegalArgumentException.class) public void testAuthTypeSSLWithNonHttpsUrl() throws Exception { final String nifiUrl = "http: Map<String,String> configs = new HashMap<>(); configs.put(NiFiConfigs.NIFI_URL, nifiUrl); configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, NiFiAuthType.SSL.name()); configs.put(NiFiConfigs.NIFI_SSL_KEYSTORE, "src/test/resources/missing.jks"); configs.put(NiFiConfigs.NIFI_SSL_KEYSTORE_PASSWORD, "password"); configs.put(NiFiConfigs.NIFI_SSL_KEYSTORE_TYPE, "JKS"); configs.put(NiFiConfigs.NIFI_SSL_TRUSTSTORE, "src/test/resources/missing.jks"); configs.put(NiFiConfigs.NIFI_SSL_TRUSTSTORE_PASSWORD, "password"); configs.put(NiFiConfigs.NIFI_SSL_TRUSTSTORE_TYPE, "JKS"); NiFiConnectionMgr.getNiFiClient("nifi", configs); }
@Test(expected = IllegalArgumentException.class) public void testAuthTypeSSLMissingConfigs() throws Exception { final String nifiUrl = "http: Map<String,String> configs = new HashMap<>(); configs.put(NiFiConfigs.NIFI_URL, nifiUrl); configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, NiFiAuthType.SSL.name()); NiFiConnectionMgr.getNiFiClient("nifi", configs); }