From 3aadee86741968cf92fff7fa7b969d7b40864f23 Mon Sep 17 00:00:00 2001 From: Conor Mongey Date: Tue, 5 Jan 2021 01:07:35 +0000 Subject: [PATCH 1/7] Allow setting of headers in api client --- api/api.go | 9 +++++++++ api/api_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/api/api.go b/api/api.go index e54d37bb9..f0d4022b8 100644 --- a/api/api.go +++ b/api/api.go @@ -155,6 +155,8 @@ type Config struct { // // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig + + Headers map[string]string } // ClientConfig copies the configuration with a new client address, region, and @@ -527,6 +529,7 @@ type request struct { body io.Reader obj interface{} ctx context.Context + header http.Header } // setQueryOptions is used to annotate the request with @@ -621,6 +624,7 @@ func (r *request) toHTTP() (*http.Request, error) { req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password) } + req.Header = r.header req.Header.Add("Accept-Encoding", "gzip") if r.token != "" { req.Header.Set("X-Nomad-Token", r.token) @@ -649,6 +653,7 @@ func (c *Client) newRequest(method, path string) (*request, error) { Path: u.Path, RawPath: u.RawPath, }, + header: make(http.Header), params: make(map[string][]string), } if c.config.Region != "" { @@ -671,6 +676,10 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } + for key, value := range c.config.Headers { + r.header.Set(key, value) + } + return r, nil } diff --git a/api/api_test.go b/api/api_test.go index d46098796..e2dcd6321 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -341,6 +341,22 @@ func TestParseWriteMeta(t *testing.T) { } } +func TestClientHeader(t *testing.T) { + t.Parallel() + c, s := makeClient(t, func(c *Config) { + c.Headers = map[string]string{ + "Hello": "World", + } + }, nil) + defer s.Stop() + + r, _ := c.newRequest("GET", "/v1/jobs") + + if r.header.Get("Hello") != "World" { + t.Fatalf("bad: %v", r.header) + } +} + func TestQueryString(t *testing.T) { t.Parallel() c, s := makeClient(t, nil, nil) From 679864ea05c53f96474e5c394acf10f7cfc2b90c Mon Sep 17 00:00:00 2001 From: Conor Mongey Date: Tue, 5 Jan 2021 15:43:24 +0000 Subject: [PATCH 2/7] Prefer http.Header over map[string]string to allow for multi-valued headers --- api/api.go | 6 ++---- api/api_test.go | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/api/api.go b/api/api.go index f0d4022b8..0efb10bbb 100644 --- a/api/api.go +++ b/api/api.go @@ -156,7 +156,7 @@ type Config struct { // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig - Headers map[string]string + Headers http.Header } // ClientConfig copies the configuration with a new client address, region, and @@ -676,9 +676,7 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } - for key, value := range c.config.Headers { - r.header.Set(key, value) - } + r.header = c.config.Headers return r, nil } diff --git a/api/api_test.go b/api/api_test.go index e2dcd6321..0048ecdfe 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -344,8 +344,8 @@ func TestParseWriteMeta(t *testing.T) { func TestClientHeader(t *testing.T) { t.Parallel() c, s := makeClient(t, func(c *Config) { - c.Headers = map[string]string{ - "Hello": "World", + c.Headers = http.Header{ + "Hello": []string{"World"}, } }, nil) defer s.Stop() From c7cc71e2e058da5ab5b161f7e48045c9bb1c2d08 Mon Sep 17 00:00:00 2001 From: Conor Mongey Date: Tue, 5 Jan 2021 15:49:03 +0000 Subject: [PATCH 3/7] Headers -> Header --- api/api.go | 4 ++-- api/api_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/api.go b/api/api.go index 0efb10bbb..0e96043b8 100644 --- a/api/api.go +++ b/api/api.go @@ -156,7 +156,7 @@ type Config struct { // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig - Headers http.Header + Header http.Header } // ClientConfig copies the configuration with a new client address, region, and @@ -676,7 +676,7 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } - r.header = c.config.Headers + r.header = c.config.Header return r, nil } diff --git a/api/api_test.go b/api/api_test.go index 0048ecdfe..ccfc2b38e 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -344,7 +344,7 @@ func TestParseWriteMeta(t *testing.T) { func TestClientHeader(t *testing.T) { t.Parallel() c, s := makeClient(t, func(c *Config) { - c.Headers = http.Header{ + c.Header = http.Header{ "Hello": []string{"World"}, } }, nil) From b2724ba64dec69fb1b0d5ef746d9d6a7a2ca10b9 Mon Sep 17 00:00:00 2001 From: Conor Mongey Date: Tue, 5 Jan 2021 16:02:59 +0000 Subject: [PATCH 4/7] Ensure set headers have lower precedence than basic auth headers --- api/api.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index 0e96043b8..d037d3bc2 100644 --- a/api/api.go +++ b/api/api.go @@ -615,6 +615,8 @@ func (r *request) toHTTP() (*http.Request, error) { return nil, err } + req.Header = r.header + // Optionally configure HTTP basic authentication if r.url.User != nil { username := r.url.User.Username() @@ -624,7 +626,6 @@ func (r *request) toHTTP() (*http.Request, error) { req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password) } - req.Header = r.header req.Header.Add("Accept-Encoding", "gzip") if r.token != "" { req.Header.Set("X-Nomad-Token", r.token) From 214f4ad4d323ecdeba0bbd5bbe32bb7115b2012a Mon Sep 17 00:00:00 2001 From: Conor Mongey Date: Tue, 5 Jan 2021 16:03:16 +0000 Subject: [PATCH 5/7] Only override headers if they're set --- api/api.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index d037d3bc2..ed4333984 100644 --- a/api/api.go +++ b/api/api.go @@ -677,7 +677,9 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } - r.header = c.config.Header + if c.config.Header != nil { + r.header = c.config.Header + } return r, nil } From e18eac3ff4c759fa302334babcdd1305ee4962b7 Mon Sep 17 00:00:00 2001 From: Conor Mongey Date: Tue, 5 Jan 2021 16:15:38 +0000 Subject: [PATCH 6/7] make sync --- vendor/github.com/hashicorp/nomad/api/api.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/vendor/github.com/hashicorp/nomad/api/api.go b/vendor/github.com/hashicorp/nomad/api/api.go index e54d37bb9..ed4333984 100644 --- a/vendor/github.com/hashicorp/nomad/api/api.go +++ b/vendor/github.com/hashicorp/nomad/api/api.go @@ -155,6 +155,8 @@ type Config struct { // // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig + + Header http.Header } // ClientConfig copies the configuration with a new client address, region, and @@ -527,6 +529,7 @@ type request struct { body io.Reader obj interface{} ctx context.Context + header http.Header } // setQueryOptions is used to annotate the request with @@ -612,6 +615,8 @@ func (r *request) toHTTP() (*http.Request, error) { return nil, err } + req.Header = r.header + // Optionally configure HTTP basic authentication if r.url.User != nil { username := r.url.User.Username() @@ -649,6 +654,7 @@ func (c *Client) newRequest(method, path string) (*request, error) { Path: u.Path, RawPath: u.RawPath, }, + header: make(http.Header), params: make(map[string][]string), } if c.config.Region != "" { @@ -671,6 +677,10 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } + if c.config.Header != nil { + r.header = c.config.Header + } + return r, nil } From 79a4895ad7d9f794fae7a86fd3073de4c0081398 Mon Sep 17 00:00:00 2001 From: Conor Mongey Date: Wed, 6 Jan 2021 01:39:18 +0000 Subject: [PATCH 7/7] Revert "Headers -> Header" This reverts commit 71396fa721945e55f51bc90ed02522936450209b. --- api/api.go | 6 +++--- api/api_test.go | 2 +- vendor/github.com/hashicorp/nomad/api/api.go | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api/api.go b/api/api.go index ed4333984..bb553b645 100644 --- a/api/api.go +++ b/api/api.go @@ -156,7 +156,7 @@ type Config struct { // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig - Header http.Header + Headers http.Header } // ClientConfig copies the configuration with a new client address, region, and @@ -677,8 +677,8 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } - if c.config.Header != nil { - r.header = c.config.Header + if c.config.Headers != nil { + r.header = c.config.Headers } return r, nil diff --git a/api/api_test.go b/api/api_test.go index ccfc2b38e..0048ecdfe 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -344,7 +344,7 @@ func TestParseWriteMeta(t *testing.T) { func TestClientHeader(t *testing.T) { t.Parallel() c, s := makeClient(t, func(c *Config) { - c.Header = http.Header{ + c.Headers = http.Header{ "Hello": []string{"World"}, } }, nil) diff --git a/vendor/github.com/hashicorp/nomad/api/api.go b/vendor/github.com/hashicorp/nomad/api/api.go index ed4333984..bb553b645 100644 --- a/vendor/github.com/hashicorp/nomad/api/api.go +++ b/vendor/github.com/hashicorp/nomad/api/api.go @@ -156,7 +156,7 @@ type Config struct { // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig - Header http.Header + Headers http.Header } // ClientConfig copies the configuration with a new client address, region, and @@ -677,8 +677,8 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } - if c.config.Header != nil { - r.header = c.config.Header + if c.config.Headers != nil { + r.header = c.config.Headers } return r, nil