# Let only checkout create charges with an Istio AuthorizationPolicy

> Lock the payments API down to two callers with an Istio AuthorizationPolicy. The draft refuses checkout too. Find out which identity its sidecar presents.

Level: Advanced · Topic: Security · About 20 minutes
Try it in the browser: https://k8wiz.com/playground/allow-only-one-caller-with-an-istio-authorizationpolicy

The playground is a simulated Kubernetes cluster that runs in the browser. kubectl output and errors follow the real ones.

## The situation

`payments` in the `payments` namespace answers anyone today. Security wants two callers and no more: `checkout` in `shop` may create charges with `POST /charge`, and `reconciler` in `finance` may read them with `GET /charges`. `storefront` in `shop` and the old `legacy-batch` job in `default`, which has no sidecar, must be refused.

A teammate drafted an AuthorizationPolicy in `payments-authz.yaml` that allows checkout by its identity, `cluster.local/ns/shop/sa/checkout`. It isn't applied yet.

Apply it, see what it breaks, and finish the job. Test with `kubectl exec -n shop deploy/checkout -- curl -s -X POST http://payments.payments:8080/charge`.

## Objectives

1. See why the payments sidecar refuses a call (`kubectl logs deploy/payments -c istio-proxy` once a call is refused)
2. checkout can create a charge, with an ALLOW policy on payments
3. reconciler can list charges
4. reconciler can't create a charge
5. storefront is refused
6. legacy-batch, outside the mesh, is refused

## Hints

1. Apply the draft: `kubectl apply -f payments-authz.yaml`. checkout now gets `RBAC: access denied`, and `kubectl logs deploy/payments -c istio-proxy --tail=3` shows `403 - rbac_access_denied_matched_policy[none]`: no rule matched.
2. The rule matches on the identity in the client's certificate, `cluster.local/ns/NAMESPACE/sa/SERVICEACCOUNT`, and the service account comes from the Pod spec. `kubectl get pods -n shop -o custom-columns=NAME:.metadata.name,SA:.spec.serviceAccountName` shows checkout runs as `default`. The `checkout` ServiceAccount exists, but nothing uses it.
3. Don't allow `sa/default`: storefront runs as that too. Give checkout its own identity with `kubectl set serviceaccount deployment/checkout checkout -n shop`. The new Pods get a new certificate.
4. Add a second rule for `cluster.local/ns/finance/sa/reconciler` with `methods: ["GET"]` and `paths: ["/charges", "/charges/*"]`. Rules in one ALLOW policy are ORed; the fields inside one rule are ANDed.
5. legacy-batch is refused without any rule about it. It has no sidecar, so it sends plain text and has no identity, and once an ALLOW policy applies to a workload, anything no rule matches is denied.

## Solution

```bash
kubectl apply -f payments-authz.yaml
```
```bash
kubectl exec -n shop deploy/checkout -- curl -s -X POST http://payments.payments:8080/charge
```
```bash
kubectl logs deploy/payments -c istio-proxy --tail=3
```
```bash
kubectl get pods -n shop -o custom-columns=NAME:.metadata.name,SA:.spec.serviceAccountName
```
```bash
kubectl set serviceaccount deployment/checkout checkout -n shop
```
```bash
kubectl rollout status deployment/checkout -n shop
```
```bash
cat <<'EOF' | kubectl apply -f -
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
  name: payments
  namespace: payments
spec:
  selector:
    matchLabels:
      app: payments
  action: ALLOW
  rules:
  - from:
    - source:
        principals: ["cluster.local/ns/shop/sa/checkout"]
    to:
    - operation:
        methods: ["POST"]
        paths: ["/charge"]
  - from:
    - source:
        principals: ["cluster.local/ns/finance/sa/reconciler"]
    to:
    - operation:
        methods: ["GET"]
        paths: ["/charges", "/charges/*"]
EOF
```
```bash
kubectl exec -n shop deploy/checkout -- curl -s -X POST http://payments.payments:8080/charge
```
```bash
kubectl exec -n finance deploy/reconciler -- curl -s http://payments.payments:8080/charges
```
```bash
kubectl exec -n shop deploy/storefront -- curl -s -X POST http://payments.payments:8080/charge
```

## What it teaches

- Without an AuthorizationPolicy, a sidecar allows everything. As soon as one ALLOW policy applies to a workload, everything its rules don't match gets a 403. DENY policies are checked first and win.
- `principals` match the SPIFFE identity from the client's mTLS certificate: `cluster.local/ns/NAMESPACE/sa/SERVICEACCOUNT`. The service account comes from the client's Pod spec, so a workload on `default` shares its identity with every other Pod in the namespace that also runs as `default`.
- A client without a sidecar sends plain text and has no principal. A policy that names principals refuses it even while PeerAuthentication is still PERMISSIVE.
- The server's sidecar enforces the policy, and its access log names the decision: `rbac_access_denied_matched_policy[none]` for no matching ALLOW rule, or the policy and rule that denied it.
- `spec: {}` is an ALLOW policy with no rules, which allows nothing. It's the usual way to start from deny-all.
