-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_server.go
216 lines (178 loc) · 4.72 KB
/
proxy_server.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
package proxy
import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"net/http"
"net/url"
"strings"
"github.com/rancher/norman/httperror"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/proxy"
"k8s.io/client-go/rest"
)
type RemoteService struct {
cluster *v3.Cluster
transport http.RoundTripper
url urlGetter
auth authGetter
}
var (
er = &errorResponder{}
)
type urlGetter func() (url.URL, error)
type authGetter func() (string, error)
type errorResponder struct {
}
func (e *errorResponder) Error(w http.ResponseWriter, req *http.Request, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
func prefix(cluster *v3.Cluster) string {
return "/k8s/clusters/" + cluster.Name
}
func New(localConfig *rest.Config, cluster *v3.Cluster, clusterLister v3.ClusterLister, factory dialer.Factory) (*RemoteService, error) {
if cluster.Spec.Internal {
return NewLocal(localConfig, cluster)
}
return NewRemote(cluster, clusterLister, factory)
}
func NewLocal(localConfig *rest.Config, cluster *v3.Cluster) (*RemoteService, error) {
// the gvk is ignored by us, so just pass in any gvk
hostURL, _, err := rest.DefaultServerURL(localConfig.Host, localConfig.APIPath, schema.GroupVersion{}, true)
if err != nil {
return nil, err
}
transport, err := rest.TransportFor(localConfig)
if err != nil {
return nil, err
}
rs := &RemoteService{
cluster: cluster,
url: func() (url.URL, error) {
return *hostURL, nil
},
transport: transport,
}
if localConfig.BearerToken != "" {
rs.auth = func() (string, error) { return "Bearer " + localConfig.BearerToken, nil }
}
return rs, nil
}
func NewRemote(cluster *v3.Cluster, clusterLister v3.ClusterLister, factory dialer.Factory) (*RemoteService, error) {
if !v3.ClusterConditionProvisioned.IsTrue(cluster) {
return nil, httperror.NewAPIError(httperror.ClusterUnavailable, "cluster not provisioned")
}
transport := &http.Transport{}
if factory != nil {
d, err := factory.ClusterDialer(cluster.Name)
if err != nil {
return nil, err
}
transport.Dial = d
}
if cluster.Status.CACert != "" {
certBytes, err := base64.StdEncoding.DecodeString(cluster.Status.CACert)
if err != nil {
return nil, err
}
certs := x509.NewCertPool()
certs.AppendCertsFromPEM(certBytes)
transport.TLSClientConfig = &tls.Config{
RootCAs: certs,
}
}
urlGetter := func() (url.URL, error) {
newCluster, err := clusterLister.Get("", cluster.Name)
if err != nil {
return url.URL{}, err
}
u, err := url.Parse(newCluster.Status.APIEndpoint)
if err != nil {
return url.URL{}, err
}
return *u, nil
}
authGetter := func() (string, error) {
newCluster, err := clusterLister.Get("", cluster.Name)
if err != nil {
return "", err
}
return "Bearer " + newCluster.Status.ServiceAccountToken, nil
}
return &RemoteService{
cluster: cluster,
transport: transport,
url: urlGetter,
auth: authGetter,
}, nil
}
func (r *RemoteService) Close() {
}
func (r *RemoteService) Handler() http.Handler {
return r
}
func (r *RemoteService) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
u, err := r.url()
if err != nil {
er.Error(rw, req, err)
return
}
u.Path = strings.TrimPrefix(req.URL.Path, prefix(r.cluster))
u.RawQuery = req.URL.RawQuery
proto := req.Header.Get("X-Forwarded-Proto")
if proto != "" {
req.URL.Scheme = proto
} else if req.TLS == nil {
req.URL.Scheme = "http"
} else {
req.URL.Scheme = "https"
}
req.URL.Host = req.Host
if r.auth != nil {
token, err := r.auth()
if err != nil {
er.Error(rw, req, err)
return
}
req.Header.Set("Authorization", token)
}
httpProxy := proxy.NewUpgradeAwareHandler(&u, r.transport, true, false, er)
httpProxy.ServeHTTP(rw, req)
}
func (r *RemoteService) Cluster() *v3.Cluster {
return r.cluster
}
type SimpleProxy struct {
url *url.URL
transport http.RoundTripper
}
func NewSimpleProxy(host string, caData []byte) (*SimpleProxy, error) {
hostURL, _, err := rest.DefaultServerURL(host, "", schema.GroupVersion{}, true)
if err != nil {
return nil, err
}
ht := &http.Transport{}
if len(caData) > 0 {
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(caData)
ht.TLSClientConfig = &tls.Config{
RootCAs: certPool,
}
}
return &SimpleProxy{
url: hostURL,
transport: ht,
}, nil
}
func (s *SimpleProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
u := *s.url
u.Path = req.URL.Path
u.RawQuery = req.URL.RawQuery
req.URL.Scheme = "https"
req.URL.Host = req.Host
httpProxy := proxy.NewUpgradeAwareHandler(&u, s.transport, true, false, er)
httpProxy.ServeHTTP(rw, req)
}