Search in sources :

Example 11 with HandshakeException

use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.

the class TestDataTransferResource method testCreateTransactionUnauthorized.

@Test
public void testCreateTransactionUnauthorized() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final HttpFlowFileServerProtocol serverProtocol = resource.getHttpFlowFileServerProtocol(null);
    doThrow(new HandshakeException(ResponseCode.UNAUTHORIZED, "Unauthorized.")).when(serverProtocol).handshake(any());
    final ServletContext context = null;
    final UriInfo uriInfo = null;
    final InputStream inputStream = null;
    final Response response = resource.createPortTransaction("input-ports", "port-id", req, context, uriInfo, inputStream);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(401, response.getStatus());
    assertEquals(ResponseCode.UNAUTHORIZED.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) InputStream(java.io.InputStream) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) ServletContext(javax.servlet.ServletContext) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 12 with HandshakeException

use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.

the class RemoteResourceFactory method receiveResourceNegotiation.

public static <T extends VersionedRemoteResource> T receiveResourceNegotiation(final Class<T> cls, final DataInputStream dis, final DataOutputStream dos, final Class<?>[] constructorArgClasses, final Object[] constructorArgs) throws IOException, HandshakeException {
    final String resourceClassName = dis.readUTF();
    final T resource;
    try {
        @SuppressWarnings("unchecked") final Class<T> resourceClass = (Class<T>) Class.forName(resourceClassName);
        if (!cls.isAssignableFrom(resourceClass)) {
            throw new HandshakeException("Expected to negotiate a Versioned Resource of type " + cls.getName() + " but received class name of " + resourceClassName);
        }
        final Constructor<T> ctr = resourceClass.getConstructor(constructorArgClasses);
        resource = ctr.newInstance(constructorArgs);
    } catch (final Throwable t) {
        dos.write(ABORT);
        final String errorMsg = "Unable to instantiate Versioned Resource of type " + resourceClassName;
        dos.writeUTF(errorMsg);
        dos.flush();
        throw new HandshakeException(errorMsg);
    }
    final int version = dis.readInt();
    final VersionNegotiator negotiator = resource.getVersionNegotiator();
    if (negotiator.isVersionSupported(version)) {
        dos.write(RESOURCE_OK);
        dos.flush();
        negotiator.setVersion(version);
        return resource;
    } else {
        final Integer preferred = negotiator.getPreferredVersion(version);
        if (preferred == null) {
            dos.write(ABORT);
            dos.flush();
            throw new HandshakeException("Unable to negotiate an acceptable version of the resource " + resourceClassName);
        }
        dos.write(DIFFERENT_RESOURCE_VERSION);
        dos.writeInt(preferred);
        dos.flush();
        return receiveResourceNegotiation(cls, dis, dos, constructorArgClasses, constructorArgs);
    }
}
Also used : HandshakeException(org.apache.nifi.remote.exception.HandshakeException)

Example 13 with HandshakeException

use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.

the class SocketFlowFileServerProtocol method doHandshake.

@Override
protected HandshakeProperties doHandshake(Peer peer) throws IOException, HandshakeException {
    HandshakeProperties confirmed = new HandshakeProperties();
    final CommunicationsSession commsSession = peer.getCommunicationsSession();
    final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream());
    final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
    confirmed.setCommsIdentifier(dis.readUTF());
    if (versionNegotiator.getVersion() >= 3) {
        String transitUriPrefix = dis.readUTF();
        if (!transitUriPrefix.endsWith("/")) {
            transitUriPrefix = transitUriPrefix + "/";
        }
        confirmed.setTransitUriPrefix(transitUriPrefix);
    }
    final Map<String, String> properties = new HashMap<>();
    final int numProperties = dis.readInt();
    for (int i = 0; i < numProperties; i++) {
        final String propertyName = dis.readUTF();
        final String propertyValue = dis.readUTF();
        properties.put(propertyName, propertyValue);
    }
    // evaluate the properties received
    boolean responseWritten = false;
    try {
        validateHandshakeRequest(confirmed, peer, properties);
    } catch (HandshakeException e) {
        ResponseCode handshakeResult = e.getResponseCode();
        if (handshakeResult.containsMessage()) {
            handshakeResult.writeResponse(dos, e.getMessage());
        } else {
            handshakeResult.writeResponse(dos);
        }
        switch(handshakeResult) {
            case UNAUTHORIZED:
            case PORT_NOT_IN_VALID_STATE:
            case PORTS_DESTINATION_FULL:
                responseWritten = true;
                break;
            default:
                throw e;
        }
    }
    // send "OK" response
    if (!responseWritten) {
        ResponseCode.PROPERTIES_OK.writeResponse(dos);
    }
    return confirmed;
}
Also used : ResponseCode(org.apache.nifi.remote.protocol.ResponseCode) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) DataInputStream(java.io.DataInputStream) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties)

