forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel.go
368 lines (309 loc) · 9.64 KB
/
tunnel.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package tunnelserver
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/remotedialer"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)
const (
crtKeyIndex = "crtKeyIndex"
nodeKeyIndex = "nodeKeyIndex"
importedDriver = "imported"
Token = "X-API-Tunnel-Token"
Params = "X-API-Tunnel-Params"
)
type cluster struct {
Address string `json:"address"`
Token string `json:"token"`
CACert string `json:"caCert"`
}
type input struct {
Node *client.Node `json:"node"`
Cluster *cluster `json:"cluster"`
}
func NewTunnelServer(authorizer *Authorizer) *remotedialer.Server {
return remotedialer.New(authorizer.authorizeTunnel, remotedialer.DefaultErrorWriter)
}
func NewAuthorizer(context *config.ScaledContext) *Authorizer {
auth := &Authorizer{
crtIndexer: context.Management.ClusterRegistrationTokens("").Controller().Informer().GetIndexer(),
clusterLister: context.Management.Clusters("").Controller().Lister(),
nodeIndexer: context.Management.Nodes("").Controller().Informer().GetIndexer(),
machineLister: context.Management.Nodes("").Controller().Lister(),
machines: context.Management.Nodes(""),
clusters: context.Management.Clusters(""),
}
context.Management.ClusterRegistrationTokens("").Controller().Informer().AddIndexers(map[string]cache.IndexFunc{
crtKeyIndex: auth.crtIndex,
})
context.Management.Nodes("").Controller().Informer().AddIndexers(map[string]cache.IndexFunc{
nodeKeyIndex: auth.nodeIndex,
})
return auth
}
type Authorizer struct {
crtIndexer cache.Indexer
clusterLister v3.ClusterLister
nodeIndexer cache.Indexer
machineLister v3.NodeLister
machines v3.NodeInterface
clusters v3.ClusterInterface
}
type Client struct {
Cluster *v3.Cluster
Node *v3.Node
Token string
Server string
}
func (t *Authorizer) authorizeTunnel(req *http.Request) (string, bool, error) {
client, ok, err := t.Authorize(req)
if client != nil && client.Node != nil {
return client.Cluster.Name + ":" + client.Node.Name, ok, err
} else if client != nil && client.Cluster != nil {
return client.Cluster.Name, ok, err
}
return "", false, err
}
func (t *Authorizer) AuthorizeLocalNode(username, password string) (string, []string, *v3.Cluster, bool) {
cluster, err := t.getClusterByToken(password)
if err != nil || cluster == nil || cluster.Status.Driver != v3.ClusterDriverLocal {
return "", nil, nil, false
}
if username == "kube-proxy" {
return "system:kube-proxy", nil, cluster, true
}
return "system:node:" + username, []string{"system:nodes"}, cluster, true
}
func (t *Authorizer) Authorize(req *http.Request) (*Client, bool, error) {
token := req.Header.Get(Token)
if token == "" {
return nil, false, nil
}
cluster, err := t.getClusterByToken(token)
if err != nil || cluster == nil {
return nil, false, err
}
input, err := t.readInput(cluster, req)
if err != nil {
return nil, false, err
}
if input.Node != nil {
register := strings.HasSuffix(req.URL.Path, "/register")
node, ok, err := t.authorizeNode(register, cluster, input.Node, req)
if err != nil {
return nil, false, err
}
if register && node.Status.NodeConfig != nil && input.Node.CustomConfig != nil {
node = node.DeepCopy()
node.Status.NodeConfig.Address = input.Node.CustomConfig.Address
node.Status.NodeConfig.InternalAddress = input.Node.CustomConfig.InternalAddress
}
return &Client{
Cluster: cluster,
Node: node,
Token: token,
Server: req.Host,
}, ok, err
}
if input.Cluster != nil {
cluster, ok, err := t.authorizeCluster(cluster, input.Cluster, req)
return &Client{
Cluster: cluster,
Token: token,
Server: req.Host,
}, ok, err
}
return nil, false, nil
}
func (t *Authorizer) getMachine(cluster *v3.Cluster, inNode *client.Node) (*v3.Node, error) {
machineName := machineName(inNode)
machine, err := t.machineLister.Get(cluster.Name, machineName)
if apierrors.IsNotFound(err) {
if objs, err := t.nodeIndexer.ByIndex(nodeKeyIndex, fmt.Sprintf("%s/%s", cluster.Name, inNode.RequestedHostname)); err == nil {
for _, obj := range objs {
return obj.(*v3.Node), err
}
}
machine, err := t.machineLister.Get(cluster.Name, inNode.RequestedHostname)
if err == nil {
return machine, nil
}
}
return machine, err
}
func (t *Authorizer) authorizeNode(register bool, cluster *v3.Cluster, inNode *client.Node, req *http.Request) (*v3.Node, bool, error) {
machine, err := t.getMachine(cluster, inNode)
if apierrors.IsNotFound(err) {
if !register {
return nil, false, err
}
machine, err = t.createNode(inNode, cluster, req)
if err != nil {
return nil, false, err
}
} else if err != nil && machine == nil {
return nil, false, err
}
if register {
machine, err = t.updateNode(machine, inNode, cluster)
if err != nil {
return nil, false, err
}
}
machine, err = t.updateDockerInfo(machine, inNode)
return machine, true, err
}
func (t *Authorizer) createNode(inNode *client.Node, cluster *v3.Cluster, req *http.Request) (*v3.Node, error) {
customConfig := t.toCustomConfig(inNode)
if customConfig == nil {
return nil, errors.New("invalid input, missing custom config")
}
if customConfig.Address == "" {
return nil, errors.New("invalid input, address empty")
}
name := machineName(inNode)
machine := &v3.Node{
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: cluster.Name,
},
Spec: v3.NodeSpec{
Etcd: inNode.Etcd,
ControlPlane: inNode.ControlPlane,
Worker: inNode.Worker,
RequestedHostname: inNode.RequestedHostname,
CustomConfig: customConfig,
Imported: true,
},
}
return t.machines.Create(machine)
}
func (t *Authorizer) updateDockerInfo(machine *v3.Node, inNode *client.Node) (*v3.Node, error) {
if inNode.DockerInfo == nil {
return machine, nil
}
dockerInfo := &v3.DockerInfo{}
err := convert.ToObj(inNode.DockerInfo, dockerInfo)
if err != nil {
return nil, err
}
newMachine := machine.DeepCopy()
newMachine.Status.DockerInfo = dockerInfo
if !reflect.DeepEqual(machine, newMachine) {
return t.machines.Update(newMachine)
}
return machine, nil
}
func (t *Authorizer) updateNode(machine *v3.Node, inNode *client.Node, cluster *v3.Cluster) (*v3.Node, error) {
newMachine := machine.DeepCopy()
newMachine.Spec.Etcd = inNode.Etcd
newMachine.Spec.ControlPlane = inNode.ControlPlane
newMachine.Spec.Worker = inNode.Worker
if !reflect.DeepEqual(machine, newMachine) {
return t.machines.Update(newMachine)
}
return machine, nil
}
func (t *Authorizer) authorizeCluster(cluster *v3.Cluster, inCluster *cluster, req *http.Request) (*v3.Cluster, bool, error) {
var (
err error
)
if cluster.Status.Driver != importedDriver && cluster.Status.Driver != "" {
return cluster, true, nil
}
changed := false
if cluster.Status.Driver == "" {
cluster.Status.Driver = importedDriver
changed = true
}
apiEndpoint := "https://" + inCluster.Address
token := inCluster.Token
caCert := inCluster.CACert
if cluster.Status.Driver == importedDriver {
if cluster.Status.APIEndpoint != apiEndpoint ||
cluster.Status.ServiceAccountToken != token ||
cluster.Status.CACert != caCert {
cluster.Status.APIEndpoint = apiEndpoint
cluster.Status.ServiceAccountToken = token
cluster.Status.CACert = caCert
changed = true
}
}
if changed {
_, err = t.clusters.Update(cluster)
}
return cluster, true, err
}
func (t *Authorizer) readInput(cluster *v3.Cluster, req *http.Request) (*input, error) {
params := req.Header.Get(Params)
var input input
bytes, err := base64.StdEncoding.DecodeString(params)
if err != nil {
return nil, err
}
if err := json.Unmarshal(bytes, &input); err != nil {
return nil, err
}
if input.Node == nil && input.Cluster == nil {
return nil, errors.New("missing node or cluster registration info")
}
if input.Node != nil && input.Node.RequestedHostname == "" {
return nil, errors.New("invalid input, hostname empty")
}
if input.Cluster != nil && input.Cluster.Address == "" {
return nil, errors.New("invalid input, address empty")
}
if input.Cluster != nil && input.Cluster.Token == "" {
return nil, errors.New("invalid input, token empty")
}
if input.Cluster != nil && input.Cluster.CACert == "" {
return nil, errors.New("invalid input, caCert empty")
}
return &input, nil
}
func machineName(machine *client.Node) string {
digest := md5.Sum([]byte(machine.RequestedHostname))
return "m-" + hex.EncodeToString(digest[:])[:12]
}
func (t *Authorizer) getClusterByToken(token string) (*v3.Cluster, error) {
keys, err := t.crtIndexer.ByIndex(crtKeyIndex, token)
if err != nil {
return nil, err
}
for _, obj := range keys {
crt := obj.(*v3.ClusterRegistrationToken)
return t.clusterLister.Get("", crt.Spec.ClusterName)
}
return nil, errors.New("cluster not found")
}
func (t *Authorizer) crtIndex(obj interface{}) ([]string, error) {
crt := obj.(*v3.ClusterRegistrationToken)
return []string{crt.Status.Token}, nil
}
func (t *Authorizer) nodeIndex(obj interface{}) ([]string, error) {
node := obj.(*v3.Node)
return []string{fmt.Sprintf("%s/%s", node.Namespace, node.Status.NodeName)}, nil
}
func (t *Authorizer) toCustomConfig(machine *client.Node) *v3.CustomConfig {
if machine == nil || machine.CustomConfig == nil {
return nil
}
result := &v3.CustomConfig{}
if err := convert.ToObj(machine.CustomConfig, result); err != nil {
return nil
}
return result
}