DEV Community

Siri Varma Vegiraju
Siri Varma Vegiraju

Posted on

How to Invoke API using Dapr Tokens

Key Concepts

Token Creation: Dapr uses shared tokens for API authentication. While there's no imposed format, the recommended approach is generating a random byte sequence encoded in Base64:

openssl rand 16 | base64
Enter fullscreen mode Exit fullscreen mode

Configuration by Deployment Type

Self-hosted Deployments:

  • Uses the DAPR_API_TOKEN environment variable
  • Token enforcement occurs when this variable is set at daprd process launch
  • Token rotation requires updating the environment variable and restarting the process

Kubernetes Deployments:

  • Leverages Kubernetes secrets to store the shared token
  • Requires creating a secret in each namespace where token authentication is needed
  • Uses deployment annotations to indicate token usage

Configuration Examples

Self-hosted Setup:

export DAPR_API_TOKEN=<token>
Enter fullscreen mode Exit fullscreen mode

Kubernetes Secret Creation:

kubectl create secret generic dapr-api-token --from-literal=token=<token>
Enter fullscreen mode Exit fullscreen mode

Kubernetes Deployment Annotation:

annotations:
  dapr.io/enabled: "true"
  dapr.io/api-token-secret: "dapr-api-token"
Enter fullscreen mode Exit fullscreen mode

Token Rotation

Kubernetes Token Rotation:

apiVersion: v1
kind: Secret
metadata:
  name: dapr-api-token
type: Opaque
data:
  token: <your-new-token>
Enter fullscreen mode Exit fullscreen mode

Apply to namespace and restart deployments:

kubectl apply --file token-secret.yaml --namespace <namespace-name>
kubectl rollout restart deployment/<deployment-name> --namespace <namespace-name>
Enter fullscreen mode Exit fullscreen mode

Client API Usage

HTTP Requests:

curl http://localhost:3500/v1.0/metadata \
  --header "dapr-api-token: my-token"
Enter fullscreen mode Exit fullscreen mode

gRPC Protocol:
Token must be included in gRPC metadata as dapr-api-token[0]

Application Access

Kubernetes Environment Variable:

containers:
- name: mycontainer
  image: myregistry/myapp
  envFrom:
  - secretRef:
      name: dapr-api-token
Enter fullscreen mode Exit fullscreen mode

Self-hosted Environment Variable:

export DAPR_API_TOKEN=<my-dapr-token>
Enter fullscreen mode Exit fullscreen mode

Reference: https://docs.dapr.io/operations/security/api-token/

Top comments (0)