use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.
the class ClientCertAuthTest method testHttpClientCertAuthFailsOnUserMissMatchWithCN.
@Test
public void testHttpClientCertAuthFailsOnUserMissMatchWithCN() throws Exception {
ClientCertAuth clientCertAuth = new ClientCertAuth(userName -> exampleUser);
ConnectionProperties conn = new ConnectionProperties(InetAddresses.forString("127.0.0.1"), Protocol.HTTP, sslSession);
expectedException.expectMessage("Common name \"example.com\" in client certificate doesn't match username \"arthur_is_wrong\"");
clientCertAuth.authenticate("arthur_is_wrong", null, conn);
}
use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.
the class HttpAuthUpstreamHandler method handleHttpRequest.
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest request) {
SSLSession session = getSession(ctx.channel());
Tuple<String, SecureString> credentials = credentialsFromRequest(request, session, settings);
String username = credentials.v1();
SecureString password = credentials.v2();
if (username.equals(authorizedUser)) {
ctx.fireChannelRead(request);
return;
}
InetAddress address = addressFromRequestOrChannel(request, ctx.channel());
ConnectionProperties connectionProperties = new ConnectionProperties(address, Protocol.HTTP, session);
AuthenticationMethod authMethod = authService.resolveAuthenticationType(username, connectionProperties);
if (authMethod == null) {
String errorMessage = String.format(Locale.ENGLISH, "No valid auth.host_based.config entry found for host \"%s\", user \"%s\", protocol \"%s\". Did you enable TLS in your client?", address.getHostAddress(), username, Protocol.HTTP.toString());
sendUnauthorized(ctx.channel(), errorMessage);
} else {
try {
User user = authMethod.authenticate(username, password, connectionProperties);
if (user != null && LOGGER.isTraceEnabled()) {
LOGGER.trace("Authentication succeeded user \"{}\" and method \"{}\".", username, authMethod.name());
}
authorizedUser = username;
ctx.fireChannelRead(request);
} catch (Exception e) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("{} authentication failed for user={} from connection={}", authMethod.name(), username, connectionProperties.address());
}
sendUnauthorized(ctx.channel(), e.getMessage());
}
}
}
use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.
the class HostBasedAuthenticationTest method testHttpSSLOption.
@Test
public void testHttpSSLOption() throws Exception {
Settings baseConfig = Settings.builder().put(HBA_1).put("auth.host_based.config.1.protocol", "http").build();
SSLSession sslSession = mock(SSLSession.class);
when(sslSession.getPeerCertificates()).thenReturn(new Certificate[0]);
ConnectionProperties sslConnProperties = new ConnectionProperties(LOCALHOST, Protocol.HTTP, sslSession);
ConnectionProperties noSslConnProperties = new ConnectionProperties(LOCALHOST, Protocol.HTTP, null);
Settings sslConfig;
HostBasedAuthentication authService;
sslConfig = Settings.builder().put(baseConfig).put("auth.host_based.config.1." + HostBasedAuthentication.SSL.KEY, HostBasedAuthentication.SSL.OPTIONAL.VALUE).build();
authService = new HostBasedAuthentication(sslConfig, null, SystemDefaultDnsResolver.INSTANCE);
assertThat(authService.getEntry("crate", noSslConnProperties), not(Optional.empty()));
assertThat(authService.getEntry("crate", sslConnProperties), not(Optional.empty()));
sslConfig = Settings.builder().put(baseConfig).put("auth.host_based.config.1." + HostBasedAuthentication.SSL.KEY, HostBasedAuthentication.SSL.REQUIRED.VALUE).build();
authService = new HostBasedAuthentication(sslConfig, null, SystemDefaultDnsResolver.INSTANCE);
assertThat(authService.getEntry("crate", noSslConnProperties), is(Optional.empty()));
assertThat(authService.getEntry("crate", sslConnProperties), not(Optional.empty()));
sslConfig = Settings.builder().put(baseConfig).put("auth.host_based.config.1." + HostBasedAuthentication.SSL.KEY, HostBasedAuthentication.SSL.NEVER.VALUE).build();
authService = new HostBasedAuthentication(sslConfig, null, SystemDefaultDnsResolver.INSTANCE);
assertThat(authService.getEntry("crate", noSslConnProperties), not(Optional.empty()));
assertThat(authService.getEntry("crate", sslConnProperties), is(Optional.empty()));
}
use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.
the class HostBasedAuthenticationTest method testKeyOrderIsRespectedInHbaConfig.
public void testKeyOrderIsRespectedInHbaConfig() {
Settings first = Settings.builder().put("auth.host_based.config.1.method", "trust").put("auth.host_based.config.1.protocol", "pg").build();
Settings second = Settings.builder().put("auth.host_based.config.2.method", "cert").put("auth.host_based.config.2.protocol", "http").build();
// add in reverse order to test natural order of keys in config
Settings settings = Settings.builder().put(second).put(first).build();
HostBasedAuthentication hba = new HostBasedAuthentication(settings, null, SystemDefaultDnsResolver.INSTANCE);
AuthenticationMethod authMethod = hba.resolveAuthenticationType("crate", new ConnectionProperties(InetAddresses.forString("1.2.3.4"), Protocol.POSTGRES, null));
assertThat(authMethod, instanceOf(TrustAuthenticationMethod.class));
AuthenticationMethod authMethod2 = hba.resolveAuthenticationType("crate", new ConnectionProperties(InetAddresses.forString("1.2.3.4"), Protocol.HTTP, null));
assertThat(authMethod2, instanceOf(ClientCertAuth.class));
}
use of io.crate.protocols.postgres.ConnectionProperties in project crate by crate.
the class HostBasedAuthenticationTest method testResolveAuthMethod.
@Test
public void testResolveAuthMethod() {
HostBasedAuthentication authService = new HostBasedAuthentication(HBA_1, null, SystemDefaultDnsResolver.INSTANCE);
AuthenticationMethod method = authService.resolveAuthenticationType("crate", new ConnectionProperties(LOCALHOST, Protocol.POSTGRES, null));
assertThat(method, instanceOf(TrustAuthenticationMethod.class));
}
Aggregations