|
| 1 | +// Copyright 2019 HAProxy Technologies LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build e2e_sequential |
| 16 | + |
| 17 | +package accesscontrol |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/haproxytech/kubernetes-ingress/deploy/tests/e2e" |
| 23 | +) |
| 24 | + |
| 25 | +func (suite *AccessControlSuite) eventuallyReturns(clientIP string, httpStatus int) { |
| 26 | + suite.Eventually(func() bool { |
| 27 | + suite.client.Req.Header = map[string][]string{ |
| 28 | + "X-Client-IP": {clientIP}, |
| 29 | + } |
| 30 | + res, cls, err := suite.client.Do() |
| 31 | + if err != nil { |
| 32 | + suite.T().Logf("Connection ERROR: %s", err.Error()) |
| 33 | + return false |
| 34 | + } |
| 35 | + defer cls() |
| 36 | + return res.StatusCode == httpStatus |
| 37 | + }, e2e.WaitDuration, e2e.TickDuration, "waiting for call with client IP %v to return %v expired", clientIP, httpStatus) |
| 38 | +} |
| 39 | + |
| 40 | +func (suite *AccessControlSuite) Test_Whitelist() { |
| 41 | + suite.Run("Inline", func() { |
| 42 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 43 | + {"src-ip-header", " X-Client-IP"}, |
| 44 | + {"whitelist", " 192.168.2.0/24"}, |
| 45 | + } |
| 46 | + |
| 47 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 48 | + |
| 49 | + suite.eventuallyReturns("192.168.2.3", http.StatusOK) |
| 50 | + suite.eventuallyReturns("192.168.5.3", http.StatusForbidden) |
| 51 | + }) |
| 52 | + |
| 53 | + suite.Run("Patternfile", func() { |
| 54 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 55 | + {"src-ip-header", " X-Client-IP"}, |
| 56 | + {"whitelist", " patterns/ips"}, |
| 57 | + } |
| 58 | + |
| 59 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 60 | + suite.NoError(suite.test.Apply("config/patternfile-a.yml", "", nil)) |
| 61 | + |
| 62 | + suite.eventuallyReturns("192.168.0.3", http.StatusOK) |
| 63 | + suite.eventuallyReturns("192.168.2.3", http.StatusForbidden) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func (suite *AccessControlSuite) Test_Blacklist() { |
| 68 | + suite.Run("Inline", func() { |
| 69 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 70 | + {"src-ip-header", " X-Client-IP"}, |
| 71 | + {"blacklist", " 192.168.2.0/24"}, |
| 72 | + } |
| 73 | + |
| 74 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 75 | + |
| 76 | + suite.eventuallyReturns("192.168.2.3", http.StatusForbidden) |
| 77 | + suite.eventuallyReturns("192.168.5.3", http.StatusOK) |
| 78 | + }) |
| 79 | + |
| 80 | + suite.Run("Patternfile", func() { |
| 81 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 82 | + {"src-ip-header", " X-Client-IP"}, |
| 83 | + {"blacklist", " patterns/ips"}, |
| 84 | + } |
| 85 | + |
| 86 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 87 | + suite.NoError(suite.test.Apply("config/patternfile-a.yml", "", nil)) |
| 88 | + |
| 89 | + suite.eventuallyReturns("192.168.0.3", http.StatusForbidden) |
| 90 | + suite.eventuallyReturns("192.168.2.3", http.StatusOK) |
| 91 | + }) |
| 92 | +} |
0 commit comments