diff --git a/testsuite/gateway/gateway_api/gateway.py b/testsuite/gateway/gateway_api/gateway.py index ffa2237e..07e16a02 100644 --- a/testsuite/gateway/gateway_api/gateway.py +++ b/testsuite/gateway/gateway_api/gateway.py @@ -125,7 +125,7 @@ def get_tls_cert(self) -> Optional[Certificate]: tls_cert = Certificate( key=tls_cert_secret["tls.key"], certificate=tls_cert_secret["tls.crt"], - chain=tls_cert_secret["ca.crt"], + chain=tls_cert_secret["ca.crt"] if "ca.crt" in tls_cert_secret else None, ) return tls_cert diff --git a/testsuite/gateway/gateway_api/hostname.py b/testsuite/gateway/gateway_api/hostname.py index 02f3bf03..d678540d 100644 --- a/testsuite/gateway/gateway_api/hostname.py +++ b/testsuite/gateway/gateway_api/hostname.py @@ -68,4 +68,6 @@ def __init__(self, base_domain, tls_cert: Certificate = None): self.tls_cert = tls_cert def expose_hostname(self, name, gateway: Gateway) -> Hostname: - return StaticHostname(f"{name}.{self.base_domain}", gateway.get_tls_cert()) + return StaticHostname( + f"{name}.{self.base_domain}", gateway.get_tls_cert() if self.tls_cert is None else self.tls_cert + ) diff --git a/testsuite/resources/letsencrypt-stg-root-x1.pem b/testsuite/resources/letsencrypt-stg-root-x1.pem new file mode 100644 index 00000000..37655b25 --- /dev/null +++ b/testsuite/resources/letsencrypt-stg-root-x1.pem @@ -0,0 +1,32 @@ +-----BEGIN CERTIFICATE----- +MIIFmDCCA4CgAwIBAgIQU9C87nMpOIFKYpfvOHFHFDANBgkqhkiG9w0BAQsFADBm +MQswCQYDVQQGEwJVUzEzMDEGA1UEChMqKFNUQUdJTkcpIEludGVybmV0IFNlY3Vy +aXR5IFJlc2VhcmNoIEdyb3VwMSIwIAYDVQQDExkoU1RBR0lORykgUHJldGVuZCBQ +ZWFyIFgxMB4XDTE1MDYwNDExMDQzOFoXDTM1MDYwNDExMDQzOFowZjELMAkGA1UE +BhMCVVMxMzAxBgNVBAoTKihTVEFHSU5HKSBJbnRlcm5ldCBTZWN1cml0eSBSZXNl +YXJja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RFAsnP+ULx7k0aO1x +qAeaAhQ2rgo1F58hcflgqKTXnpPM02intVfiVVkX5GXpJjK5EoQtLceyGOrkxlM/ +sTPq4UrnypmsqSagWV3HcUlYtDinc+nukFk6eR4XkzXBbwKajl0YjztfrCIHOn5Q +CJL6TERVDbM/aAPly8kJ1sWGLuvvWYzMYgLzDul//rUF10gEMWaXVZV51KpS9DY/ +5CunuvCXmEQJHo7kGcViT7sETn6Jz9KOhvYcXkJ7po6d93A/jy4GKPIPnsKKNEmR +xUuXY4xRdh45tMJnLTUDdC9FIU0flTeO9/vNpVA8OPU1i14vCz+MU8KX1bV3GXm/ +fxlB7VBBjX9v5oUep0o/j68R/iDlCOM4VVfRa8gX6T2FU7fNdatvGro7uQzIvWof +gN9WUwCbEMBy/YhBSrXycKA8crgGg3x1mIsopn88JKwmMBa68oS7EHM9w7C4y71M +7DiA+/9Qdp9RBWJpTS9i/mDnJg1xvo8Xz49mrrgfmcAXTCJqXi24NatI3Oc= +-----END CERTIFICATE----- diff --git a/testsuite/tests/mgc/conftest.py b/testsuite/tests/mgc/conftest.py index 120887de..ea92c500 100644 --- a/testsuite/tests/mgc/conftest.py +++ b/testsuite/tests/mgc/conftest.py @@ -51,7 +51,7 @@ def hub_gateway(request, hub_openshift, blame, base_domain, module_label) -> MGC @pytest.fixture(scope="session") -def self_signed_cluster_issuer(): +def cluster_issuer(): """Reference to cluster self-signed certificate issuer""" return CustomReference( group="cert-manager.io", @@ -111,13 +111,13 @@ def dns_policy(blame, hub_gateway, module_label): @pytest.fixture(scope="module") -def tls_policy(blame, hub_gateway, module_label, self_signed_cluster_issuer): +def tls_policy(blame, hub_gateway, module_label, cluster_issuer): """TLSPolicy fixture""" policy = TLSPolicy.create_instance( hub_gateway.openshift, blame("tls"), parent=hub_gateway, - issuer=self_signed_cluster_issuer, + issuer=cluster_issuer, labels={"app": module_label}, ) return policy diff --git a/testsuite/tests/mgc/test_external_ca.py b/testsuite/tests/mgc/test_external_ca.py new file mode 100644 index 00000000..936bfad7 --- /dev/null +++ b/testsuite/tests/mgc/test_external_ca.py @@ -0,0 +1,77 @@ +""" +This module contains the most basic happy path test for both DNSPolicy and TLSPolicy +for a cluster with Let's Encrypt ClusterIssuer + +Prerequisites: +* multi-cluster-gateways ns is created and set as openshift["project"] +* managedclustersetbinding is created in openshift["project"] +* gateway class "kuadrant-multi-cluster-gateway-instance-per-cluster" is created +* cert-manager Operator installed +* Let's Encrypt ClusterIssuer object configured on the cluster matching the template: +apiVersion: cert-manager.io/v1 +kind: ClusterIssuer +metadata: + name: letsencrypt-staging +spec: + acme: + email: + preferredChain: ISRG Root X1 + privateKeySecretRef: + name: letsencrypt-private-key + server: 'https://acme-staging-v02.api.letsencrypt.org/directory' + solvers: + - dns01: + route53: + accessKeyID: + hostedZoneID: + region: + secretAccessKeySecretRef: + key: awsSecretAccessKey + name: aws-secret +""" + +import dataclasses +from importlib import resources + +import pytest +from openshift_client import selector +from openshift_client.model import OpenShiftPythonException + +from testsuite.gateway import Exposer, CustomReference +from testsuite.gateway.gateway_api.hostname import DNSPolicyExposer + +pytestmark = [pytest.mark.mgc] + + +@pytest.fixture(scope="module") +def cluster_issuer(): + """Reference to cluster Let's Encrypt certificate issuer""" + try: + selector("clusterissuer/letsencrypt-staging").object() + except OpenShiftPythonException as exc: + pytest.skip(f"letsencrypt-staging ClusterIssuer is not present on the cluster: {exc}") + return CustomReference( + group="cert-manager.io", + kind="ClusterIssuer", + name="letsencrypt-staging", + ) + + +@pytest.fixture(scope="module") +def exposer(base_domain, hub_gateway) -> Exposer: + """DNSPolicyExposer setup with expected TLS certificate""" + root_cert = resources.files("testsuite.resources").joinpath("letsencrypt-stg-root-x1.pem").read_text() + old_cert = hub_gateway.get_tls_cert() + return DNSPolicyExposer(base_domain, tls_cert=dataclasses.replace(old_cert, chain=old_cert.certificate + root_cert)) + + +def test_smoke_letsencrypt(client): + """ + Tests whether the backend, exposed using the HTTPRoute and Gateway, was exposed correctly, + having a tls secured endpoint with a hostname managed by MGC + """ + + result = client.get("/get") + assert not result.has_dns_error() + assert not result.has_cert_verify_error() + assert result.status_code == 200