|
| 1 | +import pytest |
| 2 | +import logging |
| 3 | + |
| 4 | +from ocs_ci.ocs.resources.pod import get_pods_having_label |
| 5 | +from ocs_ci.ocs.resources.pod import Pod |
| 6 | +from ocs_ci.ocs.exceptions import CommandFailed |
| 7 | +from ocs_ci.ocs.constants import CSI_RBDPLUGIN_LABEL_419 |
| 8 | +from ocs_ci.framework.pytest_customization.marks import ( |
| 9 | + tier1, |
| 10 | + green_squad, |
| 11 | +) |
| 12 | +from ocs_ci.framework.pytest_customization.marks import polarion_id, skipif_ocs_version |
| 13 | + |
| 14 | +log = logging |
| 15 | + |
| 16 | + |
| 17 | +@tier1 |
| 18 | +@green_squad |
| 19 | +@polarion_id("OCS-6807") |
| 20 | +@skipif_ocs_version("<4.19") |
| 21 | +class TestCSIAddonPodSecurity: |
| 22 | + """This class contains tests to Validate if CSI Addon pod enforces security |
| 23 | + by allowing HTTPS and rejecting HTTP connections. |
| 24 | + """ |
| 25 | + |
| 26 | + def test_csi_addon_pod_security(self): |
| 27 | + """ |
| 28 | + Validate that the CSI Addon pods are compliant with the Pod Security Standards. |
| 29 | +
|
| 30 | + Test Steps: |
| 31 | + 1. Fetch a pod with label 'app=openshift-storage.rbd.csi.ceph.com-nodeplugin' (CSI RBD Nodeplugin). |
| 32 | + 2. Retrieve container information for the container named 'csi-addons'. |
| 33 | + 3. Assert that the 'csi-addons' container exists in the pod. |
| 34 | + 4. Extract the port used by the 'csi-addons' container. |
| 35 | + 5. Execute a HTTPS (secure) curl command inside the 'csi-addons' container on localhost:{port}/healthz. |
| 36 | + - Verify that the pod responds correctly over HTTPS (secure connection should succeed). |
| 37 | + 6. Execute a HTTP (insecure) curl command inside the 'csi-addons' container on localhost:{port}/healthz. |
| 38 | + - Verify that the insecure connection fails as expected (CommandFailed exception raised). |
| 39 | + 7. Assert that the CSI Addon pod does not allow connections without TLS (insecure HTTP). |
| 40 | +
|
| 41 | + Expected Result: |
| 42 | + - The pod should be reachable securely over HTTPS. |
| 43 | + - The pod should reject insecure HTTP (non-TLS) connections. |
| 44 | + """ |
| 45 | + |
| 46 | + log.info("Validating CSI Addon pod security standards") |
| 47 | + |
| 48 | + pod_obj = Pod(**get_pods_having_label(CSI_RBDPLUGIN_LABEL_419)[0]) |
| 49 | + |
| 50 | + csi_addon_container = pod_obj.get_container_data("csi-addons") |
| 51 | + |
| 52 | + assert csi_addon_container, "No CSI Addon container found in the pod" |
| 53 | + |
| 54 | + port_used_by_csi_addon = csi_addon_container[0]["ports"][0]["containerPort"] |
| 55 | + |
| 56 | + # Querying to the container port with HTTPS |
| 57 | + try: |
| 58 | + pod_obj.exec_cmd_on_pod( |
| 59 | + command=f"curl -k -s https://localhost:{port_used_by_csi_addon}/healthz", |
| 60 | + container_name="csi-addons", |
| 61 | + out_yaml_format=False, |
| 62 | + ) |
| 63 | + log.info( |
| 64 | + f"CSI Addon pod is reachable securely on port {port_used_by_csi_addon}" |
| 65 | + ) |
| 66 | + except CommandFailed as e: |
| 67 | + log.error( |
| 68 | + f"CSI Addon pod is not reachable securely on port {port_used_by_csi_addon}: {str(e)}" |
| 69 | + ) |
| 70 | + pytest.fail(f"CSI Addon pod HTTPS connection failed: {str(e)}") |
| 71 | + |
| 72 | + # Now check if the pod is rejecting insecure HTTP (without TLS) |
| 73 | + with pytest.raises(CommandFailed) as exc_info: |
| 74 | + pod_obj.exec_cmd_on_pod( |
| 75 | + command=f"curl -s http://localhost:{port_used_by_csi_addon}/healthz", |
| 76 | + container_name="csi-addons", |
| 77 | + out_yaml_format=False, |
| 78 | + ) |
| 79 | + |
| 80 | + assert "command terminated" in str( |
| 81 | + exc_info.value |
| 82 | + ), "CSI Addon pod should not allow connection without TLS" |
| 83 | + log.info("CSI Addon pod correctly refused HTTP (insecure) connection") |
0 commit comments