Search in sources :

Example 36 with NiFiProperties

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

the class KnoxAuthenticationFilterTest method setUp.

@Before
public void setUp() throws Exception {
    final NiFiProperties nifiProperties = Mockito.mock(NiFiProperties.class);
    when(nifiProperties.isKnoxSsoEnabled()).thenReturn(true);
    when(nifiProperties.getKnoxCookieName()).thenReturn(COOKIE_NAME);
    knoxAuthenticationFilter = new KnoxAuthenticationFilter();
    knoxAuthenticationFilter.setProperties(nifiProperties);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Before(org.junit.Before)

Example 37 with NiFiProperties

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

the class ITAccessTokenEndpoint method setup.

@BeforeClass
public static void setup() throws Exception {
    // configure the location of the nifi properties
    File nifiPropertiesFile = new File("src/test/resources/access-control/nifi.properties");
    System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, nifiPropertiesFile.getAbsolutePath());
    NiFiProperties props = NiFiProperties.createBasicNiFiProperties(null, null);
    flowXmlPath = props.getProperty(NiFiProperties.FLOW_CONFIGURATION_FILE);
    // delete the database directory to avoid issues with re-registration in testRequestAccessUsingToken
    FileUtils.deleteDirectory(props.getDatabaseRepositoryPath().toFile());
    // load extensions
    final Bundle systemBundle = SystemBundle.create(props);
    NarClassLoaders.getInstance().init(props.getFrameworkWorkingDirectory(), props.getExtensionsWorkingDirectory());
    ExtensionManager.discoverExtensions(systemBundle, NarClassLoaders.getInstance().getBundles());
    // start the server
    SERVER = new NiFiTestServer("src/main/webapp", CONTEXT_PATH, props);
    SERVER.startServer();
    SERVER.loadFlow();
    // get the base url
    BASE_URL = SERVER.getBaseUrl() + CONTEXT_PATH;
    // create the user
    final Client client = WebUtils.createClient(null, createTrustContext(props));
    TOKEN_USER = new NiFiTestUser(client, null);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Bundle(org.apache.nifi.bundle.Bundle) SystemBundle(org.apache.nifi.nar.SystemBundle) NiFiTestServer(org.apache.nifi.integration.util.NiFiTestServer) Client(javax.ws.rs.client.Client) File(java.io.File) NiFiTestUser(org.apache.nifi.integration.util.NiFiTestUser) BeforeClass(org.junit.BeforeClass)

Example 38 with NiFiProperties

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

the class TestThreadPoolRequestReplicator method testOneNodeRejectsTwoPhaseCommit.

@Test(timeout = 15000)
public void testOneNodeRejectsTwoPhaseCommit() {
    final Set<NodeIdentifier> nodeIds = new HashSet<>();
    nodeIds.add(new NodeIdentifier("1", "localhost", 8100, "localhost", 8101, "localhost", 8102, 8103, false));
    nodeIds.add(new NodeIdentifier("2", "localhost", 8200, "localhost", 8201, "localhost", 8202, 8203, false));
    final ClusterCoordinator coordinator = createClusterCoordinator();
    final AtomicInteger requestCount = new AtomicInteger(0);
    final NiFiProperties props = NiFiProperties.createBasicNiFiProperties(null, null);
    final ThreadPoolRequestReplicator replicator = new ThreadPoolRequestReplicator(2, 5, 100, ClientBuilder.newClient(), coordinator, "1 sec", "1 sec", null, null, props) {

        @Override
        protected NodeResponse replicateRequest(final Invocation invocation, final NodeIdentifier nodeId, final String method, final URI uri, final String requestId, Map<String, String> givenHeaders, final StandardAsyncClusterResponse response) {
            // the resource builder will not expose its headers to us, so we are using Mockito's Whitebox class to extract them.
            final ClientRequest requestContext = (ClientRequest) Whitebox.getInternalState(invocation, "requestContext");
            final Object expectsHeader = requestContext.getHeaders().getFirst(ThreadPoolRequestReplicator.REQUEST_VALIDATION_HTTP_HEADER);
            final int requestIndex = requestCount.incrementAndGet();
            assertEquals(ThreadPoolRequestReplicator.NODE_CONTINUE, expectsHeader);
            if (requestIndex == 1) {
                final Response clientResponse = mock(Response.class);
                when(clientResponse.getStatus()).thenReturn(150);
                return new NodeResponse(nodeId, method, uri, clientResponse, -1L, requestId);
            } else {
                final IllegalClusterStateException explanation = new IllegalClusterStateException("Intentional Exception for Unit Testing");
                return new NodeResponse(nodeId, method, uri, explanation);
            }
        }
    };
    try {
        // set the user
        final Authentication authentication = new NiFiAuthenticationToken(new NiFiUserDetails(StandardNiFiUser.ANONYMOUS));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        final AsyncClusterResponse clusterResponse = replicator.replicate(nodeIds, HttpMethod.POST, new URI("http://localhost:80/processors/1"), new ProcessorEntity(), new HashMap<>(), true, true);
        clusterResponse.awaitMergedResponse();
        Assert.fail("Expected to get an IllegalClusterStateException but did not");
    } catch (final IllegalClusterStateException e) {
    // Expected
    } catch (final Exception e) {
        Assert.fail(e.toString());
    } finally {
        replicator.shutdown();
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Invocation(javax.ws.rs.client.Invocation) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) URI(java.net.URI) ClientRequest(org.glassfish.jersey.client.ClientRequest) NiFiUserDetails(org.apache.nifi.authorization.user.NiFiUserDetails) HashSet(java.util.HashSet) IllegalClusterStateException(org.apache.nifi.cluster.manager.exception.IllegalClusterStateException) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) DisconnectedNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.DisconnectedNodeMutableRequestException) URISyntaxException(java.net.URISyntaxException) IllegalClusterStateException(org.apache.nifi.cluster.manager.exception.IllegalClusterStateException) ProcessingException(javax.ws.rs.ProcessingException) ConnectingNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.ConnectingNodeMutableRequestException) SocketTimeoutException(java.net.SocketTimeoutException) NiFiAuthenticationToken(org.apache.nifi.web.security.token.NiFiAuthenticationToken) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) Response(javax.ws.rs.core.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Authentication(org.springframework.security.core.Authentication) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.apache.commons.collections4.map.MultiValueMap) Test(org.junit.Test)

