-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (101 loc) · 2.63 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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package main
import (
"context"
"log"
"net"
"net/http"
"os"
"strings"
)
var udsAddr = "/var/run/envoy-uds/ext-auth.sock"
func main() {
if _, err := os.Stat(udsAddr); err == nil {
if err := os.RemoveAll(udsAddr); err != nil {
log.Fatalf("failed to remove: %v", err)
}
}
listener, err := net.Listen("unix", udsAddr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
defer listener.Close()
users := TestUsers()
// Define an HTTP handler
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Check(w, r, users)
})
// Start the ext auth HTTP server
log.Printf("Ext auth server is listening on %s\n", udsAddr)
go func() {
if err := http.Serve(listener, nil); err != nil {
log.Fatalf("Ext auth server failed: %v\n", err)
}
}()
// Start the health check HTTP server
http.HandleFunc("/healthz", healthCheckHandler)
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func Check(w http.ResponseWriter, r *http.Request, users Users) {
authorization := r.Header["Authorization"]
log.Println(r.Header)
log.Println(authorization)
if len(authorization) > 0 {
extracted := strings.Fields(authorization[0])
if len(extracted) == 2 && extracted[0] == "Bearer" {
valid, user := users.Check(extracted[1])
if valid {
w.Header().Add("x-current-user", user)
w.WriteHeader(http.StatusOK)
}
}
}
w.WriteHeader(http.StatusUnauthorized)
}
// Users holds a list of users.
type Users map[string]string
// Check checks if a key could retrieve a user from a list of users.
func (u Users) Check(key string) (bool, string) {
value, ok := u[key]
if !ok {
return false, ""
}
return ok, value
}
func TestUsers() Users {
return map[string]string{
"token1": "user1",
"token2": "user2",
"token3": "user3",
}
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
client := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", udsAddr)
},
},
}
req, err := http.NewRequest("GET", "http://unix/", nil)
if err != nil {
log.Printf("Failed to create request: %v\n", err)
return
}
req.Header.Set("authorization", "Bearer token1")
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Could not check: %v", err)
}
if resp != nil && resp.StatusCode == http.StatusOK {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusServiceUnavailable)
}
}