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);
}
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();
}
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();
}
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");
}
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);
}
}
Aggregations