Example 39 with NiFiProperties

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

the class TestThreadPoolRequestReplicator method withReplicator.

private void withReplicator(final WithReplicator function, final Status status, final long delayMillis, final RuntimeException failure, final String expectedRequestChain) {
    final ClusterCoordinator coordinator = createClusterCoordinator();
    final NiFiProperties nifiProps = NiFiProperties.createBasicNiFiProperties(null, null);
    final ThreadPoolRequestReplicator replicator = new ThreadPoolRequestReplicator(2, 5, 100, ClientBuilder.newClient(), coordinator, "1 sec", "1 sec", null, null, nifiProps) {

        @Override
        protected NodeResponse replicateRequest(final Invocation invocation, final NodeIdentifier nodeId, final String method, final URI uri, final String requestId, Map<String, String> givenHeaders, final StandardAsyncClusterResponse response) {
            if (delayMillis > 0L) {
                try {
                    Thread.sleep(delayMillis);
                } catch (InterruptedException e) {
                    Assert.fail("Thread Interrupted during test");
                }
            }
            if (failure != null) {
                throw failure;
            }
            final ClientRequest requestContext = (ClientRequest) Whitebox.getInternalState(invocation, "requestContext");
            final Object proxiedEntities = requestContext.getHeaders().getFirst(ProxiedEntitiesUtils.PROXY_ENTITIES_CHAIN);
            // ensure the request chain is in the request
            Assert.assertEquals(expectedRequestChain, proxiedEntities);
            // Return given response from all nodes.
            final Response clientResponse = mock(Response.class);
            when(clientResponse.getStatus()).thenReturn(status.getStatusCode());
            return new NodeResponse(nodeId, method, uri, clientResponse, -1L, requestId);
        }
    };
    try {
        function.withReplicator(replicator);
    } catch (final Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    } finally {
        replicator.shutdown();
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Invocation(javax.ws.rs.client.Invocation) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) URI(java.net.URI) DisconnectedNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.DisconnectedNodeMutableRequestException) URISyntaxException(java.net.URISyntaxException) IllegalClusterStateException(org.apache.nifi.cluster.manager.exception.IllegalClusterStateException) ProcessingException(javax.ws.rs.ProcessingException) ConnectingNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.ConnectingNodeMutableRequestException) SocketTimeoutException(java.net.SocketTimeoutException) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) Response(javax.ws.rs.core.Response) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.apache.commons.collections4.map.MultiValueMap) ClientRequest(org.glassfish.jersey.client.ClientRequest)

