From 61a15b04cade3a5cdf6cf5aed298cf3a8074d72a Mon Sep 17 00:00:00 2001 From: Lucas Pearson Date: Thu, 28 Mar 2024 06:15:41 -0400 Subject: [PATCH 01/11] add a default user-agent header --- system/http.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/system/http.go b/system/http.go index cdbf589cf..b41c14b71 100644 --- a/system/http.go +++ b/system/http.go @@ -16,6 +16,9 @@ import ( "github.com/goss-org/goss/util" ) +const USER_AGENT_HEADER_PREFIX = "user-agent:" +const DEFAULT_USER_AGENT = "goss/x.x.x" + type HTTP interface { HTTP() string Status() (int, error) @@ -47,6 +50,11 @@ type DefHTTP struct { func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP { headers := http.Header{} + + if !hasUserAgentHeader(config.RequestHeader) { + config.RequestHeader = append(config.RequestHeader, fmt.Sprintf("%s %s", USER_AGENT_HEADER_PREFIX, DEFAULT_USER_AGENT)) + } + for _, r := range config.RequestHeader { str := strings.SplitN(r, ": ", 2) headers.Add(str[0], str[1]) @@ -209,3 +217,12 @@ func (u *DefHTTP) Body() (io.Reader, error) { return u.resp.Body, nil } + +func hasUserAgentHeader(headers []string) bool { + for _, header := range headers { + if strings.HasPrefix(header, USER_AGENT_HEADER_PREFIX) { + return true + } + } + return false +} From 4fef577962fcfaa22051bc18304a1931169f4b88 Mon Sep 17 00:00:00 2001 From: Lucas Pearson Date: Sat, 20 Apr 2024 17:36:56 -0400 Subject: [PATCH 02/11] add Version to util package; set util.Version as part of the release-build.sh script; use util.Version to make user-agent for http action --- release-build.sh | 2 +- system/http.go | 4 ++-- util/build.go | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 util/build.go diff --git a/release-build.sh b/release-build.sh index 0e1f7bfbb..9a3fa087c 100755 --- a/release-build.sh +++ b/release-build.sh @@ -22,7 +22,7 @@ fi output="${output_dir}/${output_fname}" GOOS="${os}" GOARCH="${arch}" CGO_ENABLED=0 go build \ - -ldflags "-X main.version=${version_stamp} -s -w" \ + -ldflags "-X main.version=${version_stamp} -X github.com/goss-org/goss/util.Version=${version_stamp} -s -w" \ -o "${output}" \ github.com/goss-org/goss/cmd/goss diff --git a/system/http.go b/system/http.go index b41c14b71..561183101 100644 --- a/system/http.go +++ b/system/http.go @@ -17,7 +17,7 @@ import ( ) const USER_AGENT_HEADER_PREFIX = "user-agent:" -const DEFAULT_USER_AGENT = "goss/x.x.x" +const DEFAULT_USER_AGENT_PREFIX = "goss/" type HTTP interface { HTTP() string @@ -52,7 +52,7 @@ func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.C headers := http.Header{} if !hasUserAgentHeader(config.RequestHeader) { - config.RequestHeader = append(config.RequestHeader, fmt.Sprintf("%s %s", USER_AGENT_HEADER_PREFIX, DEFAULT_USER_AGENT)) + config.RequestHeader = append(config.RequestHeader, fmt.Sprintf("%s %s%s", USER_AGENT_HEADER_PREFIX, DEFAULT_USER_AGENT_PREFIX, util.Version)) } for _, r := range config.RequestHeader { diff --git a/util/build.go b/util/build.go new file mode 100644 index 000000000..9deff372a --- /dev/null +++ b/util/build.go @@ -0,0 +1,3 @@ +package util + +var Version string From c9841dcfa5fcf168677372e46c648a4922b0a0e1 Mon Sep 17 00:00:00 2001 From: Lucas Pearson <36960305+catdevman@users.noreply.github.com> Date: Thu, 9 May 2024 04:29:53 -0400 Subject: [PATCH 03/11] Update check to lower case user input --- system/http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/http.go b/system/http.go index 561183101..d5e5abec7 100644 --- a/system/http.go +++ b/system/http.go @@ -220,7 +220,7 @@ func (u *DefHTTP) Body() (io.Reader, error) { func hasUserAgentHeader(headers []string) bool { for _, header := range headers { - if strings.HasPrefix(header, USER_AGENT_HEADER_PREFIX) { + if strings.HasPrefix(strings.ToLower(header), USER_AGENT_HEADER_PREFIX) { return true } } From 894d43810535348301428ccf338d134f487a4dc0 Mon Sep 17 00:00:00 2001 From: Lucas Pearson <36960305+catdevman@users.noreply.github.com> Date: Thu, 9 May 2024 04:31:55 -0400 Subject: [PATCH 04/11] Remove setting version in main --- release-build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release-build.sh b/release-build.sh index 9a3fa087c..2922fdac9 100755 --- a/release-build.sh +++ b/release-build.sh @@ -22,7 +22,7 @@ fi output="${output_dir}/${output_fname}" GOOS="${os}" GOARCH="${arch}" CGO_ENABLED=0 go build \ - -ldflags "-X main.version=${version_stamp} -X github.com/goss-org/goss/util.Version=${version_stamp} -s -w" \ + -ldflags "-X github.com/goss-org/goss/util.Version=${version_stamp} -s -w" \ -o "${output}" \ github.com/goss-org/goss/cmd/goss From 9da55ebdc8b6eff5b1451081935f5526ae4c9fdd Mon Sep 17 00:00:00 2001 From: Lucas Pearson <36960305+catdevman@users.noreply.github.com> Date: Thu, 9 May 2024 04:33:57 -0400 Subject: [PATCH 05/11] Remove version variable from main; use util.Version in place of mains version variable --- cmd/goss/goss.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/goss/goss.go b/cmd/goss/goss.go index 670094248..809d9905a 100644 --- a/cmd/goss/goss.go +++ b/cmd/goss/goss.go @@ -18,7 +18,6 @@ import ( "github.com/urfave/cli" ) -var version string // converts a cli context into a goss Config func newRuntimeConfigFromCLI(c *cli.Context) *util.Config { @@ -69,7 +68,7 @@ func timeoutFlag(value time.Duration) cli.DurationFlag { func main() { app := cli.NewApp() app.EnableBashCompletion = true - app.Version = version + app.Version = util.Version app.Name = "goss" app.Usage = "Quick and Easy server validation" app.Flags = []cli.Flag{ From 9761b2f77d341ccc155471928d117693f0321090 Mon Sep 17 00:00:00 2001 From: Lucas Pearson <36960305+catdevman@users.noreply.github.com> Date: Thu, 9 May 2024 05:36:46 -0400 Subject: [PATCH 06/11] Update goss-shared.yaml with httpbin check for default user-agent --- integration-tests/goss/goss-shared.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index 7966a7167..327731924 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -205,6 +205,11 @@ http: - "Foo: bar" headers: ["Content-Type: application/json"] body: ['"Foo": "bar"'] + http://httpbin/headers: + status: 200 + timeout: 60000 + headers: ["Content-Type: application/json"] + body: ['"User-Agent": "goss'] http://httpbin/headers?host: status: 200 timeout: 60000 From d9b7542ddb1801ca6b4d3e67aa26a0445c73f2ba Mon Sep 17 00:00:00 2001 From: Lucas Pearson <36960305+catdevman@users.noreply.github.com> Date: Fri, 10 May 2024 05:46:53 -0400 Subject: [PATCH 07/11] Update goss-shared.yaml Moved header tests into 1 --- integration-tests/goss/goss-shared.yaml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index 327731924..7eeadfd1c 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -204,12 +204,9 @@ http: request-headers: - "Foo: bar" headers: ["Content-Type: application/json"] - body: ['"Foo": "bar"'] - http://httpbin/headers: - status: 200 - timeout: 60000 - headers: ["Content-Type: application/json"] - body: ['"User-Agent": "goss'] + body: + - '"Foo": "bar"' + - '"user-agent": "goss/0.0.0"' http://httpbin/headers?host: status: 200 timeout: 60000 From 1b6184c68c2314c24f57949e4ca6c36b0e5f00b0 Mon Sep 17 00:00:00 2001 From: Lucas Pearson Date: Sat, 11 May 2024 05:46:49 -0400 Subject: [PATCH 08/11] go fmt over whole project --- cmd/goss/goss.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/goss/goss.go b/cmd/goss/goss.go index 809d9905a..e7ff90540 100644 --- a/cmd/goss/goss.go +++ b/cmd/goss/goss.go @@ -18,7 +18,6 @@ import ( "github.com/urfave/cli" ) - // converts a cli context into a goss Config func newRuntimeConfigFromCLI(c *cli.Context) *util.Config { cfg := &util.Config{ From 30f2f72022f868da993037cd37c93b5cf539e487 Mon Sep 17 00:00:00 2001 From: Lucas Pearson Date: Sat, 11 May 2024 09:30:23 -0400 Subject: [PATCH 09/11] changed string array back to brackets instead of dashes --- integration-tests/goss/goss-shared.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index 7eeadfd1c..ea6a044ea 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -204,9 +204,7 @@ http: request-headers: - "Foo: bar" headers: ["Content-Type: application/json"] - body: - - '"Foo": "bar"' - - '"user-agent": "goss/0.0.0"' + body: ['"Foo": "bar"', '"user-agent": "goss/0.0.0"'] http://httpbin/headers?host: status: 200 timeout: 60000 From daca6b47e931f55bb01195bf8000de78aec752cd Mon Sep 17 00:00:00 2001 From: Lucas Pearson Date: Sat, 11 May 2024 10:44:31 -0400 Subject: [PATCH 10/11] sanity check --- integration-tests/goss/goss-shared.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index ea6a044ea..7966a7167 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -204,7 +204,7 @@ http: request-headers: - "Foo: bar" headers: ["Content-Type: application/json"] - body: ['"Foo": "bar"', '"user-agent": "goss/0.0.0"'] + body: ['"Foo": "bar"'] http://httpbin/headers?host: status: 200 timeout: 60000 From acd407b7bc7f8f354ec25855f52038d1b4be36fc Mon Sep 17 00:00:00 2001 From: Ahmed Elsabbahy Date: Thu, 16 May 2024 07:30:01 -0700 Subject: [PATCH 11/11] Added: user-agent test back into test file --- integration-tests/goss/goss-shared.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index 7966a7167..220ec4e8c 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -204,7 +204,9 @@ http: request-headers: - "Foo: bar" headers: ["Content-Type: application/json"] - body: ['"Foo": "bar"'] + body: + - '"Foo": "bar"' + - '"User-Agent": "goss/0.0.0"' http://httpbin/headers?host: status: 200 timeout: 60000