Skip to content

Commit

Permalink
ensure cert provided for custom domains
Browse files Browse the repository at this point in the history
  • Loading branch information
rustyjux committed Nov 15, 2024
1 parent 268c77f commit 545a159
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
1 change: 0 additions & 1 deletion microservices/gatewayApi/v2/routes/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ def is_host_custom_domain(host):
'.cluster.local',
'.api.gov.bc.ca',
'.data.gov.bc.ca',
'.webapps.gov.bc.ca',
'.maps.gov.bc.ca',
'.openmaps.gov.bc.ca',
'.apps.gov.bc.ca',
Expand Down
44 changes: 33 additions & 11 deletions microservices/kubeApi/clients/ocp_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,20 @@ def prepare_apply_routes(ns, select_tag, hosts, root_path, data_plane, ns_templa
ssl_ref = "tls"
custom_cert_found = False

if certificates:
# Look for a matching certificate by SNI
for cert in certificates:
if host in cert['snis']:
ssl_key = format_pem_data(cert['key'])
ssl_crt = format_pem_data(cert['cert'])
logger.debug("[%s] Route A %03d using custom cert with SNI match for %s" % (select_tag, index, host))
custom_cert_found = True
break

if is_host_custom_domain(host):
logger.debug("[%s] Route A %03d Searching for custom cert for %s" % (select_tag, index, host))
if certificates:
# Look for a matching certificate by SNI
for cert in certificates:
if host in cert['snis']:
ssl_key = format_pem_data(cert['key'])
ssl_crt = format_pem_data(cert['cert'])
logger.debug("[%s] Route A %03d Found custom cert with SNI match for %s" % (select_tag, index, host))
custom_cert_found = True
break
if not custom_cert_found:
raise Exception("Custom certificate not found for host %s" % host)

if not custom_cert_found and not settings.host_transformation['enabled']:
# Fall back to existing cert mapping logic
for host_match, ssl_file_prefix in host_cert_mapping.items():
Expand Down Expand Up @@ -239,4 +243,22 @@ def get_gwa_ocp_routes(extralabels=""):
return json.loads(out)['items']

def time_secs():
return int(time.time())
return int(time.time())

def is_host_custom_domain(host):
non_custom_suffixes = [
'.cluster.local',
'.api.gov.bc.ca',
'.data.gov.bc.ca',
'.maps.gov.bc.ca',
'.openmaps.gov.bc.ca',
'.apps.gov.bc.ca',
'.apis.gov.bc.ca'
]

# Check if the host is one of the standard cert domains or a subdomain of them
for suffix in non_custom_suffixes:
if host == suffix[1:] or host.endswith(suffix):
return False

return True

0 comments on commit 545a159

Please sign in to comment.