# Let two Pods reach outside APIs after Istio's REGISTRY_ONLY switch

> The mesh lets out only hosts a ServiceEntry registers. One ServiceEntry has the wrong protocol, the other is rejected. Fix both and keep the rest blocked.

Level: Advanced · Topic: Security · About 15 minutes
Try it in the browser: https://k8wiz.com/playground/allow-external-apis-with-istio-registry-only-and-serviceentry

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

## The situation

Last night the platform team set the mesh's `outboundTrafficPolicy` to `REGISTRY_ONLY`. Sidecars now drop traffic to any host outside the cluster unless a ServiceEntry registers it. Each team has to register what it calls.

In `shop`, `checkout` calls the payment provider at `https://api.stripe.com`. Someone added a ServiceEntry for it, yet calls fail. `notifier` posts to `http://alerts.partner.example`, and its ServiceEntry, `partner-serviceentry.yaml` in the editor, is refused when you apply it.

Get both calls through, and keep every other site blocked. Test with `kubectl exec deploy/checkout -- curl -sS -m 5 https://api.stripe.com`.

## Objectives

1. See what checkout's sidecar does with the call (`kubectl logs deploy/checkout -c istio-proxy`)
2. checkout reaches https://api.stripe.com
3. notifier reaches http://alerts.partner.example
4. The mesh stays REGISTRY_ONLY, and other sites such as https://example.org stay blocked

## Hints

1. `kubectl exec deploy/checkout -- curl -sS -m 5 https://api.stripe.com` fails with `wrong version number`: something answered the TLS handshake in plain text. `kubectl logs deploy/checkout -c istio-proxy --tail=2` shows a `400 DPE http1.codec_error`: the sidecar tried to read the encrypted bytes as HTTP.
2. `kubectl get serviceentry stripe -o yaml` shows port 443 declared with `protocol: HTTP`. checkout does TLS itself, so the sidecar must pass the bytes through and route by SNI. Declare the port as `name: tls` with `protocol: TLS`.
3. `kubectl apply -f partner-serviceentry.yaml` is refused: `hosts must be FQDN if no endpoints are provided for resolution mode DNS`. With `resolution: DNS` the sidecar looks the name up, and it can't look up `*.partner.example`.
4. List the exact host, `alerts.partner.example`, and keep `resolution: DNS`. A wildcard would need `resolution: NONE`, which lets the whole domain out: more than notifier needs.
5. A host nobody registered still fails: an HTTP call gets a 502 from route `block_all`, and an HTTPS call is reset at the handshake, logged as `BlackHoleCluster`. Leave the mesh setting alone.

## Solution

```bash
kubectl exec deploy/checkout -- curl -sS -m 5 https://api.stripe.com
```
```bash
kubectl logs deploy/checkout -c istio-proxy --tail=2
```
```bash
kubectl get serviceentry stripe -o yaml
```
```bash
cat <<'EOF' | kubectl apply -f -
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
  name: stripe
  namespace: shop
spec:
  hosts:
  - api.stripe.com
  location: MESH_EXTERNAL
  resolution: DNS
  ports:
  - number: 443
    name: tls
    protocol: TLS
EOF
```
```bash
kubectl apply -f partner-serviceentry.yaml
```
```bash
cat <<'EOF' | kubectl apply -f -
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
  name: partner
  namespace: shop
spec:
  hosts:
  - alerts.partner.example
  location: MESH_EXTERNAL
  resolution: DNS
  ports:
  - number: 80
    name: http
    protocol: HTTP
EOF
```
```bash
kubectl exec deploy/checkout -- curl -sS -m 5 https://api.stripe.com
```
```bash
kubectl exec deploy/notifier -- curl -sS -m 5 -X POST http://alerts.partner.example/v1/alerts
```
```bash
kubectl exec deploy/checkout -- curl -sS -m 5 https://example.org
```

## What it teaches

- `outboundTrafficPolicy.mode` in the mesh config decides what happens to hosts Istio doesn't know. `ALLOW_ANY` passes them through, and `REGISTRY_ONLY` sends them to `BlackHoleCluster`: HTTP gets a 502 and TLS is reset.
- A ServiceEntry adds an outside host to the mesh's registry, so sidecars get a cluster for it, such as `outbound|443||api.stripe.com`.
- The port's protocol tells the sidecar how to read the traffic. When the app does TLS itself, declare `TLS` (or `HTTPS`), and the sidecar routes by SNI without decrypting. `HTTP` on a TLS port gives `400 DPE` in the log and `wrong version number` in curl.
- With `resolution: DNS` every host must be a full name. Wildcards need `resolution: NONE`, which trusts whatever address the app resolved.
- REGISTRY_ONLY in the sidecar is not a security boundary on its own: a Pod without a sidecar bypasses it. Pair it with NetworkPolicy, or an egress gateway, for traffic that must not leave.
