/
trusted_endpoints.go
241 lines (198 loc) · 5.56 KB
/
trusted_endpoints.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package trustedEndpoints
import (
_ "embed"
"encoding/json"
"fmt"
"math"
"net/url"
"strings"
"github.com/Azure/azure-kusto-go/azkustodata/errors"
"github.com/samber/lo"
)
var (
Instance = createInstance()
//go:embed well_known_kusto_endpoints.json
jsonFile []byte
)
type AllowedEndpoints struct {
AllowedKustoSuffixes []string
AllowedKustoHostnames []string
}
type WellKnownKustoEndpointsDataStruct struct {
AllowedEndpointsByLogin map[string]AllowedEndpoints
}
func createInstance() *TrustedEndpoints {
matchers := map[string]*FastSuffixMatcher{}
wellKnownData := WellKnownKustoEndpointsDataStruct{}
err := json.Unmarshal(jsonFile, &wellKnownData)
if err != nil {
panic(err.Error())
}
for key, value := range wellKnownData.AllowedEndpointsByLogin {
rules := []MatchRule{}
for _, suf := range value.AllowedKustoSuffixes {
rules = append(rules, MatchRule{suffix: suf, exact: false})
}
for _, host := range value.AllowedKustoHostnames {
rules = append(rules, MatchRule{suffix: host, exact: true})
}
f, err := newFastSuffixMatcher(rules)
if err != nil {
panic(err.Error())
}
matchers[key] = f
}
return &TrustedEndpoints{matchers: matchers}
}
// SetOverridePolicy Set a policy to override all other trusted rules
func (trusted *TrustedEndpoints) SetOverridePolicy(matcher func(string) bool) {
trusted.overrideMatcher = matcher
}
type TrustedEndpoints struct {
matchers map[string]*FastSuffixMatcher
additionalMatcher *FastSuffixMatcher
overrideMatcher func(string) bool
}
type MatchRule struct {
suffix string
exact bool
}
type FastSuffixMatcher struct {
suffixLength int
rules map[string][]MatchRule
}
func tailLowerCase(str string, length int) string {
if length <= 0 {
return ""
}
if length >= len(str) {
return strings.ToLower(str)
}
return strings.ToLower(str[len(str)-length:])
}
func (matcher *FastSuffixMatcher) isMatch(candidate string) bool {
if len(candidate) < matcher.suffixLength {
return false
}
if lst, ok := matcher.rules[tailLowerCase(candidate, matcher.suffixLength)]; ok {
for _, rule := range lst {
if strings.HasSuffix(strings.ToLower(candidate), rule.suffix) {
if len(candidate) == len(rule.suffix) || !rule.exact {
return true
}
}
}
}
return false
}
func newFastSuffixMatcher(rules []MatchRule) (*FastSuffixMatcher, error) {
minSufLen := len(lo.MinBy(rules, func(a MatchRule, cur MatchRule) bool {
return len(a.suffix) < len(cur.suffix)
}).suffix)
if minSufLen == 0 || minSufLen == math.MaxInt32 {
return nil, errors.ES(
errors.OpUnknown,
errors.KClientArgs,
"FastSuffixMatcher should have at list one rule with at least one character",
).SetNoRetry()
}
processedRules := map[string][]MatchRule{}
for _, rule := range rules {
suffix := tailLowerCase(rule.suffix, minSufLen)
if lst, ok := processedRules[suffix]; !ok {
processedRules[suffix] = []MatchRule{rule}
} else {
processedRules[suffix] = append(lst, rule)
}
}
return &FastSuffixMatcher{
suffixLength: minSufLen,
rules: processedRules,
}, nil
}
func values[T comparable, R any](m map[T]R) []R {
l := make([]R, 0, len(m))
for _, val := range m {
l = append(l, val)
}
return l
}
func createFastSuffixMatcherFromExisting(rules []MatchRule, existing *FastSuffixMatcher) (*FastSuffixMatcher, error) {
if existing == nil || len(existing.rules) == 0 {
return newFastSuffixMatcher(rules)
}
if rules == nil || len(rules) == 0 {
return existing, nil
}
for _, elem := range existing.rules {
rules = append(rules, elem...)
}
return newFastSuffixMatcher(rules)
}
// AddTrustedHosts Add or set a list of trusted endpoints rules
func (trusted *TrustedEndpoints) AddTrustedHosts(rules []MatchRule, replace bool) error {
if rules == nil || len(rules) == 0 {
if replace {
trusted.additionalMatcher = nil
}
return nil
}
if replace {
trusted.additionalMatcher = nil
}
matcher, err := createFastSuffixMatcherFromExisting(rules, trusted.additionalMatcher)
trusted.additionalMatcher = matcher
return err
}
// ValidateTrustedEndpoint Validates the endpoint uri trusted
func (trusted *TrustedEndpoints) ValidateTrustedEndpoint(endpoint string, loginEndpoint string) error {
u, err := url.Parse(endpoint)
if err != nil {
return err
}
host := u.Host
if host == "" {
host = endpoint
}
// Check that target hostname is trusted and can accept security token
return trusted.validateHostnameIsTrusted(host, loginEndpoint)
}
func isLocalAddress(host string) bool {
if host == "localhost" || host == "127.0.0.1" || host == "::1" || host == "[::1]" {
return true
}
if strings.HasPrefix(host, "127.") && len(host) <= 15 && len(host) >= 9 {
for _, c := range host {
if c != '.' && (c < '0' || c > '9') {
return false
}
}
return true
}
return false
}
func (trusted *TrustedEndpoints) validateHostnameIsTrusted(host string, loginEndpoint string) error {
// The loopback is unconditionally allowed (since we trust ourselves)
if isLocalAddress(host) {
return nil
}
// Either check the override matcher OR the matcher:
override := trusted.overrideMatcher
if override != nil && override(host) {
return nil
} else {
matcher, ok := trusted.matchers[strings.ToLower(loginEndpoint)]
if ok && (*matcher).isMatch(host) {
return nil
}
}
matcher := trusted.additionalMatcher
if matcher != nil && matcher.isMatch(host) {
return nil
}
return errors.ES(
errors.OpUnknown,
errors.KClientArgs,
fmt.Sprintf("Can't communicate with '%s' as this hostname is currently not trusted; please see https://aka.ms/kustotrustedendpoints.", host),
).SetNoRetry()
}