Search in sources :

Example 1 with AccessControl

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

the class PostgresWireProtocol method handleSingleQuery.

private CompletableFuture<?> handleSingleQuery(Statement statement, DelayableWriteChannel channel) {
    CompletableFuture<?> result = new CompletableFuture<>();
    String query;
    try {
        query = SqlFormatter.formatSql(statement);
    } catch (Exception e) {
        query = statement.toString();
    }
    AccessControl accessControl = getAccessControl.apply(session.sessionContext());
    try {
        session.analyze("", statement, Collections.emptyList(), query);
        session.bind("", "", Collections.emptyList(), null);
        DescribeResult describeResult = session.describe('P', "");
        List<Symbol> fields = describeResult.getFields();
        CompletableFuture<?> execute;
        if (fields == null) {
            RowCountReceiver rowCountReceiver = new RowCountReceiver(query, channel.bypassDelay(), accessControl);
            execute = session.execute("", 0, rowCountReceiver);
        } else {
            Messages.sendRowDescription(channel, fields, null, describeResult.relation());
            ResultSetReceiver resultSetReceiver = new ResultSetReceiver(query, channel.bypassDelay(), TransactionState.IDLE, accessControl, Lists2.map(fields, x -> PGTypes.get(x.valueType())), null);
            execute = session.execute("", 0, resultSetReceiver);
        }
        if (execute != null) {
            channel.delayWritesUntil(execute);
        }
        return session.sync();
    } catch (Throwable t) {
        Messages.sendErrorResponse(channel, accessControl, t);
        result.completeExceptionally(t);
        return result;
    }
}
Also used : SessionContext(io.crate.action.sql.SessionContext) PGTypes(io.crate.protocols.postgres.types.PGTypes) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) STARTUP_HEADER(io.crate.protocols.postgres.PostgresWireProtocol.State.STARTUP_HEADER) Locale(java.util.Locale) SQLOperations(io.crate.action.sql.SQLOperations) User(io.crate.user.User) Collection(java.util.Collection) SqlFormatter(io.crate.sql.SqlFormatter) ChannelPipeline(io.netty.channel.ChannelPipeline) Lists2(io.crate.common.collections.Lists2) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) FormatCodes.getFormatCode(io.crate.protocols.postgres.FormatCodes.getFormatCode) Session(io.crate.action.sql.Session) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) PRE_STARTUP(io.crate.protocols.postgres.PostgresWireProtocol.State.PRE_STARTUP) Symbol(io.crate.expression.symbol.Symbol) Statement(io.crate.sql.tree.Statement) DescribeResult(io.crate.action.sql.DescribeResult) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) AccessControl(io.crate.auth.AccessControl) PGType(io.crate.protocols.postgres.types.PGType) Netty4HttpServerTransport(org.elasticsearch.http.netty4.Netty4HttpServerTransport) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SocketException(java.net.SocketException) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) BiConsumer(java.util.function.BiConsumer) SqlParser(io.crate.sql.parser.SqlParser) Authentication(io.crate.auth.Authentication) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder) Nullable(javax.annotation.Nullable) Properties(java.util.Properties) AuthenticationMethod(io.crate.auth.AuthenticationMethod) DataType(io.crate.types.DataType) SSL.getSession(io.crate.protocols.SSL.getSession) Channel(io.netty.channel.Channel) Protocol(io.crate.auth.Protocol) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ResultReceiver(io.crate.action.sql.ResultReceiver) CompletableFuture(java.util.concurrent.CompletableFuture) DescribeResult(io.crate.action.sql.DescribeResult) Symbol(io.crate.expression.symbol.Symbol) SocketException(java.net.SocketException) AccessControl(io.crate.auth.AccessControl)

Aggregations

DescribeResult (io.crate.action.sql.DescribeResult)1 ResultReceiver (io.crate.action.sql.ResultReceiver)1 SQLOperations (io.crate.action.sql.SQLOperations)1 Session (io.crate.action.sql.Session)1 SessionContext (io.crate.action.sql.SessionContext)1 AccessControl (io.crate.auth.AccessControl)1 Authentication (io.crate.auth.Authentication)1 AuthenticationMethod (io.crate.auth.AuthenticationMethod)1 Protocol (io.crate.auth.Protocol)1 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)1 Lists2 (io.crate.common.collections.Lists2)1 Symbol (io.crate.expression.symbol.Symbol)1 SSL.getSession (io.crate.protocols.SSL.getSession)1 FormatCodes.getFormatCode (io.crate.protocols.postgres.FormatCodes.getFormatCode)1 PRE_STARTUP (io.crate.protocols.postgres.PostgresWireProtocol.State.PRE_STARTUP)1 STARTUP_HEADER (io.crate.protocols.postgres.PostgresWireProtocol.State.STARTUP_HEADER)1 PGType (io.crate.protocols.postgres.types.PGType)1 PGTypes (io.crate.protocols.postgres.types.PGTypes)1 SslContextProvider (io.crate.protocols.ssl.SslContextProvider)1 SqlFormatter (io.crate.sql.SqlFormatter)1