-
Notifications
You must be signed in to change notification settings - Fork 0
/
promgate.go
212 lines (185 loc) · 4.24 KB
/
promgate.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
package main
import (
"bufio"
"context"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"
"time"
)
type configStore struct {
urls []*url.URL
listen string
disableTLS bool
certFile string
certKey string
clientCAs *x509.CertPool
crl *pkix.CertificateList
}
var (
config configStore
re *regexp.Regexp
)
func init() {
urlsString := getEnv("URLS", "http://localhost:9100/metrics")
urls := []*url.URL{}
for _, urlString := range strings.Split(urlsString, ",") {
u, err := url.Parse(urlString)
if err != nil {
log.Fatal(err)
}
urls = append(urls, u)
}
listen := getEnv("LISTEN", "0.0.0.0:9443")
_, disableTLS := os.LookupEnv("DISABLE_TLS")
certFile := ""
keyFile := ""
clientCAs := x509.NewCertPool()
crl := &pkix.CertificateList{}
if !disableTLS {
caFile := getEnv("CA", "")
caData, err := ioutil.ReadFile(caFile)
if err != nil {
log.Fatal("Unable to load ca from $CA.")
}
if ok := clientCAs.AppendCertsFromPEM(caData); !ok {
log.Fatal("Unable to parse $CA as ca.")
}
crlFile := getEnv("CRL", "")
crlData, err := ioutil.ReadFile(crlFile)
if err != nil {
log.Fatal("Unable to load crl from $CRL.")
}
crl, err = x509.ParseCRL(crlData)
if err != nil {
log.Fatal("Unable to parse $CRL as crl.")
}
certFile = getEnv("CERT", "")
keyFile = getEnv("KEY", "")
}
config = configStore{
urls,
listen,
disableTLS,
certFile,
keyFile,
clientCAs,
crl,
}
re = regexp.MustCompile("^(?P<name>[^#][^ {]+)(?:{(?P<labels>.*)})? (?P<value>[0-9]+(?:\\.[0-9]+)?)")
}
func main() {
if config.disableTLS {
log.Printf("Running WITHOUT TLS!")
http.HandleFunc("/metrics", metrics)
s := &http.Server{Addr: config.listen}
log.Fatal(s.ListenAndServe())
} else {
tlsConfig := &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: config.clientCAs,
}
s := &http.Server{
Addr: config.listen,
TLSConfig: tlsConfig,
}
http.HandleFunc("/metrics", withTlsClientCheck(metrics))
log.Fatal(s.ListenAndServeTLS(config.certFile, config.certKey))
}
}
func getEnv(key string, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func withTlsClientCheck(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
for _, peer := range r.TLS.PeerCertificates {
for _, revoked := range config.crl.TBSCertList.RevokedCertificates {
if peer.SerialNumber.Cmp(revoked.SerialNumber) == 0 {
log.Printf("Revoked certificate: ", peer.Subject)
w.WriteHeader(403)
return
}
}
}
next.ServeHTTP(w, r)
}
}
func metrics(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
w.WriteHeader(200)
c := make(chan string)
var wg sync.WaitGroup
for _, urlParsed := range config.urls {
wg.Add(1)
go fetch(urlParsed, c, ctx, &wg)
}
go func() {
wg.Wait()
close(c)
}()
for res := range c {
fmt.Fprintf(w, "%s\n", res)
}
}
func extend(line string, label string) (string, error) {
match := re.FindStringSubmatch(line)
if len(match) != 4 {
return line, errors.New("Invalid Line.")
}
lineName := match[1]
lineLabels := match[2]
lineValue := match[3]
if lineLabels == "" {
lineLabels = fmt.Sprintf("sub_instance=\"%s\"", label)
} else {
lineLabels = fmt.Sprintf("%s,sub_instance=\"%s\"", lineLabels, label)
}
line = fmt.Sprintf("%s{%s} %s", lineName, lineLabels, lineValue)
return line, nil
}
func fetch(u *url.URL, c chan string, ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
urlString := u.String()
label := fmt.Sprintf("%s%s", u.Host, u.Path)
up := 0
err := error(nil)
defer func() {
c <- fmt.Sprintf("up {sub_instance=\"%s\"} %d", label, up)
if err != nil {
log.Printf("Error: %s", err)
}
}()
req, err := http.NewRequest("GET", urlString, nil)
if err != nil {
return
}
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
line, err = extend(line, label)
c <- line
}
if err := scanner.Err(); err != nil {
return
}
up = 1
}