Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to configure arbitrary headers in config #699

Merged
merged 3 commits into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ IMPROVEMENTS:
* drivers: Interpret Nomad variables in environment variables/args [GH-653]
* core: Populate job status [GH-663]
* core/cli: Print short identifiers and UX cleanup [GH-675, GH-693, GH-692]
* api: Allow users to set arbitrary headers via agent config [GH-699]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we normally handle the CHANGELOG outside of the PR itself? I think this should also be core/api as we have used before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah makes sense regarding renaming it to core/api. These days we are doing the CHANGELOG with the PRs so that we don't forget about them. Is there any downside to doing this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope just general practice so far is all. NBD!


BUG FIXES:
* cli: Handle parsing of un-named ports [GH-604]
Expand Down
4 changes: 4 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -531,4 +534,7 @@ atlas {
join = true
endpoint = "127.0.0.1:1234"
}
http_api_response_headers {
Access-Control-Allow-Origin = "*"
}
`
8 changes: 8 additions & 0 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions command/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions website/source/docs/agent/config.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ nodes, unless otherwise specified:
* `disable_anonymous_signature`: Disables providing an anonymous signature
for de-duplication with the update check. See `disable_update_check`.

* `http_api_response_headers`: This object allows adding headers to the
HTTP API responses. For example, the following config can be used to enable
CORS on the HTTP API endpoints:
```
http_api_response_headers {
Access-Control-Allow-Origin = "*"
}
```

## Server-specific Options

The following options are applicable to server agents only and need not be
Expand Down