Search in sources :

Example 16 with NiFiProperties

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

the class HostHeaderHandler method extractIPsFromNetworkInterfaces.

/**
 * Extracts the list of IP addresses from custom bound network interfaces. If both HTTPS and HTTP interfaces are
 * defined and HTTPS is enabled, only HTTPS interfaces will be returned. If none are defined, an empty list will be
 * returned.
 *
 * @param niFiProperties the NiFiProperties object
 * @return the list of IP addresses
 */
static List<String> extractIPsFromNetworkInterfaces(NiFiProperties niFiProperties) {
    Map<String, String> networkInterfaces = niFiProperties.isHTTPSConfigured() ? niFiProperties.getHttpsNetworkInterfaces() : niFiProperties.getHttpNetworkInterfaces();
    if (isNotDefined(networkInterfaces)) {
        // No custom interfaces defined
        return new ArrayList<>(0);
    } else {
        List<String> allIPAddresses = new ArrayList<>();
        for (Map.Entry<String, String> entry : networkInterfaces.entrySet()) {
            final String networkInterfaceName = entry.getValue();
            try {
                NetworkInterface ni = NetworkInterface.getByName(networkInterfaceName);
                List<String> ipAddresses = Collections.list(ni.getInetAddresses()).stream().map(inetAddress -> inetAddress.getHostAddress().toLowerCase()).collect(Collectors.toList());
                logger.debug("Resolved the following IP addresses for network interface {}: {}", new Object[] { networkInterfaceName, StringUtils.join(ipAddresses, ", ") });
                allIPAddresses.addAll(ipAddresses);
            } catch (SocketException e) {
                logger.warn("Cannot resolve network interface named " + networkInterfaceName);
            }
        }
        // Dedupe while maintaining order
        return uniqueList(allIPAddresses);
    }
}
Also used : Request(org.eclipse.jetty.server.Request) PrintWriter(java.io.PrintWriter) Logger(org.slf4j.Logger) ServletException(javax.servlet.ServletException) LoggerFactory(org.slf4j.LoggerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Objects(java.util.Objects) Strings(com.google.common.base.Strings) HttpServletRequest(javax.servlet.http.HttpServletRequest) SocketException(java.net.SocketException) List(java.util.List) NiFiProperties(org.apache.nifi.util.NiFiProperties) ScopedHandler(org.eclipse.jetty.server.handler.ScopedHandler) Map(java.util.Map) InetAddressUtils(org.apache.http.conn.util.InetAddressUtils) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) SocketException(java.net.SocketException) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) Map(java.util.Map)

Example 17 with NiFiProperties

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

the class JettyServerTest method testConfigureSslContextFactoryWithPkcsTrustStore.

