-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
69 lines (61 loc) · 2.07 KB
/
middlewares.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
package main
import (
"log"
"net/http"
limiter "github.com/ulule/limiter/v3"
stdlib "github.com/ulule/limiter/v3/drivers/middleware/stdlib"
memory "github.com/ulule/limiter/v3/drivers/store/memory"
)
// LimiterMiddleware provides limiter middleware pointer
var LimiterMiddleware *stdlib.Middleware
// initialize Limiter middleware pointer
func initLimiter(period string) {
log.Printf("limiter rate='%s'", period)
// create rate limiter with 5 req/second
rate, err := limiter.NewRateFromFormatted(period)
if err != nil {
panic(err)
}
store := memory.NewStore()
instance := limiter.New(store, rate, limiter.WithSkipList(Config.LimiterSkipList))
if Config.LimiterHeader != "" {
instance = limiter.New(
store,
rate,
limiter.WithClientIPHeader(Config.LimiterHeader),
limiter.WithSkipList(Config.LimiterSkipList))
}
LimiterMiddleware = stdlib.NewMiddleware(instance)
}
// helper to auth/authz incoming requests to the server
func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// perform authentication
status := CMSAuth.CheckAuthnAuthz(r.Header)
if !status {
log.Printf("ERROR: fail to authenticate, HTTP headers %+v\n", r.Header)
w.WriteHeader(http.StatusForbidden)
return
}
if Config.Verbose > 2 {
log.Printf("Auth layer status: %v headers: %+v\n", status, r.Header)
}
// check if user has proper roles to DBS (non GET) APIs
if r.Method != "GET" && Config.CMSRole != "" && Config.CMSGroup != "" {
status = CMSAuth.CheckCMSAuthz(r.Header, Config.CMSRole, Config.CMSGroup, "")
if !status {
log.Printf("ERROR: fail to authorize used with role=%v and group=%v, HTTP headers %+v\n", Config.CMSRole, Config.CMSGroup, r.Header)
w.WriteHeader(http.StatusUnauthorized)
return
}
}
// Call the next handler
next.ServeHTTP(w, r)
})
}
// limit middleware limits incoming requests
func limitMiddleware(next http.Handler) http.Handler {
return LimiterMiddleware.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
}))
}