Skip to content

Commit f4a6bbe

Browse files
authored
allow exact match upstream host validation (#143)
1 parent eed2c51 commit f4a6bbe

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

microservices/gatewayApi/tests/utils/test_validate_upstream.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ def test_upstream_pass_validation(app):
137137

138138
validate_upstream (y, { "perm-upstreams": ["my-namespace"]}, [], True)
139139

140+
def test_upstream_pass_validation_exact_match(app):
141+
payload = '''
142+
services:
143+
- name: my-service
144+
tags: ["ns.mytest", "another"]
145+
host: 192.168.1.1
146+
'''
147+
y = yaml.load(payload, Loader=yaml.FullLoader)
148+
149+
validate_upstream (y, { "perm-upstreams": ["192.168.1.1"]}, [], True)
150+
140151
def test_upstream_fail_validation(app):
141152
payload = '''
142153
services:

microservices/gatewayApi/utils/validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def validate_upstream_host(_host, errors, allow_protected_ns, protected_kube_nam
5252

5353
if host in restricted:
5454
errors.append("service upstream is invalid (e1)")
55-
elif host.endswith('svc'):
55+
elif host.endswith('.svc'):
5656
partials = host.split('.')
5757
# get the namespace, and make sure it is not in the protected_kube_namespaces list
5858
if len(partials) != 3:
@@ -61,7 +61,7 @@ def validate_upstream_host(_host, errors, allow_protected_ns, protected_kube_nam
6161
errors.append("service upstream is invalid (e3)")
6262
elif do_validate_upstreams and (partials[1] in perm_upstreams) is False:
6363
errors.append("service upstream is invalid (e6)")
64-
elif host.endswith('svc.cluster.local'):
64+
elif host.endswith('.svc.cluster.local'):
6565
partials = host.split('.')
6666
# get the namespace, and make sure it is not in the protected_kube_namespaces list
6767
if len(partials) != 5:
@@ -70,5 +70,5 @@ def validate_upstream_host(_host, errors, allow_protected_ns, protected_kube_nam
7070
errors.append("service upstream is invalid (e5)")
7171
elif do_validate_upstreams and (partials[1] in perm_upstreams) is False:
7272
errors.append("service upstream is invalid (e6)")
73-
elif do_validate_upstreams:
73+
elif do_validate_upstreams and (host in perm_upstreams) is False:
7474
errors.append("service upstream is invalid (e6)")

0 commit comments

Comments
 (0)