Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2014 Red Hat, Inc. Red Hat licenses this file to you under the Apache License, version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package io.fabric8.commands;
 
 import java.util.List;
 
 import  org.apache.felix.gogo.commands.Argument;
 import  org.apache.felix.gogo.commands.Command;
 import  org.apache.felix.gogo.commands.Option;
 import  org.apache.felix.service.command.CommandSession;
 import  org.apache.karaf.shell.console.AbstractAction;
 import  org.apache.karaf.shell.console.jline.Console;
 import  org.apache.karaf.shell.ssh.KarafAgentFactory;
 import  org.apache.sshd.ClientChannel;
 import  org.apache.sshd.ClientSession;
 import  org.apache.sshd.SshClient;
 import  org.apache.sshd.agent.SshAgent;
 import  org.apache.sshd.client.channel.ChannelShell;
 import  org.apache.sshd.client.future.ConnectFuture;
 import  org.apache.sshd.common.util.NoCloseInputStream;
 import  org.apache.sshd.common.util.NoCloseOutputStream;
 
 import static io.fabric8.utils.FabricValidations.validateContainerName;
 
 @Command(name = "container-connect", scope = "fabric", description = "Connect to a remote container")
 public class ContainerConnectAction extends AbstractAction {
 
     @Option(name = "-u", aliases = {"--username"}, description = "Remote user name", required = false, multiValued = false)
     private String username;
 
     @Option(name = "-p", aliases = {"--password"}, description = "Remote user password", required = false, multiValued = false)
     private String password;
 
     @Argument(index = 0, name = "container", description = "The container name", required = true, multiValued = false)
     private String container = null;
 
     @Argument(index = 1, name = "command", description = "Optional command to execute", required = false, multiValued = true)
     private List<Stringcommand;
 
     private final FabricService fabricService;
     private final KarafAgentFactory agentFactory;
     private ClientSession sshSession;
 
     ContainerConnectAction(FabricService fabricService, KarafAgentFactory agentFactory) {
         this. = fabricService;
         this. = agentFactory;
     }
 
     protected Object doExecute() throws Exception {
         String cmdStr = "";
         if ( != null) {
             StringBuilder sb = new StringBuilder();
             for (String cmd : ) {
                 if (sb.length() > 0) {
                     sb.append(' ');
                 }
                 sb.append(cmd);
             }
             sb.append("\n");
             cmdStr = sb.toString();
         }
 
         Container found = FabricCommand.getContainer();
         String sshUrl = found.getSshUrl();
         if (sshUrl == null) {
             throw new IllegalArgumentException("Container " +  + " has no SSH URL.");
         }
         String[] ssh = sshUrl.split(":");
         if (ssh.length < 2) {
             throw new IllegalArgumentException("Container " +  + " has an invalid SSH URL '" + sshUrl + "'");
         }
 
          =  != null && !.isEmpty() ?  : ShellUtils.retrieveFabricUser(session);
          =  != null ?  : ShellUtils.retrieveFabricUserPassword(session);
 
         try {
             executSshCommand(session, ssh[0], ssh[1], cmdStr);
         } catch (FabricAuthenticationException ex) {
             = null;
             = null;
            promptForSshCredentialsIfNeeded();
            executSshCommand(session, ssh[0], ssh[1], cmdStr);
        }
        return null;
    }

    
Executes the ssh command.
    private void executSshCommand(CommandSession sessionString usernameString passwordString hostnameString portString cmdthrows Exception {
        // Create the client from prototype
        SshClient client = createClient();
        String agentSocket;
        if (this..get(SshAgent.SSH_AUTHSOCKET_ENV_NAME) != null) {
            agentSocket = this..get(SshAgent.SSH_AUTHSOCKET_ENV_NAME).toString();
            client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME,agentSocket);
        }
        try {
            ConnectFuture future = client.connect(hostname, Integer.parseInt(port));
            future.await();
             = future.getSession();
            Object oldIgnoreInterrupts = this..get(Console.IGNORE_INTERRUPTS);
            this..put( Console.IGNORE_INTERRUPTS, . );
            try {
                ..println("Connected");
                boolean authed = false;
                
if (agentSocket != null) { sshSession.authAgent(username); int ret = sshSession.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0); if ((ret & ClientSession.AUTHED) == 0) { System.err.println("Agent authentication failed, falling back to password authentication."); } else { authed = true; }
                if (!authed) {
                    if (username == null) {
                        throw new FabricAuthenticationException("No username specified.");
                    }
                    log.debug("Prompting user for password");
                    String pwd = password != null ? password : ShellUtils.readLine(session"Password: "true);
                    .authPassword(usernamepwd);
                    int ret = .waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
                    if ((ret & ClientSession.AUTHED) == 0) {
                        ..println("Password authentication failed");
                    } else {
                        authed = true;
                    }
                }
                if (!authed) {
                    throw new FabricAuthenticationException("Failed to authenticate.");
                }
                //If user is authenticated credentials to session for future use.
                ShellUtils.storeFabricCredentials(sessionusernamepassword);
                ClientChannel channel;
                if (cmd != null && cmd.length() > 0) {
                    channel = .createChannel("exec"cmd);
                    channel.setIn(new ByteArrayInputStream(new byte[0]));
                } else {
                    channel = .createChannel("shell");
                    channel.setIn(new NoCloseInputStream(.));
                    ((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session));
                    ((ChannelShell) channel).setupSensibleDefaultPty();
                    ((ChannelShell) channel).setAgentForwarding(true);
                }
                channel.setOut(new NoCloseOutputStream(.));
                channel.setErr(new NoCloseOutputStream(.));
                channel.open();
                channel.waitFor(ClientChannel.CLOSED, 0);
            } finally {
                session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
                .close(false);
            }
        } finally {
            client.stop();
        }
    }
    protected SshClient createClient() {
        // must create a new client for each usage
        SshClient client = SshClient.setUpDefaultClient();
        client.setAgentFactory();
         // must start client
        client.start();
        log.trace("Created SshClient: {}"client);
        return client;
    }

    
Prompts the user for username & password.
    private void promptForSshCredentialsIfNeeded() throws IOException {
        // If the username was not configured via cli, then prompt the user for the values
        if ( == null || .isEmpty()) {
            log.debug("Prompting user for ssh login");
             = ShellUtils.readLine(session, "SSH Login for " +  + ": "false);
        }
        if ( == null) {
             = ShellUtils.readLine(session, "SSH Password for " +  + "@" +  + ": "true);
        }
    }
New to GrepCode? Check out our FAQ X