|
6 | 6 | from urllib.parse import urlparse
|
7 | 7 | from subprocess import Popen, PIPE, STDOUT
|
8 | 8 | import uuid
|
9 |
| -import logging |
10 | 9 | import json
|
11 | 10 | import requests
|
12 | 11 | import yaml
|
13 |
| -from werkzeug.exceptions import HTTPException, NotFound |
14 |
| -from flask import Blueprint, config, jsonify, request, Response, make_response, abort, g, current_app as app |
15 |
| -from io import TextIOWrapper |
| 12 | +from werkzeug.exceptions import HTTPException |
| 13 | +from flask import Blueprint, jsonify, request, make_response, abort, current_app as app |
16 | 14 | from clients.ocp_routes import get_host_list, get_route_overrides
|
17 | 15 |
|
18 | 16 | from v2.auth.auth import admin_jwt, uma_enforce
|
19 |
| - |
20 | 17 | from v2.services.namespaces import NamespaceService
|
21 | 18 |
|
22 | 19 | from clients.portal import record_gateway_event
|
23 | 20 | from clients.kong import get_routes, register_kong_certs
|
24 |
| -from clients.ocp_networksecuritypolicy import get_ocp_service_namespaces, check_nsp, apply_nsp, delete_nsp |
25 |
| -from clients.ocp_routes import prepare_apply_routes, prepare_delete_routes, apply_routes, delete_routes |
26 |
| -from clients.ocp_gateway_secret import prep_submitted_config, prep_and_apply_secret, write_submitted_config |
27 |
| - |
28 |
| -from utils.validators import host_valid |
| 21 | +from clients.ocp_gateway_secret import prep_submitted_config |
| 22 | +from utils.validators import host_valid, validate_upstream |
29 | 23 | from utils.transforms import plugins_transformations
|
30 | 24 | from utils.masking import mask
|
31 | 25 |
|
@@ -273,7 +267,11 @@ def write_config(namespace: str) -> object:
|
273 | 267 | # Validate upstream URLs are valid
|
274 | 268 | try:
|
275 | 269 | protected_kube_namespaces = json.loads(app.config['protectedKubeNamespaces'])
|
276 |
| - validate_upstream(gw_config, ns_attributes, protected_kube_namespaces) |
| 270 | + |
| 271 | + do_validate_upstreams = app.config['data_planes'][dp].get("validate-upstreams", False) |
| 272 | + |
| 273 | + log.debug("Validate upstreams %s %s" % (dp, do_validate_upstreams)) |
| 274 | + validate_upstream(gw_config, ns_attributes, protected_kube_namespaces, do_validate_upstreams) |
277 | 275 | except Exception as ex:
|
278 | 276 | traceback.print_exc()
|
279 | 277 | log.error("%s - %s" % (namespace, " Upstream Validation Errors: %s" % ex))
|
@@ -509,54 +507,6 @@ def transform_host(host):
|
509 | 507 | else:
|
510 | 508 | return host
|
511 | 509 |
|
512 |
| -def validate_upstream(yaml, ns_attributes, protected_kube_namespaces): |
513 |
| - errors = [] |
514 |
| - |
515 |
| - allow_protected_ns = ns_attributes.get('perm-protected-ns', ['deny'])[0] == 'allow' |
516 |
| - |
517 |
| - # A host must not contain a list of protected |
518 |
| - if 'services' in yaml: |
519 |
| - for service in yaml['services']: |
520 |
| - if 'url' in service: |
521 |
| - try: |
522 |
| - u = urlparse(service["url"]) |
523 |
| - if u.hostname is None: |
524 |
| - errors.append("service upstream has invalid url specified (e1)") |
525 |
| - else: |
526 |
| - validate_upstream_host(u.hostname, errors, allow_protected_ns, protected_kube_namespaces) |
527 |
| - except Exception as e: |
528 |
| - errors.append("service upstream has invalid url specified (e2)") |
529 |
| - |
530 |
| - if 'host' in service: |
531 |
| - host = service["host"] |
532 |
| - validate_upstream_host(host, errors, allow_protected_ns, protected_kube_namespaces) |
533 |
| - |
534 |
| - if len(errors) != 0: |
535 |
| - raise Exception('\n'.join(errors)) |
536 |
| - |
537 |
| - |
538 |
| -def validate_upstream_host(_host, errors, allow_protected_ns, protected_kube_namespaces): |
539 |
| - host = _host.lower() |
540 |
| - |
541 |
| - restricted = ['localhost', '127.0.0.1', '0.0.0.0'] |
542 |
| - |
543 |
| - if host in restricted: |
544 |
| - errors.append("service upstream is invalid (e1)") |
545 |
| - if host.endswith('svc'): |
546 |
| - partials = host.split('.') |
547 |
| - # get the namespace, and make sure it is not in the protected_kube_namespaces list |
548 |
| - if len(partials) != 3: |
549 |
| - errors.append("service upstream is invalid (e2)") |
550 |
| - elif partials[1] in protected_kube_namespaces and allow_protected_ns is False: |
551 |
| - errors.append("service upstream is invalid (e3)") |
552 |
| - if host.endswith('svc.cluster.local'): |
553 |
| - partials = host.split('.') |
554 |
| - # get the namespace, and make sure it is not in the protected_kube_namespaces list |
555 |
| - if len(partials) != 5: |
556 |
| - errors.append("service upstream is invalid (e4)") |
557 |
| - elif partials[1] in protected_kube_namespaces and allow_protected_ns is False: |
558 |
| - errors.append("service upstream is invalid (e5)") |
559 |
| - |
560 | 510 | def update_routes_check(yaml):
|
561 | 511 | if 'services' in yaml or 'upstreams' not in yaml:
|
562 | 512 | return True
|
|
0 commit comments