-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathproxycommand.go
More file actions
171 lines (146 loc) · 4.11 KB
/
proxycommand.go
File metadata and controls
171 lines (146 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ssh
import (
"io"
"net"
"os"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/smallstep/certificates/api"
"github.com/smallstep/cli-utils/command"
"github.com/smallstep/cli-utils/errs"
"github.com/smallstep/cli/exec"
"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/internal/sshutil"
"github.com/smallstep/cli/utils/cautils"
)
const sshDefaultPath = "/usr/bin/ssh"
func proxycommandCommand() cli.Command {
return cli.Command{
Name: "proxycommand",
Action: command.ActionFunc(proxycommandAction),
Usage: "proxy ssh connections according to the host registry",
UsageText: `**step ssh proxycommand** <user> <host> <port>
[**--provisioner**=<name>] [**--set**=<key=value>] [**--set-file**=<file>]
[**--console**] [**--offline**] [**--ca-config**=<file>]
[**--ca-url**=<uri>] [**--root**=<file>]
[**--context**=<name>] [**--fallback-context**=<name>]`,
Description: `**step ssh proxycommand** looks into the host registry
and proxies the ssh connection according to its configuration. This command
is used in the ssh client config with <ProxyCommand> keyword.
This command will add the user to the ssh-agent if necessary.
## POSITIONAL ARGUMENTS
<user>
: The remote username, and the subject used to login.
<host>
: The host to connect to.
<port>
: The port to connect to.`,
Flags: []cli.Flag{
flags.Provisioner,
flags.ProvisionerPasswordFileWithAlias,
flags.TemplateSet,
flags.TemplateSetFile,
flags.Console,
flags.Offline,
flags.CaConfig,
flags.CaURL,
flags.Root,
flags.Context,
flags.FallbackContext,
},
}
}
func proxycommandAction(ctx *cli.Context) error {
if err := errs.NumberOfArguments(ctx, 3); err != nil {
return err
}
args := ctx.Args()
user, host, port := args[0], args[1], args[2]
// Check if user is logged in
if err := doLoginIfNeeded(ctx, user); err != nil {
return err
}
// Get the configured bastion if any
r, err := getBastion(ctx, user, host)
if err != nil {
return err
}
// Connect through bastion
if r.Bastion != nil && r.Bastion.Hostname != "" {
return proxyBastion(r, user, host, port)
}
// Connect directly
return proxyDirect(host, port)
}
// doLoginIfNeeded check if the user is logged in looking at the ssh agent, if
// it's not it will do the login flow.
func doLoginIfNeeded(ctx *cli.Context, subject string) error {
return loginIfNeeded(ctx, subject, withRetryFunc(runOnFallbackContext))
}
func getBastion(ctx *cli.Context, user, host string) (*api.SSHBastionResponse, error) {
client, err := cautils.NewClient(ctx)
if err != nil {
return nil, err
}
return client.SSHBastion(&api.SSHBastionRequest{
User: user,
Hostname: host,
})
}
func proxyDirect(host, port string) error {
address := net.JoinHostPort(host, port)
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return errors.Wrap(err, "error resolving address")
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return errors.Wrapf(err, "error connecting to %s", address)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
io.Copy(conn, os.Stdin)
conn.CloseWrite()
wg.Done()
}()
wg.Add(1)
go func() {
io.Copy(os.Stdout, conn)
conn.CloseRead()
wg.Done()
}()
wg.Wait()
return nil
}
func proxyBastion(r *api.SSHBastionResponse, user, host, port string) error {
sshPath, err := exec.LookPath("ssh")
if err != nil {
sshPath = sshDefaultPath
}
args := []string{}
if r.Bastion.User != "" {
args = append(args, "-l", r.Bastion.User)
}
if r.Bastion.Port != "" {
args = append(args, "-p", r.Bastion.Port)
}
if r.Bastion.Flags != "" {
// BUG(mariano): This is a naive way of doing it as it doesn't
// support strings, but it should work for most of the cases in ssh.
// For more advance cases the package
// github.com/kballard/go-shellquote can be used.
fields := strings.Fields(r.Bastion.Flags)
args = append(args, fields...)
}
args = append(args, r.Bastion.Hostname)
if r.Bastion.Command != "" {
args = append(args, sshutil.ProxyCommand(r.Bastion.Command, user, host, port))
} else {
args = append(args, "nc", host, port)
}
exec.Exec(sshPath, args...)
return nil
}