Create a security policy

Uses: Kong Mesh
TL;DR

Secure your mesh in three steps:

  1. Issue SPIFFE workload certificates with MeshIdentity. Proxies with an identity require mTLS and deny every caller that no permission allows.
  2. Make Strict mTLS explicit with a mesh-wide MeshTLS policy.
  3. Allow each caller explicitly with a MeshTrafficPermission policy.

Prerequisites

This page is part of the Kong Mesh scenarios: the Kong Air journey series.

Complete the previous page, Install Kong Mesh and deploy Kong Air before completing this page.

Out of the box, the Kong Air services call each other in plaintext, and any workload can reach check-in-api. In this guide, flight-control becomes the only workload that check-in-api accepts.

To learn more about what this guide does, read the following reference pages:

Confirm the unsecured baseline

Before you add any security resources, confirm that flight-control can reach check-in-api:

kubectl exec -n kong-air-production deploy/flight-control -- \
  wget -q -T 5 -O- http://check-in-api.kong-air-production.svc.cluster.local:8080/

The check-in-api application responds with its pod hostname.

Issue workload identity with MeshIdentity

The MeshIdentity resource gives each workload an identity and a certificate. Kong Air runs in one zone for now, so this guide uses the Bundled provider with autogenerate. With this setting, the zone control plane generates its own certificate authority (CA). For other providers, see Providers. For trust across zones, see Multi-zone meshes.

  1. Create a MeshIdentity that issues certificates to every workload in kong-air-mesh:

    echo 'apiVersion: kuma.io/v1alpha1
    kind: MeshIdentity
    metadata:
      name: kong-air-identity
      namespace: kong-mesh-system
      labels:
        kuma.io/mesh: kong-air-mesh
    spec:
      selector:
        dataplane:
          matchLabels:
            kuma.io/mesh: kong-air-mesh
      provider:
        type: Bundled
        bundled:
          insecureAllowSelfSigned: true
          autogenerate:
            enabled: true
          meshTrustCreation: Enabled
      spiffeID:
        path: /ns/{{ .Namespace }}/sa/{{ .ServiceAccount }}' | kubectl apply -f -
  2. Wait for the MeshIdentity resource to be ready:

    kubectl wait meshidentity kong-air-identity -n kong-mesh-system \
      --for=condition=Ready --timeout=60s

    The zone control plane then sends each sidecar its certificate over xDS, so you don’t need to restart the workloads.

  3. Check that each data plane proxy received its certificate from kong-air-identity:

    kubectl get dataplaneinsights -n kong-air-production -o yaml | grep -A4 issuedBackend

    The output shows one entry for each data plane proxy.

The kong-air-identity resource gives every workload a Secure Production Identity Framework for Everyone (SPIFFE) ID. The ID path contains the workload’s namespace and Kubernetes service account:

spiffe://kong-air-mesh.zone1.mesh.local/ns/kong-air-production/sa/check-in-api
spiffe://kong-air-mesh.zone1.mesh.local/ns/kong-air-production/sa/flight-control
spiffe://kong-air-mesh.zone1.mesh.local/ns/kong-air-production/sa/passenger-portal

Now that every proxy has a workload identity, the Kong Air proxies change behavior in two ways:

  • Every inbound runs in Strict mode, so it accepts only mTLS connections.
  • Every inbound denies callers that no MeshTrafficPermission allows.

No MeshTrafficPermission exists yet, so check-in-api now rejects flight-control with 403 Forbidden. For more information, see Identity requirements.

Make strict mTLS explicit with MeshTLS

The MeshTLS policy sets the TLS mode, versions, and ciphers for mesh connections. Kong Air’s proxies already run in Strict mode because they have a workload identity. Kong Air’s compliance rules require mTLS on every connection, so you record that requirement in a mesh-wide MeshTLS policy. With this policy in place, the configuration states the Strict mode explicitly instead of relying on the default.

  1. Create a mesh-wide MeshTLS policy that sets the mode to Strict:

    echo 'apiVersion: kuma.io/v1alpha1
    kind: MeshTLS
    metadata:
      name: strict-mtls
      namespace: kong-mesh-system
      labels:
        kuma.io/mesh: kong-air-mesh
    spec:
      targetRef:
        kind: Mesh
      rules:
        - default:
            mode: Strict' | kubectl apply -f -

    In Strict mode, the receiving proxy accepts only connections with a valid mTLS certificate. The proxy rejects plaintext connections before it checks any traffic permission.

  2. Confirm that check-in-api still rejects calls from flight-control:

    kubectl exec -n kong-air-production deploy/flight-control -- wget -q -T 5 -O- http://check-in-api.kong-air-production.svc.cluster.local:8080/

    The request fails:

    wget: server returned error: HTTP/1.1 403 Forbidden

    The check-in-api sidecar returns the 403, and the request never reaches the application. The sidecar denies flight-control because no MeshTrafficPermission allows it yet.

Authorize service-to-service traffic

A MeshTrafficPermission policy lets a caller reach a destination. We recommend that you target the receiving data plane proxy and allow the caller’s SPIFFE ID explicitly, because the SPIFFE ID comes from the caller’s verified certificate.

Each Kong Air workload runs as its own Kubernetes ServiceAccount. The SPIFFE ID contains the zone, the namespace, and the service account name. The flight-control workload runs in zone1 as the flight-control service account, so its SPIFFE ID is spiffe://kong-air-mesh.zone1.mesh.local/ns/kong-air-production/sa/flight-control.

Create a MeshTrafficPermission that allows flight-control to call check-in-api:

echo 'apiVersion: kuma.io/v1alpha1
kind: MeshTrafficPermission
metadata:
  name: allow-flight-control-to-check-in
  namespace: kong-mesh-system
  labels:
    kuma.io/mesh: kong-air-mesh
spec:
  targetRef:
    kind: Dataplane
    labels:
      app: check-in-api
  rules:
    - default:
        allow:
          - spiffeID:
              type: Exact
              value: spiffe://kong-air-mesh.zone1.mesh.local/ns/kong-air-production/sa/flight-control' | kubectl apply -f -

The control plane takes up to 10 seconds to send a MeshTrafficPermission change to the data plane proxies. If a request returns an unexpected result right after you apply a policy, wait and try again.

Validate

  1. Confirm that flight-control can now reach check-in-api:

    kubectl exec -n kong-air-production deploy/flight-control -- wget -q -T 5 -O- http://check-in-api.kong-air-production.svc.cluster.local:8080/

    The response contains the check-in-api pod hostname:

    check-in-api-6b8f9c9d4f-x7z2p
  2. Confirm that check-in-api still denies every other workload. No MeshTrafficPermission allows passenger-portal to reach check-in-api, so the same request from passenger-portal fails:

    kubectl exec -n kong-air-production deploy/passenger-portal -- wget -q -T 5 -O- http://check-in-api.kong-air-production.svc.cluster.local:8080/

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!