Search in sources :

Example 26 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class NifiPropertiesWriterTest method testCanUpdateAllKeys.

@Test
public void testCanUpdateAllKeys() throws IllegalAccessException, IOException {
    String testValue = NifiPropertiesWriterTest.class.getCanonicalName();
    // Get all property keys from NiFiProperties and set them to testValue
    Set<String> propertyKeys = new HashSet<>();
    for (Field field : NiFiProperties.class.getDeclaredFields()) {
        int modifiers = field.getModifiers();
        if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers) && field.getType() == String.class) {
            if (!field.getName().toLowerCase().startsWith("default")) {
                String fieldName = (String) field.get(null);
                propertyKeys.add(fieldName);
                niFiPropertiesWriter.setPropertyValue(fieldName, testValue);
            }
        }
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    niFiPropertiesWriter.writeNiFiProperties(outputStream);
    // Verify operation worked
    Properties properties = new Properties();
    properties.load(new ByteArrayInputStream(outputStream.toByteArray()));
    for (String propertyKey : propertyKeys) {
        assertEquals(testValue, properties.getProperty(propertyKey));
    }
}
Also used : Field(java.lang.reflect.Field) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) NiFiProperties(org.apache.nifi.util.NiFiProperties) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class TlsToolkitStandaloneTest method testDifferentArg.

@Test
public void testDifferentArg() throws Exception {
    runAndAssertExitCode(ExitCode.SUCCESS, "-o", tempDir.getAbsolutePath(), "-g", "-n", TlsConfig.DEFAULT_HOSTNAME);
    X509Certificate x509Certificate = checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
    Properties nifiProperties = checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, x509Certificate);
    assertNull(nifiProperties.get("nifi.fake.property"));
    assertNotEquals(nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD), nifiProperties.getProperty(NiFiProperties.SECURITY_KEY_PASSWD));
}
Also used : Properties(java.util.Properties) NiFiProperties(org.apache.nifi.util.NiFiProperties) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test) TlsHelperTest(org.apache.nifi.toolkit.tls.util.TlsHelperTest) TlsCertificateAuthorityTest(org.apache.nifi.toolkit.tls.service.TlsCertificateAuthorityTest)

Example 28 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class TlsToolkitStandaloneTest method testDirOutput.

@Test
public void testDirOutput() throws Exception {
    runAndAssertExitCode(ExitCode.SUCCESS, "-o", tempDir.getAbsolutePath(), "-n", TlsConfig.DEFAULT_HOSTNAME);
    X509Certificate x509Certificate = checkLoadCertPrivateKey(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM);
    Properties nifiProperties = checkHostDirAndReturnNifiProperties(TlsConfig.DEFAULT_HOSTNAME, x509Certificate);
    assertNull(nifiProperties.get("nifi.fake.property"));
    assertEquals(nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD), nifiProperties.getProperty(NiFiProperties.SECURITY_KEY_PASSWD));
}
Also used : Properties(java.util.Properties) NiFiProperties(org.apache.nifi.util.NiFiProperties) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test) TlsHelperTest(org.apache.nifi.toolkit.tls.util.TlsHelperTest) TlsCertificateAuthorityTest(org.apache.nifi.toolkit.tls.service.TlsCertificateAuthorityTest)

Example 29 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class TlsToolkitStandaloneTest method checkHostDirAndReturnNifiProperties.

