def fin(): delete_crd(kube_apis.api_extensions_v1_beta1, vs_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, vsr_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, pol_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, ts_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, gc_crd_name) print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml") print("Remove the IC:") delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments["deployment-type"], namespace)
def test_responses_after_rbac_misconfiguration(self, kube_apis, crd_ingress_controller, virtual_server_setup): print("\nStep 1: rbac misconfiguration from the very start") resp = requests.get(virtual_server_setup.backend_1_url, headers={"host": virtual_server_setup.vs_host}) assert resp.status_code == 404 resp = requests.get(virtual_server_setup.backend_2_url, headers={"host": virtual_server_setup.vs_host}) assert resp.status_code == 404 print("Step 2: configure RBAC and check") patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml") wait_and_assert_status_code(200, virtual_server_setup.backend_1_url, virtual_server_setup.vs_host) wait_and_assert_status_code(200, virtual_server_setup.backend_2_url, virtual_server_setup.vs_host)
def crd_ingress_controller(cli_arguments, kube_apis, ingress_controller_prerequisites, ingress_controller_endpoint, request) -> None: """ Create an Ingress Controller with CRD enabled. :param cli_arguments: pytest context :param kube_apis: client apis :param ingress_controller_prerequisites :param ingress_controller_endpoint: :param request: pytest fixture to parametrize this method {type: complete|rbac-without-vs, extra_args: } 'type' type of test pre-configuration 'extra_args' list of IC cli arguments :return: """ namespace = ingress_controller_prerequisites.namespace print( "------------------------- Update ClusterRole -----------------------------------" ) if request.param['type'] == 'rbac-without-vs': patch_rbac(kube_apis.rbac_v1_beta1, f"{TEST_DATA}/virtual-server/rbac-without-vs.yaml") print( "------------------------- Register CRD -----------------------------------" ) crd_names = create_crds_from_yaml( kube_apis.api_extensions_v1_beta1, f"{DEPLOYMENTS}/common/custom-resource-definitions.yaml") print( "------------------------- Create IC -----------------------------------" ) name = create_ingress_controller(kube_apis.v1, kube_apis.extensions_v1_beta1, cli_arguments, namespace, request.param.get('extra_args', None)) ensure_connection_to_public_endpoint(ingress_controller_endpoint.public_ip, ingress_controller_endpoint.port, ingress_controller_endpoint.port_ssl) def fin(): for crd_name in crd_names: print("Remove the CRD:") delete_crd(kube_apis.api_extensions_v1_beta1, crd_name) print("Remove the IC:") delete_ingress_controller(kube_apis.extensions_v1_beta1, name, cli_arguments['deployment-type'], namespace) print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml") request.addfinalizer(fin)
def fin(): print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1, f"{DEPLOYMENTS}/rbac/rbac.yaml") print("Remove the DNSEndpoint CRD:") delete_crd( kube_apis.api_extensions_v1, external_dns_crd_name, ) print("Remove the IC:") delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments["deployment-type"], namespace) replace_configmap_from_yaml( kube_apis.v1, ingress_controller_prerequisites.config_map["metadata"]["name"], ingress_controller_prerequisites.namespace, f"{DEPLOYMENTS}/common/nginx-config.yaml", )
def test_responses_after_rbac_misconfiguration_on_the_fly(self, kube_apis, crd_ingress_controller, virtual_server_setup): print("Step 10: remove virtualservers from the ClusterRole and check") patch_rbac(kube_apis.rbac_v1_beta1, f"{TEST_DATA}/virtual-server/rbac-without-vs.yaml") wait_before_test(1) resp = requests.get(virtual_server_setup.backend_1_url, headers={"host": virtual_server_setup.vs_host}) assert resp.status_code == 200 resp = requests.get(virtual_server_setup.backend_2_url, headers={"host": virtual_server_setup.vs_host}) assert resp.status_code == 200 print("Step 11: restore ClusterRole and check") patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml") wait_before_test(1) resp = requests.get(virtual_server_setup.backend_1_url, headers={"host": virtual_server_setup.vs_host}) assert resp.status_code == 200 resp = requests.get(virtual_server_setup.backend_2_url, headers={"host": virtual_server_setup.vs_host}) assert resp.status_code == 200
def crd_ingress_controller(cli_arguments, kube_apis, ingress_controller_prerequisites, ingress_controller_endpoint, request) -> None: """ Create an Ingress Controller with CRD enabled. :param cli_arguments: pytest context :param kube_apis: client apis :param ingress_controller_prerequisites :param ingress_controller_endpoint: :param request: pytest fixture to parametrize this method {type: complete|rbac-without-vs, extra_args: } 'type' type of test pre-configuration 'extra_args' list of IC cli arguments :return: """ namespace = ingress_controller_prerequisites.namespace name = "nginx-ingress" vs_crd_name = get_name_from_yaml( f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_virtualservers.yaml") vsr_crd_name = get_name_from_yaml( f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_virtualserverroutes.yaml" ) pol_crd_name = get_name_from_yaml( f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_policies.yaml") ts_crd_name = get_name_from_yaml( f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_transportservers.yaml" ) gc_crd_name = get_name_from_yaml( f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_globalconfigurations.yaml" ) try: print( "------------------------- Update ClusterRole -----------------------------------" ) if request.param["type"] == "rbac-without-vs": patch_rbac(kube_apis.rbac_v1, f"{TEST_DATA}/virtual-server/rbac-without-vs.yaml") print( "------------------------- Register CRDs -----------------------------------" ) create_crd_from_yaml( kube_apis.api_extensions_v1_beta1, vs_crd_name, f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_virtualservers.yaml", ) create_crd_from_yaml( kube_apis.api_extensions_v1_beta1, vsr_crd_name, f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_virtualserverroutes.yaml", ) create_crd_from_yaml( kube_apis.api_extensions_v1_beta1, pol_crd_name, f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_policies.yaml", ) create_crd_from_yaml( kube_apis.api_extensions_v1_beta1, ts_crd_name, f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_transportservers.yaml", ) create_crd_from_yaml( kube_apis.api_extensions_v1_beta1, gc_crd_name, f"{DEPLOYMENTS}/common/crds-v1beta1/k8s.nginx.org_globalconfigurations.yaml", ) print( "------------------------- Create IC -----------------------------------" ) name = create_ingress_controller( kube_apis.v1, kube_apis.apps_v1_api, cli_arguments, namespace, request.param.get("extra_args", None), ) ensure_connection_to_public_endpoint( ingress_controller_endpoint.public_ip, ingress_controller_endpoint.port, ingress_controller_endpoint.port_ssl, ) except ApiException as ex: # Finalizer method doesn't start if fixture creation was incomplete, ensure clean up here print( f"Failed to complete CRD IC fixture: {ex}\nClean up the cluster as much as possible." ) delete_crd(kube_apis.api_extensions_v1_beta1, vs_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, vsr_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, pol_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, ts_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, gc_crd_name) print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1, f"{DEPLOYMENTS}/rbac/rbac.yaml") print("Remove the IC:") delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments["deployment-type"], namespace) def fin(): delete_crd(kube_apis.api_extensions_v1_beta1, vs_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, vsr_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, pol_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, ts_crd_name) delete_crd(kube_apis.api_extensions_v1_beta1, gc_crd_name) print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1, f"{DEPLOYMENTS}/rbac/rbac.yaml") print("Remove the IC:") delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments["deployment-type"], namespace) request.addfinalizer(fin)
def crd_ingress_controller_with_ed(cli_arguments, kube_apis, ingress_controller_prerequisites, ingress_controller_endpoint, request, crds) -> None: """ Create an Ingress Controller with CRD enabled. :param crds: the common ingress controller crds. :param cli_arguments: pytest context :param kube_apis: client apis :param ingress_controller_prerequisites :param ingress_controller_endpoint: :param request: pytest fixture to parametrize this method {type: complete|rbac-without-vs, extra_args: } 'type' type of test pre-configuration 'extra_args' list of IC cli arguments :return: """ namespace = ingress_controller_prerequisites.namespace name = "nginx-ingress" print( "---------------------- Register DNSEndpoint CRD ------------------------------" ) external_dns_crd_name = get_name_from_yaml( f"{DEPLOYMENTS}/common/crds/externaldns.nginx.org_dnsendpoints.yaml") create_crd_from_yaml( kube_apis.api_extensions_v1, external_dns_crd_name, f"{DEPLOYMENTS}/common/crds/externaldns.nginx.org_dnsendpoints.yaml", ) try: print( "------------------------- Create IC -----------------------------------" ) name = create_ingress_controller( kube_apis.v1, kube_apis.apps_v1_api, cli_arguments, namespace, request.param.get("extra_args", None), ) ensure_connection_to_public_endpoint( ingress_controller_endpoint.public_ip, ingress_controller_endpoint.port, ingress_controller_endpoint.port_ssl, ) print( "---------------- Replace ConfigMap with external-status-address --------------------" ) cm_source = f"{TEST_DATA}/virtual-server-external-dns/nginx-config.yaml" replace_configmap_from_yaml( kube_apis.v1, ingress_controller_prerequisites.config_map['metadata']['name'], ingress_controller_prerequisites.namespace, cm_source) except ApiException as ex: # Finalizer method doesn't start if fixture creation was incomplete, ensure clean up here print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1, f"{DEPLOYMENTS}/rbac/rbac.yaml") print("Remove the DNSEndpoint CRD:") delete_crd( kube_apis.api_extensions_v1, external_dns_crd_name, ) print("Remove the IC:") delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments["deployment-type"], namespace) replace_configmap_from_yaml( kube_apis.v1, ingress_controller_prerequisites.config_map["metadata"]["name"], ingress_controller_prerequisites.namespace, f"{DEPLOYMENTS}/common/nginx-config.yaml", ) pytest.fail("IC setup failed") def fin(): print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1, f"{DEPLOYMENTS}/rbac/rbac.yaml") print("Remove the DNSEndpoint CRD:") delete_crd( kube_apis.api_extensions_v1, external_dns_crd_name, ) print("Remove the IC:") delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments["deployment-type"], namespace) replace_configmap_from_yaml( kube_apis.v1, ingress_controller_prerequisites.config_map["metadata"]["name"], ingress_controller_prerequisites.namespace, f"{DEPLOYMENTS}/common/nginx-config.yaml", ) request.addfinalizer(fin)
def fin(): print("Restore the ClusterRole:") patch_rbac(kube_apis.rbac_v1, f"{DEPLOYMENTS}/rbac/rbac.yaml") print("Remove the IC:") delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments["deployment-type"], namespace)