-
Notifications
You must be signed in to change notification settings - Fork 63
test: new tests for network policy #1240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dbasunag
merged 1 commit into
opendatahub-io:main
from
dbasunag:minor_changes_new_tests
Mar 20, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tests/model_registry/model_catalog/db_check/test_model_catalog_db_validation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import pytest | ||
| from kubernetes.dynamic import DynamicClient | ||
| from ocp_resources.network_policy import NetworkPolicy | ||
| from simple_logger.logger import get_logger | ||
| from timeout_sampler import TimeoutSampler | ||
|
|
||
| from tests.model_registry.model_catalog.utils import get_postgres_pod_in_namespace | ||
| from tests.model_registry.utils import ( | ||
| wait_for_model_catalog_pod_ready_after_deletion, | ||
| ) | ||
|
|
||
| LOGGER = get_logger(name=__name__) | ||
|
|
||
|
|
||
| class TestModelCatalogDBSecret: | ||
| def test_model_catalog_postgres_secret_exists(self, model_catalog_postgres_secret_values): | ||
| """Test that model-catalog-postgres secret exists and is accessible""" | ||
| assert model_catalog_postgres_secret_values, ( | ||
| f"model-catalog-postgres secret should exist and be accessible: {model_catalog_postgres_secret_values}" | ||
| ) | ||
|
|
||
| @pytest.mark.dependency(name="test_model_catalog_postgres_password_recreation") | ||
| def test_model_catalog_postgres_password_recreation( | ||
| self, model_catalog_postgres_secret_values, recreated_model_catalog_postgres_secret | ||
| ): | ||
| """Test that secret recreation generates new password but preserves user/database name""" | ||
| # Verify database-name and database-user did NOT change | ||
| unchanged_keys = ["database-name", "database-user"] | ||
| for key in unchanged_keys: | ||
| assert model_catalog_postgres_secret_values[key] == recreated_model_catalog_postgres_secret[key], ( | ||
| f"{key} should remain the same after secret recreation" | ||
| ) | ||
|
|
||
| # Verify database-password DID change (randomization working) | ||
| assert ( | ||
| model_catalog_postgres_secret_values["database-password"] | ||
| != recreated_model_catalog_postgres_secret["database-password"] | ||
| ), "database-password should be different after secret recreation (randomized)" | ||
|
|
||
| LOGGER.info("Password randomization verified - new password generated on recreation") | ||
|
|
||
| @pytest.mark.dependency(depends=["test_model_catalog_postgres_password_recreation"]) | ||
| def test_model_catalog_pod_ready_after_secret_recreation( | ||
| self, admin_client: DynamicClient, model_registry_namespace: str | ||
| ): | ||
| """Test that model catalog pod becomes ready after secret recreation""" | ||
| # delete the postgres pod first | ||
| get_postgres_pod_in_namespace(admin_client=admin_client, namespace=model_registry_namespace).delete() | ||
| # Wait for model catalog pod to be ready after the secret deletion/recreation | ||
| wait_for_model_catalog_pod_ready_after_deletion( | ||
| client=admin_client, model_registry_namespace=model_registry_namespace | ||
| ) | ||
| LOGGER.info("Model catalog pod is ready after secret recreation") | ||
|
|
||
|
|
||
| class TestModelCatalogDBNetworkPolicy: | ||
| def test_postgres_network_policy_exists(self, model_catalog_postgres_network_policy): | ||
| """Test that postgres NetworkPolicy exists and is accessible""" | ||
| assert model_catalog_postgres_network_policy.exists, "model-catalog-postgres NetworkPolicy should exist" | ||
|
|
||
| def test_postgres_network_policy_restricts_to_port_5432(self, model_catalog_postgres_network_policy): | ||
| """Test that NetworkPolicy only allows TCP 5432 ingress""" | ||
| spec = model_catalog_postgres_network_policy.instance.spec | ||
| assert "Ingress" in spec.policyTypes, "NetworkPolicy should have Ingress policy type" | ||
| assert len(spec.ingress) == 1, "NetworkPolicy should have exactly one ingress rule" | ||
|
|
||
| port = spec.ingress[0].ports[0] | ||
| assert port.port == 5432, "NetworkPolicy should allow only PostgreSQL port 5432" | ||
| assert port.protocol == "TCP", "NetworkPolicy port should use TCP protocol" | ||
|
|
||
| def test_postgres_network_policy_allows_only_catalog_pods(self, model_catalog_postgres_network_policy): | ||
| """Test that only model-catalog pods can reach postgres""" | ||
| from_selector = model_catalog_postgres_network_policy.instance.spec.ingress[0]["from"][ | ||
| 0 | ||
| ].podSelector.matchLabels | ||
| assert from_selector["component"] == "model-catalog", ( | ||
| "Only model-catalog pods should be allowed to access postgres" | ||
| ) | ||
dbasunag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @pytest.mark.dependency(name="test_postgres_network_policy_recreation") | ||
| def test_postgres_network_policy_recreated_after_deletion( | ||
| self, | ||
| admin_client: DynamicClient, | ||
| model_catalog_postgres_network_policy, | ||
| model_registry_namespace: str, | ||
| ): | ||
| """Test that operator recreates NetworkPolicy after deletion""" | ||
| model_catalog_postgres_network_policy.delete() | ||
| get_postgres_pod_in_namespace(admin_client=admin_client, namespace=model_registry_namespace).delete() | ||
| for np in TimeoutSampler( | ||
| wait_timeout=120, | ||
| sleep=10, | ||
| func=NetworkPolicy, | ||
| client=admin_client, | ||
| name="model-catalog-postgres", | ||
| namespace=model_registry_namespace, | ||
| ): | ||
| if np.exists: | ||
| LOGGER.info("NetworkPolicy has been recreated by operator") | ||
| break | ||
dbasunag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
50 changes: 0 additions & 50 deletions
50
tests/model_registry/model_catalog/db_check/test_model_catalog_secrets.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High: Line 67 only validates the first ingress port, allowing extra open ports to slip through (CWE-284).
Severity: High.
Exploit scenario: a policy with ports
[5432, 8080]still passes because only index0is asserted.Remediation diff
As per coding guidelines,
**: "REVIEW PRIORITIES: 1. Security vulnerabilities (provide severity, exploit scenario, and remediation code)".🤖 Prompt for AI Agents