forked from stripe/smokescreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (48 loc) · 1.6 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
package main
import (
"log"
"net/http"
"github.com/sirupsen/logrus"
"github.com/stripe/smokescreen/cmd"
"github.com/stripe/smokescreen/pkg/smokescreen"
)
// This default implementation of RoleFromRequest uses the CommonName of the
// client's certificate. If no certificate is provided, the AllowMissingRole
// configuration option will control whether the request is rejected, or the
// default ACL is applied.
//
//lint:ignore U1000 unused default function
func defaultRoleFromRequest(req *http.Request) (string, error) {
if req.TLS == nil {
return "", smokescreen.MissingRoleError("defaultRoleFromRequest requires TLS")
}
if len(req.TLS.PeerCertificates) == 0 {
return "", smokescreen.MissingRoleError("client did not provide certificate")
}
return req.TLS.PeerCertificates[0].Subject.CommonName, nil
}
func convexRoleFromRequest(req *http.Request) (string, error) {
backend := req.Header.Get("Proxy-Authorization")
if backend != "" {
req.Header.Del("Proxy-Authorization")
return backend, nil
}
return "", nil
}
func main() {
conf, err := cmd.NewConfiguration(nil, nil)
if err != nil {
logrus.Fatalf("Could not create configuration: %v", err)
} else if conf != nil {
conf.RoleFromRequest = convexRoleFromRequest
conf.Log.Formatter = &logrus.JSONFormatter{}
adapter := &smokescreen.Log2LogrusWriter{
Entry: conf.Log.WithField("stdlog", "1"),
}
// Set the standard logger to use our logger's writer as output.
log.SetOutput(adapter)
log.SetFlags(0)
smokescreen.StartWithConfig(conf, nil)
}
// Otherwise, --help or --version was passed and handled by NewConfiguration, so do nothing
}