Search in sources :

Example 1 with SecurityException

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

the class ConcourseShell method main.

/**
 * Run the program...
 *
 * @param args
 * @throws Exception
 */
public static void main(String... args) throws Exception {
    try {
        ConcourseShell cash = new ConcourseShell();
        Options opts = new Options();
        JCommander parser = null;
        try {
            parser = new JCommander(opts, args);
        } catch (Exception e) {
            die(e.getMessage());
        }
        parser.setProgramName("concourse-shell");
        if (opts.help) {
            parser.usage();
            System.exit(1);
        }
        if (!Strings.isNullOrEmpty(opts.prefs)) {
            opts.prefs = FileOps.expandPath(opts.prefs, System.getProperty("user.dir.real"));
            ConcourseClientPreferences prefs = ConcourseClientPreferences.from(Paths.get(opts.prefs));
            opts.username = prefs.getUsername();
            opts.password = new String(prefs.getPasswordExplicit());
            opts.host = prefs.getHost();
            opts.port = prefs.getPort();
            opts.environment = prefs.getEnvironment();
        }
        if (Strings.isNullOrEmpty(opts.password)) {
            cash.setExpandEvents(false);
            opts.password = cash.console.readLine("Password [" + opts.username + "]: ", '*');
        }
        try {
            cash.concourse = Concourse.connect(opts.host, opts.port, opts.username, opts.password, opts.environment);
            cash.whoami = opts.username;
        } catch (Exception e) {
            if (e.getCause() instanceof TTransportException) {
                die("Unable to connect to the Concourse Server at " + opts.host + ":" + opts.port);
            } else if (e.getCause() instanceof SecurityException) {
                die("Invalid username/password combination.");
            } else {
                die(e.getMessage());
            }
        }
        if (!opts.ignoreRunCommands) {
            cash.loadExternalScript(opts.ext);
        }
        if (!Strings.isNullOrEmpty(opts.run)) {
            try {
                String result = cash.evaluate(opts.run);
                System.out.println(result);
                cash.concourse.exit();
                System.exit(0);
            } catch (IrregularEvaluationResult e) {
                die(e.getMessage());
            }
        } else {
            try {
                cash.enableInteractiveSettings();
            } catch (PermissionException e) {
                die(e.getMessage());
            }
            boolean running = true;
            String input = "";
            while (running) {
                boolean extraLineBreak = true;
                boolean clearInput = true;
                boolean clearPrompt = false;
                try {
                    input = input + cash.console.readLine().trim();
                    String result = cash.evaluate(input);
                    System.out.println(result);
                } catch (UserInterruptException e) {
                    if (Strings.isNullOrEmpty(e.getPartialLine()) && Strings.isNullOrEmpty(input)) {
                        cash.console.println("Type EXIT to quit.");
                    }
                } catch (HelpRequest e) {
                    String text = getHelpText(e.topic);
                    if (!Strings.isNullOrEmpty(text)) {
                        Process p = Runtime.getRuntime().exec(new String[] { "sh", "-c", "echo \"" + text + "\" | less > /dev/tty" });
                        p.waitFor();
                    }
                    cash.console.getHistory().removeLast();
                } catch (ExitRequest e) {
                    running = false;
                    cash.console.getHistory().removeLast();
                } catch (NewLineRequest e) {
                    extraLineBreak = false;
                } catch (ProgramCrash e) {
                    die(e.getMessage());
                } catch (MultiLineRequest e) {
                    extraLineBreak = false;
                    clearInput = false;
                    clearPrompt = true;
                } catch (IrregularEvaluationResult e) {
                    System.err.println(e.getMessage());
                } finally {
                    if (extraLineBreak) {
                        cash.console.print("\n");
                    }
                    if (clearInput) {
                        input = "";
                    }
                    if (clearPrompt) {
                        cash.console.setPrompt("> ");
                    } else {
                        cash.console.setPrompt(cash.defaultPrompt);
                    }
                }
            }
            cash.concourse.exit();
            System.exit(0);
        }
    } finally {
        TerminalFactory.get().restore();
    }
}
Also used : PermissionException(com.cinchapi.concourse.PermissionException) TTransportException(org.apache.thrift.transport.TTransportException) SecurityException(com.cinchapi.concourse.thrift.SecurityException) UserInterruptException(jline.console.UserInterruptException) TTransportException(org.apache.thrift.transport.TTransportException) UserInterruptException(jline.console.UserInterruptException) SecurityException(com.cinchapi.concourse.thrift.SecurityException) MissingMethodException(groovy.lang.MissingMethodException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ParseException(com.cinchapi.concourse.thrift.ParseException) TApplicationException(org.apache.thrift.TApplicationException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) IOException(java.io.IOException) PermissionException(com.cinchapi.concourse.PermissionException) JCommander(com.beust.jcommander.JCommander) ConcourseClientPreferences(com.cinchapi.concourse.config.ConcourseClientPreferences)

Example 2 with SecurityException

use of com.cinchapi.concourse.thrift.SecurityException 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)

Aggregations

SecurityException (com.cinchapi.concourse.thrift.SecurityException)2 JCommander (com.beust.jcommander.JCommander)1 PermissionException (com.cinchapi.concourse.PermissionException)1 ConcourseClientPreferences (com.cinchapi.concourse.config.ConcourseClientPreferences)1 ConcourseServer (com.cinchapi.concourse.server.ConcourseServer)1 AccessToken (com.cinchapi.concourse.thrift.AccessToken)1 ParseException (com.cinchapi.concourse.thrift.ParseException)1 TransactionToken (com.cinchapi.concourse.thrift.TransactionToken)1 MissingMethodException (groovy.lang.MissingMethodException)1 IOException (java.io.IOException)1 UserInterruptException (jline.console.UserInterruptException)1 TApplicationException (org.apache.thrift.TApplicationException)1 TTransportException (org.apache.thrift.transport.TTransportException)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)1