Example 14 with HandshakeException

use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.

the class SocketFlowFileServerProtocol method negotiateCodec.

@Override
public FlowFileCodec negotiateCodec(final Peer peer) throws IOException, ProtocolException {
    if (!handshakeCompleted) {
        throw new IllegalStateException("Handshake has not been completed");
    }
    if (shutdown) {
        throw new IllegalStateException("Protocol is shutdown");
    }
    logger.debug("{} Negotiating Codec with {} using {}", new Object[] { this, peer, peer.getCommunicationsSession() });
    final CommunicationsSession commsSession = peer.getCommunicationsSession();
    final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream());
    final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
    if (port == null) {
        RemoteResourceFactory.rejectCodecNegotiation(dis, dos, "Cannot transfer FlowFiles because no port was specified");
    }
    // Negotiate the FlowFileCodec to use.
    try {
        negotiatedFlowFileCodec = RemoteResourceFactory.receiveCodecNegotiation(dis, dos);
        logger.debug("{} Negotiated Codec {} with {}", new Object[] { this, negotiatedFlowFileCodec, peer });
        return negotiatedFlowFileCodec;
    } catch (final HandshakeException e) {
        throw new ProtocolException(e.toString());
    }
}
Also used : ProtocolException(org.apache.nifi.remote.exception.ProtocolException) DataOutputStream(java.io.DataOutputStream) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) DataInputStream(java.io.DataInputStream) HandshakeException(org.apache.nifi.remote.exception.HandshakeException)

Example 15 with HandshakeException

use of org.apache.nifi.remote.exception.HandshakeException in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testPortDestinationFull.

@Test
public void testPortDestinationFull() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer();
    ((HttpServerCommunicationsSession) peer.getCommunicationsSession()).putHandshakeParam(HandshakeProperty.PORT_IDENTIFIER, "port-identifier");
    final ProcessGroup processGroup = mock(ProcessGroup.class);
    final RootGroupPort port = mock(RootGroupPort.class);
    final PortAuthorizationResult authResult = mock(PortAuthorizationResult.class);
    doReturn(true).when(processGroup).isRootGroup();
    doReturn(port).when(processGroup).getOutputPort("port-identifier");
    doReturn(authResult).when(port).checkUserAuthorization(any(String.class));
    doReturn(true).when(authResult).isAuthorized();
    doReturn(true).when(port).isValid();
    doReturn(true).when(port).isRunning();
    final Set<Connection> connections = new HashSet<>();
    final Connection connection = mock(Connection.class);
    connections.add(connection);
    doReturn(connections).when(port).getConnections();
    final FlowFileQueue flowFileQueue = mock(FlowFileQueue.class);
    doReturn(flowFileQueue).when(connection).getFlowFileQueue();
    doReturn(true).when(flowFileQueue).isFull();
    serverProtocol.setRootProcessGroup(processGroup);
    try {
        serverProtocol.handshake(peer);
        fail();
    } catch (final HandshakeException e) {
        assertEquals(ResponseCode.PORTS_DESTINATION_FULL, e.getResponseCode());
    }
    assertFalse(serverProtocol.isHandshakeSuccessful());
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) RootGroupPort(org.apache.nifi.remote.RootGroupPort) Peer(org.apache.nifi.remote.Peer) ProcessGroup(org.apache.nifi.groups.ProcessGroup) Connection(org.apache.nifi.connectable.Connection) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) PortAuthorizationResult(org.apache.nifi.remote.PortAuthorizationResult) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HandshakeException (org.apache.nifi.remote.exception.HandshakeException)26 Peer (org.apache.nifi.remote.Peer)13 IOException (java.io.IOException)10 Test (org.junit.Test)10 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)9 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)8 HttpServerCommunicationsSession (org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)7 UnknownHostException (java.net.UnknownHostException)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 AccessDeniedException (org.apache.nifi.authorization.AccessDeniedException)6 BadRequestException (org.apache.nifi.remote.exception.BadRequestException)6 NotAuthorizedException (org.apache.nifi.remote.exception.NotAuthorizedException)6 RequestExpiredException (org.apache.nifi.remote.exception.RequestExpiredException)6 ApiOperation (io.swagger.annotations.ApiOperation)5 ApiResponses (io.swagger.annotations.ApiResponses)5 DataInputStream (java.io.DataInputStream)5 DataOutputStream (java.io.DataOutputStream)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 ProtocolException (org.apache.nifi.remote.exception.ProtocolException)5