Search in sources :

Example 1 with PermissionException

use of com.cinchapi.concourse.PermissionException 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 PermissionException

use of com.cinchapi.concourse.PermissionException in project concourse by cinchapi.

the class PermissionTest method testPermissionChangeTakesImmediateEffect.

@Test
public void testPermissionChangeTakesImmediateEffect() {
    createUser("jeff", "jeff", "user");
    grant("jeff", "write", "production");
    Concourse client2 = Concourse.connect(SERVER_HOST, SERVER_PORT, "jeff", "jeff", "production");
    long record = client2.add("name", "jeff");
    grant("jeff", "read", "production");
    try {
        client2.add("name", "jeff");
        Assert.fail();
    } catch (PermissionException e) {
        Assert.assertEquals(ImmutableSet.of(record), client2.inventory());
    }
}
Also used : PermissionException(com.cinchapi.concourse.PermissionException) Concourse(com.cinchapi.concourse.Concourse) ConcourseIntegrationTest(com.cinchapi.concourse.test.ConcourseIntegrationTest) Test(org.junit.Test)

Example 3 with PermissionException

use of com.cinchapi.concourse.PermissionException in project concourse by cinchapi.

the class PermissionTest method testDriverIsUsableAfterPermissionException.

@Test
public void testDriverIsUsableAfterPermissionException() {
    createUser("jeff", "jeff", "user");
    Concourse client2 = Concourse.connect(SERVER_HOST, SERVER_PORT, "jeff", "jeff");
    grant("jeff", "read", "");
    long record = client.add("foo", "foo");
    try {
        client2.set("foo", "bar", record);
        Assert.fail();
    } catch (PermissionException e) {
        Assert.assertEquals("foo", client2.get("foo", record));
    }
}
Also used : PermissionException(com.cinchapi.concourse.PermissionException) Concourse(com.cinchapi.concourse.Concourse) ConcourseIntegrationTest(com.cinchapi.concourse.test.ConcourseIntegrationTest) Test(org.junit.Test)

Aggregations

PermissionException (com.cinchapi.concourse.PermissionException)3 Concourse (com.cinchapi.concourse.Concourse)2 ConcourseIntegrationTest (com.cinchapi.concourse.test.ConcourseIntegrationTest)2 Test (org.junit.Test)2 JCommander (com.beust.jcommander.JCommander)1 ConcourseClientPreferences (com.cinchapi.concourse.config.ConcourseClientPreferences)1 ParseException (com.cinchapi.concourse.thrift.ParseException)1 SecurityException (com.cinchapi.concourse.thrift.SecurityException)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