-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
147 lines (125 loc) · 3.59 KB
/
middleware.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package traefik_ipfilter_basicauth
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
goauth "github.com/abbot/go-http-auth"
)
type BasicAuth struct {
Users []string `json:"users,omitempty"`
UsersFile string `json:"usersFile,omitempty"`
Realm string `json:"realm,omitempty"`
RemoveHeader bool `json:"removeHeader,omitempty"`
HeaderField string `json:"headerField,omitempty"`
}
type IPWhiteList struct {
SourceRange []string `json:"sourceRange,omitempty"`
}
// Config the plugin configuration.
type Config struct {
BasicAuth BasicAuth `json:"basicAuth,omitempty"`
IPWhiteList IPWhiteList `json:"ipWhiteList,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// Middleware a Middleware plugin.
type Middleware struct {
auth *goauth.BasicAuth
next http.Handler
users map[string]string
headerField string
removeHeader bool
whiteLister *Checker
name string
}
// New created a new Middleware plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.IPWhiteList.SourceRange) == 0 {
return nil, errors.New("sourceRange is empty, IPWhiteLister not created")
}
// workaround to bug in traefik/paerser who format []string{"A", "B"} to string("24║A║B") or string("║A║B")
// see: https://github.com/traefik/traefik/issues/9638
config.IPWhiteList.SourceRange = stringToSliceHook(config.IPWhiteList.SourceRange)
config.BasicAuth.Users = stringToSliceHook(config.BasicAuth.Users)
checker, err := NewChecker(config.IPWhiteList.SourceRange)
if err != nil {
return nil, fmt.Errorf("cannot parse CIDR whitelist %s: %w", config.IPWhiteList.SourceRange, err)
}
users, err := getUsers(config.BasicAuth.UsersFile, config.BasicAuth.Users, basicUserParser)
if err != nil {
return nil, err
}
m := &Middleware{
users: users,
whiteLister: checker,
removeHeader: config.BasicAuth.RemoveHeader,
headerField: config.BasicAuth.HeaderField,
next: next,
name: name,
}
realm := defaultRealm
if len(config.BasicAuth.Realm) > 0 {
realm = config.BasicAuth.Realm
}
m.auth = &goauth.BasicAuth{Realm: realm, Secrets: m.secretBasic}
return m, nil
}
func (m *Middleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
user := ""
if req.RemoteAddr == "" {
fmt.Println("RemoteAddr is empty")
return
}
err := m.whiteLister.IsAuthorized(req.RemoteAddr)
if err != nil {
fmt.Println("Try basic auth")
ok := false
if user = m.auth.CheckAuth(req); user == "" {
ok = false
} else {
ok = true
}
if !ok {
m.auth.RequireAuth(rw, req)
fmt.Println("IP is not authorize and basic auth is not valid")
return
}
req.URL.User = url.User(user)
if m.headerField != "" {
req.Header[m.headerField] = []string{user}
}
if m.removeHeader {
req.Header.Del(authorizationHeader)
}
}
fmt.Println("Request authorized")
m.next.ServeHTTP(rw, req)
}
func (m *Middleware) secretBasic(user, realm string) string {
if secret, ok := m.users[user]; ok {
return secret
}
return ""
}
func basicUserParser(user string) (string, string, error) {
split := strings.Split(user, ":")
if len(split) != 2 {
return "", "", fmt.Errorf("error parsing BasicUser: %v", user)
}
return split[0], split[1], nil
}
func stringToSliceHook(data []string) []string {
if strings.Contains(data[0], "║") {
values := strings.Split(data[0], "║")
if len(values) >= 2 && values[0] == "" && values[1] == "24" {
return values[2:]
}
return values
}
return data
}