@Test
public void testConfigureSslContextFactoryWithPkcsTrustStore() {
    // Expect that we will set Bouncy Castle provider for pkcs12 truststore
    final Map<String, String> addProps = new HashMap<>();
    String trustStoreType = KeystoreType.PKCS12.toString();
    addProps.put(NiFiProperties.SECURITY_TRUSTSTORE_TYPE, trustStoreType);
    NiFiProperties nifiProperties = NiFiProperties.createBasicNiFiProperties(null, addProps);
    SslContextFactory contextFactory = mock(SslContextFactory.class);
    JettyServer.configureSslContextFactory(contextFactory, nifiProperties);
    verify(contextFactory).setTrustStoreType(trustStoreType);
    verify(contextFactory).setTrustStoreProvider(BouncyCastleProvider.PROVIDER_NAME);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 18 with NiFiProperties

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

the class TestFlowController method testSynchronizeFlowWithProcessorReferencingControllerService.

@Test
public void testSynchronizeFlowWithProcessorReferencingControllerService() throws IOException {
    final FlowSynchronizer standardFlowSynchronizer = new StandardFlowSynchronizer(StringEncryptor.createEncryptor(nifiProperties), nifiProperties);
    // create a mock proposed data flow with the same auth fingerprint as the current authorizer
    final String authFingerprint = authorizer.getFingerprint();
    final DataFlow proposedDataFlow = Mockito.mock(DataFlow.class);
    when(proposedDataFlow.getAuthorizerFingerprint()).thenReturn(authFingerprint.getBytes(StandardCharsets.UTF_8));
    final File flowFile = new File("src/test/resources/conf/processor-with-cs-flow-0.7.0.xml");
    final String flow = IOUtils.toString(new FileInputStream(flowFile));
    when(proposedDataFlow.getFlow()).thenReturn(flow.getBytes(StandardCharsets.UTF_8));
    controller.synchronize(standardFlowSynchronizer, proposedDataFlow);
    // should be two controller services
    final Set<ControllerServiceNode> controllerServiceNodes = controller.getAllControllerServices();
    assertNotNull(controllerServiceNodes);
    assertEquals(1, controllerServiceNodes.size());
    // find the controller service that was moved to the root group
    final ControllerServiceNode rootGroupCs = controllerServiceNodes.stream().filter(c -> c.getProcessGroup() != null).findFirst().get();
    assertNotNull(rootGroupCs);
    // should be one processor
    final Set<ProcessorNode> processorNodes = controller.getGroup(controller.getRootGroupId()).getProcessors();
    assertNotNull(processorNodes);
    assertEquals(1, processorNodes.size());
    // verify the processor is still pointing at the controller service that got moved to the root group
    final ProcessorNode processorNode = processorNodes.stream().findFirst().get();
    final PropertyDescriptor procControllerServiceProp = processorNode.getProperties().entrySet().stream().filter(e -> e.getValue().equals(rootGroupCs.getIdentifier())).map(e -> e.getKey()).findFirst().get();
    assertNotNull(procControllerServiceProp);
}
Also used : MockPolicyBasedAuthorizer(org.apache.nifi.authorization.MockPolicyBasedAuthorizer) Bundle(org.apache.nifi.bundle.Bundle) Arrays(java.util.Arrays) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) FlowFileEventRepository(org.apache.nifi.controller.repository.FlowFileEventRepository) ProcessGroup(org.apache.nifi.groups.ProcessGroup) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) URL(java.net.URL) AbstractPolicyBasedAuthorizer(org.apache.nifi.authorization.AbstractPolicyBasedAuthorizer) BundleDTO(org.apache.nifi.web.api.dto.BundleDTO) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) ServiceB(org.apache.nifi.controller.service.mock.ServiceB) SchedulingStrategy(org.apache.nifi.scheduling.SchedulingStrategy) After(org.junit.After) Map(java.util.Map) MockProvenanceRepository(org.apache.nifi.provenance.MockProvenanceRepository) FlowRegistryClient(org.apache.nifi.registry.flow.FlowRegistryClient) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) Set(java.util.Set) UUID(java.util.UUID) StandardCharsets(java.nio.charset.StandardCharsets) User(org.apache.nifi.authorization.User) VariableRegistry(org.apache.nifi.registry.VariableRegistry) IOUtils(org.apache.commons.io.IOUtils) Stateful(org.apache.nifi.annotation.behavior.Stateful) Assert.assertFalse(org.junit.Assert.assertFalse) DummyProcessor(org.apache.nifi.controller.service.mock.DummyProcessor) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ExtensionManager(org.apache.nifi.nar.ExtensionManager) DummyReportingTask(org.apache.nifi.controller.service.mock.DummyReportingTask) AuditService(org.apache.nifi.admin.service.AuditService) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) Mockito.mock(org.mockito.Mockito.mock) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) HashMap(java.util.HashMap) Group(org.apache.nifi.authorization.Group) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor) Relationship(org.apache.nifi.processor.Relationship) SystemBundle(org.apache.nifi.nar.SystemBundle) LogLevel(org.apache.nifi.logging.LogLevel) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) InstanceClassLoader(org.apache.nifi.nar.InstanceClassLoader) FileBasedVariableRegistry(org.apache.nifi.registry.variable.FileBasedVariableRegistry) MalformedURLException(java.net.MalformedURLException) RequestAction(org.apache.nifi.authorization.RequestAction) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) LogRepository(org.apache.nifi.logging.LogRepository) Test(org.junit.Test) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) Mockito.when(org.mockito.Mockito.when) ServiceA(org.apache.nifi.controller.service.mock.ServiceA) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) NiFiProperties(org.apache.nifi.util.NiFiProperties) AccessPolicy(org.apache.nifi.authorization.AccessPolicy) BulletinRepository(org.apache.nifi.reporting.BulletinRepository) Assert(org.junit.Assert) Collections(java.util.Collections) LogRepositoryFactory(org.apache.nifi.logging.LogRepositoryFactory) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) File(java.io.File) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 19 with NiFiProperties

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

