Search in sources :

Example 6 with AlwaysOKAuthentication

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

the class PostgresNettyPublishPortTest method testPublishAddressOverride.

@Test
public void testPublishAddressOverride() {
    // Check override for network.publish_host
    Settings settingsWithCustomPublish = Settings.builder().put("network.publish_host", "cantbindtothis").build();
    NetworkService networkService = new NetworkService(Collections.emptyList());
    StubUserManager userManager = new StubUserManager();
    PostgresNetty psql = new PostgresNetty(settingsWithCustomPublish, mock(SQLOperations.class), userManager, networkService, new AlwaysOKAuthentication(userName -> User.CRATE_USER), new NettyBootstrap(), mock(SslContextProvider.class));
    try {
        psql.doStart();
        fail("Should have failed due to custom hostname");
    } catch (BindTransportException e) {
        // that's what we want
        assertThat(e.getCause(), instanceOf(UnknownHostException.class));
    } finally {
        psql.doStop();
        psql.close();
    }
}
Also used : PostgresNetty.resolvePublishPort(io.crate.protocols.postgres.PostgresNetty.resolvePublishPort) User(io.crate.user.User) BindTransportException(org.elasticsearch.transport.BindTransportException) Test(org.junit.Test) BindHttpException(org.elasticsearch.http.BindHttpException) UnknownHostException(java.net.UnknownHostException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) NetworkService(org.elasticsearch.common.network.NetworkService) InetAddress.getByName(java.net.InetAddress.getByName) Settings(org.elasticsearch.common.settings.Settings) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Arrays.asList(java.util.Arrays.asList) NettyBootstrap(io.crate.netty.NettyBootstrap) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) ESTestCase(org.elasticsearch.test.ESTestCase) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) SQLOperations(io.crate.action.sql.SQLOperations) StubUserManager(io.crate.user.StubUserManager) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) NetworkService(org.elasticsearch.common.network.NetworkService) BindTransportException(org.elasticsearch.transport.BindTransportException) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) SQLOperations(io.crate.action.sql.SQLOperations) Settings(org.elasticsearch.common.settings.Settings) StubUserManager(io.crate.user.StubUserManager) NettyBootstrap(io.crate.netty.NettyBootstrap) Test(org.junit.Test)

Example 7 with AlwaysOKAuthentication

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

the class PostgresNettyPublishPortTest method testBindAddressOverrideSetting.

@Test
public void testBindAddressOverrideSetting() {
    // Check override for network.bind_host
    Settings settingsWithCustomBind = Settings.builder().put("network.bind_host", "cantbindtothis").build();
    NetworkService networkService = new NetworkService(Collections.emptyList());
    StubUserManager userManager = new StubUserManager();
    PostgresNetty psql = new PostgresNetty(settingsWithCustomBind, mock(SQLOperations.class), userManager, networkService, new AlwaysOKAuthentication(userManager), new NettyBootstrap(), mock(SslContextProvider.class));
    try {
        psql.doStart();
        fail("Should have failed due to custom hostname");
    } catch (BindPostgresException e) {
        // that's what we want
        assertThat(e.getCause(), instanceOf(UnknownHostException.class));
    } finally {
        psql.doStop();
        psql.close();
    }
}
Also used : AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) NetworkService(org.elasticsearch.common.network.NetworkService) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) SQLOperations(io.crate.action.sql.SQLOperations) Settings(org.elasticsearch.common.settings.Settings) StubUserManager(io.crate.user.StubUserManager) NettyBootstrap(io.crate.netty.NettyBootstrap) Test(org.junit.Test)

Example 8 with AlwaysOKAuthentication

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

the class PostgresNettyPublishPortTest method testBindAndPublishAddressDefault.

@Test
public void testBindAndPublishAddressDefault() {
    // First check if binding to a local works
    NetworkService networkService = new NetworkService(Collections.emptyList());
    StubUserManager userManager = new StubUserManager();
    PostgresNetty psql = new PostgresNetty(Settings.EMPTY, mock(SQLOperations.class), userManager, networkService, new AlwaysOKAuthentication(userManager), new NettyBootstrap(), mock(SslContextProvider.class));
    try {
        psql.doStart();
    } finally {
        psql.doStop();
        psql.close();
    }
}
Also used : AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) NetworkService(org.elasticsearch.common.network.NetworkService) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) SQLOperations(io.crate.action.sql.SQLOperations) StubUserManager(io.crate.user.StubUserManager) NettyBootstrap(io.crate.netty.NettyBootstrap) Test(org.junit.Test)

Example 9 with AlwaysOKAuthentication

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

the class PostgresWireProtocolTest method testBindMessageCanBeReadIfTypeForParamsIsUnknown.

@Test
public void testBindMessageCanBeReadIfTypeForParamsIsUnknown() 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");
    // no type hints for parameters
    ClientMessages.sendParseMessage(buffer, "S1", "select ?, ?", new int[0]);
    List<Object> params = Arrays.asList(10, 20);
    ClientMessages.sendBindMessage(buffer, "P1", "S1", params);
    channel.writeInbound(buffer);
    channel.releaseInbound();
    Session session = sessions.get(0);
    // If the query can be retrieved via portalName it means bind worked
    assertThat(session.getQuery("P1"), is("select ?, ?"));
}
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) Session(io.crate.action.sql.Session) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 10 with AlwaysOKAuthentication

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

the class PostgresWireProtocolTest method testDescribeStatementMessage.

@Test
public void testDescribeStatementMessage() 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 ? in (1, 2, 3)", 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();
    }
    {
        // try the describe statement variant
        ByteBuf buffer = Unpooled.buffer();
        ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
        channel.writeInbound(buffer);
        channel.releaseInbound();
        // we should get back a ParameterDescription message
        channel.flushOutbound();
        ByteBuf response = channel.readOutbound();
        try {
            assertThat(response.readByte(), is((byte) 't'));
            assertThat(response.readInt(), is(10));
            assertThat(response.readShort(), is((short) 1));
            assertThat(response.readInt(), is(PGTypes.get(DataTypes.INTEGER).oid()));
        } finally {
            response.release();
        }
        // we should get back a RowDescription message
        response = channel.readOutbound();
        try {
            assertThat(response.readByte(), is((byte) 'T'));
            assertThat(response.readInt(), is(46));
            assertThat(response.readShort(), is((short) 1));
            assertThat(PostgresWireProtocol.readCString(response), is("($1 = ANY([1, 2, 3]))"));
            assertThat(response.readInt(), is(0));
            assertThat(response.readShort(), is((short) 0));
            assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
            assertThat(response.readShort(), is(PGTypes.get(DataTypes.BOOLEAN).typeLen()));
            assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
            assertThat(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)

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