forked from jwilder/gofana
/
main.go
515 lines (442 loc) · 13.4 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
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
package main
import (
"bytes"
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"github.com/go-martini/martini"
"github.com/jwilder/gofana/grafana"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/render"
"github.com/martini-contrib/secure"
"github.com/martini-contrib/sessions"
"github.com/martini-contrib/staticbin"
"github.com/stretchr/gomniauth"
"github.com/stretchr/gomniauth/providers/google"
"github.com/stretchr/objx"
"github.com/stretchr/signature"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"text/template"
)
const (
defaultSessionSecret = "e40f5e3a62f25ef48eeb03440735831a"
)
var (
db *DashboardRepository
wg sync.WaitGroup
basicAuth, authDomain string
httpAddr, httpsAddr string
sslCert, sslKey string
appDir, dbDir string
graphiteURL string
influxDBURL string
influxDBUser string
influxDBPass string
openTSDBUrl string
buildVersion string
hostAddr string
googleClientID, googleClientSecret string
sessionSecret string
version bool
)
func addCorsHeaders(w http.ResponseWriter) {
w.Header().Add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length")
w.Header().Add("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE")
w.Header().Add("Access-Control-Allow-Origin", "*")
}
func loginRequired(s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/oauth2callback") ||
strings.HasSuffix(r.URL.Path, "/signin") ||
strings.HasSuffix(r.URL.Path, "/auth") {
return
}
if s.Get("username") == nil {
http.Redirect(w, r, "/signin", http.StatusFound)
}
}
func authRedirect(params martini.Params, s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) {
providerName := params["provider"]
if providerName == "" {
http.Error(w, "Unknown provider", http.StatusBadRequest)
return
}
provider, err := gomniauth.Provider(providerName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
authUrl, err := provider.GetBeginAuthURL(nil, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
http.Redirect(w, r, authUrl, http.StatusFound)
}
func getSignin(s sessions.Session, w http.ResponseWriter, r *http.Request) {
s.Delete("username")
if !oauthEnabled() {
http.Redirect(w, r, "/", http.StatusFound)
return
}
body, err := Asset("templates/signin.html")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
tmpl, err := template.New("config.js").Parse(string(body))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
err = tmpl.Execute(w, struct {
Error string
Google bool
}{
Error: r.FormValue("error"),
Google: googleOauthEnabled(),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
}
func oauth2callback(params martini.Params, s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) {
providerName := params["provider"]
if providerName == "" {
http.Error(w, "Unknown provider", http.StatusBadRequest)
return
}
provider, err := gomniauth.Provider(providerName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
omap, err := objx.FromURLQuery(r.URL.RawQuery)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
creds, err := provider.CompleteAuth(omap)
if err != nil {
s.Delete("username")
http.Redirect(w, r, "/signin?error=Access+Denied", http.StatusFound)
log.Printf("ERROR: %s", err)
return
}
user, err := provider.GetUser(creds)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
if strings.HasSuffix(user.Email(), authDomain) {
log.Printf("%s authenticated via %s", user.Email(), providerName)
s.Set("username", user.Email())
} else {
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
func saveDashboard(w http.ResponseWriter, r *http.Request, params martini.Params) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
err = db.Save(params["id"], body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
w.Write([]byte("{}"))
}
func getDashboard(w http.ResponseWriter, r *http.Request, params martini.Params) {
id := params["id"]
if !db.Exists(id) {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Add("Content-Type", "application/json")
data, err := db.Get(id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
w.Header().Add("Content-Length", strconv.FormatInt(int64(len(string(data))), 10))
w.Write(data)
}
func deleteDashboard(w http.ResponseWriter, r *http.Request, params martini.Params) {
id := params["id"]
err := db.Delete(id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
}
func searchDashboards(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("ERROR: %s", err)
return
}
dashboards, err := db.Search(r.Form.Get("query"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("ERROR: %s", err)
return
}
w.Header().Add("Content-Type", "application/json")
body, err := json.Marshal(struct {
Dashboards []*Dashboard `json:"dashboards"`
}{Dashboards: dashboards})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
w.Write(body)
}
func gofanaDatasource(w http.ResponseWriter) {
w.Header().Add("Content-Type", "application/json")
body, err := Asset("templates/datasource.gofana.js")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
w.Write(body)
}
func gofanaConfig(w http.ResponseWriter) {
w.Header().Add("Content-Type", "application/json")
body, err := Asset("templates/config.js")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
tmpl, err := template.New("config.js").Parse(string(body))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
err = tmpl.Execute(w, struct {
GraphiteURL string
InfluxDBURL string
InfluxDBUser string
InfluxDBPass string
OpenTSDBUrl string
}{
GraphiteURL: graphiteURL,
InfluxDBURL: influxDBURL,
InfluxDBUser: influxDBUser,
InfluxDBPass: influxDBPass,
OpenTSDBUrl: openTSDBUrl,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
}
func copyHeader(source http.Header, dest *http.Header) {
for n, v := range source {
for _, vv := range v {
dest.Add(n, vv)
}
}
}
func proxyOpenTSDB(w http.ResponseWriter, r *http.Request) {
proxy(openTSDBUrl, w, r)
}
func proxyGraphite(w http.ResponseWriter, r *http.Request) {
proxy(graphiteURL, w, r)
}
func proxyInfluxDB(w http.ResponseWriter, r *http.Request) {
proxy(influxDBURL, w, r)
}
func proxy(target string, w http.ResponseWriter, r *http.Request) {
stripped := r.RequestURI[strings.Index(r.RequestURI[1:], "/")+1:]
uri := target + stripped
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
rr, err := http.NewRequest(r.Method, uri, bytes.NewBuffer(body))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
copyHeader(r.Header, &rr.Header)
// Create a client and query the target
var transport http.Transport
resp, err := transport.RoundTrip(rr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("ERROR: %s", err)
return
}
dH := w.Header()
copyHeader(resp.Header, &dH)
w.Write(body)
}
func googleOauthEnabled() bool {
return googleClientID != "" && googleClientSecret != ""
}
func oauthEnabled() bool {
return googleOauthEnabled()
}
func main() {
flag.StringVar(&appDir, "app-dir", "", "Path to grafana installation")
flag.StringVar(&dbDir, "db-dir", "dashboards", "Path to dashboard storage dir")
flag.StringVar(&authDomain, "auth-domain", "", "OAuth2 domain users must authenticated from (mydomain.com)")
flag.StringVar(&basicAuth, "auth", "", "Basic auth username (user:pw)")
flag.StringVar(&sessionSecret, "session-secret", defaultSessionSecret, "Session secret key")
flag.StringVar(&httpAddr, "http-addr", ":8080", "HTTP Server bind address")
flag.StringVar(&httpsAddr, "https-addr", ":8443", "HTTPS Server bind address")
flag.StringVar(&graphiteURL, "graphite-url", "", "Graphite URL (http://host:port)")
flag.StringVar(&influxDBURL, "influxdb-url", "", "InfluxDB URL (http://host:8086/db/mydb)")
flag.StringVar(&influxDBUser, "influxdb-user", "", "InfluxDB username")
flag.StringVar(&influxDBPass, "influxdb-pass", "", "InfluxDB password")
flag.StringVar(&openTSDBUrl, "opentsdb-url", "", "OpenTSDB URL (http://host:4242)")
flag.StringVar(&sslCert, "ssl-cert", "", "SSL cert (PEM formatted)")
flag.StringVar(&sslKey, "ssl-key", "", "SSL key (PEM formatted)")
flag.StringVar(&hostAddr, "host-addr", "http://localhost:8080", "Public server address (http://mydomain.com)")
flag.StringVar(&googleClientID, "google-client-id", "", "Google Oauth2 Client ID")
flag.StringVar(&googleClientSecret, "google-client-secret", "", "Google Oauth2 Client Sercret")
flag.BoolVar(&version, "version", false, "show version")
flag.Parse()
if version {
println(buildVersion)
return
}
if sessionSecret == defaultSessionSecret {
log.Printf("WARN: Session secret key is using the hard-coded default. Use -session-secret <value> for a live deployment.\n")
}
if graphiteURL == "" && influxDBURL == "" && openTSDBUrl == "" {
fmt.Printf("No graphite-url, influxdb-url or opentsdb-url specified.\nUse -graphite-url http://host:port or -influxdb-url http://host:8086/db/mydb or -opentsdb-url http://host:4242\n")
return
}
log.Printf("Starting gofana %s", buildVersion)
if _, err := os.Stat(dbDir); os.IsNotExist(err) {
fmt.Printf("%s does not exist. Creating.\n", dbDir)
err := os.Mkdir(dbDir, 0766)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
}
db = &DashboardRepository{Dir: dbDir}
err := db.Load()
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
logger := log.New(os.Stderr, "", log.LstdFlags)
r := martini.NewRouter()
m := martini.New()
m.Map(logger)
m.Use(martini.Recovery())
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
if sslCert != "" && sslKey != "" {
m.Use(secure.Secure(secure.Options{}))
}
b := make([]byte, 32)
_, err = rand.Read(b)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
m.Use(sessions.Sessions("session", sessions.NewCookieStore([]byte(sessionSecret))))
if oauthEnabled() {
if authDomain == "" {
fmt.Println("ERROR: No -auth-domain specified. Cannot authenticate with OAuth2.\n")
return
}
gomniauth.SetSecurityKey(signature.RandomKey(64))
providers := gomniauth.WithProviders()
if googleOauthEnabled() {
providers.Add(google.New(googleClientID, googleClientSecret, fmt.Sprintf("%s/google/oauth2callback", hostAddr)))
}
m.Use(loginRequired)
}
m.Use(addCorsHeaders)
m.Use(render.Renderer())
if basicAuth != "" && strings.Contains(basicAuth, ":") {
parts := strings.Split(basicAuth, ":")
m.Use(auth.Basic(parts[0], parts[1]))
}
var static martini.Handler
if appDir == "" {
static = staticbin.Static("grafana-1.9.1", grafana.Asset)
} else {
static = martini.Static(appDir, martini.StaticOptions{Fallback: "/index.html", Exclude: "/api/v"})
}
r.NotFound(static, http.NotFound)
r.Get("/search", searchDashboards)
r.Get("/dashboard/:id", getDashboard)
r.Post("/dashboard/:id", saveDashboard)
r.Delete("/dashboard/:id", deleteDashboard)
r.Get("/plugins/datasource.gofana.js", gofanaDatasource)
r.Get("/config.js", gofanaConfig)
r.Get("/graphite/**", proxyGraphite)
r.Post("/graphite/**", proxyGraphite)
r.Get("/influxdb/**", proxyInfluxDB)
r.Post("/influxdb/**", proxyInfluxDB)
r.Get("/opentsdb/**", proxyOpenTSDB)
r.Post("/opentsdb/**", proxyOpenTSDB)
r.Get("/:provider/auth", authRedirect)
r.Get("/:provider/oauth2callback", oauth2callback)
r.Get("/signin", getSignin)
// HTTP Listener
wg.Add(1)
go func() {
defer wg.Done()
log.Printf("HTTP listening on %s\n", httpAddr)
if err := http.ListenAndServe(httpAddr, m); err != nil {
log.Fatal(err)
}
}()
// HTTPS Listener
if sslCert != "" && sslKey != "" {
wg.Add(1)
go func() {
defer wg.Done()
log.Printf("HTTPS listening on %s", httpsAddr)
if err := http.ListenAndServeTLS(httpsAddr, sslCert, sslKey, m); err != nil {
log.Fatal(err)
}
}()
}
wg.Wait()
}