forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLAT-111995] detect and prevent polluted data return to user (#51)
- Loading branch information
Showing
5 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package store | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/thanos-io/thanos/pkg/store/storepb" | ||
) | ||
|
||
// So far we found there is a bug in prometheus tsdb code when OOO is enabled. | ||
// This is a workaround to detect the issue when tsdb selects irrelevant matched data. | ||
// See https://github.com/thanos-io/thanos/issues/7481 | ||
func detectCorruptLabels(lbls labels.Labels, matchers []storepb.LabelMatcher) bool { | ||
for _, m := range matchers { | ||
v := lbls.Get(m.Name) | ||
if v == "" { | ||
continue | ||
} | ||
if (m.Type == storepb.LabelMatcher_EQ && v != m.Value) || | ||
(m.Type == storepb.LabelMatcher_NEQ && v == m.Value) { | ||
return true | ||
} else if m.Name == labels.MetricName && | ||
(m.Type == storepb.LabelMatcher_RE || m.Type == storepb.LabelMatcher_NRE) { | ||
matcher, err := labels.NewFastRegexMatcher(m.Value) | ||
return err != nil || | ||
(m.Type == storepb.LabelMatcher_RE && !matcher.MatchString(v)) || | ||
(m.Type == storepb.LabelMatcher_NRE && matcher.MatchString(v)) | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func requestMatches(matchers []storepb.LabelMatcher) string { | ||
var b strings.Builder | ||
for _, m := range matchers { | ||
b.WriteString(m.String()) | ||
} | ||
return b.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package store | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/thanos-io/thanos/pkg/store/storepb" | ||
) | ||
|
||
func TestDetectCorruptLabels_EQ(t *testing.T) { | ||
good := labels.New(labels.Label{Name: labels.MetricName, Value: "up"}) | ||
bad := labels.New(labels.Label{Name: labels.MetricName, Value: "kube_proxy_corrupt"}) | ||
eq := []storepb.LabelMatcher{{Name: "__name__", Type: storepb.LabelMatcher_EQ, Value: "up"}} | ||
neq := []storepb.LabelMatcher{{Name: "__name__", Type: storepb.LabelMatcher_NEQ, Value: "up"}} | ||
assert.False(t, detectCorruptLabels(good, eq)) | ||
assert.True(t, detectCorruptLabels(bad, eq)) | ||
assert.False(t, detectCorruptLabels(bad, neq)) | ||
assert.True(t, detectCorruptLabels(good, neq)) | ||
} | ||
|
||
func TestDetectCorruptLabels_RE(t *testing.T) { | ||
good := labels.New(labels.Label{Name: labels.MetricName, Value: "usage_database_pool"}) | ||
bad := labels.New(labels.Label{Name: labels.MetricName, Value: "kube_proxy_corrupt"}) | ||
re := []storepb.LabelMatcher{{Name: "__name__", Type: storepb.LabelMatcher_RE, Value: "usage_.+_pool"}} | ||
nre := []storepb.LabelMatcher{{Name: "__name__", Type: storepb.LabelMatcher_NRE, Value: "usage_.+_pool"}} | ||
assert.False(t, detectCorruptLabels(good, re)) | ||
assert.True(t, detectCorruptLabels(bad, re)) | ||
assert.False(t, detectCorruptLabels(bad, nre)) | ||
assert.True(t, detectCorruptLabels(good, nre)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters