Skip to content

Commit b0d216e

Browse files
committed
Add __markers__ tenantID validation
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent ce1f0ea commit b0d216e

File tree

8 files changed

+34
-7
lines changed

8 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* [BUGFIX] Querier: Fix panic when marshaling QueryResultRequest. #6601
4444
* [BUGFIX] Ingester: Avoid resharding for query when restart readonly ingesters. #6642
4545
* [BUGFIX] Query Frontend: Fix query frontend per `user` metrics clean up. #6698
46+
* [BUGFIX] Add `__markers__` tenant ID validation. #6761
4647

4748
## 1.19.0 2025-02-27
4849

docs/guides/limitations.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ The following character sets are generally **safe for use in the tenant ID**:
3232

3333
All other characters are not safe to use. In particular, slashes `/` and whitespaces (` `) are **not supported**.
3434

35+
### Invalid tenant IDs
36+
The following tenant IDs are considered invalid in Cortex.
37+
38+
- Current directory (`.`)
39+
- Parent directory (`..`)
40+
- Markers directory (`__markers__`)
41+
3542
### Length
3643

3744
The tenant ID length should not exceed 150 bytes/characters.

pkg/storage/tsdb/tenant_deletion_mark.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/pkg/errors"
1212
"github.com/thanos-io/objstore"
1313

14-
"github.com/cortexproject/cortex/pkg/util"
14+
"github.com/cortexproject/cortex/pkg/tenant"
1515
util_log "github.com/cortexproject/cortex/pkg/util/log"
1616
)
1717

@@ -77,7 +77,7 @@ func GetLocalDeletionMarkPath(userID string) string {
7777
}
7878

7979
func GetGlobalDeletionMarkPath(userID string) string {
80-
return path.Join(util.GlobalMarkersDir, userID, TenantDeletionMarkFile)
80+
return path.Join(tenant.GlobalMarkersDir, userID, TenantDeletionMarkFile)
8181
}
8282

8383
func exists(ctx context.Context, bkt objstore.BucketReader, markerFile string) (bool, error) {

pkg/storage/tsdb/users_scanner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"github.com/go-kit/log/level"
99
"github.com/thanos-io/objstore"
1010

11-
"github.com/cortexproject/cortex/pkg/util"
11+
"github.com/cortexproject/cortex/pkg/tenant"
1212
)
1313

1414
// AllUsers returns true to each call and should be used whenever the UsersScanner should not filter out
1515
// any user due to sharding.
1616
func AllUsers(user string) (bool, error) {
17-
if user == util.GlobalMarkersDir {
17+
if user == tenant.GlobalMarkersDir {
1818
return false, nil
1919
}
2020
return true, nil
@@ -52,7 +52,7 @@ func (s *UsersScanner) ScanUsers(ctx context.Context) (users, markedForDeletion
5252
}
5353

5454
// Scan users from the __markers__ directory.
55-
err = s.bucketClient.Iter(ctx, util.GlobalMarkersDir, func(entry string) error {
55+
err = s.bucketClient.Iter(ctx, tenant.GlobalMarkersDir, func(entry string) error {
5656
// entry will be of the form __markers__/<user>/
5757
parts := strings.Split(entry, objstore.DirDelim)
5858
userID := parts[1]

pkg/tenant/resolver_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ var commonResolverTestCases = []resolverTestCase{
7676
errTenantID: errTenantIDUnsafe,
7777
errTenantIDs: errTenantIDUnsafe,
7878
},
79+
{
80+
name: "__markers__",
81+
headerValue: strptr("__markers__"),
82+
errTenantID: errTenantIDMarkers,
83+
errTenantIDs: errTenantIDMarkers,
84+
},
7985
{
8086
name: "white space",
8187
headerValue: strptr(" "),

pkg/tenant/tenant.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import (
1010
"github.com/weaveworks/common/user"
1111
)
1212

13+
const GlobalMarkersDir = "__markers__"
14+
1315
var (
1416
errTenantIDTooLong = errors.New("tenant ID is too long: max 150 characters")
1517
errTenantIDUnsafe = errors.New("tenant ID is '.' or '..'")
18+
errTenantIDMarkers = errors.New("tenant ID '__markers__' is not allowed")
1619
)
1720

1821
type errTenantIDUnsupportedCharacter struct {
@@ -66,6 +69,12 @@ func ValidTenantID(s string) error {
6669
return errTenantIDTooLong
6770
}
6871

72+
// check tenantID is "__markers__"
73+
if s == GlobalMarkersDir {
74+
return errTenantIDMarkers
75+
}
76+
77+
// check tenantID is "." or ".."
6978
if containsUnsafePathSegments(s) {
7079
return errTenantIDUnsafe
7180
}

pkg/tenant/tenant_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func TestValidTenantIDs(t *testing.T) {
3737
name: "..",
3838
err: strptr("tenant ID is '.' or '..'"),
3939
},
40+
{
41+
name: "__markers__",
42+
err: strptr("tenant ID '__markers__' is not allowed"),
43+
},
4044
} {
4145
t.Run(tc.name, func(t *testing.T) {
4246
err := ValidTenantID(tc.name)

pkg/util/allowed_tenants.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package util
22

3-
const GlobalMarkersDir = "__markers__"
3+
import "github.com/cortexproject/cortex/pkg/tenant"
44

55
// AllowedTenants that can answer whether tenant is allowed or not based on configuration.
66
// Default value (nil) allows all tenants.
@@ -36,7 +36,7 @@ func NewAllowedTenants(enabled []string, disabled []string) *AllowedTenants {
3636
}
3737

3838
func (a *AllowedTenants) IsAllowed(tenantID string) bool {
39-
if tenantID == GlobalMarkersDir {
39+
if tenantID == tenant.GlobalMarkersDir {
4040
// __markers__ is reserved for global markers and no tenant should be allowed to have that name.
4141
return false
4242
}

0 commit comments

Comments
 (0)