From 7795b6024cb9f24377b5544d1d1659eff9e6f8c7 Mon Sep 17 00:00:00 2001 From: Anton Averchenkov Date: Fri, 28 Jul 2023 14:33:14 -0400 Subject: [PATCH 1/6] Consolidate /sys/health error checks --- errors.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/errors.go b/errors.go index 1a71c8b2..a09124a2 100644 --- a/errors.go +++ b/errors.go @@ -10,6 +10,8 @@ import ( "io" "net/http" "strings" + + "golang.org/x/exp/slices" ) // IsErrorStatus returns true if the given error is either a ResponseError or a @@ -55,16 +57,15 @@ func isResponseError(req *http.Request, resp *http.Response) *ResponseError { return nil } - // 429 is returned by standby instances for /sys/health requests and should - // not be treated as an error; for other paths the status code indicates - // that the quota limit has been reached. - if resp.StatusCode == http.StatusTooManyRequests /* 429 */ && req.URL.Path == "/v1/sys/health" { - return nil - } - - // 472 & 473 are returned by (performance/secondary) standby instances for /sys/health requests and should - // not be treated as an error - if (resp.StatusCode == 472 || resp.StatusCode == 473) && req.URL.Path == "/v1/sys/health" { + // /v1/sys/health returns a few special 4xx status codes that should not be + // treated as errors: + // + // - 429 if unsealed and standby + // - 472 if disaster recovery mode replication secondary and active + // - 473 if performance standby + // + // See: https://developer.hashicorp.com/vault/api-docs/system/health + if req.URL.Path == "/v1/sys/health" && slices.Contains([]int{429, 472, 473}, resp.StatusCode) { return nil } From 91004ded8003e957781b616b9f49f156fa572d62 Mon Sep 17 00:00:00 2001 From: Anton Averchenkov <84287187+averche@users.noreply.github.com> Date: Sun, 30 Jul 2023 16:00:07 -0400 Subject: [PATCH 2/6] Use switch instead of slices.Contains Co-authored-by: Max Bowsher --- errors.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/errors.go b/errors.go index a09124a2..6306ad48 100644 --- a/errors.go +++ b/errors.go @@ -65,8 +65,12 @@ func isResponseError(req *http.Request, resp *http.Response) *ResponseError { // - 473 if performance standby // // See: https://developer.hashicorp.com/vault/api-docs/system/health - if req.URL.Path == "/v1/sys/health" && slices.Contains([]int{429, 472, 473}, resp.StatusCode) { - return nil + if req.URL.Path == "/v1/sys/health" { + switch resp.StatusCode { + case 429, 472, 473: + return nil + } + } } responseError := &ResponseError{ From 69399d9f08c172c8a4baa5e06dc10daafb5d206a Mon Sep 17 00:00:00 2001 From: Anton Averchenkov <84287187+averche@users.noreply.github.com> Date: Sun, 30 Jul 2023 16:01:24 -0400 Subject: [PATCH 3/6] cleanup --- errors.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/errors.go b/errors.go index 6306ad48..9c36ec30 100644 --- a/errors.go +++ b/errors.go @@ -10,8 +10,6 @@ import ( "io" "net/http" "strings" - - "golang.org/x/exp/slices" ) // IsErrorStatus returns true if the given error is either a ResponseError or a From c462cbc5153a1a06877745fe9757966d385895f7 Mon Sep 17 00:00:00 2001 From: Anton Averchenkov Date: Sun, 30 Jul 2023 16:03:05 -0400 Subject: [PATCH 4/6] fix build --- errors.go | 1 - 1 file changed, 1 deletion(-) diff --git a/errors.go b/errors.go index 9c36ec30..e9b24bff 100644 --- a/errors.go +++ b/errors.go @@ -69,7 +69,6 @@ func isResponseError(req *http.Request, resp *http.Response) *ResponseError { return nil } } - } responseError := &ResponseError{ StatusCode: resp.StatusCode, From 0d22c5e03c2d90b1458e69a57c51e08f36504fe1 Mon Sep 17 00:00:00 2001 From: Anton Averchenkov Date: Mon, 18 Sep 2023 13:38:30 -0400 Subject: [PATCH 5/6] PR feedback --- errors.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/errors.go b/errors.go index e9b24bff..9ffa5ef0 100644 --- a/errors.go +++ b/errors.go @@ -55,19 +55,13 @@ func isResponseError(req *http.Request, resp *http.Response) *ResponseError { return nil } - // /v1/sys/health returns a few special 4xx status codes that should not be - // treated as errors: - // - // - 429 if unsealed and standby - // - 472 if disaster recovery mode replication secondary and active - // - 473 if performance standby + // /v1/sys/health returns a few special 4xx and 5xx status codes that + // should not be treated as errors; the response will contain valuable + // health status information. // // See: https://developer.hashicorp.com/vault/api-docs/system/health - if req.URL.Path == "/v1/sys/health" { - switch resp.StatusCode { - case 429, 472, 473: - return nil - } + if req.URL.Path == "/v1/sys/health" && resp != nil { + return nil } responseError := &ResponseError{ From 911910a134189a377d63288060b1f7f419b803e3 Mon Sep 17 00:00:00 2001 From: Anton Averchenkov Date: Mon, 18 Sep 2023 13:56:45 -0400 Subject: [PATCH 6/6] comment --- errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/errors.go b/errors.go index 9ffa5ef0..215c43e0 100644 --- a/errors.go +++ b/errors.go @@ -56,7 +56,7 @@ func isResponseError(req *http.Request, resp *http.Response) *ResponseError { } // /v1/sys/health returns a few special 4xx and 5xx status codes that - // should not be treated as errors; the response will contain valuable + // should not be treated as errors; the response body will contain valuable // health status information. // // See: https://developer.hashicorp.com/vault/api-docs/system/health