use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.
the class KernelTransactions method newInstance.
public KernelTransaction newInstance(KernelTransaction.Type type, LoginContext loginContext, ClientConnectionInfo clientInfo, long timeout) {
assertCurrentThreadIsNotBlockingNewTransactions();
SecurityContext securityContext = loginContext.authorize(tokenHoldersIdLookup, namedDatabaseId.name(), securityLog);
try {
while (!newTransactionsLock.readLock().tryLock(1, TimeUnit.SECONDS)) {
assertRunning();
}
try {
assertRunning();
TransactionId lastCommittedTransaction = transactionIdStore.getLastCommittedTransaction();
KernelTransactionImplementation tx = localTxPool.acquire();
Locks.Client lockClient = locks.newClient();
tx.initialize(lastCommittedTransaction.transactionId(), lastCommittedTransaction.commitTimestamp(), lockClient, type, securityContext, timeout, userTransactionIdCounter.incrementAndGet(), clientInfo);
return tx;
} finally {
newTransactionsLock.readLock().unlock();
}
} catch (InterruptedException ie) {
Thread.interrupted();
throw new TransactionFailureException("Fail to start new transaction.", ie);
}
}
use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.
the class BuiltInProceduresTest method setup.
@BeforeEach
void setup() throws Exception {
procs.registerComponent(KernelTransaction.class, ctx -> ctx.internalTransaction().kernelTransaction(), false);
procs.registerComponent(DependencyResolver.class, Context::dependencyResolver, false);
procs.registerComponent(GraphDatabaseAPI.class, Context::graphDatabaseAPI, false);
procs.registerComponent(Transaction.class, Context::internalTransaction, true);
procs.registerComponent(SecurityContext.class, Context::securityContext, true);
procs.registerComponent(ProcedureCallContext.class, Context::procedureCallContext, true);
procs.registerComponent(SystemGraphComponents.class, ctx -> systemGraphComponents, false);
procs.registerComponent(Log.class, ctx -> log, false);
procs.registerType(Node.class, NTNode);
procs.registerType(Relationship.class, NTRelationship);
procs.registerType(Path.class, NTPath);
new SpecialBuiltInProcedures("1.3.37", Edition.COMMUNITY.toString()).accept(procs);
procs.registerProcedure(BuiltInProcedures.class);
procs.registerProcedure(BuiltInDbmsProcedures.class);
when(transaction.kernelTransaction()).thenReturn(tx);
when(tx.tokenRead()).thenReturn(tokens);
when(tx.dataRead()).thenReturn(read);
when(tx.schemaRead()).thenReturn(schemaRead);
when(tx.securityContext()).thenReturn(SecurityContext.AUTH_DISABLED);
when(callContext.isCalledFromCypher()).thenReturn(false);
when(schemaRead.snapshot()).thenReturn(schemaReadCore);
when(tokens.propertyKeyGetAllTokens()).thenAnswer(asTokens(propKeys));
when(tokens.labelsGetAllTokens()).thenAnswer(asTokens(labels));
when(tokens.relationshipTypesGetAllTokens()).thenAnswer(asTokens(relTypes));
when(schemaReadCore.indexesGetAll()).thenAnswer(i -> Iterators.concat(indexes.iterator(), uniqueIndexes.iterator()));
when(schemaReadCore.index(any(SchemaDescriptor.class))).thenAnswer((Answer<IndexDescriptor>) invocationOnMock -> {
SchemaDescriptor schema = invocationOnMock.getArgument(0);
return getIndexReference(schema);
});
when(schemaReadCore.constraintsGetAll()).thenAnswer(i -> constraints.iterator());
when(tokens.propertyKeyName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
when(tokens.nodeLabelName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
when(tokens.relationshipTypeName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
when(tokens.propertyKeyGetName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
when(tokens.labelGetName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
when(tokens.relationshipTypeGetName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
when(tokens.entityTokensGetNames(any(), any())).then(invocation -> {
EntityType type = invocation.getArgument(0);
int[] ids = invocation.getArgument(1);
Map<Integer, String> mapping = type == EntityType.NODE ? labels : relTypes;
return Arrays.stream(ids).mapToObj(mapping::get).toArray(String[]::new);
});
when(schemaReadCore.constraintsGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.indexesGetForLabel(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.indexesGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.constraintsGetForLabel(anyInt())).thenReturn(emptyIterator());
when(read.countsForNode(anyInt())).thenReturn(1L);
when(read.countsForRelationship(anyInt(), anyInt(), anyInt())).thenReturn(1L);
when(schemaReadCore.indexGetState(any(IndexDescriptor.class))).thenReturn(InternalIndexState.ONLINE);
}
use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.
the class NodeTransactionStateTestBase method shouldCountNewNodesFromTxStateRestrictedUser.
@Test
void shouldCountNewNodesFromTxStateRestrictedUser() throws Exception {
// Given
createNode();
createNode();
SecurityContext loginContext = new SecurityContext(AuthSubject.AUTH_DISABLED, new TestAccessMode(true, false, true, false), EMBEDDED_CONNECTION, null);
try (KernelTransaction tx = beginTransaction(loginContext)) {
// when
tx.dataWrite().nodeCreate();
long countTxState = tx.dataRead().countsForNode(-1);
long countNoTxState = tx.dataRead().countsForNodeWithoutTxState(-1);
// then
assertEquals(3, countTxState);
assertEquals(2, countNoTxState);
}
}
use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.
the class NodeTransactionStateTestBase method shouldCountNewLabelsFromTxStateRestrictedUser.
@Test
void shouldCountNewLabelsFromTxStateRestrictedUser() throws Exception {
// Given
Node node1 = createNode("label");
Node node2 = createNode();
SecurityContext loginContext = new SecurityContext(AuthSubject.AUTH_DISABLED, new TestAccessMode(true, false, true, false), EMBEDDED_CONNECTION, null);
try (KernelTransaction tx = beginTransaction(loginContext)) {
// when
tx.dataWrite().nodeAddLabel(node2.node, node1.labels[0]);
long countTxState = tx.dataRead().countsForNode(node1.labels[0]);
long countNoTxState = tx.dataRead().countsForNodeWithoutTxState(node1.labels[0]);
// then
assertEquals(2, countTxState);
assertEquals(1, countNoTxState);
}
}
use of org.neo4j.internal.kernel.api.security.SecurityContext 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();
}
Aggregations