From 937025b89200b5e979c408d596d3880d4479c117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20L=C3=B6sche?= Date: Mon, 7 Oct 2024 12:26:16 +0200 Subject: [PATCH] Add UserAgent headers --- .github/workflows/release.yml | 4 ++-- auth/auth.go | 4 ++++ cmd/root.go | 6 ++---- config/config.go | 7 +++++++ config/config_test.go | 37 +++++++++++++++++++++++++++++++++++ main.go | 3 --- search/search.go | 2 ++ 7 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 config/config.go create mode 100644 config/config_test.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4f97201..df27061 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,11 +35,11 @@ jobs: echo "OS_NAME=$OS_NAME" >> $GITHUB_ENV - name: Build Binary (amd64) run: | - GOARCH=amd64 go build -ldflags "-s -w -X main.version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-amd64-${{ github.ref_name }}$SUFFIX" + GOARCH=amd64 go build -ldflags "-s -w -X github.com/someengineering/fixctl/config.Version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-amd64-${{ github.ref_name }}$SUFFIX" shell: bash - name: Build Binary (arm64) run: | - GOARCH=arm64 go build -ldflags "-s -w -X main.version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-arm64-${{ github.ref_name }}$SUFFIX" + GOARCH=arm64 go build -ldflags "-s -w -X github.com/someengineering/fixctl/config.Version=v${{ github.ref_name }}" -o "${{ github.workspace }}/release/fixctl-$OS_NAME-arm64-${{ github.ref_name }}$SUFFIX" shell: bash - name: Generate Universal Binary (macOS) if: matrix.os == 'macos-latest' diff --git a/auth/auth.go b/auth/auth.go index a021fd0..be30e20 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -8,6 +8,8 @@ import ( "net/http" "net/url" "strings" + + "github.com/someengineering/fixctl/config" ) func LoginAndGetJWT(apiEndpoint, username, password string) (string, error) { @@ -21,6 +23,7 @@ func LoginAndGetJWT(apiEndpoint, username, password string) (string, error) { return "", fmt.Errorf("creating login request failed: %w", err) } + req.Header.Set("User-Agent", config.GetUserAgent()) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") req.Header.Add("Accept", "application/json") @@ -57,6 +60,7 @@ func GetJWTFromToken(apiEndpoint, fixToken string) (string, error) { if err != nil { return "", err } + req.Header.Set("User-Agent", config.GetUserAgent()) req.Header.Set("Content-Type", "application/json") client := &http.Client{} diff --git a/cmd/root.go b/cmd/root.go index 3675b7b..7872d45 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,6 +6,7 @@ import ( "github.com/sirupsen/logrus" "github.com/someengineering/fixctl/auth" + "github.com/someengineering/fixctl/config" "github.com/someengineering/fixctl/format" "github.com/someengineering/fixctl/search" "github.com/someengineering/fixctl/utils" @@ -36,6 +37,7 @@ var ( func init() { cobra.OnInitialize(initConfig) + rootCmd.Version = config.Version rootCmd.PersistentFlags().StringVar(&apiEndpoint, "endpoint", "https://app.fix.security", "API endpoint URL (env FIX_ENDPOINT)") rootCmd.PersistentFlags().StringVar(&fixToken, "token", "", "Auth token (env FIX_TOKEN)") @@ -158,7 +160,3 @@ func executeSearch(cmd *cobra.Command, args []string) { func Execute() error { return rootCmd.Execute() } - -func SetVersion(version string) { - rootCmd.Version = version -} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..63cbad6 --- /dev/null +++ b/config/config.go @@ -0,0 +1,7 @@ +package config + +var Version string = "dev" + +func GetUserAgent() string { + return "fixctl-" + Version +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..6d94d9e --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,37 @@ +package config + +import ( + "testing" +) + +func TestVersion(t *testing.T) { + originalVersion := Version + + newVersion := "v1.2.3" + Version = newVersion + if Version != newVersion { + t.Errorf("Expected version %s, but got %s", newVersion, Version) + } + + Version = originalVersion + if Version != originalVersion { + t.Errorf("Expected version %s, but got %s", originalVersion, Version) + } +} + +func TestGetUserAgent(t *testing.T) { + originalVersion := Version + + expectedUserAgent := "fixctl-dev" + if userAgent := GetUserAgent(); userAgent != expectedUserAgent { + t.Errorf("Expected User-Agent %s, but got %s", expectedUserAgent, userAgent) + } + + Version = "v1.2.3" + expectedUserAgent = "fixctl-v1.2.3" + if userAgent := GetUserAgent(); userAgent != expectedUserAgent { + t.Errorf("Expected User-Agent %s, but got %s", expectedUserAgent, userAgent) + } + + Version = originalVersion +} diff --git a/main.go b/main.go index 533bf2c..c66660a 100644 --- a/main.go +++ b/main.go @@ -7,14 +7,11 @@ import ( "github.com/someengineering/fixctl/cmd" ) -var version = "dev" - func main() { logrus.SetFormatter(&logrus.TextFormatter{ FullTimestamp: true, }) - cmd.SetVersion(version) if err := cmd.Execute(); err != nil { logrus.Errorln("Error:", err) os.Exit(1) diff --git a/search/search.go b/search/search.go index 9941074..cc168df 100644 --- a/search/search.go +++ b/search/search.go @@ -10,6 +10,7 @@ import ( "net/http" "github.com/sirupsen/logrus" + "github.com/someengineering/fixctl/config" "github.com/someengineering/fixctl/utils" ) @@ -41,6 +42,7 @@ func SearchGraph(apiEndpoint, fixJWT, workspaceID, searchStr string, withEdges b return } + req.Header.Set("User-Agent", config.GetUserAgent()) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/ndjson") req.AddCookie(&http.Cookie{