forked from discordianfish/blackbox-exporter-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (117 loc) · 3.76 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
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bytes"
"context"
"crypto/subtle"
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/blackbox_exporter/config"
"github.com/prometheus/blackbox_exporter/prober"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
yaml "gopkg.in/yaml.v2"
)
var (
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
authToken = os.Getenv("AUTH_TOKEN")
errInvalidToken = errors.New("Invalid auth token")
errInvalidPath = errors.New("Invalid path")
errInvalidProber = errors.New("Invalid prober")
responseConfigInvalid = events.APIGatewayProxyResponse{
StatusCode: http.StatusBadRequest,
Body: "Invalid config",
}
)
func requireAuth(header string) (events.APIGatewayProxyResponse, error) {
parts := strings.Split(header, " ")
if (len(parts) != 2) || parts[0] != "Bearer" || subtle.ConstantTimeCompare([]byte(parts[1]), []byte(authToken)) == 0 {
return events.APIGatewayProxyResponse{StatusCode: http.StatusForbidden, Body: errInvalidToken.Error()}, errInvalidToken
}
return events.APIGatewayProxyResponse{}, nil
}
func handle(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var (
proberp = request.Path
authHeader = request.Headers["Authorization"]
target = request.QueryStringParameters["target"]
confp = request.QueryStringParameters["config"]
)
if len(proberp) < 1 {
return events.APIGatewayProxyResponse{StatusCode: http.StatusBadRequest, Body: errInvalidPath.Error()}, nil
}
proberp = proberp[1:]
logger := log.With(logger, "target", target, "prober", proberp)
level.Debug(logger).Log("msg", "Got request")
res, err := requireAuth(authHeader)
if err != nil {
level.Debug(logger).Log("msg", err.Error())
return res, nil // Do not return an error to send 401 back.
}
registry := prometheus.NewRegistry()
module := config.Module{}
switch proberp {
case "http":
if err := yaml.Unmarshal([]byte(confp), &module.HTTP); err != nil {
level.Debug(logger).Log("msg", err.Error())
return responseConfigInvalid, nil
}
prober.ProbeHTTP(ctx, target, module, registry, logger)
case "tcp":
if err := yaml.Unmarshal([]byte(confp), &module.TCP); err != nil {
level.Debug(logger).Log("msg", err.Error())
return responseConfigInvalid, nil
}
prober.ProbeTCP(ctx, target, module, registry, logger)
case "dns":
if err := yaml.Unmarshal([]byte(confp), &module.DNS); err != nil {
level.Debug(logger).Log("msg", err.Error())
return responseConfigInvalid, nil
}
prober.ProbeDNS(ctx, target, module, registry, logger)
case "icmp":
if err := yaml.Unmarshal([]byte(confp), &module.ICMP); err != nil {
level.Debug(logger).Log("msg", err.Error())
return responseConfigInvalid, nil
}
prober.ProbeICMP(ctx, target, module, registry, logger)
default:
return events.APIGatewayProxyResponse{
StatusCode: http.StatusBadRequest,
Body: "Prober " + proberp + " does not exist",
}, nil
}
mfs, err := registry.Gather()
if err != nil {
return events.APIGatewayProxyResponse{}, err
}
headers := http.Header{}
for k, v := range request.Headers {
headers.Add(k, v)
}
contentType := expfmt.Negotiate(http.Header(headers))
buf := &bytes.Buffer{}
enc := expfmt.NewEncoder(buf, contentType)
for _, mf := range mfs {
if err := enc.Encode(mf); err != nil {
return events.APIGatewayProxyResponse{}, err
}
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{
"Content-Type": string(contentType),
"Content-Length": fmt.Sprint(buf.Len()),
},
Body: buf.String(),
}, nil
}
func main() {
lambda.Start(handle)
}