-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
223 lines (185 loc) · 5.15 KB
/
proxy.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
package proxy
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"time"
"github.com/covidtrace/jwt"
"github.com/covidtrace/utils/env"
httputils "github.com/covidtrace/utils/http"
"github.com/go-redis/redis/v7"
redisrate "github.com/go-redis/redis_rate/v8"
)
type backend struct {
qph int
proxy httputil.ReverseProxy
}
func buildDirector(b string) func(r *http.Request) {
bu, err := url.Parse(b)
if err != nil {
panic(err)
}
return func(r *http.Request) {
r.URL.Scheme = bu.Scheme
r.URL.Host = bu.Host
r.Host = bu.Host
if jwt, err := getGoogleJWT(b); err == nil {
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt))
}
}
}
func newBackend(prefix string) backend {
endpoint := env.MustGet(fmt.Sprintf("%s_BACKEND", prefix))
qph, err := strconv.Atoi(env.MustGet(fmt.Sprintf("%s_QPH", prefix)))
if err != nil {
panic(err)
}
return backend{
qph: qph,
proxy: httputil.ReverseProxy{
Director: buildDirector(endpoint),
},
}
}
var issuer *jwt.Issuer
var notary backend
var elevatedNotary backend
var operator backend
var emails backend
var limiter *redisrate.Limiter
func getGoogleJWT(audience string) (string, error) {
req, err := http.NewRequest(
"GET",
fmt.Sprintf(
"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=%s",
url.QueryEscape(audience),
),
nil,
)
if err != nil {
return "", err
}
req.Header.Set("Metadata-Flavor", "Google")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func init() {
dur, err := time.ParseDuration("1h")
if err != nil {
panic(err)
}
issuer = jwt.NewIssuer([]byte(env.MustGet("JWT_SIGNING_KEY")), "covidtrace/operator", "covidtrace/token", dur)
notary = newBackend("NOTARY")
elevatedNotary = newBackend("ELEVATED_NOTARY")
operator = newBackend("OPERATOR")
emails = newBackend("EMAILS")
rc := redis.NewClient(&redis.Options{
Addr: env.MustGet("REDIS_HOST"),
})
limiter = redisrate.NewLimiter(rc)
}
func checkAllow(key string, qph int) (bool, error) {
res, err := limiter.Allow(key, redisrate.PerHour(qph))
if err != nil {
return false, err
}
return res.Allowed, nil
}
func checkAllowReq(prefix string, qph int, r *http.Request) (bool, error) {
return checkAllow(fmt.Sprintf("%s/%s", prefix, r.Header.Get("X-Forwarded-For")), qph)
}
// Notary is the cloud function that handles requests to the notary service, rate limiting
// using the JWT hash key
func Notary(w http.ResponseWriter, r *http.Request) {
authorization, err := httputils.GetAuthorization(r, "bearer")
if err != nil {
httputils.ReplyError(w, err, http.StatusUnauthorized)
return
}
claims, err := issuer.Validate(authorization)
if err != nil {
httputils.ReplyError(w, err, http.StatusUnauthorized)
return
}
hash := claims.Hash
if hash == "" {
httputils.ReplyError(w, errors.New("Missing hash"), http.StatusUnauthorized)
return
}
allowed, err := checkAllow(fmt.Sprintf("%s/%s", "notary", hash), notary.qph)
if err != nil {
httputils.ReplyInternalServerError(w, err)
return
}
if !allowed {
httputils.ReplyError(w, errors.New("Rate limit exceeded"), http.StatusTooManyRequests)
return
}
notary.proxy.ServeHTTP(w, r)
}
// ElevatedNotary is the cloud function that handles requests to the elevated notary
// service, ensuring the token is elevated and rate limiting using the JWT hash key
func ElevatedNotary(w http.ResponseWriter, r *http.Request) {
authorization, err := httputils.GetAuthorization(r, "bearer")
if err != nil {
httputils.ReplyError(w, err, http.StatusUnauthorized)
return
}
claims, err := issuer.WithAud("covidtrace/elevated").Validate(authorization)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if claims.Role != "elevated_user" {
httputils.ReplyError(w, errors.New("role is not `elevated_user`"), http.StatusUnauthorized)
return
}
allowed, err := checkAllow(fmt.Sprintf("%s/%s", "elevatedNotary", claims.Identifier), elevatedNotary.qph)
if err != nil {
httputils.ReplyInternalServerError(w, err)
return
}
if !allowed {
httputils.ReplyError(w, errors.New("Rate limit exceeded"), http.StatusTooManyRequests)
return
}
elevatedNotary.proxy.ServeHTTP(w, r)
}
// Operator handles proxying requests to the Operator service, rate limiting by X-Forwarded-For
func Operator(w http.ResponseWriter, r *http.Request) {
allowed, err := checkAllowReq("operator", operator.qph, r)
if err != nil {
httputils.ReplyInternalServerError(w, err)
return
}
if !allowed {
httputils.ReplyError(w, errors.New("Rate limit exceeded"), http.StatusTooManyRequests)
return
}
operator.proxy.ServeHTTP(w, r)
}
// Emails proxies requests to the internal email service
func Emails(w http.ResponseWriter, r *http.Request) {
allowed, err := checkAllowReq("emails", emails.qph, r)
if err != nil {
httputils.ReplyInternalServerError(w, err)
return
}
if !allowed {
httputils.ReplyError(w, errors.New("Rate limit exceeded"), http.StatusTooManyRequests)
return
}
emails.proxy.ServeHTTP(w, r)
}