# =============================================================================
# VIBECARBON NETWORK POLICIES (CLUSTER BASELINE)
# =============================================================================
# These policies implement a "deny by default" approach with explicit allowlists
# for required communication paths.
#
# Security Principle: Pods can only communicate with explicitly allowed targets.
# =============================================================================

---
# Default deny all INGRESS for the namespace. Egress is not denied by a
# namespace-wide policy: every pod is selected by allow-dns and
# allow-intra-namespace-egress below (which makes egress default-deny for it),
# and the per-workload policies add what each one needs on top.
#
# East/west traffic inside the namespace is fine; external egress
# restrictions are enforced by the provider firewall too.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: vibecarbon
spec:
  podSelector: {}
  policyTypes:
    - Ingress

---
# Allow ALL ingress from other pods in the same namespace. The app and the
# add-ons need to reach the db and each other without us having to enumerate
# every label combination.
# External ingress still gates through traefik-policy + app-policy etc.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-intra-namespace
  namespace: vibecarbon
spec:
  podSelector: {}
  policyTypes:
    - Ingress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: vibecarbon

---
# Allow egress to pods in the same namespace. Symmetric with the
# allow-intra-namespace ingress policy above. Required because
# `allow-dns` below selects all pods with Egress policyType, which
# implicitly switches the namespace to default-deny-egress for those
# pods. Without this, a pod could resolve the `db` Service via DNS but
# its TCP connect to :5432 would be dropped (the old wait-for-db init
# containers looped for 25 minutes on every cold install exactly this way).
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-intra-namespace-egress
  namespace: vibecarbon
spec:
  podSelector: {}
  policyTypes:
    - Egress
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: vibecarbon
---
# Allow DNS resolution for all pods
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: vibecarbon
spec:
  podSelector: {}
  policyTypes:
    - Egress
  egress:
    - to:
        # Use namespaceSelector only (no podSelector) so kustomize commonLabels doesn't
        # add environment/region/role labels to the selector, which would exclude CoreDNS
        # pods (managed by k3s, they only have k8s-app: kube-dns, not overlay labels).
        - namespaceSelector:
            matchLabels:
                kubernetes.io/metadata.name: kube-system
      ports:
        - protocol: UDP
          port: 53
        - protocol: TCP
          port: 53

---
# The db pod's S3 egress (wal-g) lives with the StatefulSet:
# db-s3-egress in k8s/base/db/network-policy.yaml.

---
# H-9: allow the app pod to reach the (optional) vibecarbon-observability
# namespace. The app's services-status API polls grafana:3000, prometheus:9090
# and loki:3100 for health, and pushes logs to loki:3100. This namespace is
# default-deny-egress (allow-dns + allow-intra-namespace-egress select all pods),
# so without this the cross-namespace probes/log-push are silently dropped.
# When observability is not installed the target namespace doesn't exist and this
# rule simply matches nothing — harmless. (Egress rules are additive-allow only.)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: app-observability-egress
  namespace: vibecarbon
spec:
  podSelector:
    matchLabels:
      app: vibecarbon-app
  policyTypes:
    - Egress
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: vibecarbon-observability
      ports:
        - protocol: TCP
          port: 3000
        - protocol: TCP
          port: 9090
        - protocol: TCP
          port: 3100

---
# Traefik's side of the /admin/grafana route. Grafana's IngressRoute lives in
# vibecarbon-observability (beside its Service: Traefik has no
# allowCrossNamespace), and grafana-policy admits Traefik's ingress there, but
# traefik-policy only allows egress inside this namespace. A NetworkPolicy
# path needs both ends, so without this every /admin/grafana request was
# dropped between Traefik and Grafana and answered 502. Grafana only (port
# 3000): Prometheus and Loki are never routed publicly. Matches nothing when
# observability is not installed. Pinned by
# tests/unit/deploy/traefik-cross-namespace-egress.test.ts.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: traefik-observability-egress
  namespace: vibecarbon
spec:
  podSelector:
    matchLabels:
      app: vibecarbon-traefik
  policyTypes:
    - Egress
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: vibecarbon-observability
          podSelector:
            matchLabels:
              app: vibecarbon-grafana
      ports:
        - protocol: TCP
          port: 3000
