# Default deny all ingress traffic in sail-proxy namespace
# Following SAP "Secure development in Kyma" guidance
# NOTE: When using Istio sidecars, this can cause issues with pod-to-pod communication
# Consider removing this policy if using Istio service mesh
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: __NAMESPACE__
spec:
  podSelector: {}
  policyTypes:
  - Ingress
---
# Allow NGINX to communicate with Gateway
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-nginx-to-gateway
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: gateway
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: nginx
    ports:
    - protocol: TCP
      port: 8080
---
# Allow NGINX to communicate with OAuth2-Proxy
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-nginx-to-oauth2-proxy
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: oauth2-proxy
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: nginx
    ports:
    - protocol: TCP
      port: 4180
---
# Allow OAuth2-Proxy to communicate with Dex
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-oauth2-proxy-to-dex
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: dex
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: oauth2-proxy
    ports:
    - protocol: TCP
      port: 5556
---
# Allow Gateway and Admin to communicate with PostgreSQL
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-apps-to-postgres
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: postgres
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: gateway
    - podSelector:
        matchLabels:
          app: admin
    - podSelector:
        matchLabels:
          sail-proxy/migration: "true"  # the migration Jobs of `sail-proxy upgrade kyma`
    ports:
    - protocol: TCP
      port: 5432
---
# Allow Gateway and Admin to communicate with Valkey
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-apps-to-valkey
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: valkey
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: gateway
    - podSelector:
        matchLabels:
          app: admin
    - podSelector:
        matchLabels:
          sail-proxy/migration: "true"  # the migration Jobs of `sail-proxy upgrade kyma`
    ports:
    - protocol: TCP
      port: 6379
---
# Allow Istio ingress to reach NGINX (for public deployments)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-istio-to-nginx
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: nginx
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: istio-system
    ports:
    - protocol: TCP
      port: 8080
---
# Allow DNS resolution (required for all pods)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: __NAMESPACE__
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
---
# Allow Admin comprehensive egress
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-admin-comprehensive-egress
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: admin
  policyTypes:
  - Egress
  egress:
  # Allow DNS
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
  # Allow to Postgres
  - to:
    - podSelector:
        matchLabels:
          app: postgres
    ports:
    - protocol: TCP
      port: 5432
  # Allow to Valkey
  - to:
    - podSelector:
        matchLabels:
          app: valkey
    ports:
    - protocol: TCP
      port: 6379
  # Allow to Gateway  
  - to:
    - podSelector:
        matchLabels:
          app: gateway
    ports:
    - protocol: TCP
      port: 8080
  # Allow to nginx (for internal health checks)
  - to:
    - podSelector:
        matchLabels:
          app: nginx
    ports:
    - protocol: TCP
      port: 8080
  # Allow to Istio system (for sidecar communication when enabled)
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: istio-system
    ports:
    - protocol: TCP
      port: 15010
    - protocol: TCP
      port: 15011
    - protocol: TCP
      port: 15012
    - protocol: TCP
      port: 443
---
# Allow Gateway pods to connect to PostgreSQL (egress)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-gateway-egress-to-postgres
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: gateway
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: postgres
    ports:
    - protocol: TCP
      port: 5432
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
---
# Allow OAuth2-Proxy to connect to Dex (egress)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-oauth2-proxy-egress-to-dex
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: oauth2-proxy
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: dex
    ports:
    - protocol: TCP
      port: 5556
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
---
# Allow NGINX to connect to Gateway, OAuth2-Proxy, Dex and Admin (egress)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-nginx-egress
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: nginx
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: gateway
    ports:
    - protocol: TCP
      port: 8080
  - to:
    - podSelector:
        matchLabels:
          app: oauth2-proxy
    ports:
    - protocol: TCP
      port: 4180
  - to:
    - podSelector:
        matchLabels:
          app: dex
    ports:
    - protocol: TCP
      port: 5556
  - to:
    - podSelector:
        matchLabels:
          app: admin
    ports:
    - protocol: TCP
      port: 4004
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
---
# Allow all pods to communicate with Istio control plane (required for sidecars)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-istio-sidecar-egress
  namespace: __NAMESPACE__
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: istio-system
    ports:
    - protocol: TCP
      port: 15010
    - protocol: TCP
      port: 15011
    - protocol: TCP
      port: 15012
    - protocol: TCP
      port: 443
---
# Allow Istio sidecar internal communication
# This is required for health checks and service-to-service communication
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-istio-sidecar-internal
  namespace: __NAMESPACE__
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress
  ingress:
  # Allow all pods in the namespace to receive traffic on Envoy ports
  - from:
    - podSelector: {}
    ports:
    - protocol: TCP
      port: 15001  # Envoy outbound
    - protocol: TCP
      port: 15006  # Envoy inbound
    - protocol: TCP
      port: 15020  # Envoy health check
    - protocol: TCP
      port: 15021  # Envoy health check
    - protocol: TCP
      port: 15090  # Envoy Prometheus metrics
  egress:
  # Allow all pods to connect to other pods on Envoy ports
  - to:
    - podSelector: {}
    ports:
    - protocol: TCP
      port: 15001
    - protocol: TCP
      port: 15006
    - protocol: TCP
      port: 15020
    - protocol: TCP
      port: 15021
    - protocol: TCP
      port: 15090
---
# Allow Dex to access Kubernetes API server (required for storage backend)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dex-k8s-api
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: dex
  policyTypes:
  - Egress
  egress:
  - to: []
    ports:
    - protocol: TCP
      port: 443
    - protocol: TCP
      port: 6443
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
---
# Allow ingress to NGINX from all pods in namespace
# Critical for inter-service communication through the API gateway
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-nginx-ingress
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: nginx
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector: {}
    ports:
    - protocol: TCP
      port: 8080
---
# Allow NGINX to communicate with Admin service
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-admin-ingress-from-nginx
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: admin
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: nginx
    ports:
    - protocol: TCP
      port: 4004
---
# Allow Gateway to connect to NGINX for internal testing
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-gateway-egress-to-nginx
  namespace: __NAMESPACE__
spec:
  podSelector:
    matchLabels:
      app: gateway
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: nginx
    ports:
    - protocol: TCP
      port: 8080
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
---
# Allow the migration Jobs (pod label sail-proxy/migration, core/migrate-jobs.yaml) to reach Admin:
# the file-search migration reads its configured embedding dimensions from it
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-migrations-to-admin
  namespace: __NAMESPACE__
  labels:
    app.kubernetes.io/part-of: sail-proxy
spec:
  podSelector:
    matchLabels:
      app: admin
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          sail-proxy/migration: "true"
    ports:
    - protocol: TCP
      port: 4004
---
# Allow the migration Jobs to connect to PostgreSQL, Valkey and Admin (egress)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-migrations-egress
  namespace: __NAMESPACE__
  labels:
    app.kubernetes.io/part-of: sail-proxy
spec:
  podSelector:
    matchLabels:
      sail-proxy/migration: "true"
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: postgres
    ports:
    - protocol: TCP
      port: 5432
  - to:
    - podSelector:
        matchLabels:
          app: valkey
    ports:
    - protocol: TCP
      port: 6379
  - to:
    - podSelector:
        matchLabels:
          app: admin
    ports:
    - protocol: TCP
      port: 4004
  - to: []
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
