Skip to content

Commit 2e5121d

Browse files
author
Manuel Bovo
committed
Adding logs, and usage gianarb#33
1 parent cc5aa10 commit 2e5121d

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

api/basicauth.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
)
88

99
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"`
10+
AuthEnabled bool `split_words:"true" default:"false"`
11+
AuthUser string `split_words:"true" default:"orbiter"`
12+
AuthPass string `split_words:"true" default:"orbiter"`
13+
AuthRealm string `split_words:"true" default:"Restricted"`
1414
}
1515

1616
func wrap(h http.HandlerFunc, funx ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
@@ -24,24 +24,30 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc {
2424
return func(w http.ResponseWriter, r *http.Request) {
2525

2626
var ac AuthConfig
27-
e := envconfig.Process("ORBITER", &ac)
27+
e := envconfig.Process("orbiter", &ac)
2828
if e != nil {
2929
logrus.Fatal(e.Error())
3030
}
3131

32-
w.Header().Set("WWW-Authenticate", `Basic realm="`+ac.Realm+`"`)
32+
w.Header().Set("WWW-Authenticate", `Basic realm="`+ac.AuthRealm+`"`)
3333

3434
u, p, ok := r.BasicAuth()
3535
if ok == false {
36+
logrus.Error("No Authentication supplied")
3637
w.WriteHeader(401)
3738
w.Write([]byte("Not Authorized"))
39+
return
3840
}
3941

40-
if ac.User != u || ac.Pass != p {
42+
if ac.AuthUser != u || ac.AuthPass != p {
43+
logrus.Warnf("Invalid username or password for user %s", u)
4144
w.WriteHeader(401)
4245
w.Write([]byte("Invalid username or password"))
46+
return
4347
}
4448

49+
logrus.Infof("Succesfully authenticated with user: %s", u)
50+
4551
h.ServeHTTP(w, r)
4652
}
4753

api/router.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ func GetRouter(core *core.Core, eventChannel chan *logrus.Entry) *mux.Router {
1111
r := mux.NewRouter()
1212

1313
var ac AuthConfig
14-
e := envconfig.Process("ORBITER", &ac)
14+
envconfig.Usage("orbiter", &ac)
15+
16+
e := envconfig.Process("orbiter", &ac)
1517
if e != nil {
1618
logrus.Fatal(e.Error())
1719
}
1820

19-
if ac.Enabled {
21+
logrus.Infof("Env: %v", ac)
22+
23+
if ac.AuthEnabled {
2024
logrus.Info("Enabling Authentication")
2125
r.HandleFunc("/v1/orbiter/handle/{autoscaler_name}/{service_name}", wrap(Handle(&core.Autoscalers), basicAuth)).Methods("POST")
2226
r.HandleFunc("/v1/orbiter/handle/{autoscaler_name}/{service_name}/{direction}", wrap(Handle(&core.Autoscalers), basicAuth)).Methods("POST")

0 commit comments

Comments
 (0)