Search in sources :

Example 1 with CQLStatement

use of org.apache.cassandra.cql3.CQLStatement in project cassandra by apache.

the class CassandraAuthorizer method executeLoggedBatch.

private void executeLoggedBatch(List<CQLStatement> statements) throws RequestExecutionException, RequestValidationException {
    BatchStatement batch = new BatchStatement(0, BatchStatement.Type.LOGGED, Lists.newArrayList(Iterables.filter(statements, ModificationStatement.class)), Attributes.none());
    QueryProcessor.instance.processBatch(batch, QueryState.forInternalCalls(), BatchQueryOptions.withoutPerStatementVariables(QueryOptions.DEFAULT), System.nanoTime());
}
Also used : BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement)

Example 2 with CQLStatement

use of org.apache.cassandra.cql3.CQLStatement in project cassandra by apache.

the class CassandraAuthorizer method revokeAllFrom.

// Called when deleting a role with DROP ROLE query.
// Internal hook, so no permission checks are needed here.
// Executes a logged batch removing the granted premissions
// for the role as well as the entries from the reverse index
// table
public void revokeAllFrom(RoleResource revokee) {
    try {
        UntypedResultSet rows = process(String.format("SELECT resource FROM %s.%s WHERE role = '%s'", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.ROLE_PERMISSIONS, escape(revokee.getRoleName())));
        List<CQLStatement> statements = new ArrayList<>();
        for (UntypedResultSet.Row row : rows) {
            statements.add(QueryProcessor.getStatement(String.format("DELETE FROM %s.%s WHERE resource = '%s' AND role = '%s'", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.RESOURCE_ROLE_INDEX, escape(row.getString("resource")), escape(revokee.getRoleName())), ClientState.forInternalCalls()).statement);
        }
        statements.add(QueryProcessor.getStatement(String.format("DELETE FROM %s.%s WHERE role = '%s'", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.ROLE_PERMISSIONS, escape(revokee.getRoleName())), ClientState.forInternalCalls()).statement);
        executeLoggedBatch(statements);
    } catch (RequestExecutionException | RequestValidationException e) {
        logger.warn("CassandraAuthorizer failed to revoke all permissions of {}: {}", revokee.getRoleName(), e);
    }
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet)

Example 3 with CQLStatement

use of org.apache.cassandra.cql3.CQLStatement in project cassandra by apache.

the class CassandraAuthorizer method revokeAllOn.

// Called after a resource is removed (DROP KEYSPACE, DROP TABLE, etc.).
// Execute a logged batch removing all the permissions for the resource
// as well as the index table entry
public void revokeAllOn(IResource droppedResource) {
    try {
        UntypedResultSet rows = process(String.format("SELECT role FROM %s.%s WHERE resource = '%s'", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.RESOURCE_ROLE_INDEX, escape(droppedResource.getName())));
        List<CQLStatement> statements = new ArrayList<>();
        for (UntypedResultSet.Row row : rows) {
            statements.add(QueryProcessor.getStatement(String.format("DELETE FROM %s.%s WHERE role = '%s' AND resource = '%s'", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.ROLE_PERMISSIONS, escape(row.getString("role")), escape(droppedResource.getName())), ClientState.forInternalCalls()).statement);
        }
        statements.add(QueryProcessor.getStatement(String.format("DELETE FROM %s.%s WHERE resource = '%s'", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.RESOURCE_ROLE_INDEX, escape(droppedResource.getName())), ClientState.forInternalCalls()).statement);
        executeLoggedBatch(statements);
    } catch (RequestExecutionException | RequestValidationException e) {
        logger.warn("CassandraAuthorizer failed to revoke all permissions on {}: {}", droppedResource, e);
        return;
    }
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet)

Example 4 with CQLStatement

use of org.apache.cassandra.cql3.CQLStatement in project cassandra by apache.

the class ExecuteMessage method execute.