the class TestCuratorACLProviderFactory method testSaslAuthSchemeWithHostNoRealm.

@Test
public void testSaslAuthSchemeWithHostNoRealm() {
    final NiFiProperties nifiProperties;
    final CuratorACLProviderFactory factory;
    otherProps.put("nifi.zookeeper.kerberos.removeHostFromPrincipal", "false");
    otherProps.put("nifi.zookeeper.kerberos.removeRealmFromPrincipal", "true");
    nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, otherProps);
    factory = new CuratorACLProviderFactory();
    ZooKeeperClientConfig config = ZooKeeperClientConfig.createConfig(nifiProperties);
    ACLProvider provider = factory.create(config);
    assertFalse(provider instanceof DefaultACLProvider);
    List<ACL> acls = provider.getDefaultAcl();
    assertNotNull(acls);
    assertEquals(acls.get(0).getId().toString().trim(), "'sasl,'nifi/host");
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) ACLProvider(org.apache.curator.framework.api.ACLProvider) DefaultACLProvider(org.apache.curator.framework.imps.DefaultACLProvider) ZooKeeperClientConfig(org.apache.nifi.controller.cluster.ZooKeeperClientConfig) DefaultACLProvider(org.apache.curator.framework.imps.DefaultACLProvider) ACL(org.apache.zookeeper.data.ACL) Test(org.junit.Test)

Example 20 with NiFiProperties

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

the class TestFileSystemRepository method testMinimalArchiveCleanupIntervalHonoredAndLogged.

@Test
public void testMinimalArchiveCleanupIntervalHonoredAndLogged() throws Exception {
    // We are going to construct our own repository using different properties, so
    // we need to shutdown the existing one.
    shutdown();
    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    ListAppender<ILoggingEvent> testAppender = new ListAppender<>();
    testAppender.setName("Test");
    testAppender.start();
    root.addAppender(testAppender);
    final Map<String, String> addProps = new HashMap<>();
    addProps.put(NiFiProperties.CONTENT_ARCHIVE_CLEANUP_FREQUENCY, "1 millis");
    final NiFiProperties localProps = NiFiProperties.createBasicNiFiProperties(null, addProps);
    repository = new FileSystemRepository(localProps);
    repository.initialize(new StandardResourceClaimManager());
    repository.purge();
    boolean messageFound = false;
    String message = "The value of nifi.content.repository.archive.cleanup.frequency property " + "is set to '1 millis' which is below the allowed minimum of 1 second (1000 milliseconds). " + "Minimum value of 1 sec will be used as scheduling interval for archive cleanup task.";
    // keyword guards testAppender.list. Since we are accessing testAppender.list, we must do so in a thread-safe manner.
    synchronized (testAppender) {
        for (ILoggingEvent event : testAppender.list) {
            String actualMessage = event.getFormattedMessage();
            if (actualMessage.equals(message)) {
                assertEquals(event.getLevel(), Level.WARN);
                messageFound = true;
                break;
            }
        }
    }
    assertTrue(messageFound);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) HashMap(java.util.HashMap) ListAppender(ch.qos.logback.core.read.ListAppender) StandardResourceClaimManager(org.apache.nifi.controller.repository.claim.StandardResourceClaimManager) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.Test)

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