Skip to content
This repository was archived by the owner on Apr 17, 2019. It is now read-only.

Commit 64f4cfe

Browse files
committed
Add example
1 parent 3d958cc commit 64f4cfe

File tree

6 files changed

+155
-17
lines changed

6 files changed

+155
-17
lines changed

ingress/controllers/nginx/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ func (lbc *loadBalancerController) getUpstreamServers(ngxCfg config.Configuratio
698698
glog.V(3).Infof("error parsing rewrite annotations for Ingress rule %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
699699
}
700700

701-
wl, err := ipwhitelist.ParseAnnotations(ngxCfg.WhiteList, ing)
701+
wl, err := ipwhitelist.ParseAnnotations(ngxCfg.WhitelistSourceRange, ing)
702702
glog.V(3).Infof("nginx white list %v", wl)
703703
if err != nil {
704704
glog.V(3).Infof("error reading white list annotation in Ingress %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
This example shows how is possible to restrict access
3+
4+
echo "
5+
apiVersion: extensions/v1beta1
6+
kind: Ingress
7+
metadata:
8+
name: whitelist
9+
annotations:
10+
ingress.kubernetes.io/whitelist-source-range: "1.1.1.1/24"
11+
spec:
12+
rules:
13+
- host: foo.bar.com
14+
http:
15+
paths:
16+
- path: /
17+
backend:
18+
serviceName: echoheaders
19+
servicePort: 80
20+
" | kubectl create -f -
21+
22+
23+
Check the annotation is present in the Ingress rule:
24+
```
25+
$ kubectl get ingress whitelist -o yaml
26+
apiVersion: extensions/v1beta1
27+
kind: Ingress
28+
metadata:
29+
annotations:
30+
ingress.kubernetes.io/whitelist-source-range: 1.1.1.1/24
31+
creationTimestamp: 2016-06-09T21:39:06Z
32+
generation: 2
33+
name: whitelist
34+
namespace: default
35+
resourceVersion: "419363"
36+
selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/whitelist
37+
uid: 97b74737-2e8a-11e6-90db-080027d2dc94
38+
spec:
39+
rules:
40+
- host: whitelist.bar.com
41+
http:
42+
paths:
43+
- backend:
44+
serviceName: echoheaders
45+
servicePort: 80
46+
path: /
47+
status:
48+
loadBalancer:
49+
ingress:
50+
- ip: 172.17.4.99
51+
``
52+
53+
Finally test is not possible to access the URL
54+
55+
```
56+
$ curl -v http://172.17.4.99/ -H 'Host: whitelist.bar.com'
57+
* Trying 172.17.4.99...
58+
* Connected to 172.17.4.99 (172.17.4.99) port 80 (#0)
59+
> GET / HTTP/1.1
60+
> Host: whitelist.bar.com
61+
> User-Agent: curl/7.43.0
62+
> Accept: */*
63+
>
64+
< HTTP/1.1 403 Forbidden
65+
< Server: nginx/1.11.1
66+
< Date: Thu, 09 Jun 2016 21:56:17 GMT
67+
< Content-Type: text/html
68+
< Content-Length: 169
69+
< Connection: keep-alive
70+
<
71+
<html>
72+
<head><title>403 Forbidden</title></head>
73+
<body bgcolor="white">
74+
<center><h1>403 Forbidden</h1></center>
75+
<hr><center>nginx/1.11.1</center>
76+
</body>
77+
</html>
78+
* Connection #0 to host 172.17.4.99 left intact
79+
```
80+
81+
Removing the annotation removes the restriction
82+
83+
```
84+
* Trying 172.17.4.99...
85+
* Connected to 172.17.4.99 (172.17.4.99) port 80 (#0)
86+
> GET / HTTP/1.1
87+
> Host: whitelist.bar.com
88+
> User-Agent: curl/7.43.0
89+
> Accept: */*
90+
>
91+
< HTTP/1.1 200 OK
92+
< Server: nginx/1.11.1
93+
< Date: Thu, 09 Jun 2016 21:57:44 GMT
94+
< Content-Type: text/plain
95+
< Transfer-Encoding: chunked
96+
< Connection: keep-alive
97+
<
98+
CLIENT VALUES:
99+
client_address=10.2.89.7
100+
command=GET
101+
real path=/
102+
query=nil
103+
request_version=1.1
104+
request_uri=http://whitelist.bar.com:8080/
105+
106+
SERVER VALUES:
107+
server_version=nginx: 1.9.11 - lua: 10001
108+
109+
HEADERS RECEIVED:
110+
accept=*/*
111+
connection=close
112+
host=whitelist.bar.com
113+
user-agent=curl/7.43.0
114+
x-forwarded-for=10.2.89.1
115+
x-forwarded-host=whitelist.bar.com
116+
x-forwarded-port=80
117+
x-forwarded-proto=http
118+
x-real-ip=10.2.89.1
119+
BODY:
120+
* Connection #0 to host 172.17.4.99 left intact
121+
```
122+

ingress/controllers/nginx/nginx/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ type Configuration struct {
233233
// Responses with the “text/html” type are always compressed if UseGzip is enabled
234234
GzipTypes string `structs:"gzip-types,omitempty"`
235235

236-
// WhiteList allows limiting access to certain client addresses.
236+
// WhitelistSourceRange allows limiting access to certain client addresses
237237
// http://nginx.org/en/docs/http/ngx_http_access_module.html
238-
WhiteList []string `structs:"whitelist,omitempty"`
238+
WhitelistSourceRange []string `structs:"whitelist-source-range,omitempty"`
239239

240240
// Defines the number of worker processes. By default auto means number of available CPU cores
241241
// http://nginx.org/en/docs/ngx_core_module.html#worker_processes
@@ -274,7 +274,7 @@ func NewDefault() Configuration {
274274
VtsStatusZoneSize: "10m",
275275
UseHTTP2: true,
276276
CustomHTTPErrors: make([]int, 0),
277-
WhiteList: make([]string, 0),
277+
WhitelistSourceRange: make([]string, 0),
278278
}
279279

280280
if glog.V(5) {

ingress/controllers/nginx/nginx/ipwhitelist/main.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ package ipwhitelist
1818

1919
import (
2020
"errors"
21+
"strings"
2122

2223
"k8s.io/kubernetes/pkg/apis/extensions"
2324
"k8s.io/kubernetes/pkg/util/net/sets"
2425
)
2526

2627
const (
27-
whitelist = "ingress.kubernetes.io/whitelist"
28+
whitelist = "ingress.kubernetes.io/whitelist-source-range"
2829
)
2930

3031
var (
@@ -37,8 +38,8 @@ var (
3738
ErrInvalidCIDR = errors.New("the annotation does not contains a valid IP address or network")
3839
)
3940

40-
// Whitelist returns the CIDR
41-
type Whitelist struct {
41+
// SourceRange returns the CIDR
42+
type SourceRange struct {
4243
CIDR []string
4344
}
4445

@@ -50,30 +51,33 @@ func (a ingAnnotations) whitelist() ([]string, error) {
5051
return nil, ErrMissingWhitelist
5152
}
5253

53-
ipnet, err := sets.ParseIPNets(val)
54+
values := strings.Split(val, ",")
55+
ipnets, err := sets.ParseIPNets(values...)
5456
if err != nil {
5557
return nil, ErrInvalidCIDR
5658
}
5759

58-
nets := make([]string, 0)
59-
for k := range ipnet {
60-
nets = append(nets, k)
60+
cidrs := make([]string, 0)
61+
for k := range ipnets {
62+
cidrs = append(cidrs, k)
6163
}
6264

63-
return nets, nil
65+
return cidrs, nil
6466
}
6567

6668
// ParseAnnotations parses the annotations contained in the ingress
67-
// rule used to configure upstream check parameters
68-
func ParseAnnotations(whiteList []string, ing *extensions.Ingress) (*Whitelist, error) {
69+
// rule used to limit access to certain client addresses or networks.
70+
// Multiple ranges can specified using commas as separator
71+
// e.g. `18.0.0.0/8,56.0.0.0/8`
72+
func ParseAnnotations(whiteList []string, ing *extensions.Ingress) (*SourceRange, error) {
6973
if ing.GetAnnotations() == nil {
70-
return &Whitelist{whiteList}, ErrMissingWhitelist
74+
return &SourceRange{whiteList}, ErrMissingWhitelist
7175
}
7276

7377
wl, err := ingAnnotations(ing.GetAnnotations()).whitelist()
7478
if err != nil {
7579
wl = whiteList
7680
}
7781

78-
return &Whitelist{wl}, err
82+
return &SourceRange{wl}, err
7983
}

ingress/controllers/nginx/nginx/ipwhitelist/main_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,16 @@ func TestAnnotations(t *testing.T) {
8383
if !reflect.DeepEqual(wl, enet) {
8484
t.Errorf("Expected %v but returned %s", enet, wl)
8585
}
86+
87+
data[whitelist] = "10.0.0.0/24,10.0.1.0/25"
88+
ing.SetAnnotations(data)
89+
90+
wl, err = ingAnnotations(ing.GetAnnotations()).whitelist()
91+
if err != nil {
92+
t.Errorf("Unexpected error: %v", err)
93+
}
94+
95+
if len(wl) != 2 {
96+
t.Errorf("Expected 2 netwotks but %v was returned", len(wl))
97+
}
8698
}

ingress/controllers/nginx/nginx/nginx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type Location struct {
100100
RateLimit ratelimit.RateLimit
101101
Redirect rewrite.Redirect
102102
SecureUpstream bool
103-
Whitelist ipwhitelist.Whitelist
103+
Whitelist ipwhitelist.SourceRange
104104
}
105105

106106
// LocationByPath sorts location by path

0 commit comments

Comments
 (0)