Search in sources :

Example 1 with AlwaysOKAuthentication

use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.

the class PostgresWireProtocolTest method test_channel_is_flushed_after_receiving_flush_request.

@Test
public void test_channel_is_flushed_after_receiving_flush_request() throws Exception {
    SQLOperations sqlOperations = mock(SQLOperations.class);
    Session session = mock(Session.class);
    when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
    AtomicBoolean flushed = new AtomicBoolean(false);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler) {

        @Override
        public Channel flush() {
            flushed.set(true);
            return super.flush();
        }
    };
    ByteBuf buffer = Unpooled.buffer();
    ClientMessages.sendStartupMessage(buffer, "doc");
    ClientMessages.sendParseMessage(buffer, "", "select ?", new int[0]);
    ClientMessages.sendFlush(buffer);
    channel.writeInbound(buffer);
    channel.releaseInbound();
    assertThat(flushed.get(), is(true));
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) User(io.crate.user.User) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SecureString(org.elasticsearch.common.settings.SecureString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) SQLOperations(io.crate.action.sql.SQLOperations) Session(io.crate.action.sql.Session) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 2 with AlwaysOKAuthentication

use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.

the class PostgresWireProtocolTest method test_row_description_for_statement_on_single_table_includes_table_oid.

@Test
public void test_row_description_for_statement_on_single_table_includes_table_oid() 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 buffer = Unpooled.buffer();
        ClientMessages.sendStartupMessage(buffer, "doc");
        ClientMessages.sendParseMessage(buffer, "S1", "SELECT name FROM users", new int[0]);
        channel.writeInbound(buffer);
        channel.releaseInbound();
        // we're not interested in the startup, parse, or bind replies
        channel.flushOutbound();
        channel.releaseOutbound();
        channel.outboundMessages().clear();
    }
    {
        ByteBuf buffer = Unpooled.buffer();
        ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
        channel.writeInbound(buffer);
        channel.releaseInbound();
        // we should get back a ParameterDescription message, but not interesting for this test case
        channel.flushOutbound();
        ByteBuf response = channel.readOutbound();
        response.release();
        // we should get back a RowDescription message
        response = channel.readOutbound();
        try {
            assertThat(response.readByte(), is((byte) 'T'));
            assertThat(response.readInt(), is(29));
            assertThat(response.readShort(), is((short) 1));
            assertThat(PostgresWireProtocol.readCString(response), is("name"));
            assertThat("table_oid", response.readInt(), is(893280107));
            assertThat("attr_num", response.readShort(), is((short) 1));
            var pgType = PGTypes.get(DataTypes.STRING);
            assertThat(response.readInt(), is(pgType.oid()));
            assertThat(response.readShort(), is(pgType.typeLen()));
            assertThat(response.readInt(), is(pgType.typeMod()));
            assertThat("format_code", response.readShort(), is((short) 0));
        } finally {
            response.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) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 3 with AlwaysOKAuthentication

use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.

the class PostgresWireProtocolTest method testSessionCloseOnTerminationMessage.

@Test
public void testSessionCloseOnTerminationMessage() throws Exception {
    SQLOperations sqlOperations = mock(SQLOperations.class);
    Session session = mock(Session.class);
    when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    ByteBuf buffer = Unpooled.buffer();
    ClientMessages.sendStartupMessage(buffer, "doc");
    ClientMessages.sendTermination(buffer);
    channel.writeInbound(buffer);
    channel.releaseInbound();
    verify(session, times(1)).close();
}
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) User(io.crate.user.User) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SecureString(org.elasticsearch.common.settings.SecureString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) SQLOperations(io.crate.action.sql.SQLOperations) Session(io.crate.action.sql.Session) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with AlwaysOKAuthentication

use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.

the class PostgresWireProtocolTest method submitQueriesThroughSimpleQueryMode.

