# service-mesh-policy / kubernetes / t0 "Quick start"
#
# For a cluster already running Istio, with sidecar injection on for the
# namespace shop:
#   1. every workload in the mesh must use mutual TLS, so plain connections
#      are refused and every caller has a verifiable identity
#   2. nothing in shop accepts a call unless a policy allows it
#   3. one policy allows the frontend to call the backend, by the frontend's
#      service account, which is its identity in the mesh
#
# Change shop, frontend, and backend to your own, then:
#   kubectl apply -f policies.yaml
#
# Verify:
#   From a pod in shop running as any service account other than frontend:
#     curl -s -o /dev/null -w '%{http_code}\n' http://backend.shop:8080/api/health
#     Expect: 403, with the body RBAC: access denied.
#   From the frontend's pod, the same call returns 200.
#   From a pod with no sidecar, the connection is reset: no mutual TLS.
---
# In the root namespace, so it covers the whole mesh.
apiVersion: security.istio.io/v1
kind: PeerAuthentication
metadata:
  name: default
  namespace: istio-system
spec:
  mtls:
    mode: STRICT
---
# An ALLOW policy with no rules matches nothing, so every call to every
# workload in the namespace is refused until another policy allows it.
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
  name: allow-nothing
  namespace: shop
spec: {}
---
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
  name: frontend-calls-backend
  namespace: shop
spec:
  selector:
    matchLabels:
      app: backend
  action: ALLOW
  rules:
    - from:
        - source:
            principals: ["cluster.local/ns/shop/sa/frontend"]
      to:
        - operation:
            methods: ["GET", "POST"]
            paths: ["/api/*"]
