Enable API token authentication in Dapr
By default, Dapr relies on the network boundary to limit access to its public API. If you plan on exposing the Dapr API outside of that boundary, or if your deployment demands an additional level of security, consider enabling the token authentication for Dapr APIs. This will cause Dapr to require every incoming gRPC and HTTP request for its APIs for to include authentication token, before allowing that request to pass through.
Dapr uses JWT tokens for API authentication.
To configure API authentication, start by generating your token using any JWT token compatible tool (e.g. ) and your secret.
Configure API token authentication in Dapr
The token authentication configuration is slightly different for either Kubernetes or self-hosted Dapr deployments:
In self-hosting scenario, Dapr looks for the presence of environment variable. If that environment variable is set while daprd
process launches, Dapr will enforce authentication on its public APIs:
Kubernetes
In Kubernetes deployment, Dapr leverages Kubernetes secrets store to hold the JWT token. To configure Dapr APIs authentication start by creating a new secret:
kubectl create secret generic dapr-api-token --from-literal=token=<token>
To indicate to Dapr to use that secret to secure its public APIs, add an annotation to your Deployment template spec:
annotations:
dapr.io/enabled: "true"
When deployed, Dapr sidecar injector will automatically create a secret reference and inject the actual value into DAPR_API_TOKEN
environment variable.
To rotate the configured token in self-hosted, simply set the DAPR_API_TOKEN
environment variable to the new value and restart the daprd
process.
Kubernetes
To rotate the configured token in Kubernates, update the previously created secret with the new token in each namespace. You can do that using kubectl patch
command, but the easiest way to update these in each namespace is by using manifest:
And then apply it to each namespace:
kubectl apply --file token-secret.yaml --namespace <namespace-name>
Adding JWT token to client API invocations
Once token authentication is configured in Dapr, all clients invoking Dapr API will have to append the JWT token to every request:
In case of HTTP, Dapr inspect the incoming request for presence of parameter in HTTP header:
gRPC
When using gRPC protocol, Dapr will inspect the incoming calls for the API token on the gRPC metadata:
dapr-api-token[0].
In Kubernetes, it’s recommended to mount the secret to your pod as an environment variable, as shown in the example below, where a Kubernetes secret with the name dapr-api-token
is used to hold the token.
containers:
- name: mycontainer
image: myregistry/myapp
envFrom:
name: dapr-api-token
Self-hosted
In self-hosted mode, you can set the token as an environment variable for your app:
Related Links
- Learn about
- Learn HowTo authenticate requests from Dapr using token authentication