-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
524 lines (455 loc) · 13.3 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
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
package main
import (
"crypto/tls"
"encoding/gob"
"errors"
"fmt"
"log"
"math"
"net"
"net/http"
"os"
"regexp"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"github.com/Rudd-O/curvetls"
"github.com/anxiousmodernman/goth/gothic"
"github.com/asdine/storm"
"github.com/codegangsta/negroni"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
// TODO look into newer versions of grpcweb and wsproxy. Have they merged?
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/johanbrandhorst/protobuf/wsproxy"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/anxiousmodernman/co-chair/backend"
"github.com/anxiousmodernman/co-chair/config"
"github.com/anxiousmodernman/co-chair/grpcclient"
"github.com/anxiousmodernman/co-chair/proto/server"
"github.com/dchest/uniuri"
)
// Version is our software version.
var Version = "0.1.0"
var (
// Store is our sessions store.
// TODO: this doesn't have to be a global
Store *sessions.CookieStore
logger *logrus.Logger
)
func init() {
secret := os.Getenv("COCHAIR_COOKIESTORE_SECRET")
if secret == "" {
secret = uniuri.NewLen(64)
}
Store = sessions.NewCookieStore([]byte(secret))
logger = logrus.StandardLogger()
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
DisableSorting: true,
})
// set the grpc logger
grpclog.SetLoggerV2(grpclog.NewLoggerV2(logger.Out, logger.Out, logger.Out))
// set up sessions store
store := sessions.NewFilesystemStore(os.TempDir(), []byte(secret))
store.MaxLength(math.MaxInt64)
gothic.Store = store // TODO remove?
// prevent error:
// gob: type not registered for interface: map[string]interface {}
var t map[string]interface{}
gob.Register(t)
}
func main() {
app := cli.NewApp()
app.Version = Version
// shared flags
var (
conf = cli.StringFlag{
Name: "conf",
Usage: "path to config file",
}
)
dbFlag := cli.StringFlag{
Name: "db",
Usage: "path to boltdb file",
Value: "co-chair.db",
}
apiCert := cli.StringFlag{
Name: "apiCert",
Usage: "for grpc mgmt api: path to pem encoded tls certificate",
Value: "./cert.pem",
}
apiClientValidation := cli.BoolTFlag{
Name: "apiClientValidation",
Usage: "an api client's curvetls public keys must exist in database",
}
apiKey := cli.StringFlag{
Name: "apiKey",
Usage: "for grpc mgmt api: path to pem encoded tls private key",
Value: "./key.pem",
}
apiPort := cli.StringFlag{
Name: "apiPort",
Usage: "port number for grpc mgmt api",
}
webCert := cli.StringFlag{
Name: "webUICert",
Usage: "for web ui: path to pem encoded tls certificate",
Value: "./cert.pem",
}
webDomain := cli.StringFlag{
Name: "webUIDomain",
Usage: "for web ui: our fully-qualified domain name",
Value: "localhost",
EnvVar: "COCHAIR_WEBUI_DOMAIN",
}
webKey := cli.StringFlag{
Name: "webUIKey",
Usage: "for web ui: path to pem encoded tls private key",
Value: "./key.pem",
}
webPort := cli.StringFlag{
Name: "webUIPort",
Usage: "port number for web ui",
Value: "2016",
}
webAssetsPath := cli.StringFlag{
Name: "webAssetsPath",
Usage: "serve given directory if provided, else use binary-embedded assets",
}
proxyCert := cli.StringFlag{
Name: "proxyCert",
Usage: "for proxy: path to pem encoded tls certificate",
Value: "./cert.pem",
}
proxyKey := cli.StringFlag{
Name: "proxyKey",
Usage: "for proxy: path to pem encoded tls private key",
Value: "./key.pem",
}
proxyPort := cli.StringFlag{
Name: "proxyPort",
Usage: "port number for http proxy",
Value: "8080",
}
proxyInsecurePort := cli.StringFlag{
Name: "proxyInsecurePort",
Usage: "if provided, start a plaintext HTTP proxy on this port",
}
auth0ClientID := cli.StringFlag{
Name: "auth0ClientID",
Usage: "Auth0 Client ID for this co-chair instance",
EnvVar: "COCHAIR_AUTH0_CLIENTID",
}
auth0Domain := cli.StringFlag{
Name: "auth0Domain",
Usage: "Auth0 Domain for this co-chair instance",
EnvVar: "COCHAIR_AUTH0_DOMAIN",
}
auth0Secret := cli.StringFlag{
Name: "auth0Secret",
Usage: "Auth0 Secret",
EnvVar: "COCHAIR_AUTH0_SECRET",
}
bypassAuth0 := cli.BoolFlag{
Name: "bypassAuth0",
Usage: "totally bypass auth0; insecure development mode",
}
// client-only flags
var (
upstreamDomain = cli.StringFlag{
Name: "domain",
Usage: "an upstream domain; pair with --\"ips\"",
}
upstreamIPs = cli.StringSliceFlag{
Name: "ips",
Usage: "comma-separated list of the real host:port of an upstream; pair with --\"domain\"",
}
)
app.Commands = []cli.Command{
cli.Command{
Name: "example-config",
Usage: "print an example co-chair TOML config to stdout",
Action: func(_ *cli.Context) error {
fmt.Println(config.ExampleConfig)
return nil
},
},
cli.Command{
Name: "gen-client-keys",
Usage: "generate a (curvetls) client keypair and add public key to the keystore; co-chair cannot be running",
Flags: []cli.Flag{conf, dbFlag},
Action: func(ctx *cli.Context) error {
// name is the identifier of our keypair
name := ctx.Args().First()
return genClientKeypair(name, ctx.String("db"))
},
},
cli.Command{
Name: "put",
Usage: "add an upstream to the proxy",
Flags: []cli.Flag{conf, upstreamDomain, upstreamIPs},
Action: func(ctx *cli.Context) error {
clientConf, err := grpcclient.NewClientConfig(ctx.String("conf"))
if err != nil {
return err
}
c, err := grpcclient.NewCoChairClient(clientConf)
if err != nil {
return err
}
return c.Put(ctx.String("domain"), ctx.StringSlice("ips"))
},
},
cli.Command{
Name: "serve",
Usage: "run co-chair",
Flags: []cli.Flag{dbFlag, apiCert, apiClientValidation, apiKey, apiPort,
webCert, webDomain, webKey, webPort, webAssetsPath,
proxyCert, proxyKey, proxyPort, proxyInsecurePort,
auth0ClientID, auth0Domain, auth0Secret, bypassAuth0,
conf},
Action: func(ctx *cli.Context) error {
conf, err := config.FromCLIOpts(ctx)
if err != nil {
return err
}
return serve(conf)
},
},
cli.Command{
Name: "state",
Usage: "report the proxy's upstream configuration",
Flags: []cli.Flag{conf, upstreamDomain},
Action: func(ctx *cli.Context) error {
clientConf, err := grpcclient.NewClientConfig(ctx.String("conf"))
if err != nil {
return err
}
c, err := grpcclient.NewCoChairClient(clientConf)
if err != nil {
return err
}
return c.State(ctx.String("domain"))
},
},
cli.Command{
Name: "systemd-install",
Usage: "installs a systemd unit file and config directory",
Flags: []cli.Flag{conf},
Action: func(ctx *cli.Context) error {
conf, err := config.FromCLIOpts(ctx)
if err != nil {
return err
}
return config.SystemDInstall(conf)
},
},
}
if err := app.Run(os.Args); err != nil {
logger.Fatal(err)
}
}
func serve(conf config.Config) error {
// TODO: construct storm.DB here and pass to constructors instead of
// grabbing the field off the Proxy.
// NewProxy gives us a Proxy, our concrete implementation of the
// interface generated by the grpc protobuf compiler.
px, err := backend.NewProxy(conf.DBPath)
if err != nil {
log.Fatalf("proxy init: %v", err)
}
// KeyStore is an interface, so it is nil if unset.
var keystore curvetls.KeyStore
if conf.APIClientValidation {
keystore = &backend.StormKeystore{DB: px.DB}
}
// curvetls transport security
serverPub, serverPriv, err := backend.RetrieveServerKeys(px.DB)
if err != nil {
return fmt.Errorf("could not retrieve server's keypair %v", err)
}
creds := curvetls.NewGRPCServerCredentials(serverPub, serverPriv, keystore)
grpcOnlyServer := grpc.NewServer(grpc.Creds(creds))
server.RegisterProxyServer(grpcOnlyServer, px)
// gRPC over websockets management API
gs := grpc.NewServer()
server.RegisterProxyServer(gs, px)
wrappedServer := grpcweb.WrapServer(gs)
webTLScreds, err := credentials.NewClientTLSFromFile(conf.WebUICert, "")
if err != nil {
return errors.New("missing web ui tls cert")
}
wsproxy := wsproxy.WrapServer(
wrappedServer,
wsproxy.WithLogger(logger),
wsproxy.WithTransportCredentials(webTLScreds))
// Note: routes are evaluated in the order they're defined.
p := mux.NewRouter()
// Set up our authentication handler, with optional bypass
authHandler := IsAuthenticated
if conf.BypassAuth0 {
logger.Info("insecure configuration: bypassing auth0 protection for webUI")
authHandler = func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
next(w, r)
}
}
p.Handle("/login", negroni.New(
setConf(conf),
negroni.HandlerFunc(withLog),
negroni.Wrap(http.HandlerFunc(loginLink)),
)).Methods("GET")
p.Handle("/auth/{provider}/callback", negroni.New(
setConf(conf),
negroni.HandlerFunc(withLog),
negroni.Wrap(http.HandlerFunc(oauthCallbackHandler)),
)).Methods("GET")
p.Handle("/auth/{provider}", negroni.New(
setConf(conf),
negroni.HandlerFunc(withLog),
negroni.Wrap(http.HandlerFunc(loginHandler)),
)).Methods("GET")
p.Handle("/logout/{provider}", negroni.New(
setConf(conf),
negroni.HandlerFunc(withLog),
negroni.Wrap(http.HandlerFunc(logoutHandler)),
)).Methods("GET")
allowCORS := handlers.CORS(
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}),
handlers.AllowedHeaders([]string{"Content-Type", "X-Grpc-Web", "content-type", "x-grpc-web"}),
)
// All websockets requests are POSTs to some {method} on this route.
p.Handle("/web.Proxy/{method}", negroni.New(
setConf(conf),
negroni.HandlerFunc(withLog),
negroni.HandlerFunc(authHandler),
negroni.Wrap(allowCORS(websocketsProxy(wsproxy))),
)).Methods("POST", "OPTIONS")
// Dynamically construct static handlers
// serve frontend from embedded binary assets
if conf.WebAssetsPath != "" {
logger.Info("WebAssetsPath: ", conf.WebAssetsPath)
p.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
match, _ := regexp.MatchString("/.*", r.URL.Path)
return match
}).Handler(
negroni.New(
setConf(conf),
negroni.HandlerFunc(withLog),
negroni.HandlerFunc(authHandler),
negroni.Wrap(staticFromDiskHandler(conf.WebAssetsPath)),
)).Methods("GET")
} else {
p.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
match, _ := regexp.MatchString("/.*", r.URL.Path)
return match
}).Handler(
negroni.New(
setConf(conf),
negroni.HandlerFunc(withLog),
negroni.HandlerFunc(authHandler),
negroni.Wrap(http.HandlerFunc(staticHandler)),
)).Methods("GET")
}
// else we serve the "static" folder
if true {
walker := func(r *mux.Route, rt *mux.Router, anc []*mux.Route) error {
rx, _ := r.GetPathRegexp()
tmpl, _ := r.GetPathTemplate()
logger.Infof("route regex: %v", rx)
logger.Infof("route template: %v", tmpl)
return nil
}
p.Walk(mux.WalkFunc(walker))
}
// Web server for our Vecty/GopherJS management UI
httpsSrv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", conf.WebUIPort),
// TODO fix me, make me configurable based on local/prod builds
Handler: handlers.CORS(handlers.AllowedOrigins([]string{"*"}))(p),
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: 120 * time.Second,
TLSConfig: &tls.Config{
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
},
}
grpcAPI := make(chan error)
proxy := make(chan error)
pureGRPC := make(chan error)
// Only start an external API listener if we're validating client keys.
if conf.APIClientValidation {
logger.Infof("starting external gRPC listener on port %s", conf.APIPort)
lis, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", conf.APIPort))
if err != nil {
return fmt.Errorf("listener error: %v", err)
}
go func() { pureGRPC <- grpcOnlyServer.Serve(lis) }()
}
// Start our web UI listener
go func() {
logger.Info("Serving Web UI on https://" + httpsSrv.Addr)
grpcAPI <- httpsSrv.ListenAndServeTLS(conf.WebUICert, conf.WebUIKey)
}()
fwdr, err := backend.NewTCPForwarder(
backend.WithDB(px.DB),
backend.WithAddr(fmt.Sprintf("0.0.0.0:%s", conf.ProxyPort)),
backend.WithLogger(logger),
)
if err != nil {
return err
}
// TCPForwarder routine
if err := fwdr.Start(); err != nil {
return fmt.Errorf("start TCPFowarder: %v", err)
}
if conf.ProxyInsecurePort != "" {
// should we even support this?
}
for {
select {
case err := <-grpcAPI:
return err
case err := <-proxy:
return err
case err := <-pureGRPC:
return err
}
}
}
func genClientKeypair(name, dbPath string) error {
// NOTE this function directly accesses the database. It's a command line
// feature, and assumes local access to the database file.
db, err := storm.Open(dbPath)
if err != nil {
return err
}
serverPub, _, err := backend.RetrieveServerKeys(db)
if err != nil {
return err
}
priv, pub, err := curvetls.GenKeyPair()
if err != nil {
return err
}
clientKP := backend.KeyPair{Name: name, Pub: pub.String()}
if err := db.Save(&clientKP); err != nil {
return err
}
msg := `The following are new curveTLS public and private keys.\n`
fmt.Println(msg)
fmt.Println("client public key (new):\t", pub)
fmt.Println("client private key (new):\t", priv)
fmt.Println("server public key:\t", serverPub)
return nil
}