From 088df00edc4ed6c418097925c1b006c2dff98dfa Mon Sep 17 00:00:00 2001
From: Clark Boylan <clark.boylan@gmail.com>
Date: Tue, 28 Jan 2025 12:14:02 -0800
Subject: [PATCH] Update graphite to send CORS headers even on 400 responses

Newer grafana sends an options request that graphite responds to with a
400 response. This response did not include allowed origin headers
because it is a failure case. Update this header and the allowed methods
header to always be included even on 400 or other error responses.

This should ideally address the CORS errors we see with updated grafana.
An alternative is to update grafana to proxy the requests for us, but
this is less flexible as other tools may not have built in proxies.

The suggestion comes from this stackoverflow question and answer:

  https://stackoverflow.com/questions/20414669/nginx-add-headers-when-returning-400-codes

Change-Id: Icf1179d35e420384da72af839ca329548226ee63
---
 .../roles/graphite/templates/graphite-statsd.conf.j2      | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/playbooks/roles/graphite/templates/graphite-statsd.conf.j2 b/playbooks/roles/graphite/templates/graphite-statsd.conf.j2
index a440e27241..aef03914ea 100644
--- a/playbooks/roles/graphite/templates/graphite-statsd.conf.j2
+++ b/playbooks/roles/graphite/templates/graphite-statsd.conf.j2
@@ -52,10 +52,10 @@ server {
     proxy_set_header  X-Real-IP $remote_addr;
     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
 
-    add_header 'Access-Control-Allow-Origin' '*';
-    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
-    add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Grafana-Device-Id';
-    add_header 'Access-Control-Allow-Credentials' 'true';
+    add_header 'Access-Control-Allow-Origin' '*' always;
+    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
+    add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Grafana-Device-Id' always;
+    add_header 'Access-Control-Allow-Credentials' 'true' always;
   }
 
 }