Search in sources :

Example 1 with CredentialCollectionException

use of org.apache.knox.gateway.shell.CredentialCollectionException in project knox by apache.

the class WebHDFSCommand method establishSession.

private KnoxSession establishSession(String mountPoint, String url) {
    CredentialCollector dlg;
    try {
        dlg = login();
    } catch (CredentialCollectionException e) {
        e.printStackTrace();
        return null;
    }
    String username = dlg.name();
    String password = new String(dlg.chars());
    KnoxSession session = null;
    try {
        session = KnoxSession.login(url, username, password);
        sessions.put(mountPoint, session);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return session;
}
Also used : CredentialCollector(org.apache.knox.gateway.shell.CredentialCollector) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) URISyntaxException(java.net.URISyntaxException)

Example 2 with CredentialCollectionException

use of org.apache.knox.gateway.shell.CredentialCollectionException in project knox by apache.

the class DataSourceCommand method execute.

@SuppressWarnings({ "unchecked", "PMD.CloseResource" })
@Override
public Object execute(List<String> args) {
    Map<String, KnoxDataSource> dataSources = getDataSources();
    if (args.isEmpty()) {
        args.add("list");
    }
    if (args.get(0).equalsIgnoreCase("add")) {
        KnoxDataSource ds = new KnoxDataSource(args.get(1), args.get(2), args.get(3), args.get(4));
        dataSources.put(ds.getName(), ds);
        getVariables().put(KNOXDATASOURCES, dataSources);
        persistDataSources();
    } else if (args.get(0).equalsIgnoreCase("remove")) {
        if (dataSources == null || dataSources.isEmpty()) {
            return "No datasources to remove.";
        }
        // if the removed datasource is currently selected, unselect it
        dataSources.remove(args.get(1));
        if (getVariables().get(KNOXDATASOURCE) != null) {
            if (args.get(1) != null) {
                if (((String) getVariables().get(KNOXDATASOURCE)).equals(args.get(1))) {
                    System.out.println("unselecting datasource.");
                    getVariables().put(KNOXDATASOURCE, "");
                }
            } else {
                System.out.println("Missing datasource name to remove.");
            }
        }
        getVariables().put(KNOXDATASOURCES, dataSources);
        persistDataSources();
    } else if (args.get(0).equalsIgnoreCase("list")) {
    // valid command no additional work needed though
    } else if (args.get(0).equalsIgnoreCase("select")) {
        if (dataSources == null || dataSources.isEmpty()) {
            return "No datasources to select from.";
        }
        KnoxDataSource dsValue = dataSources.get(args.get(1));
        Connection conn = getConnectionFromSession(dsValue);
        try {
            if (conn == null || conn.isClosed()) {
                String username = null;
                char[] pass = null;
                if (dsValue.getAuthnType().equalsIgnoreCase("basic")) {
                    CredentialCollector dlg;
                    try {
                        dlg = login();
                    } catch (CredentialCollectionException e) {
                        e.printStackTrace();
                        return "Error: Credential collection failure.";
                    }
                    username = dlg.name();
                    pass = dlg.chars();
                }
                try {
                    getConnection(dsValue, username, new String(pass));
                } catch (Exception e) {
                    e.printStackTrace();
                    return "Error: Connection creation failure.";
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (dataSources.containsKey(args.get(1))) {
            getVariables().put(KNOXDATASOURCE, args.get(1));
        }
        KnoxShellTable datasource = new KnoxShellTable();
        datasource.title("Knox DataSource Selected");
        datasource.header("Name").header("Connect String").header("Driver").header("Authn Type");
        datasource.row().value(dsValue.getName()).value(dsValue.getConnectStr()).value(dsValue.getDriver()).value(dsValue.getAuthnType());
        return datasource;
    } else {
        return "ERROR: unknown datasources command.";
    }
    return buildTable();
}
Also used : KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) SQLException(java.sql.SQLException) CredentialCollector(org.apache.knox.gateway.shell.CredentialCollector) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException) Connection(java.sql.Connection) KnoxDataSource(org.apache.knox.gateway.shell.KnoxDataSource) SQLException(java.sql.SQLException) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException)

Example 3 with CredentialCollectionException

use of org.apache.knox.gateway.shell.CredentialCollectionException in project knox by apache.

the class LoginCommand method execute.

@SuppressWarnings("unchecked")
@Override
public Object execute(List<String> args) {
    KnoxSession session = null;
    KnoxLoginDialog dlg = new KnoxLoginDialog();
    try {
        dlg.collect();
        if (dlg.ok) {
            session = KnoxSession.login(args.get(0), dlg.username, new String(dlg.pass));
            getVariables().put("__knoxsession", session);
        }
    } catch (CredentialCollectionException | URISyntaxException e) {
        e.printStackTrace();
    }
    return "Session established for: " + args.get(0);
}
Also used : CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) URISyntaxException(java.net.URISyntaxException)

Example 4 with CredentialCollectionException

use of org.apache.knox.gateway.shell.CredentialCollectionException in project knox by apache.

the class KnoxLine method executeShell.

private void executeShell() {
    String sql;
    System.out.println(" _                    _ _            ");
    System.out.println("| | ___ __   _____  _| (_)_ __   ___ ");
    System.out.println("| |/ / '_ \\ / _ \\ \\/ / | | '_ \\ / _ \\");
    System.out.println("|   <| | | | (_) >  <| | | | | |  __/");
    System.out.println("|_|\\_\\_| |_|\\___/_/\\_\\_|_|_| |_|\\\\__|");
    System.out.println("powered by Apache Knox");
    System.out.println("");
    System.out.println("");
    while (true) {
        sql = System.console().readLine("knoxline> ");
        if (sql != null && !sql.isEmpty()) {
            if (sql.startsWith(":ds") || sql.startsWith(":datasource")) {
                try {
                    processDataSourceCommand(sql);
                } catch (CredentialCollectionException | SQLException e) {
                    e.printStackTrace();
                }
            } else {
                // Configure JDBC connection
                if (datasource != null) {
                    System.out.println(sql);
                    try {
                        establishConnection();
                        try (Statement statement = conn.createStatement()) {
                            if (statement.execute(sql)) {
                                try (ResultSet resultSet = statement.getResultSet()) {
                                    KnoxShellTable table = KnoxShellTable.builder().jdbc().resultSet(resultSet);
                                    System.out.println(table.toString());
                                    System.out.println("\nRows: " + table.getRows().size() + "\n");
                                }
                            }
                        }
                    } catch (SQLException e) {
                        System.out.println("SQL Exception encountered... " + e.getMessage());
                    }
                } else {
                    System.out.println("No datasource selected. Use :ds select {datasource-name}");
                }
            }
        }
    }
}
Also used : KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) SQLException(java.sql.SQLException) Statement(java.sql.Statement) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException) ResultSet(java.sql.ResultSet)

Aggregations

CredentialCollectionException (org.apache.knox.gateway.shell.CredentialCollectionException)4 URISyntaxException (java.net.URISyntaxException)2 SQLException (java.sql.SQLException)2 CredentialCollector (org.apache.knox.gateway.shell.CredentialCollector)2 KnoxSession (org.apache.knox.gateway.shell.KnoxSession)2 KnoxShellTable (org.apache.knox.gateway.shell.table.KnoxShellTable)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 KnoxDataSource (org.apache.knox.gateway.shell.KnoxDataSource)1