-
Notifications
You must be signed in to change notification settings - Fork 51
/
http.go
534 lines (460 loc) · 14 KB
/
http.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package httpproxy
import (
"context"
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/applicationproxy/markedconn"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/nfqdatapath/tokenaccessor"
"github.com/aporeto-inc/trireme-lib/controller/pkg/pucontext"
"github.com/aporeto-inc/trireme-lib/controller/pkg/secrets"
"github.com/aporeto-inc/trireme-lib/controller/pkg/urisearch"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/aporeto-inc/trireme-lib/utils/cache"
"go.uber.org/zap"
"github.com/dgrijalva/jwt-go"
"github.com/vulcand/oxy/forward"
)
// JWTClaims is the structure of the claims we are sending on the wire.
type JWTClaims struct {
jwt.StandardClaims
SourceID string
Scopes []string
Profile []string
}
// Config maintains state for proxies connections from listen to backend.
type Config struct {
cert *tls.Certificate
ca *x509.CertPool
keyPEM string
certPEM string
secrets secrets.Secrets
tokenaccessor tokenaccessor.TokenAccessor
collector collector.EventCollector
puContext string
puFromIDCache cache.DataStore
exposedAPICache cache.DataStore
dependentAPICache cache.DataStore
jwtCache cache.DataStore
applicationProxy bool
mark int
server *http.Server
fwd *forward.Forwarder
fwdTLS *forward.Forwarder
sync.RWMutex
}
// NewHTTPProxy creates a new instance of proxy reate a new instance of Proxy
func NewHTTPProxy(
tp tokenaccessor.TokenAccessor,
c collector.EventCollector,
puContext string,
puFromIDCache cache.DataStore,
caPool *x509.CertPool,
exposedAPICache cache.DataStore,
dependentAPICache cache.DataStore,
jwtCache cache.DataStore,
applicationProxy bool,
mark int,
secrets secrets.Secrets,
) *Config {
return &Config{
collector: c,
tokenaccessor: tp,
puFromIDCache: puFromIDCache,
puContext: puContext,
ca: caPool,
exposedAPICache: exposedAPICache,
dependentAPICache: dependentAPICache,
applicationProxy: applicationProxy,
jwtCache: jwtCache,
mark: mark,
secrets: secrets,
}
}
// RunNetworkServer runs an HTTP network server. If TLS is needed, the
// listener should be already a TLS listener.
func (p *Config) RunNetworkServer(ctx context.Context, l net.Listener, encrypted bool) error {
p.Lock()
defer p.Unlock()
if p.server != nil {
return fmt.Errorf("Server already running")
}
// If its an encrypted, wrap it in a TLS context.
if encrypted {
config := &tls.Config{
GetCertificate: p.GetCertificateFunc(),
ClientAuth: tls.RequestClientCert,
}
l = tls.NewListener(l, config)
}
// Create an encrypted downstream transport
encryptedTransport := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: p.ca,
},
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
raddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
conn, err := markedconn.DialMarkedTCP("tcp", nil, raddr, p.mark)
if err != nil {
return nil, err
}
tlsConn := tls.Client(conn, &tls.Config{
ServerName: getServerName(addr),
RootCAs: p.ca,
InsecureSkipVerify: false,
})
return tlsConn, nil
},
}
// Create an unencrypted transport for talking to the application
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
raddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
conn, err := markedconn.DialMarkedTCP("tcp", nil, raddr, p.mark)
if err != nil {
return nil, fmt.Errorf("Failed to dial remote: %s", err)
}
return conn, nil
},
}
var err error
p.fwdTLS, err = forward.New(forward.RoundTripper(encryptedTransport))
if err != nil {
return fmt.Errorf("Cannot initialize encrypted transport: %s", err)
}
p.fwd, err = forward.New(forward.RoundTripper(transport))
if err != nil {
return fmt.Errorf("Cannot initialize unencrypted transport: %s", err)
}
processor := p.processAppRequest
if !p.applicationProxy {
processor = p.processNetRequest
}
p.server = &http.Server{
Handler: http.HandlerFunc(processor),
}
go func() {
<-ctx.Done()
p.server.Close() // nolint
}()
go p.server.Serve(l) // nolint
return nil
}
// ShutDown terminates the server.
func (p *Config) ShutDown() error {
return p.server.Close()
}
// UpdateSecrets updates the secrets
func (p *Config) UpdateSecrets(cert *tls.Certificate, caPool *x509.CertPool, s secrets.Secrets, certPEM, keyPEM string) {
p.Lock()
defer p.Unlock()
p.cert = cert
p.ca = caPool
p.secrets = s
p.certPEM = certPEM
p.keyPEM = keyPEM
}
// GetCertificateFunc implements the TLS interface for getting the certificate. This
// allows us to update the certificates of the connection on the fly.
func (p *Config) GetCertificateFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
p.RLock()
defer p.RUnlock()
if p.cert != nil {
return p.cert, nil
}
return nil, fmt.Errorf("no cert available")
}
}
func (p *Config) processAppRequest(w http.ResponseWriter, r *http.Request) {
pu, err := p.puFromIDCache.Get(p.puContext)
if err != nil {
zap.L().Error("Cannot find policy, dropping request")
http.Error(w, fmt.Sprintf("Cannot handle request: %s", err), http.StatusInternalServerError)
}
puContext := pu.(*pucontext.PUContext)
token, err := p.createClientToken(puContext)
if err != nil {
http.Error(w, fmt.Sprintf("Cannot handle request: %s", err), http.StatusForbidden)
return
}
if p.isSecretsRequest(w, r) {
return
}
r.URL, err = url.ParseRequestURI("http://" + r.Host)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid host name: %s ", err), http.StatusUnprocessableEntity)
return
}
r.Header.Add("X-APORETO-KEY", string(p.secrets.TransmittedKey()))
r.Header.Add("X-APORETO-AUTH", token)
p.fwdTLS.ServeHTTP(w, r)
}
func (p *Config) processNetRequest(w http.ResponseWriter, r *http.Request) {
record := &collector.FlowRecord{
ContextID: p.puContext,
Destination: &collector.EndPoint{
URI: r.RequestURI,
Type: collector.PU,
},
Source: &collector.EndPoint{
Type: collector.PU,
},
Action: policy.Reject,
}
defer p.collector.CollectFlowEvent(record)
pctx, err := p.puFromIDCache.Get(p.puContext)
if err != nil {
zap.L().Error("Cannot find policy, dropping request")
http.Error(w, fmt.Sprintf("Cannot handle request: %s", err), http.StatusForbidden)
return
}
record.Tags = pctx.(*pucontext.PUContext).Annotations()
record.Destination.ID = pctx.(*pucontext.PUContext).ManagementID()
token := r.Header.Get("X-APORETO-AUTH")
if token != "" {
r.Header.Del("X-APORETO-AUTH")
}
key := r.Header.Get("X-APORETO-KEY")
if key != "" {
r.Header.Del("X-APORETO-LEN")
}
port := "80"
if strings.Contains(r.Host, ":") {
_, port, err = net.SplitHostPort(r.Host)
if err != nil {
zap.L().Error("Invalid HTTP port parameter", zap.Error(err))
http.Error(w, fmt.Sprintf("Invalid HTTP port parameter: %s", err), http.StatusUnprocessableEntity)
return
}
}
_port, _ := strconv.Atoi(port)
record.Destination.Port = uint16(_port)
data, err := p.exposedAPICache.Get(p.puContext)
if err != nil {
zap.L().Error("API service not recognized", zap.Error(err))
http.Error(w, fmt.Sprintf("API service not recognized: %s", err), http.StatusUnprocessableEntity)
return
}
apiCaches := data.(map[string]*urisearch.APICache)
apiCache, ok := apiCaches[port]
if !ok {
zap.L().Error("Uknown service", zap.Error(err))
http.Error(w, fmt.Sprintf("Unknown service: %s", err), http.StatusUnprocessableEntity)
return
}
jwtcache, err := p.jwtCache.Get(p.puContext)
if err != nil {
zap.L().Warn("No JWT cache found for this pu", zap.Error(err))
}
jwtCert, ok := jwtcache.(map[string]*x509.Certificate)[port]
if !ok {
zap.L().Warn("No JWT found for this port")
}
found, t := apiCache.Find(r.Method, r.RequestURI)
if !found {
zap.L().Error("Uknown or unauthorized service", zap.Error(err))
http.Error(w, fmt.Sprintf("Unknown or unauthorized service"), http.StatusForbidden)
return
}
userAttributes := parseUserAttributes(r, jwtCert)
if len(userAttributes) > 0 {
userRecord := &collector.UserRecord{Claims: userAttributes}
p.collector.CollectUserEvent(userRecord)
record.Source.UserID = userRecord.ID
}
if err = p.parseClientToken(pctx.(*pucontext.PUContext), token, key, t.([]string), userAttributes, record); err != nil {
zap.L().Error("Unauthorized request", zap.Error(err))
http.Error(w, fmt.Sprintf("Unauthorized access: %s", err), http.StatusUnauthorized)
return
}
r.URL, err = url.ParseRequestURI("http://localhost:" + port)
if err != nil {
zap.L().Error("Invalid HTTP Host parameter", zap.Error(err))
http.Error(w, fmt.Sprintf("Invalid HTTP Host parameter: %s", err), http.StatusUnprocessableEntity)
return
}
record.Action = policy.Accept
p.fwd.ServeHTTP(w, r)
}
func (p *Config) createClientToken(puContext *pucontext.PUContext) (string, error) {
claims := &JWTClaims{
StandardClaims: jwt.StandardClaims{
Issuer: p.server.Addr,
ExpiresAt: time.Now().Add(10 * time.Second).Unix(),
},
Profile: puContext.Identity().Tags,
Scopes: puContext.Scopes(),
SourceID: puContext.ManagementID(),
}
return jwt.NewWithClaims(jwt.SigningMethodES256, claims).SignedString(p.secrets.EncodingKey())
}
func (p *Config) parseClientToken(puContext *pucontext.PUContext, token string, txtKey string, apitags []string, userAttributes []string, record *collector.FlowRecord) error {
for _, user := range userAttributes {
for _, a := range apitags {
if user == a {
return nil
}
}
}
key, err := p.secrets.VerifyPublicKey([]byte(txtKey))
if err != nil {
return fmt.Errorf("Invalid Service Token")
}
claims := &JWTClaims{}
_, err = jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
ekey, ok := key.(*ecdsa.PublicKey)
if !ok {
return nil, fmt.Errorf("Invalid key")
}
return ekey, nil
})
if err != nil {
return fmt.Errorf("Error parsing token: %s", err)
}
record.Source.ID = claims.SourceID
for _, c := range claims.Profile {
for _, a := range apitags {
if a == c {
return nil
}
}
}
for _, c := range claims.Scopes {
for _, a := range apitags {
if a == c {
return nil
}
}
}
zap.L().Warn("No match found in API token",
zap.Strings("User Attributes", userAttributes),
zap.Strings("API Policy", apitags),
zap.Strings("PU Claims", claims.Profile),
zap.Strings("PU Scopes", claims.Scopes),
)
return fmt.Errorf("Not found")
}
func (p *Config) isSecretsRequest(w http.ResponseWriter, r *http.Request) bool {
data, err := p.dependentAPICache.Get(p.puContext)
if err != nil {
http.Error(w, fmt.Sprintf("Cannot handle request - unknown dependencies: %s", err), http.StatusForbidden)
return false
}
port := "80"
host := r.Host
if strings.Contains(r.Host, ":") {
host, port, err = net.SplitHostPort(r.Host)
if err != nil {
zap.L().Error("Invalid HTTP port parameter", zap.Error(err))
http.Error(w, fmt.Sprintf("Invalid HTTP port parameter: %s", err), http.StatusUnprocessableEntity)
return false
}
}
apiCache, ok := data.(map[string]*urisearch.APICache)[host+":"+port]
if !ok {
return false
}
// Look for a local request for certificates
if found, _ := apiCache.Find(r.Method, r.RequestURI); !found {
return false
}
switch r.RequestURI {
case "/certificate":
if _, err := w.Write([]byte(p.certPEM)); err != nil {
zap.L().Error("Unable to write response")
}
case "/key":
if _, err := w.Write([]byte(p.keyPEM)); err != nil {
zap.L().Error("Unable to write response")
}
default:
http.Error(w, fmt.Sprintf("Uknown"), http.StatusBadRequest)
}
return true
}
func getServerName(addr string) string {
parts := strings.Split(addr, ":")
if len(parts) == 2 {
return parts[0]
}
return addr
}
// InternalMidgardClaims HACK: Remove
type InternalMidgardClaims struct {
Realm string `json:"realm"`
Data map[string]string `json:"data"`
jwt.StandardClaims
}
func parseUserAttributes(r *http.Request, cert *x509.Certificate) []string {
attributes := []string{}
for _, cert := range r.TLS.PeerCertificates {
attributes = append(attributes, "user="+cert.Subject.CommonName)
for _, email := range cert.EmailAddresses {
attributes = append(attributes, "email="+email)
}
}
authorization := r.Header.Get("Authorization")
if len(authorization) < 7 {
return attributes
}
authorization = strings.TrimPrefix(authorization, "Bearer ")
if len(authorization) == 0 {
return attributes
}
// Use a generic claims map. This allows us to customize the user attributes
// by providing the right scopes in the API policy.
claims := &jwt.MapClaims{}
token, err := jwt.ParseWithClaims(authorization, claims, func(token *jwt.Token) (interface{}, error) {
switch token.Method {
case token.Method.(*jwt.SigningMethodECDSA):
return cert.PublicKey.(*ecdsa.PublicKey), nil
case token.Method.(*jwt.SigningMethodRSA):
return cert.PublicKey.(*rsa.PublicKey), nil
default:
return nil, fmt.Errorf("Unknown signing method")
}
})
// We can't decode it. Just ignore the user attributes at this point.
if err != nil || token == nil {
zap.L().Warn("Identified toke, but it is invalid", zap.Error(err))
return attributes
}
if !token.Valid {
return attributes
}
for k, v := range *claims {
if slice, ok := v.([]string); ok {
for _, data := range slice {
attributes = append(attributes, k+"="+data)
}
}
if attr, ok := v.(string); ok {
attributes = append(attributes, k+"="+attr)
}
if kv, ok := v.(map[string]interface{}); ok {
for key, value := range kv {
if attr, ok := value.(string); ok {
attributes = append(attributes, k+":"+key+"="+attr)
}
}
}
}
return attributes
}