2
0

test_mtls-client-certificate.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pathlib
  2. import pytest
  3. from requests.exceptions import SSLError
  4. @pytest.fixture(scope="session")
  5. def clientcerts():
  6. """
  7. Pytest fixture to provide paths to client certificates and keys.
  8. """
  9. current_file_path = pathlib.Path(__file__)
  10. clientcerts_path = current_file_path.parent.joinpath("clientcerts")
  11. return {
  12. "valid_client_cert": clientcerts_path.joinpath("Valid.crt"),
  13. "valid_client_key": clientcerts_path.joinpath("Valid.key"),
  14. "revoked_client_cert": clientcerts_path.joinpath("Revoked.crt"),
  15. "revoked_client_key": clientcerts_path.joinpath("Revoked.key"),
  16. }
  17. @pytest.mark.parametrize("description, url, cert, expected_code, expected_text", [
  18. #Enforced: Test connection to a website with mTLS enabled without providing a client certificate.
  19. ("Enforced: No client certificate, virtual_host", "https://mtls-enabled.nginx-proxy.tld/port", None, 400, "400 No required SSL certificate was sent"),
  20. ("Enforced: No client certificate, virtual_path", "https://mtls-enabled.nginx-proxy.tld/bar/port", None, 400, "400 No required SSL certificate was sent"),
  21. ("Enforced: No client certificate, regex", "https://regex.nginx-proxy.tld/port", None, 400, "400 No required SSL certificate was sent"),
  22. ("Enforced: No client certificate, global CA", "https://global-mtls-enabled.nginx-proxy.tld/port", None, 400, "400 No required SSL certificate was sent"),
  23. #Authenticated: Test connection to a website with mTLS enabled providing a valid client certificate.
  24. ("Authenticated: Valid client certificate, virtual_host", "https://mtls-enabled.nginx-proxy.tld/port", "valid", 200, "answer from port 81\n"),
  25. ("Authenticated: Valid client certificate, virtual_path", "https://mtls-enabled.nginx-proxy.tld/bar/port", "valid", 200, "answer from port 83\n"),
  26. ("Authenticated: Valid client certificate, regex", "https://regex.nginx-proxy.tld/port", "valid", 200, "answer from port 85\n"),
  27. ("Authenticated: Valid client certificate, global CA", "https://global-mtls-enabled.nginx-proxy.tld/port", "valid", 200, "answer from port 81\n"),
  28. #Revoked: Test connection to a website with mTLS enabled providing a revoked client certificate on the CRL.
  29. ("Revoked: Invalid client certificate, virtual_host", "https://mtls-enabled.nginx-proxy.tld/port", "revoked", 400, "400 The SSL certificate error"),
  30. ("Revoked: Invalid client certificate, virtual_path", "https://mtls-enabled.nginx-proxy.tld/bar/port", "revoked", 400, "400 The SSL certificate error"),
  31. ("Revoked: Invalid client certificate, regex", "https://regex.nginx-proxy.tld/port", "revoked", 400, "400 The SSL certificate error"),
  32. ("Revoked: Invalid client certificate, global CA", "https://global-mtls-enabled.nginx-proxy.tld/port", "revoked", 400, "400 The SSL certificate error"),
  33. #Optional: Test connection to a website with optional mTLS. Access is not blocked but can be controlled with "$ssl_client_verify" directive. We assert on /foo if $ssl_client_verify = SUCCESS response with status code 418.
  34. ("Optional, Not enforced: No client certificate", "https://mtls-optional.nginx-proxy.tld/port", None, 200, "answer from port 82\n"),
  35. ("Optional: Enforced, Valid client certificate", "https://mtls-optional.nginx-proxy.tld/foo/port", "valid", 418, "ssl_client_verify is SUCCESS"),
  36. ("Optional, Not enforced: No client certificate", "https://mtls-optional.nginx-proxy.tld/bar/port", None, 200, "answer from port 84\n"),
  37. ("Optional: Enforced, Valid client certificate", "https://mtls-optional.nginx-proxy.tld/foo/bar/port", "valid", 418, "ssl_client_verify is SUCCESS"),
  38. ("Optional, Not enforced: No client certificate, global CA", "https://global-mtls-optional.nginx-proxy.tld/port", None, 200, "answer from port 82\n"),
  39. ("Optional: Enforced, Valid client certificate, global CA", "https://global-mtls-optional.nginx-proxy.tld/foo/port", "valid", 418, "ssl_client_verify is SUCCESS"),
  40. ])
  41. def test_mtls_client_certificates(docker_compose, nginxproxy, clientcerts, description, url, cert, expected_code, expected_text):
  42. """
  43. Parameterized test for mTLS client certificate scenarios.
  44. """
  45. if cert == "valid":
  46. client_cert = (clientcerts["valid_client_cert"], clientcerts["valid_client_key"])
  47. elif cert == "revoked":
  48. client_cert = (clientcerts["revoked_client_cert"], clientcerts["revoked_client_key"])
  49. else:
  50. client_cert = None
  51. r = nginxproxy.get(url, cert=client_cert if client_cert else None)
  52. assert r.status_code == expected_code
  53. assert expected_text in r.text