This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
/
teleport.go
170 lines (155 loc) · 4.97 KB
/
teleport.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
/*
Copyright 2018 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clients
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"strconv"
"github.com/gravitational/gravity/lib/constants"
"github.com/gravitational/gravity/lib/defaults"
"github.com/gravitational/gravity/lib/ops"
"github.com/gravitational/gravity/lib/utils"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/auth/native"
"github.com/gravitational/teleport/lib/client"
teledefaults "github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/cloudflare/cfssl/csr"
"github.com/gravitational/license/authority"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
)
// Teleport returns a new teleport client
func Teleport(operator ops.Operator, proxyHost, clusterName string) (*client.TeleportClient, error) {
auth, tlsConfig, err := authenticateWithTeleport(operator)
if err != nil {
return nil, trace.Wrap(err)
}
host, webPort, sshPort, err := utils.ParseProxyAddr(
proxyHost,
strconv.Itoa(defaults.GravityServicePort),
strconv.Itoa(teledefaults.SSHProxyListenPort))
if err != nil {
return nil, trace.Wrap(err)
}
return client.NewClient(&client.Config{
Username: constants.OpsCenterUser,
AuthMethods: auth,
SkipLocalAuth: true,
HostLogin: defaults.SSHUser,
WebProxyAddr: fmt.Sprintf("%v:%v", host, webPort),
SSHProxyAddr: fmt.Sprintf("%v:%v", host, sshPort),
SiteName: clusterName,
HostKeyCallback: sshHostCheckerAcceptAny,
TLS: tlsConfig,
Env: map[string]string{
defaults.PathEnv: defaults.PathEnvVal,
},
})
}
// TeleportProxy returns a new teleport proxy client
func TeleportProxy(ctx context.Context, operator ops.Operator, proxyHost, clusterName string) (*client.ProxyClient, error) {
teleport, err := Teleport(operator, proxyHost, clusterName)
if err != nil {
return nil, trace.Wrap(err)
}
return teleport.ConnectToProxy(ctx)
}
// TeleportAuth returns a new teleport auth server client
func TeleportAuth(ctx context.Context, operator ops.Operator, proxyHost, clusterName string) (*AuthClient, error) {
teleport, err := Teleport(operator, proxyHost, clusterName)
if err != nil {
return nil, trace.Wrap(err)
}
// Teleport auth server prior to version 3.0 didn't support TLS so
// do not attempt to connect if there's no TLS information, otherwise
// we'll get panic.
if teleport.TLS == nil {
return nil, trace.BadParameter("auth server %v does not support TLS", proxyHost)
}
proxyClient, err := teleport.ConnectToProxy(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
authClient, err := proxyClient.ConnectToCurrentCluster(ctx, false)
if err != nil {
return nil, trace.Wrap(err)
}
return &AuthClient{
ClientI: authClient,
proxyClient: proxyClient,
}, nil
}
// Close closes this client
func (r *AuthClient) Close() error {
return r.proxyClient.Close()
}
// AuthClient represents the client to the auth server
type AuthClient struct {
io.Closer
auth.ClientI
proxyClient *client.ProxyClient
}
func authenticateWithTeleport(operator ops.Operator) ([]ssh.AuthMethod, *tls.Config, error) {
keygen, err := native.New()
if err != nil {
return nil, nil, trace.Wrap(err)
}
private, public, err := keygen.GenerateKeyPair("")
if err != nil {
return nil, nil, trace.Wrap(err)
}
csr, key, err := authority.GenerateCSR(csr.CertificateRequest{
CN: constants.OpsCenterUser,
Names: []csr.Name{{O: defaults.SystemAccountOrg}},
}, private)
if err != nil {
return nil, nil, trace.Wrap(err)
}
response, err := operator.SignSSHKey(ops.SSHSignRequest{
User: constants.OpsCenterUser,
AccountID: defaults.SystemAccountID,
PublicKey: public,
TTL: defaults.CertTTL,
AllowedLogins: []string{defaults.SSHUser},
CSR: csr,
})
if err != nil {
return nil, nil, trace.Wrap(err)
}
signer, err := sshutils.NewSigner(private, response.Cert)
if err != nil {
return nil, nil, trace.Wrap(err)
}
var tlsConfig *tls.Config
// older clusters do not return TLS certificate
if response.TLSCert != nil {
tlsConfig, err = (&client.Key{
TLSCert: response.TLSCert,
Priv: key,
TrustedCA: []auth.TrustedCerts{{
TLSCertificates: [][]byte{response.CACert},
}},
}).ClientTLSConfig()
if err != nil {
return nil, nil, trace.Wrap(err)
}
}
return []ssh.AuthMethod{ssh.PublicKeys(signer)}, tlsConfig, nil
}
func sshHostCheckerAcceptAny(hostID string, remote net.Addr, key ssh.PublicKey) error {
return nil
}