Search in sources :

Example 1 with ClientConnectionInfo

use of org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo in project neo4j by neo4j.

the class ClientConnectionInfoTest method connectionDetailsForHttpQuerySource.

@Test
void connectionDetailsForHttpQuerySource() {
    // given
    ClientConnectionInfo clientConnection = new HttpConnectionInfo("http-42", "http", new InetSocketAddress("127.0.0.1", 1337), null, "/db/neo4j/tx/45/commit");
    // when
    String connectionDetails = clientConnection.asConnectionDetails();
    // then
    assertEquals("server-session\thttp\t127.0.0.1\t/db/neo4j/tx/45/commit", connectionDetails);
}
Also used : ClientConnectionInfo(org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo) InetSocketAddress(java.net.InetSocketAddress) HttpConnectionInfo(org.neo4j.kernel.impl.query.clientconnection.HttpConnectionInfo) Test(org.junit.jupiter.api.Test)

Example 2 with ClientConnectionInfo

use of org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo in project neo4j by neo4j.

the class Neo4jTransactionalContextTest method rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit.

@SuppressWarnings("ConstantConditions")
@Test
void rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit() throws TransactionFailureException {
    // Given
    InternalTransaction userTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
    KernelTransaction.Type transactionType = KernelTransaction.Type.IMPLICIT;
    SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
    ClientConnectionInfo connectionInfo = ClientConnectionInfo.EMBEDDED_CONNECTION;
    when(userTransaction.transactionType()).thenReturn(transactionType);
    when(userTransaction.clientInfo()).thenReturn(connectionInfo);
    when(userTransaction.securityContext()).thenReturn(securityContext);
    when(userTransaction.terminationReason()).thenReturn(Optional.empty());
    GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
    KernelStatement initialStatement = mock(KernelStatement.class);
    KernelTransaction initialKTX = mockTransaction(initialStatement);
    QueryRegistry initialQueryRegistry = mock(QueryRegistry.class);
    ExecutingQuery executingQuery = mock(ExecutingQuery.class);
    KernelStatement secondStatement = mock(KernelStatement.class);
    KernelTransaction secondKTX = mockTransaction(secondStatement);
    QueryRegistry secondQueryRegistry = mock(QueryRegistry.class);
    when(transactionFactory.beginKernelTransaction(transactionType, securityContext, connectionInfo)).thenReturn(secondKTX);
    when(executingQuery.databaseId()).thenReturn(Optional.of(namedDatabaseId));
    Mockito.doThrow(RuntimeException.class).when(initialKTX).commit();
    when(initialStatement.queryRegistration()).thenReturn(initialQueryRegistry);
    when(userTransaction.kernelTransaction()).thenReturn(initialKTX, initialKTX, secondKTX);
    when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
    Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, userTransaction, initialStatement, executingQuery, transactionFactory);
    // When
    assertThrows(RuntimeException.class, context::commitAndRestartTx);
    Object[] mocks = { userTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX };
    InOrder order = Mockito.inOrder(mocks);
    // (0) Constructor
    order.verify(userTransaction).transactionType();
    order.verify(userTransaction).securityContext();
    order.verify(userTransaction).clientInfo();
    // not terminated check
    order.verify(userTransaction).terminationReason();
    // (1) Collect statistics
    order.verify(initialKTX).executionStatistics();
    // (3) Register new
    order.verify(secondKTX).acquireStatement();
    order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
    // (4) Unregister, and close old
    order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
    order.verify(userTransaction).rollback();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InOrder(org.mockito.InOrder) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) QueryRegistry(org.neo4j.kernel.api.QueryRegistry) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ClientConnectionInfo(org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ReturnsDeepStubs(org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs) Test(org.junit.jupiter.api.Test)

Example 3 with ClientConnectionInfo

use of org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo in project neo4j by neo4j.

the class Neo4jTransactionalContextTest method neverStopsExecutingQueryDuringCommitAndRestartTx.

@Test
void neverStopsExecutingQueryDuringCommitAndRestartTx() throws TransactionFailureException {
    // Given
    KernelTransaction initialKTX = mockTransaction(statement);
    InternalTransaction userTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
    KernelTransaction.Type transactionType = KernelTransaction.Type.IMPLICIT;
    SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
    ClientConnectionInfo connectionInfo = ClientConnectionInfo.EMBEDDED_CONNECTION;
    when(userTransaction.transactionType()).thenReturn(transactionType);
    when(userTransaction.securityContext()).thenReturn(securityContext);
    when(userTransaction.terminationReason()).thenReturn(Optional.empty());
    when(userTransaction.clientInfo()).thenReturn(connectionInfo);
    QueryRegistry initialQueryRegistry = mock(QueryRegistry.class);
    ExecutingQuery executingQuery = mock(ExecutingQuery.class);
    KernelStatement secondStatement = mock(KernelStatement.class);
    KernelTransaction secondKTX = mockTransaction(secondStatement);
    QueryRegistry secondQueryRegistry = mock(QueryRegistry.class);
    when(transactionFactory.beginKernelTransaction(transactionType, securityContext, connectionInfo)).thenReturn(secondKTX);
    when(executingQuery.databaseId()).thenReturn(Optional.of(namedDatabaseId));
    when(statement.queryRegistration()).thenReturn(initialQueryRegistry);
    when(userTransaction.kernelTransaction()).thenReturn(initialKTX, initialKTX, secondKTX);
    when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
    Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, userTransaction, statement, executingQuery, transactionFactory);
    // When
    context.commitAndRestartTx();
    // Then
    Object[] mocks = { userTransaction, initialKTX, initialQueryRegistry, secondQueryRegistry, secondKTX };
    InOrder order = Mockito.inOrder(mocks);
    // (0) Constructor
    order.verify(userTransaction).transactionType();
    order.verify(userTransaction).securityContext();
    order.verify(userTransaction).clientInfo();
    // not terminated check
    order.verify(userTransaction).terminationReason();
    // (1) Collect stats
    order.verify(initialKTX).executionStatistics();
    // (3) Register new
    order.verify(secondKTX).acquireStatement();
    order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
    // (4) Unregister, and close old
    order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
    order.verify(initialKTX).commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InOrder(org.mockito.InOrder) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryRegistry(org.neo4j.kernel.api.QueryRegistry) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ClientConnectionInfo(org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ReturnsDeepStubs(org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs) Test(org.junit.jupiter.api.Test)

Example 4 with ClientConnectionInfo

use of org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo in project neo4j by neo4j.

the class BoltChannelTest method shouldExposeClientConnectionInfo.

@Test
void shouldExposeClientConnectionInfo() {
    EmbeddedChannel channel = new EmbeddedChannel();
    BoltChannel boltChannel = new BoltChannel("bolt-42", "my-bolt", channel, ChannelProtector.NULL);
    ClientConnectionInfo info1 = boltChannel.info();
    assertEquals("bolt-42", info1.connectionId());
    assertEquals("bolt", info1.protocol());
    assertEquals(SocketAddress.format(channel.remoteAddress()), info1.clientAddress());
    boltChannel.updateUser("Tom", "my-driver");
    ClientConnectionInfo info2 = boltChannel.info();
    assertEquals("bolt-42", info2.connectionId());
    assertEquals("bolt", info2.protocol());
    assertEquals(SocketAddress.format(channel.remoteAddress()), info2.clientAddress());
    assertThat(info2.asConnectionDetails()).contains("my-driver");
}
Also used : ClientConnectionInfo(org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 5 with ClientConnectionInfo

use of org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo in project neo4j by neo4j.

the class AuthorizationDisabledFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    validateRequestType(servletRequest);
    validateResponseType(servletResponse);
    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;
    try {
        ClientConnectionInfo connectionInfo = HttpConnectionInfoFactory.create(request);
        LoginContext loginContext = getAuthDisabledLoginContext(connectionInfo);
        String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
        JettyHttpConnection.updateUserForCurrentConnection(loginContext.subject().username(), userAgent);
        filterChain.doFilter(new AuthorizedRequestWrapper(BASIC_AUTH, "neo4j", request, loginContext), servletResponse);
    } catch (AuthorizationViolationException e) {
        unauthorizedAccess(e.getMessage()).accept(response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ClientConnectionInfo(org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthorizationViolationException(org.neo4j.graphdb.security.AuthorizationViolationException)

Aggregations

ClientConnectionInfo (org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo)8 Test (org.junit.jupiter.api.Test)5 InetSocketAddress (java.net.InetSocketAddress)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 InOrder (org.mockito.InOrder)2 ReturnsDeepStubs (org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs)2 AuthorizationViolationException (org.neo4j.graphdb.security.AuthorizationViolationException)2 LoginContext (org.neo4j.internal.kernel.api.security.LoginContext)2 SecurityContext (org.neo4j.internal.kernel.api.security.SecurityContext)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 QueryRegistry (org.neo4j.kernel.api.QueryRegistry)2 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)2 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)2 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 Principal (java.security.Principal)1 AuthProviderFailedException (org.neo4j.graphdb.security.AuthProviderFailedException)1 AuthProviderTimeoutException (org.neo4j.graphdb.security.AuthProviderTimeoutException)1