-
Notifications
You must be signed in to change notification settings - Fork 55
/
client.go
156 lines (129 loc) · 3.49 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
package cmd
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
v1 "github.com/infrahq/infra/internal/v1"
"github.com/natefinch/lumberjack"
)
const (
INFRA_HIDDEN_DIR = ".infra"
CLIENT_DIR = "client"
)
var (
ClientTimeoutDuration = 5 * time.Minute
errorLogger log.Logger // writes errors from the proxy handler to a file
)
func RunLocalClient() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
errorLogger.SetOutput(&lumberjack.Logger{
Filename: filepath.Join(homeDir, INFRA_HIDDEN_DIR, CLIENT_DIR, "proxy_error.log"),
MaxSize: 1, // megabyte
MaxBackups: 1,
})
errorLogger.SetPrefix(time.Now().Format("2006-01-02 15:04:05 "))
timer := time.NewTimer(ClientTimeoutDuration)
go func() {
<-timer.C
os.Exit(0)
}()
proxyHandler := func(w http.ResponseWriter, r *http.Request) {
components := strings.Split(r.URL.Path, "/")
if len(components) < 3 {
http.Error(w, "path not found", http.StatusNotFound)
return
}
name := components[2]
contents, err := ioutil.ReadFile(filepath.Join(homeDir, INFRA_HIDDEN_DIR, "destinations"))
if err != nil {
fmt.Println(err)
return
}
var destinations []v1.Destination
err = json.Unmarshal(contents, &destinations)
if err != nil {
http.Error(w, "could not read destinations from ~/.infra/destinations", http.StatusInternalServerError)
fmt.Println(err)
return
}
var destination *v1.Destination
for i := range destinations {
if destinations[i].Name == name {
destination = &destinations[i]
}
}
if destination == nil {
fmt.Println("could not load destination information for destination " + name)
return
}
var endpoint, ca, saToken string
namespace := "default"
if kube := destination.GetKubernetes(); kube != nil {
endpoint = kube.Endpoint
ca = kube.Ca
namespace = kube.Namespace
saToken = kube.SaToken
}
remote, err := url.Parse(endpoint + "/api/v1/namespaces/" + namespace + "/services/http:infra-engine:80/proxy/proxy")
if err != nil {
errorLogger.Println(err)
return
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(ca))
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
},
}
timer.Reset(ClientTimeoutDuration)
r.Header.Add("X-Infra-Authorization", r.Header.Get("Authorization"))
r.Header.Set("Authorization", "Bearer "+saToken)
http.StripPrefix("/client/"+name, proxy).ServeHTTP(w, r)
}
mux := http.NewServeMux()
mux.HandleFunc("/client/", proxyHandler)
certBytes, err := ioutil.ReadFile(filepath.Join(homeDir, INFRA_HIDDEN_DIR, CLIENT_DIR, "cert.pem"))
if err != nil {
return err
}
keyBytes, err := ioutil.ReadFile(filepath.Join(homeDir, INFRA_HIDDEN_DIR, CLIENT_DIR, "key.pem"))
if err != nil {
return err
}
keypair, err := tls.X509KeyPair(certBytes, keyBytes)
if err != nil {
return err
}
tlsConfig := &tls.Config{}
tlsConfig.Certificates = []tls.Certificate{keypair}
tlsServer := &http.Server{
Addr: "127.0.0.1:32710",
TLSConfig: tlsConfig,
Handler: mux,
}
l, err := net.Listen("tcp", "127.0.0.1:32710")
if err != nil {
return err
}
if err = ioutil.WriteFile(filepath.Join(homeDir, INFRA_HIDDEN_DIR, CLIENT_DIR, "pid"), []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
return err
}
return tlsServer.ServeTLS(l, "", "")
}