-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
193 lines (156 loc) · 4.91 KB
/
client.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package ssh
import (
"context"
"encoding/base64"
"fmt"
"net"
"os"
"github.com/gravitational/teleport/api/client/proxy"
"github.com/gravitational/teleport/api/profile"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/tcache"
"namespacelabs.dev/foundation/internal/workspace/dirs"
)
var (
sshTransports = tcache.NewCache[*ssh.Client]()
)
type Endpoint struct {
User string
PrivateKeyPath string
AgentSockPath string
Address string
TeleportProfileName string
}
type Deferred struct {
CacheKey string
Dial func(context.Context) (*ssh.Client, error)
}
func Establish(ctx context.Context, endpoint Endpoint) (*Deferred, error) {
debugLogger := console.Debug(ctx)
if endpoint.User == "" {
return nil, fnerrors.New("transport.ssh: user is required")
}
if endpoint.Address == "" {
return nil, fnerrors.New("transport.ssh: address is required")
}
sshAddr, sshPort, err := net.SplitHostPort(endpoint.Address)
if err != nil {
sshAddr = endpoint.Address
sshPort = "22"
}
key, keyKey, err := parseAuth(endpoint.PrivateKeyPath)
if err != nil {
return nil, err
}
var config *ssh.ClientConfig
var dialer func(context.Context, string) (net.Conn, error)
if endpoint.TeleportProfileName != "" {
if endpoint.AgentSockPath != "" {
return nil, fnerrors.New("ssh: can't use both teleport_profile_name and agent_sock_path")
}
if endpoint.PrivateKeyPath != "" {
return nil, fnerrors.New("ssh: can't use private_key_path with teleport_profile_name")
}
p, err := profile.FromDir("", endpoint.TeleportProfileName)
if err != nil {
return nil, err
}
tlscfg, err := p.TLSConfig()
if err != nil {
return nil, err
}
sshcfg, err := p.SSHClientConfig()
if err != nil {
return nil, err
}
clt, err := proxy.NewClient(ctx, proxy.ClientConfig{
ProxyAddress: p.WebProxyAddr,
TLSRoutingEnabled: p.TLSRoutingEnabled,
TLSConfig: tlscfg,
SSHConfig: sshcfg,
ALPNConnUpgradeRequired: p.TLSRoutingConnUpgradeRequired && p.TLSRoutingEnabled,
})
if err != nil {
return nil, err
}
// Hardcoded default port. See https://github.com/gravitational/teleport/blob/da589355d4ea55de276062db09f440c6fefdb2d6/lib/defaults/defaults.go#L48
sshPort = "3022"
fmt.Fprintf(debugLogger, "ssh: using teleport via %q\n", p.SiteName)
config = sshcfg
dialer = func(ctx context.Context, addr string) (net.Conn, error) {
conn, _, err := clt.DialHost(ctx, addr, p.SiteName, nil)
return conn, err
}
} else {
config = &ssh.ClientConfig{
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
fmt.Fprintf(debugLogger, "ssh: connected to %q (%s)\n", hostname, remote)
return nil
},
}
dialer = func(ctx context.Context, addr string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "tcp", addr)
}
if key != nil {
config.Auth = append(config.Auth, key)
}
if endpoint.AgentSockPath != "" {
path, err := dirs.ExpandHome(os.ExpandEnv(endpoint.AgentSockPath))
if err != nil {
return nil, fnerrors.New("failed to resolve ssh agent path: %w", err)
}
keyKey += ":agent=" + path
conn, err := net.Dial("unix", path)
if err != nil {
return nil, fnerrors.New("failed to connect to ssh agent: %w", err)
}
agentClient := agent.NewClient(conn)
config.Auth = append(config.Auth, ssh.PublicKeysCallback(agentClient.Signers))
}
}
config.User = endpoint.User
cachekey := fmt.Sprintf("%s:%s@%s:%s", endpoint.User, keyKey, sshAddr, sshPort)
return &Deferred{
CacheKey: cachekey,
Dial: func(ctx context.Context) (*ssh.Client, error) {
return sshTransports.Compute(cachekey, func() (*ssh.Client, error) {
fmt.Fprintf(debugLogger, "ssh: will dial to %s:%s\n", sshAddr, sshPort)
addrport := fmt.Sprintf("%s:%s", sshAddr, sshPort)
conn, err := dialer(ctx, addrport)
if err != nil {
return nil, err
}
c, chans, reqs, err := ssh.NewClientConn(conn, addrport, config)
if err != nil {
return nil, err
}
return ssh.NewClient(c, chans, reqs), nil
})
},
}, nil
}
func parseAuth(privateKeyPath string) (ssh.AuthMethod, string, error) {
if privateKeyPath != "" {
key, err := parsePrivateKey(privateKeyPath)
if err != nil {
return nil, "", err
}
keyKey := base64.RawStdEncoding.EncodeToString(key.PublicKey().Marshal())
return ssh.PublicKeys(key), keyKey, nil
}
return nil, "", nil
}
func parsePrivateKey(keyPath string) (ssh.Signer, error) {
buff, err := os.ReadFile(keyPath)
if err != nil {
return nil, err
}
return ssh.ParsePrivateKey(buff)
}