Search in sources :

Example 26 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class PostgresWireProtocolTest method testCrateServerVersionIsReceivedOnStartup.

@Test
public void testCrateServerVersionIsReceivedOnStartup() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    ByteBuf buf = Unpooled.buffer();
    ClientMessages.sendStartupMessage(buf, "doc");
    channel.writeInbound(buf);
    channel.releaseInbound();
    ByteBuf respBuf;
    respBuf = channel.readOutbound();
    try {
        // Auth OK
        assertThat((char) respBuf.readByte(), is('R'));
    } finally {
        respBuf.release();
    }
    respBuf = channel.readOutbound();
    try {
        // ParameterStatus
        assertThat((char) respBuf.readByte(), is('S'));
        // length
        respBuf.readInt();
        String key = PostgresWireProtocol.readCString(respBuf);
        String value = PostgresWireProtocol.readCString(respBuf);
        assertThat(key, is("crate_version"));
        assertThat(value, is(Version.CURRENT.externalNumber()));
    } finally {
        respBuf.release();
    }
}
Also used : Arrays(java.util.Arrays) SessionContext(io.crate.action.sql.SessionContext) PGTypes(io.crate.protocols.postgres.types.PGTypes) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Provider(org.elasticsearch.common.inject.Provider) Unpooled(io.netty.buffer.Unpooled) DependencyCarrier(io.crate.planner.DependencyCarrier) Settings(org.elasticsearch.common.settings.Settings) After(org.junit.After) Map(java.util.Map) SecureString(org.elasticsearch.common.settings.SecureString) SQLOperations(io.crate.action.sql.SQLOperations) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) User(io.crate.user.User) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Session(io.crate.action.sql.Session) Version(org.elasticsearch.Version) DataTypes(io.crate.types.DataTypes) UserManager(io.crate.user.UserManager) Matchers.is(org.hamcrest.Matchers.is) DescribeResult(io.crate.action.sql.DescribeResult) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) AccessControl(io.crate.auth.AccessControl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Authentication(io.crate.auth.Authentication) SQLExecutor(io.crate.testing.SQLExecutor) StubUserManager(io.crate.user.StubUserManager) Nullable(javax.annotation.Nullable) Before(org.junit.Before) ArgumentMatchers.anyChar(org.mockito.ArgumentMatchers.anyChar) JobKilledException(io.crate.exceptions.JobKilledException) AuthenticationMethod(io.crate.auth.AuthenticationMethod) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) ChannelHandler(io.netty.channel.ChannelHandler) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SecureString(org.elasticsearch.common.settings.SecureString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 27 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class PostgresWireProtocolTest method testHandleEmptySimpleQuery.

@Test
public void testHandleEmptySimpleQuery() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class), sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> null), null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    ByteBuf buffer = Unpooled.buffer();
    try {
        Messages.writeCString(buffer, ";".getBytes(StandardCharsets.UTF_8));
        ctx.handleSimpleQuery(buffer, new DelayableWriteChannel(channel));
    } finally {
        buffer.release();
    }
    ByteBuf firstResponse = channel.readOutbound();
    byte[] responseBytes = new byte[5];
    try {
        firstResponse.readBytes(responseBytes);
        // EmptyQueryResponse: 'I' | int32 len
        assertThat(responseBytes, is(new byte[] { 'I', 0, 0, 0, 4 }));
    } finally {
        firstResponse.release();
    }
    ByteBuf secondResponse = channel.readOutbound();
    try {
        responseBytes = new byte[6];
        secondResponse.readBytes(responseBytes);
        // ReadyForQuery: 'Z' | int32 len | 'I'
        assertThat(responseBytes, is(new byte[] { 'Z', 0, 0, 0, 5, 'I' }));
    } finally {
        secondResponse.release();
    }
}
Also used : Arrays(java.util.Arrays) SessionContext(io.crate.action.sql.SessionContext) PGTypes(io.crate.protocols.postgres.types.PGTypes) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Provider(org.elasticsearch.common.inject.Provider) Unpooled(io.netty.buffer.Unpooled) DependencyCarrier(io.crate.planner.DependencyCarrier) Settings(org.elasticsearch.common.settings.Settings) After(org.junit.After) Map(java.util.Map) SecureString(org.elasticsearch.common.settings.SecureString) SQLOperations(io.crate.action.sql.SQLOperations) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) User(io.crate.user.User) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Session(io.crate.action.sql.Session) Version(org.elasticsearch.Version) DataTypes(io.crate.types.DataTypes) UserManager(io.crate.user.UserManager) Matchers.is(org.hamcrest.Matchers.is) DescribeResult(io.crate.action.sql.DescribeResult) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) AccessControl(io.crate.auth.AccessControl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Authentication(io.crate.auth.Authentication) SQLExecutor(io.crate.testing.SQLExecutor) StubUserManager(io.crate.user.StubUserManager) Nullable(javax.annotation.Nullable) Before(org.junit.Before) ArgumentMatchers.anyChar(org.mockito.ArgumentMatchers.anyChar) JobKilledException(io.crate.exceptions.JobKilledException) AuthenticationMethod(io.crate.auth.AuthenticationMethod) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) ChannelHandler(io.netty.channel.ChannelHandler) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) SQLOperations(io.crate.action.sql.SQLOperations) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 28 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class UpdatePlanner method createCollectAndMerge.

