From 79420d78cf86cc287f7f7a933effb95d15bedbfd Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 20 Aug 2017 20:13:05 -0700 Subject: [PATCH] agent: Adding X-Nomad-Token header parsing --- command/agent/http.go | 9 +++++++++ command/agent/http_test.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/command/agent/http.go b/command/agent/http.go index 13624d283..78a49049b 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -358,9 +358,18 @@ func (s *HTTPServer) parseRegion(req *http.Request, r *string) { } } +// parseToken is used to parse the X-Nomad-Token param +func (s *HTTPServer) parseToken(req *http.Request, token *string) { + if other := req.Header.Get("X-Nomad-Token"); other != "" { + *token = other + return + } +} + // parse is a convenience method for endpoints that need to parse multiple flags func (s *HTTPServer) parse(resp http.ResponseWriter, req *http.Request, r *string, b *structs.QueryOptions) bool { s.parseRegion(req, r) + s.parseToken(req, &b.SecretID) parseConsistency(req, b) parsePrefix(req, b) return parseWait(resp, req, b) diff --git a/command/agent/http_test.go b/command/agent/http_test.go index faecc93f2..0b7b02543 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -338,6 +338,24 @@ func TestParseRegion(t *testing.T) { } } +func TestParseToken(t *testing.T) { + t.Parallel() + s := makeHTTPServer(t, nil) + defer s.Shutdown() + + req, err := http.NewRequest("GET", "/v1/jobs", nil) + req.Header.Add("X-Nomad-Token", "foobar") + if err != nil { + t.Fatalf("err: %v", err) + } + + var token string + s.Server.parseToken(req, &token) + if token != "foobar" { + t.Fatalf("bad %s", token) + } +} + // TestHTTP_VerifyHTTPSClient asserts that a client certificate signed by the // appropriate CA is required when VerifyHTTPSClient=true. func TestHTTP_VerifyHTTPSClient(t *testing.T) {