private Properties checkHostDirAndReturnNifiProperties(String hostname, String dnPrefix, String dnSuffix, X509Certificate rootCert) throws Exception {
    File hostDir = new File(tempDir, hostname);
    Properties nifiProperties = new Properties();
    try (InputStream inputStream = new FileInputStream(new File(hostDir, TlsToolkitStandalone.NIFI_PROPERTIES))) {
        nifiProperties.load(inputStream);
    }
    String trustStoreType = nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_TYPE);
    assertEquals(KeystoreType.JKS.toString().toLowerCase(), trustStoreType.toLowerCase());
    KeyStore trustStore = KeyStoreUtils.getTrustStore(trustStoreType);
    try (InputStream inputStream = new FileInputStream(new File(hostDir, "truststore." + trustStoreType))) {
        trustStore.load(inputStream, nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_PASSWD).toCharArray());
    }
    String trustStoreFilename = BaseTlsToolkitCommandLine.TRUSTSTORE + trustStoreType;
    assertEquals("./conf/" + trustStoreFilename, nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE));
    Certificate certificate = trustStore.getCertificate(TlsToolkitStandalone.NIFI_CERT);
    assertEquals(rootCert, certificate);
    String keyStoreType = nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_TYPE);
    String keyStoreFilename = BaseTlsToolkitCommandLine.KEYSTORE + keyStoreType;
    File keyStoreFile = new File(hostDir, keyStoreFilename);
    assertEquals("./conf/" + keyStoreFilename, nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE));
    KeyStore keyStore = KeyStoreUtils.getKeyStore(keyStoreType);
    char[] keyStorePassword = nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD).toCharArray();
    try (InputStream inputStream = new FileInputStream(keyStoreFile)) {
        keyStore.load(inputStream, keyStorePassword);
    }
    char[] keyPassword = nifiProperties.getProperty(NiFiProperties.SECURITY_KEY_PASSWD).toCharArray();
    if (keyPassword == null || keyPassword.length == 0) {
        keyPassword = keyStorePassword;
    }
    KeyStore.Entry entry = keyStore.getEntry(TlsToolkitStandalone.NIFI_KEY, new KeyStore.PasswordProtection(keyPassword));
    assertEquals(KeyStore.PrivateKeyEntry.class, entry.getClass());
    KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) entry;
    Certificate[] certificateChain = privateKeyEntry.getCertificateChain();
    assertEquals(2, certificateChain.length);
    assertEquals(rootCert, certificateChain[1]);
    certificateChain[1].verify(rootCert.getPublicKey());
    certificateChain[0].verify(rootCert.getPublicKey());
    TlsConfig tlsConfig = new TlsConfig();
    tlsConfig.setDnPrefix(dnPrefix);
    tlsConfig.setDnSuffix(dnSuffix);
    assertEquals(tlsConfig.calcDefaultDn(hostname), CertificateUtils.convertAbstractX509Certificate(certificateChain[0]).getSubjectX500Principal().getName());
    TlsCertificateAuthorityTest.assertPrivateAndPublicKeyMatch(privateKeyEntry.getPrivateKey(), certificateChain[0].getPublicKey());
    return nifiProperties;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) NiFiProperties(org.apache.nifi.util.NiFiProperties) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) TlsConfig(org.apache.nifi.toolkit.tls.configuration.TlsConfig) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 30 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class TestSiteToSiteResource method getSiteToSiteResource.

private SiteToSiteResource getSiteToSiteResource(final NiFiServiceFacade serviceFacade, final Map<String, String> additionalProperties) {
    final NiFiProperties properties = NiFiProperties.createBasicNiFiProperties(null, additionalProperties);
    final SiteToSiteResource resource = new SiteToSiteResource(properties) {

        @Override
        protected void authorizeSiteToSite() {
        }
    };
    resource.setProperties(properties);
    resource.setServiceFacade(serviceFacade);
    return resource;
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties)

Aggregations

NiFiProperties (org.apache.nifi.util.NiFiProperties)98 Test (org.junit.Test)63 HashMap (java.util.HashMap)28 Properties (java.util.Properties)24 File (java.io.File)16 Bundle (org.apache.nifi.bundle.Bundle)13 Matchers.anyString (org.mockito.Matchers.anyString)13 IOException (java.io.IOException)10 HashSet (java.util.HashSet)10 Map (java.util.Map)8 X509Certificate (java.security.cert.X509Certificate)7 Mockito.anyString (org.mockito.Mockito.anyString)7 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 SystemBundle (org.apache.nifi.nar.SystemBundle)6 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IdentityMapping (org.apache.nifi.authorization.util.IdentityMapping)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileInputStream (java.io.FileInputStream)4