-
Notifications
You must be signed in to change notification settings - Fork 18
/
listener.go
110 lines (98 loc) · 3.84 KB
/
listener.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
// Copyright 2023 The ClusterLink Authors.
// 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 server
import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"github.com/clusterlink-net/clusterlink/pkg/controlplane/api"
)
// CreateListener starts a listener to an imported service.
func (d *Dataplane) CreateListener(name, ip string, port uint32) {
listenTarget := ip + ":" + strconv.Itoa(int(port))
d.listenerEnd[name] = make(chan bool)
d.logger.Infof("Starting a listener for imported service %s at %s.", name, listenTarget)
acceptor, err := net.Listen("tcp", listenTarget)
if err != nil {
d.logger.Infof("Error listening to por: %v.", err)
return
}
go func() {
if err = d.serveEgressConnections(name, acceptor); err != nil {
d.logger.Errorf("Failed to serve egress connection on %s: %+v.", listenTarget, err)
}
}()
<-d.listenerEnd[name]
d.logger.Infof("Ending the listener for imported service %s at %s.", name, listenTarget)
acceptor.Close()
}
func (d *Dataplane) serveEgressConnections(name string, listener net.Listener) error {
for {
d.logger.Infof("Serving for imported service %s at %s.", name, listener.Addr())
conn, err := listener.Accept()
if err != nil {
d.logger.Error("Failed to accept egress connection", err)
return err
}
d.logger.Debugf(
"Received an egress connection at listener for imported service %s from %s.", name, conn.RemoteAddr().String())
d.logger.Debugf("Connection: %+v.", conn)
targetPeer, accessToken, err := d.getEgressAuth(name, strings.Split(conn.RemoteAddr().String(), ":")[0])
if err != nil {
d.logger.Infof("Failed egress authorization: %v.", err)
conn.Close()
continue
}
d.logger.Infof("Received auth from controlplane: target peer: %s with %s", targetPeer, accessToken)
targetHost, err := d.GetClusterHost(targetPeer)
if err != nil {
d.logger.Errorf("Unable to get cluster host :%v.", err)
conn.Close()
continue
}
tlsConfig := d.parsedCertData.ClientConfig(targetHost)
go func() {
err := d.initiateEgressConnection(targetPeer, accessToken, conn, tlsConfig)
if err != nil {
d.logger.Errorf("Failed to initiate egress connection: %v.", err)
conn.Close()
}
}()
}
}
// getEgressAuth returns the target cluster and authorization token for the outgoing connection.
func (d *Dataplane) getEgressAuth(name, sourceIP string) (string, string, error) { //nolint:gocritic // unnamedResult
url := "https://" + d.controlplaneTarget + api.DataplaneEgressAuthorizationPath
egressAuthReq, err := http.NewRequest(http.MethodPost, url, http.NoBody)
if err != nil {
return "", "", err
}
egressAuthReq.Close = true
components := strings.SplitN(name, "/", 2)
egressAuthReq.Header.Add(api.ClientIPHeader, sourceIP)
egressAuthReq.Header.Add(api.ImportNamespaceHeader, components[0])
egressAuthReq.Header.Add(api.ImportNameHeader, components[1])
egressAuthResp, err := d.apiClient.Do(egressAuthReq)
if err != nil {
d.logger.Errorf("Unable to send auth/egress request: %v.", err)
return "", "", err
}
defer egressAuthResp.Body.Close()
if egressAuthResp.StatusCode != http.StatusOK {
d.logger.Infof("Failed to obtain egress authorization: %s", egressAuthResp.Status)
return "", "", fmt.Errorf("failed egress authorization:%s", egressAuthResp.Status)
}
return egressAuthResp.Header.Get(api.TargetClusterHeader), egressAuthResp.Header.Get(api.AuthorizationHeader), nil
}