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