Skip to content

Commit 00728e7

Browse files
julienstraefiker
authored andcommitted
IPStrategy for selecting IP in whitelist
1 parent 1ec4e03 commit 00728e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2436
-1829
lines changed

anonymize/anonymize_config_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ func TestDo_globalConfiguration(t *testing.T) {
8080
TrustForwardHeader: true,
8181
},
8282
},
83-
WhitelistSourceRange: []string{"foo WhitelistSourceRange 1", "foo WhitelistSourceRange 2", "foo WhitelistSourceRange 3"},
84-
Compress: &configuration.Compress{},
83+
WhiteList: &types.WhiteList{
84+
SourceRange: []string{
85+
"127.0.0.1/32",
86+
},
87+
},
88+
Compress: &configuration.Compress{},
8589
ProxyProtocol: &configuration.ProxyProtocol{
8690
TrustedIPs: []string{"127.0.0.1/32", "192.168.0.1"},
8791
},
@@ -125,8 +129,12 @@ func TestDo_globalConfiguration(t *testing.T) {
125129
TrustForwardHeader: true,
126130
},
127131
},
128-
WhitelistSourceRange: []string{"fii WhitelistSourceRange 1", "fii WhitelistSourceRange 2", "fii WhitelistSourceRange 3"},
129-
Compress: &configuration.Compress{},
132+
WhiteList: &types.WhiteList{
133+
SourceRange: []string{
134+
"127.0.0.1/32",
135+
},
136+
},
137+
Compress: &configuration.Compress{},
130138
ProxyProtocol: &configuration.ProxyProtocol{
131139
TrustedIPs: []string{"127.0.0.1/32", "192.168.0.1"},
132140
},

autogen/gentemplates/gen.go

