|
1 | 1 | package api
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "net/http" |
5 |
| - "os" |
6 |
| - |
7 | 4 | "github.com/Sirupsen/logrus"
|
8 | 5 | "github.com/gianarb/orbiter/core"
|
9 | 6 | "github.com/gorilla/mux"
|
| 7 | + "github.com/kelseyhightower/envconfig" |
10 | 8 | )
|
11 | 9 |
|
12 |
| -func wrap(h http.HandlerFunc, funx ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc { |
13 |
| - for _, f := range funx { |
14 |
| - h = f(h) |
| 10 | +func GetRouter(core *core.Core, eventChannel chan *logrus.Entry) *mux.Router { |
| 11 | + r := mux.NewRouter() |
| 12 | + |
| 13 | + var ac AuthConfig |
| 14 | + e := envconfig.Process("ORBITER", &ac) |
| 15 | + if e != nil { |
| 16 | + logrus.Fatal(e.Error()) |
15 | 17 | }
|
16 |
| - return h |
17 |
| -} |
18 | 18 |
|
19 |
| -func basicAuth(h http.HandlerFunc) http.HandlerFunc { |
20 |
| - return func(w http.ResponseWriter, r *http.Request) { |
21 |
| - w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) |
22 |
| - user := os.Getenv("ORBITER_AUTH_USER") |
23 |
| - pass := os.Getenv("ORBITER_AUTH_PASS") |
24 |
| - |
25 |
| - u, p, ok := r.BasicAuth() |
26 |
| - if ok == false { |
27 |
| - w.WriteHeader(401) |
28 |
| - w.Write([]byte("Not Authorized")) |
29 |
| - return |
30 |
| - } |
31 |
| - |
32 |
| - if user != u || pass != p { |
33 |
| - w.WriteHeader(401) |
34 |
| - w.Write([]byte("Not Authorized")) |
35 |
| - return |
36 |
| - } |
37 |
| - |
38 |
| - h.ServeHTTP(w, r) |
| 19 | + if ac.Enabled { |
| 20 | + logrus.Info("Enabling Authentication") |
| 21 | + r.HandleFunc("/v1/orbiter/handle/{autoscaler_name}/{service_name}", wrap(Handle(&core.Autoscalers), basicAuth)).Methods("POST") |
| 22 | + r.HandleFunc("/v1/orbiter/handle/{autoscaler_name}/{service_name}/{direction}", wrap(Handle(&core.Autoscalers), basicAuth)).Methods("POST") |
| 23 | + r.HandleFunc("/v1/orbiter/autoscaler", AutoscalerList(core.Autoscalers)).Methods("GET") |
| 24 | + r.HandleFunc("/v1/orbiter/health", Health()).Methods("GET") |
| 25 | + r.HandleFunc("/v1/orbiter/events", Events(eventChannel)).Methods("GET") |
| 26 | + |
| 27 | + } else { |
| 28 | + |
| 29 | + r.HandleFunc("/v1/orbiter/handle/{autoscaler_name}/{service_name}", Handle(&core.Autoscalers)).Methods("POST") |
| 30 | + r.HandleFunc("/v1/orbiter/handle/{autoscaler_name}/{service_name}/{direction}", Handle(&core.Autoscalers)).Methods("POST") |
| 31 | + r.HandleFunc("/v1/orbiter/autoscaler", AutoscalerList(core.Autoscalers)).Methods("GET") |
| 32 | + r.HandleFunc("/v1/orbiter/health", Health()).Methods("GET") |
| 33 | + r.HandleFunc("/v1/orbiter/events", Events(eventChannel)).Methods("GET") |
39 | 34 | }
|
40 |
| -} |
41 | 35 |
|
42 |
| -func GetRouter(core *core.Core, eventChannel chan *logrus.Entry) *mux.Router { |
43 |
| - r := mux.NewRouter() |
44 |
| - r.HandleFunc("/handle/{autoscaler_name}/{service_name}", |
45 |
| - wrap(Handle(&core.Autoscalers), basicAuth)).Methods("POST") |
46 |
| - r.HandleFunc("/handle/{autoscaler_name}/{service_name}/{direction}", |
47 |
| - wrap(Handle(&core.Autoscalers), basicAuth)).Methods("POST") |
| 36 | + // This lines will be removed October 2017. They are here to offer a soft migation path. |
| 37 | + r.HandleFunc("/handle/{autoscaler_name}/{service_name}", Handle(&core.Autoscalers)).Methods("POST") |
| 38 | + r.HandleFunc("/handle/{autoscaler_name}/{service_name}/{direction}", Handle(&core.Autoscalers)).Methods("POST") |
48 | 39 | r.HandleFunc("/autoscaler", AutoscalerList(core.Autoscalers)).Methods("GET")
|
49 | 40 | r.HandleFunc("/health", Health()).Methods("GET")
|
50 | 41 | r.HandleFunc("/events", Events(eventChannel)).Methods("GET")
|
| 42 | + |
51 | 43 | r.NotFoundHandler = NotFound{}
|
52 | 44 | return r
|
53 | 45 | }
|
0 commit comments