-
Notifications
You must be signed in to change notification settings - Fork 0
/
lokiproxy.go
590 lines (531 loc) · 14.3 KB
/
lokiproxy.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/remram44/lokiproxy/internal/config"
"github.com/remram44/lokiproxy/internal/parser"
)
// GET /loki/api/v1/query
// query params to change: query
// query params to pass: limit, time, direction
// GET /loki/api/v1/query_range
// query params to change: query
// query params to pass: limit, start, end, since, step, interval, direction
// GET /loki/api/v1/series
// query params to change: match[]
// query params to pass: start, end, since
// can be POST form-urlencoded
// GET /loki/api/v1/tail
// query params to change: query
// query params to pass: delay_for, limit, start
// GET /loki/api/v1/labels
// query params to pass: start, end, since
// GET /loki/api/v1/label/<name>/values
// query params to change/add: query
// query params to pass: start, end, since
// GET /loki/api/v1/index/stats
// query params to change: query
// query params to pass: start, end
// can be POST form-urlencoded
var proxyClient *http.Client
var lokiUrl url.URL
var oidcVerifier *oidc.IDTokenVerifier
var identityMap *config.FileMultiMap
var allowAlerts bool = false
func main() {
ctx, cancelCtx := context.WithCancel(context.Background())
proxyClient = &http.Client{}
// Read upstream URL
{
arg := os.Getenv("LOKIPROXY_UPSTREAM_URL")
if arg == "" {
log.Fatalf("LOKIPROXY_UPSTREAM_URL is not set")
}
r, err := url.Parse(arg)
if err != nil {
log.Fatalf("can't parse Loki upstream URL: %s", err)
}
lokiUrl = *r
}
log.Printf("upstream scheme: %s", lokiUrl.Scheme)
// Read upstream TLS configuration
{
var rootCAs *x509.CertPool
argCA := os.Getenv("LOKIPROXY_UPSTREAM_CA")
if argCA != "" {
pem, err := os.ReadFile(argCA)
if err != nil {
log.Fatalf("can't open upstream CA certificate bundle: %s", err)
}
rootCAs = x509.NewCertPool()
rootCAs.AppendCertsFromPEM(pem)
log.Printf("upstream custom CA loaded")
}
var certificates []tls.Certificate
argCert := os.Getenv("LOKIPROXY_UPSTREAM_CERT")
argKey := os.Getenv("LOKIPROXY_UPSTREAM_KEY")
if argCert != "" || argKey != "" {
cert, err := tls.LoadX509KeyPair(argCert, argKey)
if err != nil {
log.Fatalf("can't load client certificate: %s", err)
}
certificates = []tls.Certificate{cert}
log.Printf("upstream client certificate loaded")
}
proxyClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: certificates,
RootCAs: rootCAs,
},
}
}
// Read frontend TLS configuration
var serverTLSConfig *tls.Config
var serverCertificate *[2]string
{
argCert := os.Getenv("LOKIPROXY_FRONTEND_CERT")
argKey := os.Getenv("LOKIPROXY_FRONTEND_KEY")
argCA := os.Getenv("LOKIPROXY_FRONTEND_CA")
if argCert != "" || argKey != "" {
serverCertificate = &[2]string{argCert, argKey}
log.Printf("frontend certificate set")
var rootCAs *x509.CertPool
if argCA != "" {
pem, err := os.ReadFile(argCA)
if err != nil {
log.Fatalf("can't open frontend CA certificate bundle: %s", err)
}
rootCAs = x509.NewCertPool()
rootCAs.AppendCertsFromPEM(pem)
serverTLSConfig = &tls.Config{
ClientCAs: rootCAs,
ClientAuth: tls.RequireAndVerifyClientCert,
}
log.Printf("frontend CA loaded")
log.Printf("frontend using mTLS")
} else {
log.Printf("frontend using TLS")
}
} else {
if argCA != "" {
log.Fatalf("can't use frontend CA without a frontend certificate")
}
log.Printf("frontend not using TLS")
}
}
// Create OIDC verifier
{
argProvider := os.Getenv("LOKIPROXY_OIDC_PROVIDER")
if argProvider == "" {
log.Fatalf("LOKIPROXY_OIDC_PROVIDER is not set")
}
argClientID := os.Getenv("LOKIPROXY_OIDC_CLIENT_ID")
if argClientID == "" {
log.Fatalf("LOKIPROXY_OIDC_CLIENT_ID is not set")
}
provider, err := oidc.NewProvider(ctx, argProvider)
if err != nil {
log.Fatalf("can't create OIDC provider: %s", err)
}
oidcVerifier = provider.Verifier(&oidc.Config{ClientID: argClientID})
}
// Read list of user namespaces
{
arg := os.Getenv("LOKIPROXY_IDENTITY_MAP_FILE")
if arg == "" {
log.Fatalf("LOKIPROXY_IDENTITY_MAP_FILE is not set")
}
var err error
identityMap, err = config.NewFileMultiMap(arg, cancelCtx)
if err != nil {
log.Fatalf("error loading identity map: %s", err)
}
}
// Read listen address
listenAddr := os.Getenv("LOKIPROXY_LISTEN_ADDR")
if listenAddr == "" {
log.Fatalf("LOKIPROXY_LISTEN_ADDR is not set")
}
// Read whether to allow alerts (which don't come with an ID Token)
{
arg := os.Getenv("LOKIPROXY_ALLOW_ALERTS")
if arg == "" || arg == "0" || arg == "false" || arg == "no" {
allowAlerts = false
} else if arg == "1" || arg == "true" || arg == "yes" {
allowAlerts = true
} else {
log.Fatalf("unrecognized value for LOKIPROXY_ALLOW_ALERTS: %#v", arg)
}
}
// Set up routes
mux := http.NewServeMux()
mux.HandleFunc("/", handle404)
mux.HandleFunc("/loki/api/v1/query", handleQuery)
mux.HandleFunc("/loki/api/v1/query_range", handleQueryRange)
mux.HandleFunc("/loki/api/v1/series", handleSeries)
mux.HandleFunc("/loki/api/v1/tail", handleTail)
mux.HandleFunc("/loki/api/v1/labels", handleLabels)
mux.HandleFunc("/loki/api/v1/label/{label}/values", handleLabelValues)
mux.HandleFunc("/loki/api/v1/index/stats", handleIndexStats)
// Create HTTP server
server := http.Server{
Addr: listenAddr,
Handler: mux,
TLSConfig: serverTLSConfig,
}
context.AfterFunc(ctx, func() { server.Close() })
log.Printf("Listening on %s", listenAddr)
var err error
if serverCertificate == nil {
err = server.ListenAndServe()
} else {
err = server.ListenAndServeTLS(serverCertificate[0], serverCertificate[1])
}
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}
func makeProxyUrl(path string) url.URL {
r := lokiUrl
r.Path = path
return r
}
func getRequiredLabelsForUser(res http.ResponseWriter, req *http.Request) (map[string]interface{}, bool) {
// Get user identity
idTokens := req.Header["X-Id-Token"]
if idTokens == nil || len(idTokens) != 1 {
if idTokens == nil && allowAlerts {
alertHeaders := req.Header["Fromalert"]
if len(alertHeaders) == 1 && alertHeaders[0] == "true" {
log.Print("allowing alert")
return make(map[string]interface{}), true
}
}
http.Error(res, "missing ID token", 410)
return nil, false
}
idToken, err := oidcVerifier.Verify(req.Context(), idTokens[0])
if err != nil {
log.Print("invalid ID token")
http.Error(res, "invalid ID token", 410)
return nil, false
}
log.Printf("id token: %#v", idToken.Subject)
// Get required labels for user
requiredLabels, ok := identityMap.Get(idToken.Subject)
if !ok {
log.Printf("request from unknown user %s", idToken.Subject)
http.Error(res, "unknown user", 403)
return nil, false
}
return requiredLabels, true
}
func respondWithProxy(
path string,
args url.Values,
res http.ResponseWriter,
setArgs map[string][]string,
passthroughArgs []string,
ctx context.Context,
) {
// Assemble query parameters
proxyQuery := make(url.Values)
for _, key := range passthroughArgs {
values, ok := args[key]
if ok {
proxyQuery[key] = values
}
}
for key, values := range setArgs {
proxyQuery[key] = values
}
// Do request
proxyUrl := makeProxyUrl(path)
proxyUrl.RawQuery = proxyQuery.Encode()
proxyReq, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
proxyUrl.String(),
nil,
)
if err != nil {
log.Printf("error creating proxy request: %s", err)
http.Error(res, "internal error", 500)
return
}
proxyRes, err := proxyClient.Do(proxyReq)
if err != nil {
log.Printf("error sending proxy request: %s", err)
http.Error(res, "internal error", 503)
return
}
// Send response
res.Header()["Content-Type"] = proxyRes.Header["Content-Type"]
res.WriteHeader(proxyRes.StatusCode)
_, _ = io.Copy(res, proxyRes.Body)
}
func handle404(res http.ResponseWriter, req *http.Request) {
log.Printf("404: %s %s", req.Method, req.URL.Path)
http.Error(res, "Not found", 404)
}
func handleQuery(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
if err := req.ParseForm(); err != nil {
http.Error(res, "invalid form data", 400)
return
}
if len(req.Form["query"]) != 1 {
http.Error(res, "one query expected", 400)
return
}
query := req.Form["query"][0]
// Find user, get required labels
requiredLabels, ok := getRequiredLabelsForUser(res, req)
if !ok {
return
}
// Rewrite query
query, err := parser.ProcessQuery(query, requiredLabels)
if err != nil {
http.Error(res, fmt.Sprintf("error parsing query: %s", err), 400)
return
}
// Proxy
respondWithProxy(
"/loki/api/v1/query",
req.Form,
res,
map[string][]string{"query": []string{query}},
[]string{"limit", "time", "direction"},
req.Context(),
)
} else {
log.Printf("got %s to query", req.Method)
http.Error(res, "use method GET", 400)
return
}
}
func handleQueryRange(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
if err := req.ParseForm(); err != nil {
http.Error(res, "invalid form data", 400)
return
}
if len(req.Form["query"]) != 1 {
http.Error(res, "one query expected", 400)
return
}
query := req.Form["query"][0]
// Find user, get allowed namespaces
requiredLabels, ok := getRequiredLabelsForUser(res, req)
if !ok {
return
}
// Rewrite query
query, err := parser.ProcessQuery(query, requiredLabels)
if err != nil {
http.Error(res, fmt.Sprintf("error parsing query: %s", err), 400)
return
}
// Proxy
respondWithProxy(
"/loki/api/v1/query_range",
req.Form,
res,
map[string][]string{"query": []string{query}},
[]string{"limit", "start", "end", "since", "step", "interval", "direction"},
req.Context(),
)
} else {
log.Printf("got %s to query_range", req.Method)
http.Error(res, "use method GET", 400)
return
}
}
func handleSeries(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" || req.Method == "POST" {
if err := req.ParseForm(); err != nil {
http.Error(res, "invalid form data", 400)
return
}
queries := req.Form["match[]"]
// Find user, get allowed namespaces
requiredLabels, ok := getRequiredLabelsForUser(res, req)
if !ok {
return
}
// Rewrite query
for key := range queries {
var err error
queries[key], err = parser.ProcessQuery(queries[key], requiredLabels)
if err != nil {
http.Error(res, fmt.Sprintf("error parsing query: %s", err), 400)
return
}
}
// Proxy
respondWithProxy(
"/loki/api/v1/series",
req.Form,
res,
map[string][]string{"query": queries},
[]string{"limit", "start", "end", "since", "step", "interval", "direction"},
req.Context(),
)
} else {
log.Printf("got %s to series", req.Method)
http.Error(res, "use methods GET or POST", 400)
return
}
}
func handleTail(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
if err := req.ParseForm(); err != nil {
http.Error(res, "invalid form data", 400)
return
}
if len(req.Form["query"]) != 1 {
http.Error(res, "one query expected", 400)
return
}
query := req.Form["query"][0]
// Find user, get allowed namespaces
requiredLabels, ok := getRequiredLabelsForUser(res, req)
if !ok {
return
}
// Rewrite query
query, err := parser.ProcessQuery(query, requiredLabels)
if err != nil {
http.Error(res, fmt.Sprintf("error parsing query: %s", err), 400)
return
}
// Proxy
respondWithProxy(
"/loki/api/v1/tail",
req.Form,
res,
map[string][]string{"query": []string{query}},
[]string{"delay_for", "limit", "start"},
req.Context(),
)
} else {
log.Printf("got %s to tail", req.Method)
http.Error(res, "use method GET", 400)
return
}
}
func handleLabels(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
if err := req.ParseForm(); err != nil {
http.Error(res, "invalid form data", 400)
return
}
// Proxy
respondWithProxy(
"/loki/api/v1/labels",
req.Form,
res,
map[string][]string{},
[]string{"start", "end", "since"},
req.Context(),
)
} else {
log.Printf("got %s to labels", req.Method)
http.Error(res, "use method GET", 400)
return
}
}
func handleLabelValues(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
if err := req.ParseForm(); err != nil {
http.Error(res, "invalid form data", 400)
return
}
var query string
if len(req.Form["query"]) == 1 {
query = req.Form["query"][0]
} else if len(req.Form["query"]) == 0 {
// No query is acceptable, treat as {} if we need to add labels
} else {
http.Error(res, "only one query expected", 400)
return
}
// Find user, get required labels
requiredLabels, ok := getRequiredLabelsForUser(res, req)
if !ok {
return
}
// Rewrite query
if len(query) > 0 || len(requiredLabels) > 0 {
if len(query) == 0 {
query = "{}"
}
var err error
query, err = parser.ProcessQuery(query, requiredLabels)
if err != nil {
http.Error(res, fmt.Sprintf("error parsing query: %s", err), 400)
return
}
}
// Proxy
respondWithProxy(
fmt.Sprintf("/loki/api/v1/label/%s/values", req.PathValue("label")),
req.Form,
res,
map[string][]string{"query": []string{query}},
[]string{"start", "end"},
req.Context(),
)
} else {
log.Printf("got %s to label values", req.Method)
http.Error(res, "use method GET", 400)
return
}
}
func handleIndexStats(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" || req.Method == "POST" {
if err := req.ParseForm(); err != nil {
http.Error(res, "invalid form data", 400)
return
}
if len(req.Form["query"]) != 1 {
http.Error(res, "one query expected", 400)
return
}
query := req.Form["query"][0]
// Find user, get allowed namespaces
requiredLabels, ok := getRequiredLabelsForUser(res, req)
if !ok {
return
}
// Rewrite query
query, err := parser.ProcessQuery(query, requiredLabels)
if err != nil {
http.Error(res, fmt.Sprintf("error parsing query: %s", err), 400)
return
}
// Proxy
respondWithProxy(
"/loki/api/v1/index/stats",
req.Form,
res,
map[string][]string{"query": []string{query}},
[]string{"start", "end"},
req.Context(),
)
} else {
log.Printf("got %s to index/stats", req.Method)
http.Error(res, "use methods GET or POST", 400)
return
}
}