// Returns every permission on the resource granted to the user.
  public Set<Permission> authorize(AuthenticatedUser user, IResource resource) {
    if (user.isSuper()) return Permission.ALL;

    UntypedResultSet result;
    try {
      ResultMessage.Rows rows =
          authorizeStatement.execute(
              QueryState.forInternalCalls(),
              new QueryOptions(
                  ConsistencyLevel.ONE,
                  Lists.newArrayList(
                      ByteBufferUtil.bytes(user.getName()),
                      ByteBufferUtil.bytes(resource.getName()))));
      result = UntypedResultSet.create(rows.result);
    } catch (RequestValidationException e) {
      throw new AssertionError(e); // not supposed to happen
    } catch (RequestExecutionException e) {
      logger.warn("CassandraAuthorizer failed to authorize {} for {}", user, resource);
      return Permission.NONE;
    }

    if (result.isEmpty() || !result.one().has(PERMISSIONS)) return Permission.NONE;

    Set<Permission> permissions = EnumSet.noneOf(Permission.class);
    for (String perm : result.one().getSet(PERMISSIONS, UTF8Type.instance))
      permissions.add(Permission.valueOf(perm));
    return permissions;
  }
Esempio n. 2
0
  public Message.Response execute(QueryState state) {
    try {
      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();

        ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
        builder.put("query", query);
        if (options.getPageSize() > 0)
          builder.put("page_size", Integer.toString(options.getPageSize()));

        Tracing.instance.begin("Execute CQL3 query", builder.build());
      }

      Message.Response response =
          state.getClientState().getCQLQueryHandler().process(query, state, options);
      if (options.skipMetadata() && response instanceof ResultMessage.Rows)
        ((ResultMessage.Rows) response).result.metadata.setSkipMetadata();

      if (tracingId != null) response.setTracingId(tracingId);

      return response;
    } catch (Exception e) {
      if (!((e instanceof RequestValidationException) || (e instanceof RequestExecutionException)))
        logger.error("Unexpected error during query", e);
      return ErrorMessage.fromException(e);
    } finally {
      Tracing.instance.stopSession();
    }
  }
  public Message.Response execute(QueryState state) {
    try {
      UUID tracingId = null;
      if (isTracingRequested()) {
        tracingId = UUIDGen.getTimeUUID();
        state.prepareTracingSession(tracingId);
      }

      if (state.traceNextQuery()) {
        state.createTracingSession();
        Tracing.instance().begin("Preparing CQL3 query", ImmutableMap.of("query", query));
      }

      Message.Response response = QueryProcessor.prepare(query, state.getClientState(), false);

      if (tracingId != null) response.setTracingId(tracingId);

      return response;
    } catch (Exception e) {
      return ErrorMessage.fromException(e);
    } finally {
      Tracing.instance().stopSession();
    }
  }