private static ExecutionPlan createCollectAndMerge(PlannerContext plannerCtx, TableInfo tableInfo, Reference idReference, Projection updateProjection, WhereClause where, int numOutPuts, int maxRowsPerNode, Projection... mergeProjections) {
    SessionContext sessionContext = plannerCtx.transactionContext().sessionContext();
    Routing routing = plannerCtx.allocateRouting(tableInfo, where, RoutingProvider.ShardSelection.PRIMARIES, sessionContext);
    RoutedCollectPhase collectPhase = new RoutedCollectPhase(plannerCtx.jobId(), plannerCtx.nextExecutionPhaseId(), "collect", routing, tableInfo.rowGranularity(), List.of(idReference), singletonList(updateProjection), Optimizer.optimizeCasts(where.queryOrFallback(), plannerCtx), DistributionInfo.DEFAULT_BROADCAST);
    Collect collect = new Collect(collectPhase, TopN.NO_LIMIT, 0, numOutPuts, maxRowsPerNode, null);
    return Merge.ensureOnHandler(collect, plannerCtx, List.of(mergeProjections));
}
Also used : Collect(io.crate.planner.node.dql.Collect) SessionContext(io.crate.action.sql.SessionContext) Routing(io.crate.metadata.Routing) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase)

Example 29 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class AccessControlMayExecuteTest method test_reset_session_authorization_from_normal_user_succeeds.

@Test
public void test_reset_session_authorization_from_normal_user_succeeds() {
    e.analyzer.analyze(SqlParser.createStatement("RESET SESSION AUTHORIZATION"), new SessionContext(superUser, user), ParamTypeHints.EMPTY);
    assertThat(validationCallArguments.size(), is(0));
}
Also used : SessionContext(io.crate.action.sql.SessionContext) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 30 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class AccessControlMayExecuteTest method test_set_session_user_from_normal_user_to_default_succeeds.

@Test
public void test_set_session_user_from_normal_user_to_default_succeeds() {
    e.analyzer.analyze(SqlParser.createStatement("SET SESSION AUTHORIZATION DEFAULT"), new SessionContext(superUser, user), ParamTypeHints.EMPTY);
    assertThat(validationCallArguments.size(), is(0));
}
Also used : SessionContext(io.crate.action.sql.SessionContext) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

SessionContext (io.crate.action.sql.SessionContext)31 Test (org.junit.Test)26 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)18 SQLExecutor (io.crate.testing.SQLExecutor)14 User (io.crate.user.User)13 List (java.util.List)13 ArrayList (java.util.ArrayList)12 DependencyCarrier (io.crate.planner.DependencyCarrier)11 DataTypes (io.crate.types.DataTypes)11 Arrays (java.util.Arrays)11 Map (java.util.Map)11 DescribeResult (io.crate.action.sql.DescribeResult)10 SQLOperations (io.crate.action.sql.SQLOperations)10 Session (io.crate.action.sql.Session)10 AccessControl (io.crate.auth.AccessControl)10 AlwaysOKAuthentication (io.crate.auth.AlwaysOKAuthentication)10 Authentication (io.crate.auth.Authentication)10 AuthenticationMethod (io.crate.auth.AuthenticationMethod)10 JobKilledException (io.crate.exceptions.JobKilledException)10 JobsLogs (io.crate.execution.engine.collect.stats.JobsLogs)10