-
Notifications
You must be signed in to change notification settings - Fork 873
Expand file tree
/
Copy pathtenant.go
More file actions
159 lines (131 loc) · 3.7 KB
/
Copy pathtenant.go
File metadata and controls
159 lines (131 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package users
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"github.com/weaveworks/common/user"
)
const (
GlobalMarkersDir = "__markers__"
// QuerierMetaCacheDirName is the reserved name of the directory, inside the bucket store
// sync directory (-blocks-storage.bucket-store.sync-dir), where the querier's bucket-scan
// blocks finder keeps its own on-disk block-meta caches
// (<sync-dir>/__querier__/<tenant>/). The querier and the store-gateway can share the sync
// directory when running in the same process, and each reconciles its own on-disk state
// against a different view of which tenants are live, so their per-tenant caches must not
// overlap. Like GlobalMarkersDir, the name is rejected as a tenant ID and skipped by the
// users scanner, so nothing can treat it as a tenant.
QuerierMetaCacheDirName = "__querier__"
)
var (
errTenantIDTooLong = errors.New("tenant ID is too long: max 150 characters")
errTenantIDUnsafe = errors.New("tenant ID is '.' or '..'")
errTenantIDMarkers = errors.New("tenant ID '__markers__' is not allowed")
errTenantIDUserIndex = errors.New("tenant ID 'user-index.json.gz' is not allowed")
errTenantIDQuerierMetaDir = errors.New("tenant ID '__querier__' is not allowed")
)
type errTenantIDUnsupportedCharacter struct {
pos int
tenantID string
}
func (e *errTenantIDUnsupportedCharacter) Error() string {
return fmt.Sprintf(
"tenant ID '%s' contains unsupported character '%c'",
e.tenantID,
e.tenantID[e.pos],
)
}
const tenantIDsLabelSeparator = "|"
// NormalizeTenantIDs is creating a normalized form by sorting and de-duplicating the list of tenantIDs
func NormalizeTenantIDs(tenantIDs []string) []string {
sort.Strings(tenantIDs)
count := len(tenantIDs)
if count <= 1 {
return tenantIDs
}
posOut := 1
for posIn := 1; posIn < count; posIn++ {
if tenantIDs[posIn] != tenantIDs[posIn-1] {
tenantIDs[posOut] = tenantIDs[posIn]
posOut++
}
}
return tenantIDs[0:posOut]
}
// ValidTenantID validate tenantID
func ValidTenantID(s string) error {
// check if it contains invalid runes
for pos, r := range s {
if !isSupported(r) {
return &errTenantIDUnsupportedCharacter{
tenantID: s,
pos: pos,
}
}
}
if err := CheckTenantIDLength(s); err != nil {
return err
}
if err := CheckTenantIDIsSupported(s); err != nil {
return err
}
return nil
}
func CheckTenantIDLength(s string) error {
if len(s) > 150 {
return errTenantIDTooLong
}
return nil
}
func CheckTenantIDIsSupported(s string) error {
// check tenantID is "__markers__"
if s == GlobalMarkersDir {
return errTenantIDMarkers
}
if s == "user-index.json.gz" {
return errTenantIDUserIndex
}
// check tenantID is the querier's reserved meta cache directory
if s == QuerierMetaCacheDirName {
return errTenantIDQuerierMetaDir
}
// check tenantID is "." or ".."
if containsUnsafePathSegments(s) {
return errTenantIDUnsafe
}
return nil
}
func JoinTenantIDs(tenantIDs []string) string {
return strings.Join(tenantIDs, tenantIDsLabelSeparator)
}
// this checks if a rune is supported in tenant IDs (according to
// https://cortexmetrics.io/docs/guides/limitations/#tenant-id-naming)
func isSupported(c rune) bool {
// characters
if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') {
return true
}
// digits
if '0' <= c && c <= '9' {
return true
}
// special
return c == '!' ||
c == '-' ||
c == '_' ||
c == '.' ||
c == '*' ||
c == '\'' ||
c == '(' ||
c == ')'
}
// TenantIDsFromOrgID extracts different tenants from an orgID string value
//
// ignore stutter warning
//
//nolint:revive
func TenantIDsFromOrgID(orgID string) ([]string, error) {
return TenantIDs(user.InjectOrgID(context.TODO(), orgID))
}