-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (46 loc) · 1.26 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
package main
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/stripe/smokescreen/cmd"
"github.com/stripe/smokescreen/pkg/smokescreen"
)
func main() {
conf, err := cmd.NewConfiguration(nil, nil)
if err != nil {
log.Fatal(err)
}
if conf == nil {
os.Exit(1)
}
password, ok := os.LookupEnv("PROXY_PASSWORD")
if !ok {
fmt.Println("missing environment variable: PROXY_PASSWORD")
os.Exit(1)
}
conf.RoleFromRequest = func(request *http.Request) (string, error) {
fail := func(err string) (string, error) { return "", fmt.Errorf(err) }
auth := strings.SplitN(request.Header.Get("Proxy-Authorization"), " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
return fail("authorization failed")
}
payload, _ := base64.StdEncoding.DecodeString(auth[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || pair[1] != password {
return fail("authorization failed")
}
return "authed", nil
}
conf.Healthcheck = http.HandlerFunc(OK)
smokescreen.StartWithConfig(conf, nil)
}
func OK(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}