Search in sources :

Example 6 with SecurityContext

use of org.neo4j.internal.kernel.api.security.SecurityContext 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 7 with SecurityContext

use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.

the class SecurityContextDescriptionTest method shouldMakeNiceDescriptionAuthDisabled.

@Test
void shouldMakeNiceDescriptionAuthDisabled() {
    SecurityContext disabled = SecurityContext.AUTH_DISABLED;
    assertThat(disabled.description()).isEqualTo("AUTH_DISABLED with FULL");
}
Also used : SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Test(org.junit.jupiter.api.Test)

Example 8 with SecurityContext

use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.

the class SecurityContextDescriptionTest method shouldMakeNiceDescriptionOverridden.

@Test
void shouldMakeNiceDescriptionOverridden() {
    SecurityContext overridden = context.withMode(new OverriddenAccessMode(context.mode(), AccessMode.Static.READ));
    assertThat(overridden.description()).isEqualTo("user 'johan' with FULL overridden by READ");
}
Also used : OverriddenAccessMode(org.neo4j.kernel.impl.api.security.OverriddenAccessMode) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Test(org.junit.jupiter.api.Test)

Example 9 with SecurityContext

use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.

the class SecurityContextDescriptionTest method shouldMakeNiceDescriptionAuthDisabledAndRestricted.

@Test
void shouldMakeNiceDescriptionAuthDisabledAndRestricted() {
    SecurityContext disabled = SecurityContext.AUTH_DISABLED;
    SecurityContext restricted = disabled.withMode(new RestrictedAccessMode(disabled.mode(), AccessMode.Static.READ));
    assertThat(restricted.description()).isEqualTo("AUTH_DISABLED with FULL restricted to READ");
}
Also used : RestrictedAccessMode(org.neo4j.kernel.impl.api.security.RestrictedAccessMode) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Test(org.junit.jupiter.api.Test)

Example 10 with SecurityContext

use of org.neo4j.internal.kernel.api.security.SecurityContext in project neo4j by neo4j.

the class AllStoreHolder method callProcedure.

private RawIterator<AnyValue[], ProcedureException> callProcedure(int id, AnyValue[] input, final AccessMode.Static procedureMode, ProcedureCallContext procedureCallContext) throws ProcedureException {
    ktx.assertOpen();
    AccessMode mode = ktx.securityContext().mode();
    if (!mode.allowsExecuteProcedure(id)) {
        String message = format("Executing procedure is not allowed for %s.", ktx.securityContext().description());
        throw ktx.securityAuthorizationHandler().logAndGetAuthorizationException(ktx.securityContext(), message);
    }
    final SecurityContext procedureSecurityContext = mode.shouldBoostProcedure(id) ? ktx.securityContext().withMode(new OverriddenAccessMode(mode, procedureMode)).withMode(AdminAccessMode.FULL) : ktx.securityContext().withMode(new RestrictedAccessMode(mode, procedureMode));
    final RawIterator<AnyValue[], ProcedureException> procedureCall;
    try (KernelTransaction.Revertable ignore = ktx.overrideWith(procedureSecurityContext);
        Statement statement = ktx.acquireStatement()) {
        procedureCall = globalProcedures.callProcedure(prepareContext(procedureSecurityContext, procedureCallContext), id, input, statement);
    }
    return createIterator(procedureSecurityContext, procedureCall);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) OverriddenAccessMode(org.neo4j.kernel.impl.api.security.OverriddenAccessMode) RestrictedAccessMode(org.neo4j.kernel.impl.api.security.RestrictedAccessMode) Statement(org.neo4j.kernel.api.Statement) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) AdminAccessMode(org.neo4j.internal.kernel.api.security.AdminAccessMode) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode) RestrictedAccessMode(org.neo4j.kernel.impl.api.security.RestrictedAccessMode) OverriddenAccessMode(org.neo4j.kernel.impl.api.security.OverriddenAccessMode) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException)

Aggregations

SecurityContext (org.neo4j.internal.kernel.api.security.SecurityContext)25 Test (org.junit.jupiter.api.Test)17 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)15 TestAccessMode (org.neo4j.internal.kernel.api.security.TestAccessMode)7 RestrictedAccessMode (org.neo4j.kernel.impl.api.security.RestrictedAccessMode)5 AccessMode (org.neo4j.internal.kernel.api.security.AccessMode)4 OverriddenAccessMode (org.neo4j.kernel.impl.api.security.OverriddenAccessMode)3 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)2 InOrder (org.mockito.InOrder)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2 ReturnsDeepStubs (org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs)2 DependencyResolver (org.neo4j.common.DependencyResolver)2