mirror of
https://github.com/kemko/nomad.git
synced 2026-01-08 11:25:41 +03:00
Allow users to configure arbitrary headers in config
This commit is contained in:
@@ -101,6 +101,10 @@ type Config struct {
|
||||
|
||||
// List of config files that have been loaded (in order)
|
||||
Files []string
|
||||
|
||||
// HTTPAPIResponseHeaders allows users to configure the Nomad http agent to
|
||||
// set arbritrary headers on API responses
|
||||
HTTPAPIResponseHeaders map[string]string `hcl:"http_api_response_headers"`
|
||||
}
|
||||
|
||||
// AtlasConfig is used to enable an parameterize the Atlas integration
|
||||
|
||||
@@ -453,6 +453,9 @@ func TestConfig_LoadConfigString(t *testing.T) {
|
||||
Join: true,
|
||||
Endpoint: "127.0.0.1:1234",
|
||||
},
|
||||
HTTPAPIResponseHeaders: map[string]string{
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
}
|
||||
|
||||
// Check parsing
|
||||
@@ -531,4 +534,7 @@ atlas {
|
||||
join = true
|
||||
endpoint = "127.0.0.1:1234"
|
||||
}
|
||||
http_api_response_headers {
|
||||
Access-Control-Allow-Origin = "*"
|
||||
}
|
||||
`
|
||||
|
||||
@@ -152,6 +152,7 @@ func (e *codedError) Code() int {
|
||||
// wrap is used to wrap functions to make them more convenient
|
||||
func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Request) (interface{}, error)) func(resp http.ResponseWriter, req *http.Request) {
|
||||
f := func(resp http.ResponseWriter, req *http.Request) {
|
||||
setHeaders(resp, s.agent.config.HTTPAPIResponseHeaders)
|
||||
// Invoke the handler
|
||||
reqURL := req.URL.String()
|
||||
start := time.Now()
|
||||
@@ -229,6 +230,13 @@ func setMeta(resp http.ResponseWriter, m *structs.QueryMeta) {
|
||||
setKnownLeader(resp, m.KnownLeader)
|
||||
}
|
||||
|
||||
// setHeaders is used to set canonical response header fields
|
||||
func setHeaders(resp http.ResponseWriter, headers map[string]string) {
|
||||
for field, value := range headers {
|
||||
resp.Header().Set(http.CanonicalHeaderKey(field), value)
|
||||
}
|
||||
}
|
||||
|
||||
// parseWait is used to parse the ?wait and ?index query params
|
||||
// Returns true on error
|
||||
func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
|
||||
|
||||
@@ -104,6 +104,26 @@ func TestSetMeta(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetHeaders(t *testing.T) {
|
||||
s := makeHTTPServer(t, nil)
|
||||
s.Agent.config.HTTPAPIResponseHeaders = map[string]string{"foo": "bar"}
|
||||
defer s.Cleanup()
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
return &structs.Job{Name: "foo"}, nil
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/kv/key", nil)
|
||||
s.Server.wrap(handler)(resp, req)
|
||||
header := resp.Header().Get("foo")
|
||||
|
||||
if header != "bar" {
|
||||
t.Fatalf("expected header: %v, actual: %v", "bar", header)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestContentTypeIsJSON(t *testing.T) {
|
||||
s := makeHTTPServer(t, nil)
|
||||
defer s.Cleanup()
|
||||
|
||||
Reference in New Issue
Block a user