Search in sources :

Example 21 with SecurityContext

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

the class OperationsFacade method callProcedure.

private RawIterator<Object[], ProcedureException> callProcedure(QualifiedName name, Object[] input, final AccessMode override) throws ProcedureException {
    statement.assertOpen();
    final SecurityContext procedureSecurityContext = tx.securityContext().withMode(override);
    final RawIterator<Object[], ProcedureException> procedureCall;
    try (KernelTransaction.Revertable ignore = tx.overrideWith(procedureSecurityContext)) {
        BasicContext ctx = new BasicContext();
        ctx.put(Context.KERNEL_TRANSACTION, tx);
        ctx.put(Context.THREAD, Thread.currentThread());
        ctx.put(Context.SECURITY_CONTEXT, procedureSecurityContext);
        procedureCall = procedures.callProcedure(ctx, name, input);
    }
    return new RawIterator<Object[], ProcedureException>() {

        @Override
        public boolean hasNext() throws ProcedureException {
            try (KernelTransaction.Revertable ignore = tx.overrideWith(procedureSecurityContext)) {
                return procedureCall.hasNext();
            }
        }

        @Override
        public Object[] next() throws ProcedureException {
            try (KernelTransaction.Revertable ignore = tx.overrideWith(procedureSecurityContext)) {
                return procedureCall.next();
            }
        }
    };
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) RawIterator(org.neo4j.collection.RawIterator)

Example 22 with SecurityContext

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

the class KernelTransactionImplementation method overrideWith.

@Override
public Revertable overrideWith(SecurityContext context) {
    SecurityContext oldContext = this.securityContext;
    this.securityContext = context;
    return () -> this.securityContext = oldContext;
}
Also used : SecurityContext(org.neo4j.kernel.api.security.SecurityContext)

Example 23 with SecurityContext

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

the class KernelTransactionsTest method exceptionWhenStartingNewTransactionOnShutdownInstance.

@Test
public void exceptionWhenStartingNewTransactionOnShutdownInstance() throws Throwable {
    KernelTransactions kernelTransactions = newKernelTransactions();
    SecurityContext securityContext = mock(SecurityContext.class);
    availabilityGuard.shutdown();
    expectedException.expect(DatabaseShutdownException.class);
    kernelTransactions.newInstance(KernelTransaction.Type.explicit, securityContext, 0L);
}
Also used : SecurityContext(org.neo4j.kernel.api.security.SecurityContext) Test(org.junit.Test)

Example 24 with SecurityContext

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

the class KernelTransactionsTest method startNewTransactionOnRestartedKErnelTransactions.

@Test
public void startNewTransactionOnRestartedKErnelTransactions() throws Throwable {
    KernelTransactions kernelTransactions = newKernelTransactions();
    SecurityContext securityContext = mock(SecurityContext.class);
    kernelTransactions.stop();
    kernelTransactions.start();
    assertNotNull("New transaction created by restarted kernel transactions component.", kernelTransactions.newInstance(KernelTransaction.Type.explicit, securityContext, 0L));
}
Also used : SecurityContext(org.neo4j.kernel.api.security.SecurityContext) Test(org.junit.Test)

Example 25 with SecurityContext

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

the class TransactionEventsIT method shouldGetSpecifiedUsernameAndMetaDataInTXData.

@Test
public void shouldGetSpecifiedUsernameAndMetaDataInTXData() {
    final AtomicReference<String> usernameRef = new AtomicReference<>();
    final AtomicReference<Map<String, Object>> metaDataRef = new AtomicReference<>();
    db.registerTransactionEventHandler(getBeforeCommitHandler(txData -> {
        usernameRef.set(txData.username());
        metaDataRef.set(txData.metaData());
    }));
    AuthSubject subject = mock(AuthSubject.class);
    when(subject.username()).thenReturn("Christof");
    SecurityContext securityContext = new SecurityContext.Frozen(subject, AccessMode.Static.WRITE);
    Map<String, Object> metadata = genericMap("username", "joe");
    runTransaction(securityContext, metadata);
    assertThat("Should have specified username", usernameRef.get(), equalTo("Christof"));
    assertThat("Should have metadata with specified username", metaDataRef.get(), equalTo(metadata));
}
Also used : Label(org.neo4j.graphdb.Label) AnonymousContext(org.neo4j.kernel.api.security.AnonymousContext) RootCauseMatcher(org.neo4j.test.mockito.matcher.RootCauseMatcher) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) DatabaseRule(org.neo4j.test.rule.DatabaseRule) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) RandomRule(org.neo4j.test.rule.RandomRule) MapUtil.genericMap(org.neo4j.helpers.collection.MapUtil.genericMap) Map(java.util.Map) AccessMode(org.neo4j.kernel.api.security.AccessMode) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Transaction(org.neo4j.graphdb.Transaction) ExpectedException(org.junit.rules.ExpectedException) AuthSubject(org.neo4j.kernel.api.security.AuthSubject) ImpermanentDatabaseRule(org.neo4j.test.rule.ImpermanentDatabaseRule) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RuleChain(org.junit.rules.RuleChain) Consumer(java.util.function.Consumer) List(java.util.List) Rule(org.junit.Rule) Relationship(org.neo4j.graphdb.Relationship) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RelationshipType(org.neo4j.graphdb.RelationshipType) PropertyContainer(org.neo4j.graphdb.PropertyContainer) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AuthSubject(org.neo4j.kernel.api.security.AuthSubject) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) MapUtil.genericMap(org.neo4j.helpers.collection.MapUtil.genericMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

SecurityContext (org.neo4j.kernel.api.security.SecurityContext)33 Test (org.junit.Test)20 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)6 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)4 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 IOException (java.io.IOException)2 Principal (java.security.Principal)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Consumes (javax.ws.rs.Consumes)2 Produces (javax.ws.rs.Produces)2 InOrder (org.mockito.InOrder)2 ReturnsDeepStubs (org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs)2 Transaction (org.neo4j.graphdb.Transaction)2 QueryRegistryOperations (org.neo4j.kernel.api.QueryRegistryOperations)2 Statement (org.neo4j.kernel.api.Statement)2 InvalidArgumentsException (org.neo4j.kernel.api.exceptions.InvalidArgumentsException)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