-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
transport.go
362 lines (317 loc) · 10.1 KB
/
transport.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
/*
Copyright 2020 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 app
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
"path"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/api/types/wrappers"
apiutils "github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/lib"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/srv/app/common"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/oxy/forward"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)
// transportConfig is configuration for a rewriting transport.
type transportConfig struct {
app types.Application
publicPort string
cipherSuites []uint16
jwt string
w events.StreamWriter
traits wrappers.Traits
log logrus.FieldLogger
user string
}
// Check validates configuration.
func (c *transportConfig) Check() error {
if c.w == nil {
return trace.BadParameter("stream writer missing")
}
if c.app == nil {
return trace.BadParameter("app missing")
}
if c.publicPort == "" {
return trace.BadParameter("public port missing")
}
if c.jwt == "" {
return trace.BadParameter("jwt missing")
}
if c.log == nil {
c.log = logrus.WithField(trace.Component, "transport")
}
return nil
}
// transport is a rewriting http.RoundTripper that can audit and forward
// requests to an internal application.
type transport struct {
closeContext context.Context
c *transportConfig
tr http.RoundTripper
uri *url.URL
ws *websocketTransport
}
// newTransport creates a new transport.
func newTransport(ctx context.Context, c *transportConfig) (*transport, error) {
if err := c.Check(); err != nil {
return nil, trace.Wrap(err)
}
// Parse the target address once then inject it into all requests.
uri, err := url.Parse(c.app.GetURI())
if err != nil {
return nil, trace.Wrap(err)
}
// Clone and configure the transport.
tr, err := defaults.Transport()
if err != nil {
return nil, trace.Wrap(err)
}
tr.TLSClientConfig, err = configureTLS(c)
if err != nil {
return nil, trace.Wrap(err)
}
return &transport{
closeContext: ctx,
c: c,
uri: uri,
tr: tr,
ws: newWebsocketTransport(uri, tr.TLSClientConfig),
}, nil
}
// RoundTrip will rewrite the request, forward the request to the target
// application, emit an event to the audit log, then rewrite the response.
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
// Check if the request path needs re-writing. This occurs when the URI
// contains a path like http://localhost:8080/app/acme, but the request comes
// to https://publicAddr. In that case do a 302 to the correct path instead
// of doing path re-writing on all requests. This is a workaround to make
// sure Teleport does not break SPA.
if location, ok := t.needsPathRedirect(r); ok {
return &http.Response{
Status: http.StatusText(http.StatusFound),
StatusCode: http.StatusFound,
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Body: http.NoBody,
Header: http.Header{
"Location": []string{location},
},
TLS: r.TLS,
}, nil
}
// Perform any request rewriting needed before forwarding the request.
if err := t.rewriteRequest(r); err != nil {
return nil, trace.Wrap(err)
}
// Forward the request to the target application and emit an audit event.
resp, err := t.tr.RoundTrip(r)
if err != nil {
return nil, trace.Wrap(err)
}
// Emit the event to the audit log.
if err := t.emitAuditEvent(r, resp); err != nil {
return nil, trace.Wrap(err)
}
// Perform any response rewriting needed before returning the request.
if err := t.rewriteResponse(resp); err != nil {
return nil, trace.Wrap(err)
}
return resp, nil
}
// rewriteRequest applies any rewriting rules to the request before it's forwarded.
func (t *transport) rewriteRequest(r *http.Request) error {
// Update the target address of the request so it's forwarded correctly.
r.URL.Scheme = t.uri.Scheme
r.URL.Host = t.uri.Host
// Add headers from rewrite configuration.
if t.c.app.GetRewrite() != nil && len(t.c.app.GetRewrite().Headers) > 0 {
t.rewriteHeaders(r)
}
// Add in JWT headers.
r.Header.Set(teleport.AppJWTHeader, t.c.jwt)
r.Header.Set(teleport.AppCFHeader, t.c.jwt)
return nil
}
// rewriteHeaders applies headers rewrites from the application configuration.
func (t *transport) rewriteHeaders(r *http.Request) {
for _, header := range t.c.app.GetRewrite().Headers {
if common.IsReservedHeader(header.Name) {
t.c.log.Debugf("Not rewriting Teleport header %q.", header.Name)
continue
}
values, err := services.ApplyValueTraits(header.Value, t.c.traits)
if err != nil {
t.c.log.Debugf("Failed to apply traits to %q: %v.", header.Value, err)
continue
}
r.Header.Del(header.Name)
for _, value := range values {
switch http.CanonicalHeaderKey(header.Name) {
case teleport.HostHeader:
r.Host = value
default:
r.Header.Add(header.Name, value)
}
}
}
}
// needsPathRedirect checks if the request should be redirected to a different path.
// At the moment, the only time a redirect happens is if URI specified is not
// "/" and the public address being requested is "/".
func (t *transport) needsPathRedirect(r *http.Request) (string, bool) {
// If the URI for the application has no path specified, nothing to be done.
uriPath := path.Clean(t.uri.Path)
if uriPath == "." {
uriPath = "/"
}
if uriPath == "/" {
return "", false
}
// For simplicity, only support redirecting to the URI path if the root path
// is requested.
reqPath := path.Clean(r.URL.Path)
if reqPath == "." {
reqPath = "/"
}
if reqPath != "/" {
return "", false
}
u := url.URL{
Scheme: "https",
Host: net.JoinHostPort(t.c.app.GetPublicAddr(), t.c.publicPort),
Path: uriPath,
}
return u.String(), true
}
// rewriteResponse applies any rewriting rules to the response before returning it.
func (t *transport) rewriteResponse(resp *http.Response) error {
switch {
case t.c.app.GetRewrite() != nil && len(t.c.app.GetRewrite().Redirect) > 0:
err := t.rewriteRedirect(resp)
if err != nil {
return trace.Wrap(err)
}
default:
}
return nil
}
// rewriteRedirect applies redirect rules to the response.
func (t *transport) rewriteRedirect(resp *http.Response) error {
if isRedirect(resp.StatusCode) {
// Parse the "Location" header.
u, err := url.Parse(resp.Header.Get("Location"))
if err != nil {
return trace.Wrap(err)
}
// If the redirect location is one of the hosts specified in the list of
// redirects, rewrite the header.
if apiutils.SliceContainsStr(t.c.app.GetRewrite().Redirect, host(u.Host)) {
u.Scheme = "https"
u.Host = net.JoinHostPort(t.c.app.GetPublicAddr(), t.c.publicPort)
}
resp.Header.Set("Location", u.String())
}
return nil
}
// emitAuditEvent writes the request and response to audit stream.
func (t *transport) emitAuditEvent(req *http.Request, resp *http.Response) error {
appSessionRequestEvent := &apievents.AppSessionRequest{
Metadata: apievents.Metadata{
Type: events.AppSessionRequestEvent,
Code: events.AppSessionRequestCode,
},
Method: req.Method,
Path: req.URL.Path,
RawQuery: req.URL.RawQuery,
StatusCode: uint32(resp.StatusCode),
AppMetadata: apievents.AppMetadata{
AppURI: t.c.app.GetURI(),
AppPublicAddr: t.c.app.GetPublicAddr(),
AppName: t.c.app.GetName(),
},
}
if err := t.c.w.EmitAuditEvent(t.closeContext, appSessionRequestEvent); err != nil {
return trace.Wrap(err)
}
return nil
}
// configureTLS creates and configures a *tls.Config that will be used for
// mutual authentication.
func configureTLS(c *transportConfig) (*tls.Config, error) {
tlsConfig := utils.TLSConfig(c.cipherSuites)
// Don't verify the server's certificate if Teleport was started with
// the --insecure flag, or 'insecure_skip_verify' was specifically requested in
// the application config.
tlsConfig.InsecureSkipVerify = (lib.IsInsecureDevMode() || c.app.GetInsecureSkipVerify())
return tlsConfig, nil
}
// host returns the host from a host:port string.
func host(addr string) string {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return addr
}
return host
}
// isRedirect returns true if the status code is a 3xx code.
func isRedirect(code int) bool {
if code >= http.StatusMultipleChoices && code <= http.StatusPermanentRedirect {
return true
}
return false
}
// websocketTransport combines parameters for websockets transport.
//
// Implements forward.ReqRewriter.
type websocketTransport struct {
uri *url.URL
dialer forward.Dialer
}
// newWebsocketTransport returns transport that knows how to rewrite and
// dial websocket requests.
func newWebsocketTransport(uri *url.URL, tlsConfig *tls.Config) *websocketTransport {
return &websocketTransport{
uri: uri,
dialer: func(network, address string) (net.Conn, error) {
// Request is going to "wss://".
if uri.Scheme == "https" {
return tls.Dial(network, address, tlsConfig)
}
// Request is going to "ws://".
return net.Dial(network, address)
},
}
}
// Rewrite rewrites the websocket request.
func (r *websocketTransport) Rewrite(req *http.Request) {
// Update scheme and host to those of the target app's to make sure
// it's forwarded correctly.
req.URL.Scheme = "ws"
if r.uri.Scheme == "https" {
req.URL.Scheme = "wss"
}
req.URL.Host = r.uri.Host
req.Host = r.uri.Host
}