Example 40 with NiFiProperties

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

the class TestThreadPoolRequestReplicator method testMultipleRequestWithTwoPhaseCommit.

@Test(timeout = 15000)
public void testMultipleRequestWithTwoPhaseCommit() {
    final Set<NodeIdentifier> nodeIds = new HashSet<>();
    final NodeIdentifier nodeId = new NodeIdentifier("1", "localhost", 8100, "localhost", 8101, "localhost", 8102, 8103, false);
    nodeIds.add(nodeId);
    final ClusterCoordinator coordinator = mock(ClusterCoordinator.class);
    when(coordinator.getConnectionStatus(Mockito.any(NodeIdentifier.class))).thenReturn(new NodeConnectionStatus(nodeId, NodeConnectionState.CONNECTED));
    final AtomicInteger requestCount = new AtomicInteger(0);
    final NiFiProperties props = NiFiProperties.createBasicNiFiProperties(null, null);
    final ThreadPoolRequestReplicator replicator = new ThreadPoolRequestReplicator(2, 5, 100, ClientBuilder.newClient(), coordinator, "1 sec", "1 sec", null, null, props) {

        @Override
        protected NodeResponse replicateRequest(final Invocation invocation, final NodeIdentifier nodeId, final String method, final URI uri, final String requestId, Map<String, String> givenHeaders, final StandardAsyncClusterResponse response) {
            // the resource builder will not expose its headers to us, so we are using Mockito's Whitebox class to extract them.
            final ClientRequest requestContext = (ClientRequest) Whitebox.getInternalState(invocation, "requestContext");
            final Object expectsHeader = requestContext.getHeaders().getFirst(ThreadPoolRequestReplicator.REQUEST_VALIDATION_HTTP_HEADER);
            final int statusCode;
            if (requestCount.incrementAndGet() == 1) {
                assertEquals(ThreadPoolRequestReplicator.NODE_CONTINUE, expectsHeader);
                statusCode = 150;
            } else {
                assertNull(expectsHeader);
                statusCode = Status.OK.getStatusCode();
            }
            // Return given response from all nodes.
            final Response clientResponse = mock(Response.class);
            when(clientResponse.getStatus()).thenReturn(statusCode);
            return new NodeResponse(nodeId, method, uri, clientResponse, -1L, requestId);
        }
    };
    try {
        // set the user
        final Authentication authentication = new NiFiAuthenticationToken(new NiFiUserDetails(StandardNiFiUser.ANONYMOUS));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        final AsyncClusterResponse clusterResponse = replicator.replicate(nodeIds, HttpMethod.POST, new URI("http://localhost:80/processors/1"), new ProcessorEntity(), new HashMap<>(), true, true);
        clusterResponse.awaitMergedResponse();
        // Ensure that we received two requests - the first should contain the X-NcmExpects header; the second should not.
        // These assertions are validated above, in the overridden replicateRequest method.
        assertEquals(2, requestCount.get());
    } catch (final Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    } finally {
        replicator.shutdown();
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Invocation(javax.ws.rs.client.Invocation) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) URI(java.net.URI) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) ClientRequest(org.glassfish.jersey.client.ClientRequest) NiFiUserDetails(org.apache.nifi.authorization.user.NiFiUserDetails) HashSet(java.util.HashSet) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) DisconnectedNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.DisconnectedNodeMutableRequestException) URISyntaxException(java.net.URISyntaxException) IllegalClusterStateException(org.apache.nifi.cluster.manager.exception.IllegalClusterStateException) ProcessingException(javax.ws.rs.ProcessingException) ConnectingNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.ConnectingNodeMutableRequestException) SocketTimeoutException(java.net.SocketTimeoutException) NiFiAuthenticationToken(org.apache.nifi.web.security.token.NiFiAuthenticationToken) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) Response(javax.ws.rs.core.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Authentication(org.springframework.security.core.Authentication) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.apache.commons.collections4.map.MultiValueMap) 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