From 1b523c09d480d3c22022cd40f5918b0274191952 Mon Sep 17 00:00:00 2001 From: Lucas Pearson <36960305+catdevman@users.noreply.github.com> Date: Thu, 16 May 2024 10:53:25 -0400 Subject: [PATCH] add a default user-agent header (#892) * add a default user-agent header * 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 * Update check to lower case user input * Remove setting version in main * Remove version variable from main; use util.Version in place of mains version variable * Update goss-shared.yaml with httpbin check for default user-agent * Update goss-shared.yaml Moved header tests into 1 * go fmt over whole project * changed string array back to brackets instead of dashes * sanity check * Added: user-agent test back into test file --------- Co-authored-by: Ahmed Elsabbahy --- cmd/goss/goss.go | 4 +--- integration-tests/goss/goss-shared.yaml | 4 +++- release-build.sh | 2 +- system/http.go | 17 +++++++++++++++++ util/build.go | 3 +++ 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 util/build.go diff --git a/cmd/goss/goss.go b/cmd/goss/goss.go index 670094248..e7ff90540 100644 --- a/cmd/goss/goss.go +++ b/cmd/goss/goss.go @@ -18,8 +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 { cfg := &util.Config{ @@ -69,7 +67,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{ 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 diff --git a/release-build.sh b/release-build.sh index 0e1f7bfbb..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} -s -w" \ + -ldflags "-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 cdbf589cf..d5e5abec7 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_PREFIX = "goss/" + 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%s", USER_AGENT_HEADER_PREFIX, DEFAULT_USER_AGENT_PREFIX, util.Version)) + } + 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(strings.ToLower(header), USER_AGENT_HEADER_PREFIX) { + return true + } + } + return false +} 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