-
Notifications
You must be signed in to change notification settings - Fork 2
/
exceptbasicauth.go
164 lines (140 loc) · 4.11 KB
/
exceptbasicauth.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package traefik_plugin_exception_authbasic
import (
"context"
"fmt"
"log"
"net"
"net/http"
"strings"
)
// Config is the configuration for this plugin
type Config struct {
AllowIPList []string `json:"allowIPList,omitempty"`
User string `json:"user"`
Password string `json:"password"`
PreventUser bool `json:"preventUser"`
IPHeaders []string `json:"ipHeaders"`
Headers map[string]string `json:"headers"`
}
// CreateConfig creates a new configuration for this plugin
func CreateConfig() *Config {
return &Config{
Headers: make(map[string]string),
}
}
// ExceptBasicAuth represents the basic properties of this plugin
type ExceptBasicAuth struct {
next http.Handler
name string
config *Config
allowedIPs []*net.IP
allowedIPNets []*net.IPNet
authHeaders map[string]string
}
// New creates a new instance of this plugin
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
var allowedIPs []*net.IP
var allowedIPNets []*net.IPNet
for _, allowedIP := range config.AllowIPList {
ip, ipNet, err := parseIP(allowedIP)
if err != nil {
log.Printf("Failed to parse ip %s: %v", allowedIP, err)
} else if ip != nil {
allowedIPs = append(allowedIPs, ip)
} else if ipNet != nil {
allowedIPNets = append(allowedIPNets, ipNet)
}
}
return &ExceptBasicAuth{
name: name,
next: next,
config: config,
allowedIPNets: allowedIPNets,
allowedIPs: allowedIPs,
authHeaders: config.Headers,
}, nil
}
func (e *ExceptBasicAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
extractedIPs := e.extractIP(req)
// we "whitelist" the request (i.e. adding the pre-configured Auth header) on two rules
// - either the remote ip is allowed
// - or it contains a preconfigured, valid header
if (len(extractedIPs) > 0 && e.isAnyIPAllowed(extractedIPs)) || e.hasValidHeader(req) {
req.SetBasicAuth(e.config.User, e.config.Password)
} else if e.config.PreventUser && req.Header.Get("Authorization") != "" {
user, _, ok := req.BasicAuth()
if ok && user == e.config.User {
rw.WriteHeader(http.StatusUnauthorized)
return
}
}
e.next.ServeHTTP(rw, req)
}
func (e *ExceptBasicAuth) extractIP(req *http.Request) []string {
var possibleIPs []string
for _, header := range e.config.IPHeaders {
headerVal := req.Header.Get(header)
if headerVal != "" {
for _, possibleIP := range strings.Split(headerVal, ",") {
parsedIP := net.ParseIP(strings.TrimSpace(possibleIP))
if parsedIP != nil {
possibleIPs = append(possibleIPs, strings.TrimSpace(possibleIP))
}
}
}
}
if len(possibleIPs) < 1 {
// fallback on req.RemoteAddr if no source header is configured
// or could be found in the request
ip, _, err := net.SplitHostPort(req.RemoteAddr)
if err == nil {
possibleIPs = append(possibleIPs, ip)
}
}
return possibleIPs
}
func (e *ExceptBasicAuth) isAnyIPAllowed(ips []string) bool {
for _, ip := range ips {
parsedIP := net.ParseIP(ip)
if parsedIP != nil && e.isIPAllowed(parsedIP) {
return true
}
}
return false
}
func (e *ExceptBasicAuth) isIPAllowed(ip net.IP) bool {
for _, allowedIP := range e.allowedIPs {
if allowedIP.Equal(ip) {
return true
}
}
for _, allowedIPnet := range e.allowedIPNets {
if allowedIPnet.Contains(ip) {
return true
}
}
return false
}
func parseIP(allowedIP string) (*net.IP, *net.IPNet, error) {
if strings.Contains(allowedIP, "/") {
_, ipNet, err := net.ParseCIDR(allowedIP)
if err != nil {
return nil, nil, fmt.Errorf("unable to parse %s as cidr, skipping", allowedIP)
}
return nil, ipNet, err
}
parsedIP := net.ParseIP(allowedIP)
if parsedIP == nil {
return nil, nil, fmt.Errorf("unable to parse ip %s, skipping", allowedIP)
}
return &parsedIP, nil, nil
}
func (e *ExceptBasicAuth) hasValidHeader(req *http.Request) bool {
for headerKey, headerValue := range e.authHeaders {
actualHeaderValue := req.Header.Get(headerKey)
if actualHeaderValue == headerValue || (actualHeaderValue != "" && headerValue == "*") {
return true
}
}
return false
}