func (s *SSHCommandSuite) TestCopy(c *gc.C) { var opts ssh.Options opts.EnablePTY() opts.AllowPasswordAuthentication() opts.SetIdentities("x", "y") opts.SetPort(2022) err := s.client.Copy([]string{"/tmp/blah", "[email protected]:baz"}, &opts) c.Assert(err, jc.ErrorIsNil) out, err := ioutil.ReadFile(s.fakescp + ".args") c.Assert(err, jc.ErrorIsNil) // EnablePTY has no effect for Copy c.Assert(string(out), gc.Equals, s.fakescp+" -o StrictHostKeyChecking no -o ServerAliveInterval 30 -i x -i y -P 2022 /tmp/blah [email protected]:baz\n") // Try passing extra args err = s.client.Copy([]string{"/tmp/blah", "[email protected]:baz", "-r", "-v"}, &opts) c.Assert(err, jc.ErrorIsNil) out, err = ioutil.ReadFile(s.fakescp + ".args") c.Assert(err, jc.ErrorIsNil) c.Assert(string(out), gc.Equals, s.fakescp+" -o StrictHostKeyChecking no -o ServerAliveInterval 30 -i x -i y -P 2022 /tmp/blah [email protected]:baz -r -v\n") // Try interspersing extra args err = s.client.Copy([]string{"-r", "/tmp/blah", "-v", "[email protected]:baz"}, &opts) c.Assert(err, jc.ErrorIsNil) out, err = ioutil.ReadFile(s.fakescp + ".args") c.Assert(err, jc.ErrorIsNil) c.Assert(string(out), gc.Equals, s.fakescp+" -o StrictHostKeyChecking no -o ServerAliveInterval 30 -i x -i y -P 2022 -r /tmp/blah -v [email protected]:baz\n") }
func (s *SSHCommandSuite) TestCommandEnablePTY(c *gc.C) { var opts ssh.Options opts.EnablePTY() s.assertCommandArgs(c, s.commandOptions([]string{echoCommand, "123"}, &opts), fmt.Sprintf("%s -o StrictHostKeyChecking no -o PasswordAuthentication no -o ServerAliveInterval 30 -t -t localhost %s 123", s.fakessh, echoCommand), ) }
// getSSHOptions configures and returns SSH options and proxy settings. func (c *SSHCommon) getSSHOptions(enablePty bool) (*ssh.Options, error) { var options ssh.Options // TODO(waigani) do not save fingerprint only until this bug is addressed: // lp:892552. Also see lp:1334481. options.SetKnownHostsFile("/dev/null") if enablePty { options.EnablePTY() } var err error if c.proxy, err = c.proxySSH(); err != nil { return nil, err } else if c.proxy { if err := c.setProxyCommand(&options); err != nil { return nil, err } } return &options, nil }
// getSSHOptions configures SSH options based on command line // arguments and the SSH targets specified. func (c *SSHCommon) getSSHOptions(enablePty bool, targets ...*resolvedTarget) (*ssh.Options, error) { var options ssh.Options if c.noHostKeyChecks { options.SetStrictHostKeyChecking(ssh.StrictHostChecksNo) options.SetKnownHostsFile("/dev/null") } else { knownHostsPath, err := c.generateKnownHosts(targets) if err != nil { return nil, errors.Trace(err) } // There might not be a custom known_hosts file if the SSH // targets are specified using arbitrary hostnames or // addresses. In this case, the user's personal known_hosts // file is used. if knownHostsPath != "" { // When a known_hosts file has been generated, enforce // strict host key checking. options.SetStrictHostKeyChecking(ssh.StrictHostChecksYes) options.SetKnownHostsFile(knownHostsPath) } else { // If the user's personal known_hosts is used, also use // the user's personal StrictHostKeyChecking preferences. options.SetStrictHostKeyChecking(ssh.StrictHostChecksUnset) } } if enablePty { options.EnablePTY() } if c.proxy { if err := c.setProxyCommand(&options); err != nil { return nil, err } } return &options, nil }
// InitUbuntuUser adds the ubuntu user if it doesn't // already exist, updates its ~/.ssh/authorized_keys, // and enables passwordless sudo for it. // // InitUbuntuUser will initially attempt to login as // the ubuntu user, and verify that passwordless sudo // is enabled; only if this is false will there be an // attempt with the specified login. // // authorizedKeys may be empty, in which case the file // will be created and left empty. // // stdin and stdout will be used for remote sudo prompts, // if the ubuntu user must be created/updated. func InitUbuntuUser(host, login, authorizedKeys string, stdin io.Reader, stdout io.Writer) error { logger.Infof("initialising %q, user %q", host, login) // To avoid unnecessary prompting for the specified login, // initUbuntuUser will first attempt to ssh to the machine // as "ubuntu" with password authentication disabled, and // ensure that it can use sudo without a password. // // Note that we explicitly do not allocate a PTY, so we // get a failure if sudo prompts. cmd := ssh.Command("ubuntu@"+host, []string{"sudo", "-n", "true"}, nil) if cmd.Run() == nil { logger.Infof("ubuntu user is already initialised") return nil } // Failed to login as ubuntu (or passwordless sudo is not enabled). // Use specified login, and execute the initUbuntuScript below. if login != "" { host = login + "@" + host } script := fmt.Sprintf(initUbuntuScript, utils.ShQuote(authorizedKeys)) var options ssh.Options options.AllowPasswordAuthentication() options.EnablePTY() cmd = ssh.Command(host, []string{"sudo", "/bin/bash -c " + utils.ShQuote(script)}, &options) var stderr bytes.Buffer cmd.Stdin = stdin cmd.Stdout = stdout // for sudo prompt cmd.Stderr = &stderr if err := cmd.Run(); err != nil { if stderr.Len() != 0 { err = fmt.Errorf("%v (%v)", err, strings.TrimSpace(stderr.String())) } return err } return nil }