+57-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configuration/configuration.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (gc *GlobalConfiguration) SetEffectiveConfiguration(configFile string) {
108108
if len(gc.EntryPoints) == 0 {
109109
gc.EntryPoints = map[string]*EntryPoint{"http": {
110110
Address: ":80",
111-
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
111+
ForwardedHeaders: &ForwardedHeaders{},
112112
}}
113113
gc.DefaultEntryPoints = []string{"http"}
114114
}
@@ -126,18 +126,7 @@ func (gc *GlobalConfiguration) SetEffectiveConfiguration(configFile string) {
126126
entryPoint := gc.EntryPoints[entryPointName]
127127
// ForwardedHeaders must be remove in the next breaking version
128128
if entryPoint.ForwardedHeaders == nil {
129-
entryPoint.ForwardedHeaders = &ForwardedHeaders{Insecure: true}
130-
}
131-
132-
if len(entryPoint.WhitelistSourceRange) > 0 {
133-
log.Warnf("Deprecated configuration found: %s. Please use %s.", "whiteListSourceRange", "whiteList.sourceRange")
134-
135-
if entryPoint.WhiteList == nil {
136-
entryPoint.WhiteList = &types.WhiteList{
137-
SourceRange: entryPoint.WhitelistSourceRange,
138-
}
139-
entryPoint.WhitelistSourceRange = nil
140-
}
129+
entryPoint.ForwardedHeaders = &ForwardedHeaders{}
141130
}
142131
}
143132

configuration/entrypoints.go

+51-33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configuration
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67

78
"github.com/containous/traefik/log"
@@ -11,20 +12,19 @@ import (
1112

1213
// EntryPoint holds an entry point configuration of the reverse proxy (ip, port, TLS...)
1314
type EntryPoint struct {
14-
Address string
15-
TLS *tls.TLS `export:"true"`
16-
Redirect *types.Redirect `export:"true"`
17-
Auth *types.Auth `export:"true"`
18-
WhitelistSourceRange []string // Deprecated
19-
WhiteList *types.WhiteList `export:"true"`
20-
Compress *Compress `export:"true"`
21-
ProxyProtocol *ProxyProtocol `export:"true"`
22-
ForwardedHeaders *ForwardedHeaders `export:"true"`
15+
Address string
16+
TLS *tls.TLS `export:"true"`
17+
Redirect *types.Redirect `export:"true"`
18+
Auth *types.Auth `export:"true"`
19+
WhiteList *types.WhiteList `export:"true"`
20+
Compress *Compress `export:"true"`
21+
ProxyProtocol *ProxyProtocol `export:"true"`
22+
ForwardedHeaders *ForwardedHeaders `export:"true"`
23+
ClientIPStrategy *types.IPStrategy `export:"true"`
2324
}
2425

2526
// Compress contains compress configuration
26-
type Compress struct {
27-
}
27+
type Compress struct{}
2828

2929
// ProxyProtocol contains Proxy-Protocol configuration
3030
type ProxyProtocol struct {
@@ -68,11 +68,6 @@ func (ep *EntryPoints) Type() string {
6868
func (ep *EntryPoints) Set(value string) error {
6969
result := parseEntryPointsConfiguration(value)
7070

71-
var whiteListSourceRange []string
72-
if len(result["whitelistsourcerange"]) > 0 {
73-
whiteListSourceRange = strings.Split(result["whitelistsourcerange"], ",")
74-
}
75-
7671
var compress *Compress
7772
if len(result["compress"]) > 0 {
7873
compress = &Compress{}
@@ -84,29 +79,42 @@ func (ep *EntryPoints) Set(value string) error {
8479
}
8580

8681
(*ep)[result["name"]] = &EntryPoint{
87-
Address: result["address"],
88-
TLS: configTLS,
89-
Auth: makeEntryPointAuth(result),
90-
Redirect: makeEntryPointRedirect(result),
91-
Compress: compress,
92-
WhitelistSourceRange: whiteListSourceRange,
93-
WhiteList: makeWhiteList(result),
94-
ProxyProtocol: makeEntryPointProxyProtocol(result),
95-
ForwardedHeaders: makeEntryPointForwardedHeaders(result),
82+
Address: result["address"],
83+
TLS: configTLS,
84+
Auth: makeEntryPointAuth(result),
85+
Redirect: makeEntryPointRedirect(result),
86+
Compress: compress,
87+
WhiteList: makeWhiteList(result),
88+
ProxyProtocol: makeEntryPointProxyProtocol(result),
89+
ForwardedHeaders: makeEntryPointForwardedHeaders(result),
90+
ClientIPStrategy: makeIPStrategy("clientipstrategy", result),
9691
}
9792

9893
return nil
9994
}
10095

10196
func makeWhiteList(result map[string]string) *types.WhiteList {
102-
var wl *types.WhiteList
10397
if rawRange, ok := result["whitelist_sourcerange"]; ok {
104-
wl = &types.WhiteList{
105-
SourceRange: strings.Split(rawRange, ","),
106-
UseXForwardedFor: toBool(result, "whitelist_usexforwardedfor"),
98+
return &types.WhiteList{
99+
SourceRange: strings.Split(rawRange, ","),
100+
IPStrategy: makeIPStrategy("whitelist_ipstrategy", result),
107101
}
108102
}
109-
return wl
103+
return nil
104+
}
105+
106+
func makeIPStrategy(prefix string, result map[string]string) *types.IPStrategy {
107+
depth := toInt(result, prefix+"_depth")
108+
excludedIPs := result[prefix+"_excludedips"]
109+
110+
if depth == 0 && len(excludedIPs) == 0 {
111+
return nil
112+
}
113+
114+
return &types.IPStrategy{
115+
Depth: depth,
116+
ExcludedIPs: strings.Split(excludedIPs, ","),
117+
}
110118
}
111119

112120
func makeEntryPointAuth(result map[string]string) *types.Auth {
@@ -184,15 +192,14 @@ func makeEntryPointProxyProtocol(result map[string]string) *ProxyProtocol {
184192
}
185193

186194
if proxyProtocol != nil && proxyProtocol.Insecure {
187-
log.Warn("ProxyProtocol.Insecure:true is dangerous. Please use 'ProxyProtocol.TrustedIPs:IPs' and remove 'ProxyProtocol.Insecure:true'")
195+
log.Warn("ProxyProtocol.insecure:true is dangerous. Please use 'ProxyProtocol.TrustedIPs:IPs' and remove 'ProxyProtocol.insecure:true'")
188196
}
189197

190198
return proxyProtocol
191199
}
192200

193201
func makeEntryPointForwardedHeaders(result map[string]string) *ForwardedHeaders {
194-
// TODO must be changed to false by default in the next breaking version.
195-
forwardedHeaders := &ForwardedHeaders{Insecure: true}
202+
forwardedHeaders := &ForwardedHeaders{}
196203
if _, ok := result["forwardedheaders_insecure"]; ok {
197204
forwardedHeaders.Insecure = toBool(result, "forwardedheaders_insecure")
198205
}
@@ -300,3 +307,14 @@ func toBool(conf map[string]string, key string) bool {
300307
}
301308
return false
302309
}
310+
311+
func toInt(conf map[string]string, key string) int {
312+
if val, ok := conf[key]; ok {
313+
intVal, err := strconv.Atoi(val)
314+
if err != nil {
315+
return 0
316+
}
317+
return intVal
318+
}
319+
return 0
320+
}

0 commit comments

Comments
 (0)