Search in sources :

Example 36 with TransactionToken

use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.

the class ConcourseServer method stage.

@Override
@TranslateClientExceptions
@PluginRestricted
@VerifyAccessToken
public TransactionToken stage(AccessToken creds, String env) throws TException {
    TransactionToken token = new TransactionToken(creds, Time.now());
    Transaction transaction = getEngine(env).startTransaction();
    transactions.put(token, transaction);
    Logger.info("Started Transaction {}", transaction);
    return token;
}
Also used : Transaction(com.cinchapi.concourse.server.storage.Transaction) TransactionToken(com.cinchapi.concourse.thrift.TransactionToken) PluginRestricted(com.cinchapi.concourse.server.plugin.PluginRestricted) VerifyAccessToken(com.cinchapi.concourse.server.aop.VerifyAccessToken) TranslateClientExceptions(com.cinchapi.concourse.server.aop.TranslateClientExceptions)

Example 37 with TransactionToken

use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.

the class PluginSerializerTest method randomRemoteMessage.

/**
 * Generate a random {@link RemoteMessage} for testing.
 *
 * @return a RemoteMessage
 */
private RemoteMessage randomRemoteMessage() {
    int seed = Random.getInt();
    RemoteMessage message;
    if (seed % 3 == 0) {
        message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteAttributeExchange", Random.getString(), Random.getString());
    } else if (seed % 2 == 0) {
        AccessToken creds = new AccessToken(ByteBuffer.wrap(Random.getString().getBytes(StandardCharsets.UTF_8)));
        TransactionToken transaction = new TransactionToken(creds, Time.now());
        String method = Random.getSimpleString();
        String environment = Random.getSimpleString();
        int argsCount = Math.abs(Random.getInt()) % 8;
        ComplexTObject[] args = new ComplexTObject[argsCount];
        for (int i = 0; i < argsCount; ++i) {
            args[i] = ComplexTObject.fromJavaObject(Random.getObject());
        }
        message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteMethodRequest", method, creds, transaction, environment, args);
    } else {
        AccessToken creds = new AccessToken(ByteBuffer.wrap(Random.getString().getBytes(StandardCharsets.UTF_8)));
        ComplexTObject response = ComplexTObject.fromJavaObject(Random.getObject());
        message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteMethodResponse", creds, response);
    }
    return message;
}
Also used : RemoteMessage(com.cinchapi.concourse.server.plugin.RemoteMessage) TransactionToken(com.cinchapi.concourse.thrift.TransactionToken) ComplexTObject(com.cinchapi.concourse.thrift.ComplexTObject) AccessToken(com.cinchapi.concourse.thrift.AccessToken)

Example 38 with TransactionToken

use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.

the class AccessTokenVerificationAdvice method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    AccessToken token = null;
    TransactionToken transaction = null;
    Object[] args = invocation.getArguments();
    int index = 0;
    while (token == null && (transaction == null || index < args.length)) {
        Object arg = args[index];
        if (arg instanceof AccessToken) {
            token = (AccessToken) arg;
        } else if (arg instanceof TransactionToken) {
            transaction = (TransactionToken) arg;
        }
        ++index;
    }
    if (token != null) {
        ConcourseServer concourse = (ConcourseServer) invocation.getThis();
        if (concourse.inspector().isValidToken(token)) {
            if (transaction == null || (transaction != null && transaction.getAccessToken().equals(token) && concourse.inspector().isValidTransaction(transaction))) {
                return invocation.proceed();
            } else {
                throw new IllegalArgumentException("Invalid transaction");
            }
        } else {
            throw new SecurityException("Invalid access token");
        }
    } else {
        throw new SecurityException("Unauthorized");
    }
}
Also used : TransactionToken(com.cinchapi.concourse.thrift.TransactionToken) AccessToken(com.cinchapi.concourse.thrift.AccessToken) SecurityException(com.cinchapi.concourse.thrift.SecurityException) ConcourseServer(com.cinchapi.concourse.server.ConcourseServer)

Example 39 with TransactionToken

use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.

the class HttpServer method initialize.

/**
 * Initialize a {@link EndpointContainer container} by registering all of
 * its
 * endpoints.
 *
 * @param container the {@link EndpointContainer} to initialize
 */
