Skip to content

Commit 21c8186

Browse files
author
Manuel Bovo
committed
optional http basic authentication using env vars gianarb#33
1 parent 63ebf36 commit 21c8186

File tree

3 files changed

+76
-36
lines changed

3 files changed

+76
-36
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
bin/
2-
.idea/
2+
.idea/

api/basicauth.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package api
2+
3+
import (
4+
"github.com/Sirupsen/logrus"
5+
"github.com/kelseyhightower/envconfig"
6+
"net/http"
7+
)
8+
9+
type AuthConfig struct {
10+
Enabled bool `default:"false"`
11+
User string `envconfig:"AUTH_USER" default:"orbiter"`
12+
Pass string `envconfig:"AUTH_PASS" default:"orbiter"`
13+
Realm string `envconfig:"AUTH_REALM" default:"Restricted"`
14+
}
15+
16+
func wrap(h http.HandlerFunc, funx ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
17+
for _, f := range funx {
18+
h = f(h)
19+
}
20+
return h
21+
}
22+
23+
func basicAuth(h http.HandlerFunc) http.HandlerFunc {
24+
return func(w http.ResponseWriter, r *http.Request) {
25+
26+
var ac AuthConfig
27+
e := envconfig.Process("ORBITER", &ac)
28+
if e != nil {
29+
logrus.Fatal(e.Error())
30+
}
31+
32+
w.Header().Set("WWW-Authenticate", `Basic realm="`+ac.Realm+`"`)
33+
34+
u, p, ok := r.BasicAuth()
35+
if ok == false {
36+
w.WriteHeader(401)
37+
w.Write([]byte("Not Authorized"))
38+
}
39+
40+
if ac.User != u || ac.Pass != p {
41+
w.WriteHeader(401)
42+
w.Write([]byte("Invalid username or password"))
43+
}
44+
45+
h.ServeHTTP(w, r)
46+
}
47+
48+
}

api/router.go

+27-35
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,45 @@
11
package api
22

33
import (
4-
"net/http"
5-
"os"
6-
74
"github.com/Sirupsen/logrus"
85
"github.com/gianarb/orbiter/core"
96
"github.com/gorilla/mux"
7+
"github.com/kelseyhightower/envconfig"
108
)
119

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())
1517
}
16-
return h
17-
}
1818

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")
3934
}
40-
}
4135

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")
4839
r.HandleFunc("/autoscaler", AutoscalerList(core.Autoscalers)).Methods("GET")
4940
r.HandleFunc("/health", Health()).Methods("GET")
5041
r.HandleFunc("/events", Events(eventChannel)).Methods("GET")
42+
5143
r.NotFoundHandler = NotFound{}
5244
return r
5345
}

0 commit comments

Comments
 (0)