Moiz Zaveri
Moiz Zaveri

Reputation: 11

SendGrid wehbook events do not have "Accept" header

We have integrated SendGrid into our project to send emails, and for logging purposes, we configured one of our application endpoints in SendGrid's webhooks to receive events related to the emails we're sending.

However, our application is deployed on Azure Kubernetes and is accessible publicly via an Application Gateway. The gateway has a firewall that identifies the API requests from the webhook with an error message: "Request Missing an Accept Header." Since I don't want to disable the firewall rule entirely (as it helps identify other requests missing the header), is there a way to add custom headers to the webhook requests?

Upvotes: 0

Views: 57

Answers (1)

Arko
Arko

Reputation: 3851

Since you can't directly modify the headers sent by SendGrid's webhooks, workaround is to set up a proxy endpoint that adds the necessary Accept header before forwarding the requests to your actual endpoint. As you said you are running it on k8s you can create an Nginx proxy configuration file to add the "Accept" header and forward requests to your main application endpoint.

events {}

http {
    server {
        listen 8080;

        location / {
            # Add the Accept header
            proxy_set_header Accept "application/json";

            # Forward to the main application endpoint
            proxy_pass http://main-app-service.default.svc.cluster.local:80;
        }
    }
}

As you can see this configuration will add an Accept: application/json header and forward requests to main-app-service, which is the name of your main application’s Kubernetes Service in the default or whatever namespace you gave. Replace this with your actual service name as well.

Put this in a Dockerfile

# Use an Nginx base image
FROM nginx:alpine

# Copy the Nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Expose port 8080
EXPOSE 8080

# Run Nginx
CMD ["nginx", "-g", "daemon off;"]

build and push

docker build -t arkoacr.azurecr.io/webhook-proxy:latest .
az acr login --name arkoacr
docker push arkoacr.azurecr.io/webhook-proxy:latest

enter image description here

and reference in your webhook-proxy-deployment and service file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webhook-proxy
  namespace: webhook-proxy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webhook-proxy
  template:
    metadata:
      labels:
        app: webhook-proxy
    spec:
      containers:
        - name: nginx
          image: arkoacr.azurecr.io/webhook-proxy:latest
          ports:
            - containerPort: 8080
          resources:
            requests:
              memory: "64Mi"
              cpu: "250m"
            limits:
              memory: "128Mi"
              cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
  name: webhook-proxy
  namespace: webhook-proxy
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: webhook-proxy

enter image description here

This IP will be your new SendGrid webhook endpoint.

kubectl get svc -n webhook-proxy

enter image description here

Log into your SendGrid account and navigate to the Webhook settings. Update the webhook endpoint to the external IP obtained in the previous step. Done. This should add the Accept header and forward the request to your main application endpoint.

Upvotes: 0

Related Questions