mirror of
https://github.com/ditkrg/traefik-users-blocker-plugin.git
synced 2026-01-23 22:17:01 +00:00
Compare commits
No commits in common. "main" and "v0.1.3" have entirely different histories.
@ -13,3 +13,4 @@ testData:
|
|||||||
paths:
|
paths:
|
||||||
- base: /v1/users
|
- base: /v1/users
|
||||||
path: /testValue
|
path: /testValue
|
||||||
|
- base: /v1/organizations
|
||||||
|
|||||||
34
main.go
34
main.go
@ -8,13 +8,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Rule struct {
|
|
||||||
AllowedSubPaths []string `json:"allowedSubPaths,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Path struct {
|
type Path struct {
|
||||||
Path string `json:"base,omitempty"`
|
Base string `json:"base,omitempty"`
|
||||||
Rule Rule `json:"rule,omitempty"`
|
Path string `json:"path,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -42,8 +38,8 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range config.Paths {
|
for _, path := range config.Paths {
|
||||||
if path.Path == "" {
|
if path.Base == "" {
|
||||||
return nil, fmt.Errorf("Paths.Path cannot be empty")
|
return nil, fmt.Errorf("Paths.Base cannot be empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,29 +71,15 @@ func (a *UsersBlocker) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range a.paths {
|
for _, path := range a.paths {
|
||||||
isPathMatched := strings.HasPrefix(req.URL.Path, path.Path)
|
blockedPath := path.Base + path.Path
|
||||||
|
isPathBlocked := strings.HasPrefix(req.URL.Path, blockedPath)
|
||||||
|
|
||||||
if !isPathMatched {
|
if isPathBlocked {
|
||||||
a.next.ServeHTTP(rw, req)
|
message := fmt.Sprintf("blocked path %s (matched with %s) for user %s", req.URL.Path, blockedPath, userId)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(path.Rule.AllowedSubPaths) == 0 {
|
|
||||||
message := fmt.Sprintf("blocked path %s (matched with %s) for user %s", req.URL.Path, path.Path, userId)
|
|
||||||
os.Stdout.WriteString(message)
|
os.Stdout.WriteString(message)
|
||||||
http.Error(rw, message, http.StatusForbidden)
|
http.Error(rw, message, http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, allowedSubPath := range path.Rule.AllowedSubPaths {
|
|
||||||
isAllowedSubPathMatched := strings.HasPrefix(req.URL.Path, path.Path+allowedSubPath)
|
|
||||||
if !isAllowedSubPathMatched {
|
|
||||||
message := fmt.Sprintf("blocked path %s (matched with %s) for user %s", req.URL.Path, path.Path+path.Path+allowedSubPath, userId)
|
|
||||||
os.Stdout.WriteString(message)
|
|
||||||
http.Error(rw, message, http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
a.next.ServeHTTP(rw, req)
|
a.next.ServeHTTP(rw, req)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user