Skip to content

Commit 92e7bbd

Browse files
authored
fix(api): fall back to GET on forbidden POSTs (#2030)
Signed-off-by: immanuwell <pchpr.00@list.ru>
1 parent 7d30a88 commit 92e7bbd

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

api/prometheus/v1/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,8 +1545,8 @@ func (h *apiClientImpl) Do(ctx context.Context, req *http.Request) (*http.Respon
15451545
return resp, []byte(result.Data), result.Warnings, err
15461546
}
15471547

1548-
// DoGetFallback will attempt to do the request as-is, and on a 405 or 501 it
1549-
// will fallback to a GET request.
1548+
// DoGetFallback will attempt to do the request as-is, and on a 403, 405, or
1549+
// 501 it will fallback to a GET request.
15501550
func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error) {
15511551
encodedArgs := args.Encode()
15521552
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(encodedArgs))
@@ -1564,7 +1564,7 @@ func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url.
15641564
req.Header["Idempotency-Key"] = nil
15651565

15661566
resp, body, warnings, err := h.Do(ctx, req)
1567-
if resp != nil && (resp.StatusCode == http.StatusMethodNotAllowed || resp.StatusCode == http.StatusNotImplemented) {
1567+
if resp != nil && (resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusMethodNotAllowed || resp.StatusCode == http.StatusNotImplemented) {
15681568
u.RawQuery = encodedArgs
15691569
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
15701570
if err != nil {

api/prometheus/v1/api_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,13 @@ func TestDoGetFallback(t *testing.T) {
19211921

19221922
body, _ := json.Marshal(apiResp)
19231923

1924+
if req.Method == http.MethodPost {
1925+
if req.URL.Path == "/blockPost403" {
1926+
http.Error(w, string(body), http.StatusForbidden)
1927+
return
1928+
}
1929+
}
1930+
19241931
if req.Method == http.MethodPost {
19251932
if req.URL.Path == "/blockPost405" {
19261933
http.Error(w, string(body), http.StatusMethodNotAllowed)
@@ -1965,6 +1972,22 @@ func TestDoGetFallback(t *testing.T) {
19651972
t.Fatalf("Mismatch in values")
19661973
}
19671974

1975+
// Do a fallback to a get on 403.
1976+
u.Path = "/blockPost403"
1977+
_, b, _, err = api.DoGetFallback(context.TODO(), u, v)
1978+
if err != nil {
1979+
t.Fatalf("Error doing local request: %v", err)
1980+
}
1981+
if err := json.Unmarshal(b, resp); err != nil {
1982+
t.Fatal(err)
1983+
}
1984+
if resp.Method != http.MethodGet {
1985+
t.Fatalf("Mismatch method")
1986+
}
1987+
if resp.Values != v.Encode() {
1988+
t.Fatalf("Mismatch in values")
1989+
}
1990+
19681991
// Do a fallback to a get on 405.
19691992
u.Path = "/blockPost405"
19701993
_, b, _, err = api.DoGetFallback(context.TODO(), u, v)

0 commit comments

Comments
 (0)