-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_security.go
50 lines (40 loc) · 1.22 KB
/
server_security.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
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/spf13/pflag"
)
// TODO: generally speaking, we need a fine-grained RBAC system.
type ServerSecurityOptions struct {
// EnabledTableOrViews list of table or view names that are accessible (read & write).
EnabledTableOrViews []string
}
func (opts *ServerSecurityOptions) bindCLIFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(
&opts.EnabledTableOrViews,
"security-allow-table",
[]string{},
"list of table or view names that are accessible (read & write)",
)
}
func (opts *ServerSecurityOptions) defaults() error {
return nil
}
func (opts *ServerSecurityOptions) createTableOrViewAccessCheckMiddleware(
responseErr func(w http.ResponseWriter, err error),
) func(http.Handler) http.Handler {
accessibleTableOrViews := make(map[string]struct{})
for _, t := range opts.EnabledTableOrViews {
accessibleTableOrViews[t] = struct{}{}
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
target := chi.URLParam(req, routeVarTableOrView)
if _, ok := accessibleTableOrViews[target]; !ok {
responseErr(w, ErrAccessRestricted)
return
}
next.ServeHTTP(w, req)
})
}
}