Search in sources :

Example 1 with ConnectionProperties

use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.

the class HostBasedAuthenticationTest method testFilterEntriesSimple.

@Test
public void testFilterEntriesSimple() {
    HostBasedAuthentication authService = new HostBasedAuthentication(HBA_1, null, SystemDefaultDnsResolver.INSTANCE);
    Optional entry;
    entry = authService.getEntry("crate", new ConnectionProperties(LOCALHOST, Protocol.POSTGRES, null));
    assertThat(entry.isPresent(), is(true));
    entry = authService.getEntry("cr8", new ConnectionProperties(LOCALHOST, Protocol.POSTGRES, null));
    assertThat(entry.isPresent(), is(false));
    entry = authService.getEntry("crate", new ConnectionProperties(InetAddresses.forString("10.0.0.1"), Protocol.POSTGRES, null));
    assertThat(entry.isPresent(), is(false));
}
Also used : Optional(java.util.Optional) ConnectionProperties(io.crate.protocols.postgres.ConnectionProperties) Test(org.junit.Test)

Example 2 with ConnectionProperties

use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.

the class HostBasedAuthenticationTest method test_filter_entries_hostname.

@Test
public void test_filter_entries_hostname() {
    HostBasedAuthentication authService = new HostBasedAuthentication(HBA_5, null, inMemoryResolver);
    Optional entry = authService.getEntry("crate", new ConnectionProperties(InetAddresses.forString(TEST_DNS_IP), Protocol.POSTGRES, null));
    assertTrue(entry.isPresent());
}
Also used : Optional(java.util.Optional) ConnectionProperties(io.crate.protocols.postgres.ConnectionProperties) Test(org.junit.Test)

Example 3 with ConnectionProperties

use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.

the class HostBasedAuthenticationTest method testFilterEntriesCIDR.

@Test
public void testFilterEntriesCIDR() {
    Settings settings = Settings.builder().put(HBA_2).put(HBA_3).build();
    HostBasedAuthentication authService = new HostBasedAuthentication(settings, null, SystemDefaultDnsResolver.INSTANCE);
    Optional<Map.Entry<String, Map<String, String>>> entry;
    entry = authService.getEntry("crate", new ConnectionProperties(InetAddresses.forString("123.45.67.89"), Protocol.POSTGRES, null));
    assertTrue(entry.isPresent());
    assertThat(entry.get().getValue().get("method"), is("fake"));
    entry = authService.getEntry("cr8", new ConnectionProperties(InetAddresses.forString("127.0.0.1"), Protocol.POSTGRES, null));
    assertTrue(entry.isPresent());
    assertThat(entry.get().getValue().get("method"), is("md5"));
    entry = authService.getEntry("cr8", new ConnectionProperties(InetAddresses.forString("123.45.67.89"), Protocol.POSTGRES, null));
    assertThat(entry.isPresent(), is(false));
}
Also used : ConnectionProperties(io.crate.protocols.postgres.ConnectionProperties) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Example 4 with ConnectionProperties

use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.

the class HostBasedAuthHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (authError != null) {
        ReferenceCountUtil.release(msg);
        Netty4TcpChannel tcpChannel = ctx.channel().attr(Netty4Transport.CHANNEL_KEY).get();
        CloseableChannel.closeChannel(tcpChannel, true);
        throw authError;
    }
    Channel channel = ctx.channel();
    InetAddress remoteAddress = Netty4HttpServerTransport.getRemoteAddress(channel);
    ConnectionProperties connectionProperties = new ConnectionProperties(remoteAddress, Protocol.TRANSPORT, SSL.getSession(channel));
    String userName = User.CRATE_USER.name();
    var authMethod = authentication.resolveAuthenticationType(userName, connectionProperties);
    if (authMethod == null) {
        ReferenceCountUtil.release(msg);
        authError = new AuthenticationException("No valid auth.host_based entry found for: " + remoteAddress);
        Netty4TcpChannel tcpChannel = ctx.channel().attr(Netty4Transport.CHANNEL_KEY).get();
        CloseableChannel.closeChannel(tcpChannel, true);
        throw authError;
    }
    try {
        authMethod.authenticate(userName, null, connectionProperties);
        ctx.pipeline().remove(this);
        super.channelRead(ctx, msg);
    } catch (Exception e) {
        ReferenceCountUtil.release(msg);
        authError = e;
        Netty4TcpChannel tcpChannel = ctx.channel().attr(Netty4Transport.CHANNEL_KEY).get();
        CloseableChannel.closeChannel(tcpChannel, true);
        throw e;
    }
}
Also used : AuthenticationException(javax.security.sasl.AuthenticationException) Channel(io.netty.channel.Channel) CloseableChannel(org.elasticsearch.common.network.CloseableChannel) ConnectionProperties(io.crate.protocols.postgres.ConnectionProperties) InetAddress(java.net.InetAddress) AuthenticationException(javax.security.sasl.AuthenticationException)

Example 5 with ConnectionProperties

use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.

the class ClientCertAuthTest method setUpSsl.

@Before
public void setUpSsl() throws Exception {
    var notBefore = new Date(System.currentTimeMillis() - 86400000L * 365);
    var notAfter = new Date(253402300799000L);
    SelfSignedCertificate ssc = new SelfSignedCertificate("example.com", notBefore, notAfter, "RSA", 2048);
    sslSession = mock(SSLSession.class);
    when(sslSession.getPeerCertificates()).thenReturn(new Certificate[] { ssc.cert() });
    sslConnWithCert = new ConnectionProperties(InetAddresses.forString("127.0.0.1"), Protocol.POSTGRES, sslSession);
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) ConnectionProperties(io.crate.protocols.postgres.ConnectionProperties) SSLSession(javax.net.ssl.SSLSession) Date(java.util.Date) Before(org.junit.Before)

Aggregations

ConnectionProperties (io.crate.protocols.postgres.ConnectionProperties)14 Test (org.junit.Test)10 Settings (org.elasticsearch.common.settings.Settings)5 SSLSession (javax.net.ssl.SSLSession)4 InetAddress (java.net.InetAddress)2 Optional (java.util.Optional)2 AuthenticationWithSSLIntegrationTest.getAbsoluteFilePathFromClassPath (io.crate.auth.AuthenticationWithSSLIntegrationTest.getAbsoluteFilePathFromClassPath)1 User (io.crate.user.User)1 Channel (io.netty.channel.Channel)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 Path (java.nio.file.Path)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 AuthenticationException (javax.security.sasl.AuthenticationException)1 CloseableChannel (org.elasticsearch.common.network.CloseableChannel)1 SecureString (org.elasticsearch.common.settings.SecureString)1 Before (org.junit.Before)1