Skip to content

Commit 30d010f

Browse files
committed
return 401 when tenant ID is missing in PRW2
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent bf8ae57 commit 30d010f

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [BUGFIX] Fix nil when ingester_query_max_attempts > 1. #7369
1111
* [BUGFIX] Querier: Fix queryWithRetry and labelsWithRetry returning (nil, nil) on cancelled context by propagating ctx.Err(). #7370
1212
* [BUGFIX] Metrics Helper: Fix non-deterministic bucket order in merged histograms by sorting buckets after map iteration, matching Prometheus client library behavior. #7380
13+
* [BUGFIX] Distributor: Return HTTP 401 Unauthorized when tenant ID resolution fails in the Prometheus Remote Write 2.0 path. #7389
1314

1415
## 1.21.0 in progress
1516

pkg/util/push/push.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func Handler(remoteWrite2Enabled bool, acceptUnknownRemoteWriteContentType bool,
9090
handlePRW2 := func() {
9191
userID, err := users.TenantID(ctx)
9292
if err != nil {
93+
http.Error(w, err.Error(), http.StatusUnauthorized)
9394
return
9495
}
9596

pkg/util/push/push_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,3 +1339,26 @@ func Test_convertV2RequestToV1_ExplicitStartTimestampTakesPrecedence(t *testing.
13391339
assert.Equal(t, int64(0), v1Req.Timeseries[0].Histograms[0].StartTimestampMs)
13401340
})
13411341
}
1342+
1343+
func TestHandler_remoteWriteV2_UnauthorizedWithoutTenantID(t *testing.T) {
1344+
var limits validation.Limits
1345+
flagext.DefaultValues(&limits)
1346+
overrides := validation.NewOverrides(limits, nil)
1347+
1348+
pushCalled := false
1349+
pushFunc := func(ctx context.Context, req *cortexpb.WriteRequest) (*cortexpb.WriteResponse, error) {
1350+
pushCalled = true
1351+
return &cortexpb.WriteResponse{}, nil
1352+
}
1353+
1354+
handler := Handler(true, false, 100000, overrides, nil, pushFunc, nil)
1355+
1356+
req := createRequest(t, createPrometheusRemoteWriteV2Protobuf(t), true)
1357+
1358+
resp := httptest.NewRecorder()
1359+
handler.ServeHTTP(resp, req)
1360+
1361+
assert.Equal(t, http.StatusUnauthorized, resp.Code)
1362+
assert.Contains(t, resp.Body.String(), user.ErrNoOrgID.Error())
1363+
assert.False(t, pushCalled, "push function must not be called when tenant ID is missing")
1364+
}

0 commit comments

Comments
 (0)