This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
116 lines (99 loc) · 2.66 KB
/
main.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
package main
import (
"crypto/x509"
"io/ioutil"
"log"
"net/http"
"time"
"code.cloudfoundry.org/loggregator/metricemitter"
"code.cloudfoundry.org/loggregator/plumbing"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"code.cloudfoundry.org/loggregator/trafficcontroller/app"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
conf, err := app.LoadConfig()
if err != nil {
log.Panicf("Unable to load config: %s", err)
}
credentials, err := plumbing.NewClientCredentials(
conf.GRPC.CertFile,
conf.GRPC.KeyFile,
conf.GRPC.CAFile,
"metron",
)
if err != nil {
log.Fatalf("Could not use GRPC creds for client: %s", err)
}
// metric-documentation-v2: setup function
metricClient, err := metricemitter.NewClient(
conf.Agent.GRPCAddress,
metricemitter.WithGRPCDialOptions(grpc.WithTransportCredentials(credentials)),
metricemitter.WithOrigin("loggregator.trafficcontroller"),
metricemitter.WithPulseInterval(conf.MetricEmitterInterval),
metricemitter.WithSourceID("traffic_controller"),
)
if err != nil {
log.Fatalf("Couldn't connect to metric emitter: %s", err)
}
tc := app.NewTrafficController(
conf,
conf.DisableAccessControl,
metricClient,
uaaHTTPClient(conf),
ccHTTPClient(conf),
)
tc.Start()
}
func uaaHTTPClient(conf *app.Config) *http.Client {
tlsConfig := plumbing.NewTLSConfig()
tlsConfig.InsecureSkipVerify = conf.SkipCertVerify
tlsConfig.RootCAs = loadUaaCA(conf)
transport := &http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
}
return &http.Client{
Timeout: 20 * time.Second,
Transport: transport,
}
}
func ccHTTPClient(conf *app.Config) *http.Client {
tlsConfig, err := plumbing.NewClientMutualTLSConfig(
conf.CCTLSClientConfig.CertFile,
conf.CCTLSClientConfig.KeyFile,
conf.CCTLSClientConfig.CAFile,
conf.CCTLSClientConfig.ServerName,
)
if err != nil {
log.Fatalf("Unable to create CC HTTP Client: %s", err)
}
tlsConfig.InsecureSkipVerify = conf.SkipCertVerify
transport := &http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
}
return &http.Client{
Timeout: 20 * time.Second,
Transport: transport,
}
}
func loadUaaCA(conf *app.Config) *x509.CertPool {
if conf.UaaHost != "" {
caCert, err := ioutil.ReadFile(conf.UaaCACert)
if err != nil {
log.Fatalf("Failed to read UAA CA certificate: %s", err)
}
certPool := x509.NewCertPool()
ok := certPool.AppendCertsFromPEM(caCert)
if !ok {
log.Fatal("Failed to parse UAA CA certificate.")
}
return certPool
}
return nil
}