public Message.Response execute(QueryState state, long queryStartNanoTime) {
    try {
        QueryHandler handler = ClientState.getCQLQueryHandler();
        ParsedStatement.Prepared prepared = handler.getPrepared(statementId);
        if (prepared == null)
            throw new PreparedQueryNotFoundException(statementId);
        options.prepare(prepared.boundNames);
        CQLStatement statement = prepared.statement;
        if (options.getPageSize() == 0)
            throw new ProtocolException("The page size cannot be 0");
        UUID tracingId = null;
        if (isTracingRequested()) {
            tracingId = UUIDGen.getTimeUUID();
            state.prepareTracingSession(tracingId);
        }
        if (state.traceNextQuery()) {
            state.createTracingSession(getCustomPayload());
            ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
            if (options.getPageSize() > 0)
                builder.put("page_size", Integer.toString(options.getPageSize()));
            if (options.getConsistency() != null)
                builder.put("consistency_level", options.getConsistency().name());
            if (options.getSerialConsistency() != null)
                builder.put("serial_consistency_level", options.getSerialConsistency().name());
            builder.put("query", prepared.rawCQLStatement);
            for (int i = 0; i < prepared.boundNames.size(); i++) {
                ColumnSpecification cs = prepared.boundNames.get(i);
                String boundName = cs.name.toString();
                String boundValue = cs.type.asCQL3Type().toCQLLiteral(options.getValues().get(i), options.getProtocolVersion());
                if (boundValue.length() > 1000) {
                    boundValue = boundValue.substring(0, 1000) + "...'";
                }
                //Here we prefix boundName with the index to avoid possible collission in builder keys due to
                //having multiple boundValues for the same variable
                builder.put("bound_var_" + Integer.toString(i) + "_" + boundName, boundValue);
            }
            Tracing.instance.begin("Execute CQL3 prepared query", state.getClientAddress(), builder.build());
        }
        // Some custom QueryHandlers are interested by the bound names. We provide them this information
        // by wrapping the QueryOptions.
        QueryOptions queryOptions = QueryOptions.addColumnSpecifications(options, prepared.boundNames);
        Message.Response response = handler.processPrepared(statement, state, queryOptions, getCustomPayload(), queryStartNanoTime);
        if (options.skipMetadata() && response instanceof ResultMessage.Rows)
            ((ResultMessage.Rows) response).result.metadata.setSkipMetadata();
        if (tracingId != null)
            response.setTracingId(tracingId);
        return response;
    } catch (Exception e) {
        JVMStabilityInspector.inspectThrowable(e);
        return ErrorMessage.fromException(e);
    } finally {
        Tracing.instance.stopSession();
    }
}
Also used : CQLStatement(org.apache.cassandra.cql3.CQLStatement) QueryHandler(org.apache.cassandra.cql3.QueryHandler) ColumnSpecification(org.apache.cassandra.cql3.ColumnSpecification) ParsedStatement(org.apache.cassandra.cql3.statements.ParsedStatement) PreparedQueryNotFoundException(org.apache.cassandra.exceptions.PreparedQueryNotFoundException) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ImmutableMap(com.google.common.collect.ImmutableMap) PreparedQueryNotFoundException(org.apache.cassandra.exceptions.PreparedQueryNotFoundException) UUID(java.util.UUID)

Example 5 with CQLStatement

use of org.apache.cassandra.cql3.CQLStatement in project cassandra by apache.

the class UFIdentificationTest method assertFunctions.

private void assertFunctions(String cql, String... function) {
    CQLStatement stmt = QueryProcessor.getStatement(cql, ClientState.forInternalCalls()).statement;
    assertFunctions(stmt, function);
}
Also used : CQLStatement(org.apache.cassandra.cql3.CQLStatement)

Aggregations

UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)3 CQLStatement (org.apache.cassandra.cql3.CQLStatement)2 Predicate (com.google.common.base.Predicate)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 UUID (java.util.UUID)1 ColumnSpecification (org.apache.cassandra.cql3.ColumnSpecification)1 QueryHandler (org.apache.cassandra.cql3.QueryHandler)1 QueryOptions (org.apache.cassandra.cql3.QueryOptions)1 BatchStatement (org.apache.cassandra.cql3.statements.BatchStatement)1 ParsedStatement (org.apache.cassandra.cql3.statements.ParsedStatement)1 PreparedQueryNotFoundException (org.apache.cassandra.exceptions.PreparedQueryNotFoundException)1