Skip to content

Commit

Permalink
Fix SSH access for environments with auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Nov 30, 2016
1 parent 33a0b3c commit 7f78a4d
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func hostSSH(ctx *cli.Context) error {
return err
}

config, err := lookupConfig(ctx)
if err != nil {
return err
}

hostname := ""
args := ctx.Args()

Expand All @@ -53,36 +58,26 @@ func hostSSH(ctx *cli.Context) error {
return fmt.Errorf("Failed to find hostname in %v", args)
}

host, err := Lookup(c, hostname, "host")
hostResource, err := Lookup(c, hostname, "host")
if err != nil {
return err
}

var physicalHost client.PhysicalHost
err = c.GetLink(*host, "physicalHost", &physicalHost)
host, err := c.Host.ById(hostResource.Id)
if err != nil {
return err
}

if physicalHost.Type != "machine" {
return fmt.Errorf("Can only SSH to docker-machine created hosts. No custom hosts")
}

key, err := getSSHKey(hostname, physicalHost)
key, err := getSSHKey(hostname, *host, config.AccessKey, config.SecretKey)
if err != nil {
return err
}

ips := client.IpAddressCollection{}
if err := c.GetLink(*host, "ipAddresses", &ips); err != nil {
return err
}

if len(ips.Data) == 0 {
if host.AgentIpAddress == "" {
return fmt.Errorf("Failed to find IP for %s", hostname)
}

return processExitCode(callSSH(key, ips.Data[0].Address, ctx.Args()))
return processExitCode(callSSH(key, host.AgentIpAddress, ctx.Args()))
}

func callSSH(content []byte, ip string, args []string) error {
Expand Down Expand Up @@ -121,13 +116,19 @@ func callSSH(content []byte, ip string, args []string) error {
return cmd.Run()
}

func getSSHKey(hostname string, physicalHost client.PhysicalHost) ([]byte, error) {
link, ok := physicalHost.Links["config"]
func getSSHKey(hostname string, host client.Host, accessKey, secretKey string) ([]byte, error) {
link, ok := host.Links["config"]
if !ok {
return nil, fmt.Errorf("Failed to find SSH key for %s", hostname)
}

resp, err := http.Get(link)
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(accessKey, secretKey)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7f78a4d

Please sign in to comment.