# Source: https://docs.datadoghq.com/containers/kubernetes/appsec.md

---
title: App and API Protection for Kubernetes
description: >-
  Automatically enable App and API Protection for your Kubernetes ingress
  proxies and gateways
breadcrumbs: Docs > Containers > Kubernetes > App and API Protection for Kubernetes
---

# App and API Protection for Kubernetes

{% callout %}
##### App and API Protection for Kubernetes is in Preview

App and API Protection for Kubernetes automatically configures supported Kubernetes ingress proxies and gateways. Try it today!

[JOIN THE PREVIEW](https://www.datadoghq.com/product-preview/kubernetes-gateway-security-injector/)
{% /callout %}

This page describes how to set up [App and API Protection](https://docs.datadoghq.com/security/application_security/) for Kubernetes to automatically configure supported Kubernetes ingress proxies and gateways to run API discovery, threat detection, and inline blocking at the edge of the infrastructure.

## Overview{% #overview %}

App and API Protection for Kubernetes automatically configures supported ingress proxies and gateways in your Kubernetes cluster to enable Application Security monitoring. This eliminates the need for manual proxy configuration and provides API-wide security coverage without modifying individual services or deploying tracers across your application fleet.

### What performs the automatic configuration?{% #what-performs-the-automatic-configuration %}

App and API Protection for Kubernetes uses a Kubernetes controller (running in the Datadog Cluster Agent) that:

- **Automatically detects** supported proxies in your cluster (Envoy Gateway, Istio)
- **Configures proxies** to route traffic through an external Application Security processor
- **Enables threat detection** for all traffic passing through your ingress layer
- **Simplifies operations** through centralized configuration with Helm

### Supported proxies{% #supported-proxies %}

- **Envoy Gateway**: Automatically creates `EnvoyExtensionPolicy` resources
- **Istio**: Automatically creates `EnvoyFilter` resources in the Istio system namespace

More proxies are available via manual installation on the global [setup page](https://docs.datadoghq.com/security/application_security/setup/kubernetes/).

## Limitations{% #limitations %}

- Requires Datadog Cluster Agent 7.73.0 or later
- External processor must be manually deployed and scaled
- Deployed service may require an appropriate network policy:
  - From the proxy pods on the service port
  - To the Datadog Agent for traces
- For specific proxy version compatibility, see:
  - [Envoy Gateway compatibility](https://docs.datadoghq.com/security/application_security/setup/compatibility/envoy-gateway)
  - [Istio compatibility](https://docs.datadoghq.com/security/application_security/setup/compatibility/istio)

## Prerequisites{% #prerequisites %}

Before enabling App and API Protection for Kubernetes, ensure you have:

- A running Kubernetes cluster (version 1.20 or later)
- [Datadog Cluster Agent 7.73.0+](https://docs.datadoghq.com/containers/kubernetes/installation/) installed and configured in your cluster
- One or more supported proxies installed:
  - [Envoy Gateway](https://gateway.envoyproxy.io/)
  - [Istio](https://istio.io/)
- [Remote Configuration](https://docs.datadoghq.com/agent/remote_config/?tab=helm#enabling-remote-configuration) enabled to allow blocking attackers through the Datadog UI

## How it works{% #how-it-works %}

App and API Protection for Kubernetes uses **External Mode**, where a single Application Security processor deployment serves all gateway traffic in your cluster.

### Architecture{% #architecture %}

- **External Processor Deployment**: You deploy a centralized Application Security processor as a Kubernetes Deployment with an associated Service.
- **Automatic Proxy Detection**: The controller watches for supported proxy resources in your cluster using Kubernetes informers.
- **Automatic Configuration**: When proxies are detected, the controller creates the necessary configuration:
  - For Envoy Gateway: Creates `EnvoyExtensionPolicy` resources that reference the external processor service
  - For Istio: Creates `EnvoyFilter` resources in the Istio system namespace
- **Traffic Processing**: Gateways route traffic to the external processor via the Kubernetes service for security analysis.

### Benefits{% #benefits %}

- **Resource Efficient**: A single shared processor handles traffic from all gateways
- **Centralized Management**: One deployment to monitor, scale, and configure
- **Infrastructure-as-Code**: Manage configuration through Helm values
- **Non-Invasive**: No application code changes required
- **Scalable**: Easily add new gateways without additional configuration

## Setup{% #setup %}

### Step 1: Deploy the external processor{% #step-1-deploy-the-external-processor %}

Deploy the Datadog Application Security external processor service. This processor analyzes traffic forwarded from your gateways.

For detailed deployment instructions and configuration options, see the [Envoy Gateway documentation](https://docs.datadoghq.com/security/application_security/setup/kubernetes/envoy-gateway) or [Istio documentation](https://docs.datadoghq.com/security/application_security/setup/kubernetes/istio).

Example processor deployment:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: datadog-aap-extproc-deployment
  namespace: datadog
spec:
  replicas: 2
  selector:
    matchLabels:
      app: datadog-aap-extproc
  template:
    metadata:
      labels:
        app: datadog-aap-extproc
    spec:
      containers:
      - name: datadog-aap-extproc-container
        image: ghcr.io/datadog/dd-trace-go/service-extensions-callout:v2.4.0
        ports:
        - name: grpc
          containerPort: 443
        - name: health
          containerPort: 80
        env:
        # Use the address of the datadog agent service in your cluster
        - name: DD_AGENT_HOST
          value: "datadog-agent.datadog.svc.cluster.local"

        - name: DD_SERVICE_EXTENSION_TLS
          value: "false"
        readinessProbe:
          httpGet:
            path: /
            port: health
          initialDelaySeconds: 5
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /
            port: health
          initialDelaySeconds: 15
          periodSeconds: 20
---
apiVersion: v1
kind: Service
metadata:
  name: datadog-aap-extproc-service
  namespace: datadog
spec:
  ports:
  - name: grpc
    port: 443
    targetPort: grpc
  selector:
    app: datadog-aap-extproc
  type: ClusterIP
```

Apply the manifest:

```bash
kubectl apply -f datadog-aap-extproc-service.yaml
```

### Step 2: Enable automatic configuration{% #step-2-enable-automatic-configuration %}

Enable automatic configuration in the Datadog Cluster Agent and configure it to use your external processor service.

Configure the integration using either Helm or the Datadog Operator.

**Note:** The processor service name (`datadog-aap-extproc-service`) must match the name of the service you deployed in Step 1.

{% tab title="Datadog Operator" %}
This option requires Datadog Operator v1.22.0+.

Add annotations to your `DatadogAgent` resource. The service name annotation is required and must match your external processor service:

```yaml
apiVersion: datadoghq.com/v2alpha1
kind: DatadogAgent
metadata:
  name: datadog
  annotations:
    agent.datadoghq.com/appsec.injector.enabled: "true"
    agent.datadoghq.com/appsec.injector.processor.service.name: "datadog-aap-extproc-service"  # Required: must match your external processor service name
    agent.datadoghq.com/appsec.injector.processor.service.namespace: "datadog"
spec:
  # ... your existing DatadogAgent configuration
```

Apply the configuration:

```bash
kubectl apply -f datadog-agent.yaml
```

{% /tab %}

{% tab title="Helm" %}
Configure App and API Protection for Kubernetes using Helm values. Add the following to your `values.yaml`:

```yaml
datadog:
  appsec:
    injector:
      enabled: true
      processor:
        service:
          name: datadog-aap-extproc-service  # Required: must match your external processor service name
          namespace: datadog                 # Must match the namespace where the service is deployed
```

Install or upgrade the Datadog Helm chart (v3.153+):

```bash
helm upgrade -i datadog-agent datadog/datadog -f values.yaml
```

{% /tab %}

### Step 3: Verify installation{% #step-3-verify-installation %}

After applying your configuration, verify the Datadog Cluster Agent has started automatic configuration:

```bash
kubectl logs -n datadog deployment/datadog-cluster-agent | grep appsec
```

Look for log messages indicating it has started and detected proxies.

#### Verify proxy configuration{% #verify-proxy-configuration %}

For **Envoy Gateway**, check that `EnvoyExtensionPolicy` resources were created:

```bash
kubectl get envoyextensionpolicy -A
```

For **Istio**, check that `EnvoyFilter` resources were created:

```bash
kubectl get envoyfilter -n istio-system
```

The Datadog Cluster Agent will produce events for each operation done in the cluster whenever it resulted in failure or success.

#### Test traffic processing{% #test-traffic-processing %}

Send requests through your gateway and verify they appear in the Datadog App and API Protection UI:

1. Navigate to [Security > Application Security](https://app.datadoghq.com/security/appsec) in Datadog.
1. Look for security signals from your gateway traffic.
1. Verify that threat detection is active.

## Configuration reference{% #configuration-reference %}

### Automatic configuration options{% #automatic-configuration-options %}

| Helm Parameter                | Datadog Operator Annotation                                       | Type       | Default                                                         | Description                                                                             |
| ----------------------------- | ----------------------------------------------------------------- | ---------- | --------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| `enabled`                     | `agent.datadoghq.com/appsec.injector.enabled`                     | Boolean    | `false`                                                         | Enable or disable the integration                                                       |
| `autoDetect`                  | `agent.datadoghq.com/appsec.injector.autoDetect`                  | Boolean    | `true`                                                          | Automatically detect and configure supported proxies                                    |
| `proxies`                     | `agent.datadoghq.com/appsec.injector.proxies`                     | JSON array | `[]`                                                            | Manual list of proxy types to configure. Valid values: `["envoy-gateway"]`, `["istio"]` |
| `processor.service.name`      | `agent.datadoghq.com/appsec.injector.processor.service.name`      | String     | **Required.** Name of the external processor Kubernetes Service |
| `processor.service.namespace` | `agent.datadoghq.com/appsec.injector.processor.service.namespace` | String     | Defaults to the namespace where the Cluster Agent is running    | Namespace where the external processor Service is deployed                              |
| `processor.address`           | `agent.datadoghq.com/appsec.injector.processor.address`           | String     | `{service.name}.{service.namespace}.svc`                        | Full service address override                                                           |
| `processor.port`              | `agent.datadoghq.com/appsec.injector.processor.port`              | Integer    | `443`                                                           | Port of the external processor service                                                  |

### Proxy types{% #proxy-types %}

- `envoy-gateway`: Configures Envoy Gateway using `EnvoyExtensionPolicy` resources
- `istio`: Configures Istio using global `EnvoyFilter` resources in the Istio system namespace

### Opting out specific resources{% #opting-out-specific-resources %}

You can exclude specific Gateway or GatewayClass resources from automatic configuration by adding a label:

```yaml
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: my-gateway
  namespace: my-namespace
  labels:
    appsec.datadoghq.com/enabled: "false"  # Exclude this gateway from automatic configuration
spec:
  # ... gateway configuration
```

Resources with the `appsec.datadoghq.com/enabled: "false"` label will be ignored. This is useful when you want to:

- Manually configure specific gateways
- Temporarily disable App and API Protection for testing
- Exclude certain gateways from security monitoring

**Note**: By default, all resources are included. Only resources with the label explicitly set to `"false"` are excluded.

## Troubleshooting{% #troubleshooting %}

All errors are logged as Kubernetes events. Make sure to check for events on the Gateway or GatewayClass you wish to instrument.

### Automatic configuration not detecting proxies{% #automatic-configuration-not-detecting-proxies %}

**Symptom**: No `EnvoyExtensionPolicy` or `EnvoyFilter` resources are created.

**Solutions**:

- Check that `autoDetect` is set to `true` or proxies are manually specified
- Verify the Cluster Agent logs for proxy detection messages
- Ensure your proxies are installed and have the expected Kubernetes resources (Gateway, GatewayClass)
- Try manually specifying proxy types using the `proxies` parameter

### EnvoyExtensionPolicy or EnvoyFilter not created{% #envoyextensionpolicy-or-envoyfilter-not-created %}

**Symptom**: The controller is running but configuration resources are missing.

**Solutions**:

- Check Cluster Agent logs for RBAC permission errors
- Verify the Cluster Agent service account has permissions to create `EnvoyExtensionPolicy` or `EnvoyFilter` resources
- Ensure the processor service exists and is accessible
- Check for conflicting existing policies or filters

### Traffic not being processed{% #traffic-not-being-processed %}

**Symptom**: No security events appear in the Datadog UI.

**Solutions**:

- Verify the external processor deployment is running: `kubectl get pods -n datadog -l app=datadog-aap-extproc`
- Look for warning logs in your reverse proxies concerning this part of the configuration.
- Check processor logs for connection errors: `kubectl logs -n datadog -l app=datadog-aap-extproc`
- Verify the processor service is correctly configured and resolvable
- Test connectivity from gateway pods to the processor service
- Ensure [Remote Configuration](https://docs.datadoghq.com/agent/remote_config/?tab=helm#enabling-remote-configuration) is enabled in your Datadog Agent

### External processor connection issues{% #external-processor-connection-issues %}

**Symptom**: Gateways cannot reach the external processor.

**Solutions**:

- Verify the processor service name and namespace match your configuration
- Check for NetworkPolicy rules blocking cross-namespace traffic
- For Envoy Gateway: Ensure `ReferenceGrant` resources exist for cross-namespace service references
- Test DNS resolution from gateway pods: `nslookup datadog-aap-extproc-service.datadog.svc.cluster.local`
- Verify the processor port configuration matches the service definition

### RBAC permission errors{% #rbac-permission-errors %}

**Symptom**: Cluster Agent logs show permission denied errors.

**Solutions**:

- Verify the Cluster Agent ClusterRole includes permissions for:
  - `gateway.envoyproxy.io/envoyextensionpolicies`
  - `networking.istio.io/envoyfilters`
  - `gateway.networking.k8s.io/gateways`
  - `gateway.networking.k8s.io/gatewayclasses`
- Check that the ClusterRoleBinding references the correct service account
- Make sure you are using the newest version of the Datadog Helm Chart or Operator.

## Further Reading{% #further-reading %}

- [Collect your application traces](https://docs.datadoghq.com/containers/kubernetes/apm/)
- [Collect your application logs](https://docs.datadoghq.com/containers/kubernetes/log/)
- [App and API Protection for Envoy Gateway](https://docs.datadoghq.com/security/application_security/setup/kubernetes/envoy-gateway)
- [App and API Protection for Istio](https://docs.datadoghq.com/security/application_security/setup/kubernetes/istio)
- [OOTB App and API Protection Rules](https://docs.datadoghq.com/security/default_rules/?category=cat-application-security)
- [Troubleshooting App and API Protection](https://docs.datadoghq.com/security/application_security/troubleshooting)