fix(security): harden default NetworkPolicy for client and admin ports#514
Open
fix(security): harden default NetworkPolicy for client and admin ports#514
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the operator-generated default NetworkPolicy for Dragonfly instances to prevent unintended cluster-wide access to the client/memcached ports and to prevent cross-namespace access to the admin port via spoofed pod labels.
Changes:
- Restrict client port (6379) and memcached port ingress to same-namespace pods only.
- Pin admin port (9999) operator access to the operator’s namespace using
kubernetes.io/metadata.name, with a secure same-namespace fallback when the operator namespace is unknown. - Add
POD_NAMESPACE(Downward API) to kustomize and Helm deployment manifests and plumb the resolved operator namespace through controllers into resource generation.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
internal/resources/resources.go |
Extends resource generation API to accept operatorNamespace and tightens NetworkPolicy ingress peers accordingly. |
internal/resources/resources_test.go |
Updates/extends tests to validate same-namespace restrictions and operator-namespace pinning + fallback behavior. |
internal/resources/image_test.go |
Updates tests for the new GenerateDragonflyResources signature. |
internal/resources/const.go |
Adds the well-known namespace label key constant used for namespace pinning. |
internal/controller/dragonfly_instance.go |
Passes operatorNamespace into resource generation during reconciliation. |
internal/controller/base_controller.go |
Wires OperatorNamespace from reconciler into per-instance controller state. |
config/manager/manager.yaml |
Adds POD_NAMESPACE env var via Downward API for kustomize deployment. |
cmd/main.go |
Resolves operator namespace (env/file) and injects it into reconcilers; logs resolution outcome. |
charts/dragonfly-operator/templates/deployment.yaml |
Adds POD_NAMESPACE env var via Downward API for Helm deployment. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Abhra303
approved these changes
Apr 9, 2026
| if ns, ok := os.LookupEnv("POD_NAMESPACE"); ok && ns != "" { | ||
| return ns | ||
| } | ||
| if data, err := os.ReadFile(saFile); err == nil { |
Contributor
There was a problem hiding this comment.
did you test this? (if POD_NAMESPACE is not available)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes the issue #509 where the default NetworkPolicy was effectively granting
unrestricted access to port 6379 and allowing any pod with the
control-plane: controller-managerlabel to reach the admin port fromany namespace.
instead of being open to the entire cluster
the
kubernetes.io/metadata.namewell-known label, preventing cross-namespaceaccess via spoofed pod labels
port defaults to same-namespace-only access
POD_NAMESPACEenv var to both Helm chart and kustomize deploymentmanifests via the Downward API for reliable namespace detection