From 64502a3f636c0feff572b515c10d2cbec1a95bca Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Wed, 16 Apr 2025 11:16:02 +0200 Subject: [PATCH 1/4] Remove .idea --- .gitignore | 2 +- .idea/.gitignore | 8 -------- .idea/check_influxdb.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 5 files changed, 1 insertion(+), 32 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/check_influxdb.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 052f6a0..a8a8094 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.idea +.idea/ check_influxdb vendor/ coverage.html diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/check_influxdb.iml b/.idea/check_influxdb.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/check_influxdb.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index c0d09e6..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 47286c11f472ac4dd0b3c233d3eb079366944456 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Wed, 16 Apr 2025 11:17:18 +0200 Subject: [PATCH 2/4] Add ARM builds --- .goreleaser.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.goreleaser.yml b/.goreleaser.yml index 5be559c..8643719 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -8,6 +8,7 @@ builds: - CGO_ENABLED=0 goarch: - amd64 + - arm64 goos: - linux - windows @@ -30,6 +31,7 @@ archives: {{- if eq .Os "windows" }}Windows{{ end }} {{- if eq .Os "darwin" }}Darwin{{ end }} {{- if eq .Arch "amd64" }}_x86_64{{ end }} + {{- if eq .Arch "arm64" }}_arm64{{ end }} checksum: name_template: 'checksums.txt' snapshot: From 632b535a0cf506f8d7419c188d55af993a4f1beb Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Wed, 16 Apr 2025 11:28:29 +0200 Subject: [PATCH 3/4] Add CLI flag to add additional HTTP headers --- README.md | 2 ++ cmd/config.go | 15 +++++++++++++++ cmd/health_test.go | 15 +++++++++++++++ cmd/root.go | 3 +++ internal/client/client.go | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+) diff --git a/README.md b/README.md index 1ad67f8..9ed1301 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ Global Flags: --key-file string Specify the Key File for TLS authentication (CHECK_INFLUXDB_KEY_FILE) -i, --insecure Skip the verification of the server's TLS certificate -t, --timeout int Timeout in seconds for the CheckPlugin (default 30) + --header strings Additional HTTP header to include in the request. Can be used multiple times. + Keys and values are separated by a colon (--header "X-Custom: example"). -h, --help help for check_influxdb -v, --version version for check_influxdb ``` diff --git a/cmd/config.go b/cmd/config.go index 8d905a6..e4f8aa2 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -24,6 +24,7 @@ type Config struct { KeyFile string `env:"CHECK_INFLUXDB_KEY_FILE"` Token string `env:"CHECK_INFLUXDB_TOKEN"` Organization string `env:"CHECK_INFLUXDB_ORGANISATION"` + Headers []string Port int Insecure bool Secure bool @@ -82,6 +83,20 @@ func (c *Config) NewClient() *client.Client { rt = checkhttpconfig.NewBasicAuthRoundTripper(u, p, rt) } + // If extra headers are set, parse them and add them to the request + if len(c.Headers) > 0 { + headers := make(map[string]string) + + for _, h := range c.Headers { + head := strings.Split(h, ":") + if len(head) == 2 { + headers[strings.TrimSpace(head[0])] = strings.TrimSpace(head[1]) + } + } + + rt = client.NewHeadersRoundTripper(headers, rt) + } + return client.NewClient(u.String(), c.Token, c.Organization, rt) } diff --git a/cmd/health_test.go b/cmd/health_test.go index 9817d8e..50bf000 100644 --- a/cmd/health_test.go +++ b/cmd/health_test.go @@ -63,6 +63,21 @@ func TestHealthCmd(t *testing.T) { args: []string{"run", "../main.go", "health"}, expected: "[UNKNOWN] - InfluxDB Version 3 not supported (*errors.errorString)\nexit status 3\n", }, + { + name: "health-extra-header", + httpreturn: func(w http.ResponseWriter, r *http.Request) { + foobar := r.Header.Get("X-Foobar") + if foobar == "Barfoo" { + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"checks":[],"message":"ready for queries and writes","name":"influxdb","status":"pass","version":"1.8.10"}`)) + return + } + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(`Wrong Header!`)) + }, + args: []string{"run", "../main.go", "--header", "X-Foobar: Barfoo", "health"}, + expected: "[OK] - InfluxDB Status: pass\n", + }, } for _, test := range tests { diff --git a/cmd/root.go b/cmd/root.go index 1bd5153..3667f32 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -59,6 +59,9 @@ func init() { "Skip the verification of the server's TLS certificate") pfs.IntVarP(&Timeout, "timeout", "t", Timeout, "Timeout in seconds for the CheckPlugin") + pfs.StringSliceVarP(&cliConfig.Headers, "header", "", nil, + `Additional HTTP header to include in the request. Can be used multiple times. +Keys and values are separated by a colon (--header "X-Custom: example").`) rootCmd.Flags().SortFlags = false pfs.SortFlags = false diff --git a/internal/client/client.go b/internal/client/client.go index 4396bc4..4fb569c 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -69,3 +69,39 @@ func (c *Client) Version() (influxdb.APIVersion, error) { return v, nil } + +type headersRoundTripper struct { + headers map[string]string + rt http.RoundTripper +} + +// NewHeadersRoundTripper adds the given headers to a request +func NewHeadersRoundTripper(headers map[string]string, rt http.RoundTripper) http.RoundTripper { + return &headersRoundTripper{headers, rt} +} + +func (rt *headersRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + // RoundTrip should not modify the request, except for + // consuming and closing the Request's Body. + req = cloneRequest(req) + + for key, value := range rt.headers { + req.Header.Add(key, value) + } + + return rt.rt.RoundTrip(req) +} + +// cloneRequest returns a clone of the provided *http.Request +func cloneRequest(r *http.Request) *http.Request { + // Shallow copy of the struct. + r2 := new(http.Request) + *r2 = *r + // Deep copy of the Header. + r2.Header = make(http.Header) + for k, s := range r.Header { + r2.Header[k] = s + } + + return r2 +} From ed7ad6561a364ebeab984f55394d5e6db38fa398 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Wed, 16 Apr 2025 11:29:43 +0200 Subject: [PATCH 4/4] Change CI to use Go stable --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c7bc26d..242d083 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,8 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: 1.24 + go-version: stable + check-latest: true - name: Test run: go test -v ./...