| 1 |
package ssh |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"io" |
| 6 |
"net" |
| 7 |
"os" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/pkg/errors" |
| 13 |
"github.com/smallstep/certificates/api" |
| 14 |
"github.com/smallstep/certificates/authority/provisioner" |
| 15 |
"github.com/smallstep/certificates/ca" |
| 16 |
"github.com/smallstep/cli/command" |
| 17 |
"github.com/smallstep/cli/crypto/keys" |
| 18 |
"github.com/smallstep/cli/crypto/sshutil" |
| 19 |
"github.com/smallstep/cli/errs" |
| 20 |
"github.com/smallstep/cli/exec" |
| 21 |
"github.com/smallstep/cli/flags" |
| 22 |
"github.com/smallstep/cli/utils/cautils" |
| 23 |
"github.com/urfave/cli" |
| 24 |
"golang.org/x/crypto/ssh" |
| 25 |
) |
| 26 |
|
| 27 |
const sshDefaultPath = "/usr/bin/ssh" |
| 28 |
|
| 29 |
func proxycommandCommand() cli.Command { |
| 30 |
return cli.Command{ |
| 31 |
Name: "proxycommand", |
| 32 |
Action: command.ActionFunc(proxycommandAction), |
| 33 |
Usage: "proxy ssh connections according to the host registry", |
| 34 |
UsageText: `**step ssh proxycommand** <user> <host> <port> |
| 35 |
[**--provisioner**=<name>] [**--set**=<key=value>] [**--set-file**=<path>] |
| 36 |
[**--ca-url**=<uri>] [**--root**=<file>] [**--offline**] [**--ca-config**=<path>]`, |
| 37 |
Description: `**step ssh proxycommand** looks into the host registry |
| 38 |
and proxies the ssh connection according to its configuration. This command |
| 39 |
is used in the ssh client config with <ProxyCommand> keyword. |
| 40 |
|
| 41 |
This command will add the user to the ssh-agent if necessary. |
| 42 |
|
| 43 |
## POSITIONAL ARGUMENTS |
| 44 |
|
| 45 |
<user> |
| 46 |
: The remote username, and the subject used to login. |
| 47 |
|
| 48 |
<host> |
| 49 |
: The host to connect to. |
| 50 |
|
| 51 |
<port> |
| 52 |
: The port to connect to.`, |
| 53 |
Flags: []cli.Flag{ |
| 54 |
flags.Provisioner, |
| 55 |
flags.TemplateSet, |
| 56 |
flags.TemplateSetFile, |
| 57 |
flags.CaURL, |
| 58 |
flags.Root, |
| 59 |
flags.Offline, |
| 60 |
flags.CaConfig, |
| 61 |
}, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func proxycommandAction(ctx *cli.Context) error { |
| 66 |
if err := errs.NumberOfArguments(ctx, 3); err != nil { |
| 67 |
return err |
| 68 |
} |
| 69 |
|
| 70 |
args := ctx.Args() |
| 71 |
user, host, port := args[0], args[1], args[2] |
| 72 |
|
| 73 |
|
| 74 |
if err := doLoginIfNeeded(ctx, user); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
r, err := getBastion(ctx, user, host) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
if r.Bastion != nil && r.Bastion.Hostname != "" { |
| 86 |
return proxyBastion(r, user, host, port) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
return proxyDirect(host, port) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
func doLoginIfNeeded(ctx *cli.Context, subject string) error { |
| 96 |
templateData, err := flags.ParseTemplateData(ctx) |
| 97 |
if err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
|
| 101 |
agent, err := sshutil.DialAgent() |
| 102 |
if err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
|
| 106 |
client, err := cautils.NewClient(ctx) |
| 107 |
if err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
if roots, err := client.SSHRoots(); err == nil && len(roots.UserKeys) > 0 { |
| 113 |
userKeys := make([]ssh.PublicKey, len(roots.UserKeys)) |
| 114 |
for i, uk := range roots.UserKeys { |
| 115 |
userKeys[i] = uk.PublicKey |
| 116 |
} |
| 117 |
exists, err := agent.HasKeys(sshutil.WithSignatureKey(userKeys), sshutil.WithRemoveExpiredCerts(time.Now())) |
| 118 |
if err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
if exists { |
| 122 |
return nil |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
flow, err := cautils.NewCertificateFlow(ctx) |
| 128 |
if err != nil { |
| 129 |
return err |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
principals := []string{subject} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
validAfter := provisioner.NewTimeDuration(time.Now().Add(-1 * time.Minute)) |
| 139 |
validBefore := provisioner.TimeDuration{} |
| 140 |
|
| 141 |
token, err := flow.GenerateSSHToken(ctx, subject, cautils.SSHUserSignType, principals, validAfter, validBefore) |
| 142 |
if err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if email, ok := tokenHasEmail(token); ok { |
| 150 |
subject = email |
| 151 |
} |
| 152 |
|
| 153 |
caClient, err := flow.GetClient(ctx, token) |
| 154 |
if err != nil { |
| 155 |
return err |
| 156 |
} |
| 157 |
|
| 158 |
version, err := caClient.Version() |
| 159 |
if err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
var identityCSR api.CertificateRequest |
| 165 |
var identityKey crypto.PrivateKey |
| 166 |
if version.RequireClientAuthentication { |
| 167 |
csr, key, err := ca.CreateIdentityRequest(subject) |
| 168 |
if err != nil { |
| 169 |
return err |
| 170 |
} |
| 171 |
identityCSR = *csr |
| 172 |
identityKey = key |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
pub, priv, err := keys.GenerateDefaultKeyPair() |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
|
| 181 |
sshPub, err := ssh.NewPublicKey(pub) |
| 182 |
if err != nil { |
| 183 |
return errors.Wrap(err, "error creating public key") |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
resp, err := caClient.SSHSign(&api.SSHSignRequest{ |
| 188 |
PublicKey: sshPub.Marshal(), |
| 189 |
OTT: token, |
| 190 |
Principals: principals, |
| 191 |
CertType: provisioner.SSHUserCert, |
| 192 |
KeyID: subject, |
| 193 |
ValidAfter: validAfter, |
| 194 |
ValidBefore: validBefore, |
| 195 |
IdentityCSR: identityCSR, |
| 196 |
TemplateData: templateData, |
| 197 |
}) |
| 198 |
if err != nil { |
| 199 |
return err |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if version.RequireClientAuthentication { |
| 204 |
if err := ca.WriteDefaultIdentity(resp.IdentityCertificate, identityKey); err != nil { |
| 205 |
return err |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if err := agent.AddCertificate(subject, resp.Certificate.Certificate, priv); err != nil { |
| 211 |
return err |
| 212 |
} |
| 213 |
|
| 214 |
return nil |
| 215 |
} |
| 216 |
|
| 217 |
func getBastion(ctx *cli.Context, user, host string) (*api.SSHBastionResponse, error) { |
| 218 |
client, err := cautils.NewClient(ctx) |
| 219 |
if err != nil { |
| 220 |
return nil, err |
| 221 |
} |
| 222 |
return client.SSHBastion(&api.SSHBastionRequest{ |
| 223 |
User: user, |
| 224 |
Hostname: host, |
| 225 |
}) |
| 226 |
} |
| 227 |
|
| 228 |
func proxyDirect(host, port string) error { |
| 229 |
address := net.JoinHostPort(host, port) |
| 230 |
addr, err := net.ResolveTCPAddr("tcp", address) |
| 231 |
if err != nil { |
| 232 |
return errors.Wrap(err, "error resolving address") |
| 233 |
} |
| 234 |
|
| 235 |
conn, err := net.DialTCP("tcp", nil, addr) |
| 236 |
if err != nil { |
| 237 |
return errors.Wrapf(err, "error connecting to %s", address) |
| 238 |
} |
| 239 |
|
| 240 |
var wg sync.WaitGroup |
| 241 |
wg.Add(1) |
| 242 |
go func() { |
| 243 |
io.Copy(conn, os.Stdin) |
| 244 |
conn.CloseWrite() |
| 245 |
wg.Done() |
| 246 |
}() |
| 247 |
wg.Add(1) |
| 248 |
go func() { |
| 249 |
io.Copy(os.Stdout, conn) |
| 250 |
conn.CloseRead() |
| 251 |
wg.Done() |
| 252 |
}() |
| 253 |
|
| 254 |
wg.Wait() |
| 255 |
return nil |
| 256 |
} |
| 257 |
|
| 258 |
func proxyBastion(r *api.SSHBastionResponse, user, host, port string) error { |
| 259 |
sshPath, err := exec.LookPath("ssh") |
| 260 |
if err != nil { |
| 261 |
sshPath = sshDefaultPath |
| 262 |
} |
| 263 |
|
| 264 |
args := []string{} |
| 265 |
if r.Bastion.User != "" { |
| 266 |
args = append(args, "-l", r.Bastion.User) |
| 267 |
} |
| 268 |
if r.Bastion.Port != "" { |
| 269 |
args = append(args, "-p", r.Bastion.Port) |
| 270 |
} |
| 271 |
if r.Bastion.Flags != "" { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
fields := strings.Fields(r.Bastion.Flags) |
| 277 |
args = append(args, fields...) |
| 278 |
} |
| 279 |
args = append(args, r.Bastion.Hostname) |
| 280 |
if r.Bastion.Command != "" { |
| 281 |
args = append(args, sshutil.ProxyCommand(r.Bastion.Command, user, host, port)) |
| 282 |
} else { |
| 283 |
args = append(args, "nc", host, port) |
| 284 |
} |
| 285 |
exec.Exec(sshPath, args...) |
| 286 |
return nil |
| 287 |
} |