Search in sources :

Example 6 with NiFiProperties

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

the class NarThreadContextClassLoaderTest method validateWithDefaultConstructor.

@Test
public void validateWithDefaultConstructor() throws Exception {
    NiFiProperties properties = NiFiProperties.createBasicNiFiProperties("src/test/resources/nifi.properties", null);
    Bundle systemBundle = SystemBundle.create(properties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    assertTrue(NarThreadContextClassLoader.createInstance(WithDefaultConstructor.class.getName(), WithDefaultConstructor.class, properties) instanceof WithDefaultConstructor);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Bundle(org.apache.nifi.bundle.Bundle) Test(org.junit.Test)

Example 7 with NiFiProperties

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

the class NarThreadContextClassLoaderTest method validateWithPropertiesConstructorInstantiationFailure.

@Test(expected = IllegalStateException.class)
public void validateWithPropertiesConstructorInstantiationFailure() throws Exception {
    Map<String, String> additionalProperties = new HashMap<>();
    additionalProperties.put("fail", "true");
    NiFiProperties properties = NiFiProperties.createBasicNiFiProperties("src/test/resources/nifi.properties", additionalProperties);
    Bundle systemBundle = SystemBundle.create(properties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    NarThreadContextClassLoader.createInstance(WithPropertiesConstructor.class.getName(), WithPropertiesConstructor.class, properties);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) HashMap(java.util.HashMap) Bundle(org.apache.nifi.bundle.Bundle) Test(org.junit.Test)

Example 8 with NiFiProperties

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

the class NarUnpackerTest method testUnpackNarsFromNonDir.

@Test
public void testUnpackNarsFromNonDir() throws IOException {
    final File nonDir = new File("./target/file.txt");
    nonDir.createNewFile();
    nonDir.deleteOnExit();
    final Map<String, String> others = new HashMap<>();
    others.put("nifi.nar.library.directory.alt", nonDir.toString());
    NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", others);
    final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties, SystemBundle.create(properties));
    assertNull(extensionMapping);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.Test)

Example 9 with NiFiProperties

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

the class NarUnpackerTest method testUnpackNarsFromNonExistantDir.

@Test
public void testUnpackNarsFromNonExistantDir() {
    final File nonExistantDir = new File("./target/this/dir/should/not/exist/");
    nonExistantDir.delete();
    nonExistantDir.deleteOnExit();
    final Map<String, String> others = new HashMap<>();
    others.put("nifi.nar.library.directory.alt", nonExistantDir.toString());
    NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", others);
    final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties, SystemBundle.create(properties));
    assertTrue(extensionMapping.getAllExtensionNames().keySet().contains("org.apache.nifi.processors.dummy.one"));
    assertEquals(1, extensionMapping.getAllExtensionNames().size());
    final File extensionsWorkingDir = properties.getExtensionsWorkingDirectory();
    File[] extensionFiles = extensionsWorkingDir.listFiles();
    assertEquals(1, extensionFiles.length);
    assertEquals("dummy-one.nar-unpacked", extensionFiles[0].getName());
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.Test)

Example 10 with NiFiProperties

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

the class ProtectedNiFiProperties method getUnprotectedProperties.

/**
 * Returns the unprotected {@link NiFiProperties} instance. If none of the properties
 * loaded are marked as protected, it will simply pass through the internal instance.
 * If any are protected, it will drop the protection scheme keys and translate each
 * protected value (encrypted, HSM-retrieved, etc.) into the raw value and store it
 * under the original key.
 * <p>
 * If any property fails to unprotect, it will save that key and continue. After
 * attempting all properties, it will throw an exception containing all failed
 * properties. This is necessary because the order is not enforced, so all failed
 * properties should be gathered together.
 *
 * @return the NiFiProperties instance with all raw values
 * @throws SensitivePropertyProtectionException if there is a problem unprotecting one or more keys
 */
public NiFiProperties getUnprotectedProperties() throws SensitivePropertyProtectionException {
    if (hasProtectedKeys()) {
        logger.info("There are {} protected properties of {} sensitive properties ({}%)", getProtectedPropertyKeys().size(), getSensitivePropertyKeys().size(), getPercentOfSensitivePropertiesProtected());
        Properties rawProperties = new Properties();
        Set<String> failedKeys = new HashSet<>();
        for (String key : getPropertyKeys()) {
            /* Three kinds of keys
                 * 1. protection schemes -- skip
                 * 2. protected keys -- unprotect and copy
                 * 3. normal keys -- copy over
                 */
            if (key.endsWith(".protected")) {
            // Do nothing
            } else if (isPropertyProtected(key)) {
                try {
                    rawProperties.setProperty(key, unprotectValue(key, getProperty(key)));
                } catch (SensitivePropertyProtectionException e) {
                    logger.warn("Failed to unprotect '{}'", key, e);
                    failedKeys.add(key);
                }
            } else {
                rawProperties.setProperty(key, getProperty(key));
            }
        }
        if (!failedKeys.isEmpty()) {
            if (failedKeys.size() > 1) {
                logger.warn("Combining {} failed keys [{}] into single exception", failedKeys.size(), StringUtils.join(failedKeys, ", "));
                throw new MultipleSensitivePropertyProtectionException("Failed to unprotect keys", failedKeys);
            } else {
                throw new SensitivePropertyProtectionException("Failed to unprotect key " + failedKeys.iterator().next());
            }
        }
        NiFiProperties unprotected = new StandardNiFiProperties(rawProperties);
        return unprotected;
    } else {
        logger.debug("No protected properties");
        return getInternalNiFiProperties();
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Properties(java.util.Properties) NiFiProperties(org.apache.nifi.util.NiFiProperties) HashSet(java.util.HashSet)

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