private void submitQueriesThroughSimpleQueryMode(String statements, @Nullable Throwable failure) {
    SQLOperations sqlOperations = Mockito.mock(SQLOperations.class);
    Session session = mock(Session.class);
    SessionContext sessionContext = new SessionContext(User.CRATE_USER);
    when(session.sessionContext()).thenReturn(sessionContext);
    when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
    DescribeResult describeResult = mock(DescribeResult.class);
    when(describeResult.getFields()).thenReturn(null);
    when(session.describe(anyChar(), anyString())).thenReturn(describeResult);
    when(session.transactionState()).thenReturn(TransactionState.IDLE);
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionCtx -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    if (failure != null) {
        when(session.sync()).thenAnswer(invocationOnMock -> {
            Messages.sendErrorResponse(channel, AccessControl.DISABLED, failure);
            return CompletableFuture.failedFuture(failure);
        });
    } else {
        when(session.sync()).thenReturn(CompletableFuture.completedFuture(null));
    }
    sendStartupMessage(channel);
    readAuthenticationOK(channel);
    skipParameterMessages(channel);
    readReadyForQueryMessage(channel);
    ByteBuf query = Unpooled.buffer();
    try {
        // the actual statements don't have to be valid as they are not executed
        Messages.writeCString(query, statements.getBytes(StandardCharsets.UTF_8));
        ctx.handleSimpleQuery(query, new DelayableWriteChannel(channel));
    } finally {
        query.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) DescribeResult(io.crate.action.sql.DescribeResult) User(io.crate.user.User) SessionContext(io.crate.action.sql.SessionContext) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SecureString(org.elasticsearch.common.settings.SecureString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) SQLOperations(io.crate.action.sql.SQLOperations) Session(io.crate.action.sql.Session)

Example 5 with AlwaysOKAuthentication

use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.

the class SslReqHandlerTest method testSslReqHandler.

@Test
public void testSslReqHandler() {
    PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class), sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> null), // use a simple ssl context
    getSelfSignedSslContextProvider());
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    sendSslRequest(channel);
    // We should get back an 'S'...
    ByteBuf responseBuffer = channel.readOutbound();
    byte response = responseBuffer.readByte();
    assertEquals(response, 'S');
    responseBuffer.release();
    // ...and continue encrypted (ssl handler)
    assertThat(channel.pipeline().first(), instanceOf(SslHandler.class));
}
Also used : SslContext(io.netty.handler.ssl.SslContext) BeforeClass(org.junit.BeforeClass) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) AccessControl(io.crate.auth.AccessControl) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test) Unpooled(io.netty.buffer.Unpooled) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Protocol(io.crate.auth.Protocol) Settings(org.elasticsearch.common.settings.Settings) ByteBuf(io.netty.buffer.ByteBuf) SslHandler(io.netty.handler.ssl.SslHandler) Locale(java.util.Locale) After(org.junit.After) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) ESTestCase(org.elasticsearch.test.ESTestCase) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) SQLOperations(io.crate.action.sql.SQLOperations) Mockito.mock(org.mockito.Mockito.mock) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) SQLOperations(io.crate.action.sql.SQLOperations) SslHandler(io.netty.handler.ssl.SslHandler) Test(org.junit.Test)

Aggregations

AlwaysOKAuthentication (io.crate.auth.AlwaysOKAuthentication)17 Settings (org.elasticsearch.common.settings.Settings)16 Test (org.junit.Test)16 SQLOperations (io.crate.action.sql.SQLOperations)15 StubUserManager (io.crate.user.StubUserManager)14 User (io.crate.user.User)13 Collections (java.util.Collections)12 List (java.util.List)12 TimeUnit (java.util.concurrent.TimeUnit)12 Version (org.elasticsearch.Version)12 After (org.junit.After)12 AccessControl (io.crate.auth.AccessControl)11 ByteBuf (io.netty.buffer.ByteBuf)11 Unpooled (io.netty.buffer.Unpooled)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)11 Map (java.util.Map)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 Nullable (javax.annotation.Nullable)11