private static void initialize(EndpointContainer container) {
    for (final Endpoint endpoint : container.endpoints()) {
        String action = endpoint.getAction();
        Route route = new Route(endpoint.getPath()) {

            @Override
            public Object handle(Request request, Response response) {
                response.type(endpoint.getContentType().toString());
                // The HttpRequests preprocessor assigns attributes to the
                // request in order for the Endpoint to make calls into
                // ConcourseServer.
                AccessToken creds = (AccessToken) request.attribute(GlobalState.HTTP_ACCESS_TOKEN_ATTRIBUTE);
                String environment = MoreObjects.firstNonNull((String) request.attribute(GlobalState.HTTP_ENVIRONMENT_ATTRIBUTE), GlobalState.DEFAULT_ENVIRONMENT);
                String fingerprint = (String) request.attribute(GlobalState.HTTP_FINGERPRINT_ATTRIBUTE);
                // does the fingerprint match?
                if ((boolean) request.attribute(GlobalState.HTTP_REQUIRE_AUTH_ATTRIBUTE) && creds == null) {
                    halt(401);
                }
                if (!Strings.isNullOrEmpty(fingerprint) && !fingerprint.equals(HttpRequests.getFingerprint(request))) {
                    Logger.warn("Request made with mismatching fingerprint. Expecting {} but got {}", HttpRequests.getFingerprint(request), fingerprint);
                    halt(401);
                }
                TransactionToken transaction = null;
                try {
                    Long timestamp = Longs.tryParse((String) request.attribute(GlobalState.HTTP_TRANSACTION_TOKEN_ATTRIBUTE));
                    transaction = creds != null && timestamp != null ? new TransactionToken(creds, timestamp) : transaction;
                } catch (NullPointerException e) {
                }
                try {
                    return endpoint.serve(request, response, creds, transaction, environment);
                } catch (Exception e) {
                    if (e instanceof HttpError) {
                        response.status(((HttpError) e).getCode());
                    } else if (e instanceof SecurityException || e instanceof java.lang.SecurityException) {
                        response.removeCookie(GlobalState.HTTP_AUTH_TOKEN_COOKIE);
                        response.status(401);
                    } else if (e instanceof IllegalArgumentException) {
                        response.status(400);
                    } else {
                        response.status(500);
                        Logger.error("", e);
                    }
                    JsonObject json = new JsonObject();
                    json.addProperty("error", e.getMessage());
                    return json.toString();
                }
            }
        };
        if (action.equals("get")) {
            Spark.get(route);
        } else if (action.equals("post")) {
            Spark.post(route);
        } else if (action.equals("put")) {
            Spark.put(route);
        } else if (action.equals("delete")) {
            Spark.delete(route);
        } else if (action.equals("upsert")) {
            Spark.post(route);
            Spark.put(route);
        } else if (action.equals("options")) {
            Spark.options(route);
        }
    }
}
Also used : TransactionToken(com.cinchapi.concourse.thrift.TransactionToken) Request(spark.Request) JsonObject(com.google.gson.JsonObject) Response(spark.Response) AccessToken(com.cinchapi.concourse.thrift.AccessToken) HttpError(com.cinchapi.concourse.server.http.errors.HttpError) Route(spark.Route)

Example 40 with TransactionToken

use of com.cinchapi.concourse.thrift.TransactionToken in project concourse by cinchapi.

the class ConcourseThriftDriver method abort.

@Override
public void abort() {
    execute(() -> {
        if (transaction != null) {
            final TransactionToken token = transaction;
            transaction = null;
            core.abort(creds, token, environment);
        }
        return null;
    });
}
Also used : TransactionToken(com.cinchapi.concourse.thrift.TransactionToken)

Aggregations

TransactionToken (com.cinchapi.concourse.thrift.TransactionToken)62 AccessToken (com.cinchapi.concourse.thrift.AccessToken)59 VerifyAccessToken (com.cinchapi.concourse.server.aop.VerifyAccessToken)56 Permission (com.cinchapi.concourse.security.Permission)55 Role (com.cinchapi.concourse.security.Role)55 Internal (com.cinchapi.concourse.server.aop.Internal)55 TranslateClientExceptions (com.cinchapi.concourse.server.aop.TranslateClientExceptions)55 VerifyReadPermission (com.cinchapi.concourse.server.aop.VerifyReadPermission)55 VerifyWritePermission (com.cinchapi.concourse.server.aop.VerifyWritePermission)55 ConcourseManagementService (com.cinchapi.concourse.server.management.ConcourseManagementService)55 PluginManager (com.cinchapi.concourse.server.plugin.PluginManager)55 PluginRestricted (com.cinchapi.concourse.server.plugin.PluginRestricted)55 AbstractSyntaxTree (com.cinchapi.ccl.syntax.AbstractSyntaxTree)54 NaturalLanguage (com.cinchapi.ccl.util.NaturalLanguage)54 AnyStrings (com.cinchapi.common.base.AnyStrings)54 Array (com.cinchapi.common.base.Array)54 CheckedExceptions (com.cinchapi.common.base.CheckedExceptions)54 Reflection (com.cinchapi.common.reflect.Reflection)54 Constants (com.cinchapi.concourse.Constants)54 Link (com.cinchapi.concourse.Link)54