-
Notifications
You must be signed in to change notification settings - Fork 111
/
security.go
182 lines (162 loc) · 4.24 KB
/
security.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
package runtime
import (
"crypto/md5"
"encoding/binary"
"encoding/hex"
"fmt"
"sync"
"time"
"github.com/hashicorp/golang-lru/simplelru"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/compilers/rillv1"
"go.uber.org/zap"
)
type securityEngine struct {
cache *simplelru.LRU
lock sync.Mutex
logger *zap.Logger
}
func newSecurityEngine(cacheSize int, logger *zap.Logger) *securityEngine {
cache, err := simplelru.NewLRU(cacheSize, nil)
if err != nil {
panic(err)
}
return &securityEngine{cache: cache, logger: logger}
}
var openAccess = &ResolvedMetricsViewSecurity{
Access: true,
RowFilter: "",
Include: nil,
Exclude: nil,
}
type ResolvedMetricsViewSecurity struct {
Access bool
RowFilter string
Include []string
Exclude []string
ExcludeAll bool
}
func computeCacheKey(instanceID string, mv *runtimev1.MetricsViewSpec, lastUpdatedOn time.Time, attributes map[string]any) (string, error) {
hash := md5.New()
_, err := hash.Write([]byte(instanceID))
if err != nil {
return "", err
}
_, err = hash.Write([]byte(mv.Table))
if err != nil {
return "", err
}
_, err = hash.Write([]byte(lastUpdatedOn.String()))
if err != nil {
return "", err
}
// go through attributes in a deterministic order (alphabetical by keys)
if attributes["admin"] != nil {
err = binary.Write(hash, binary.BigEndian, attributes["admin"])
if err != nil {
return "", err
}
}
if attributes["email"] != nil {
_, err = hash.Write([]byte(attributes["email"].(string)))
if err != nil {
return "", err
}
}
if attributes["groups"] != nil {
for _, g := range attributes["groups"].([]interface{}) {
_, err = hash.Write([]byte(g.(string)))
if err != nil {
return "", err
}
}
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func (p *securityEngine) resolveMetricsViewSecurity(instanceID, environment string, mv *runtimev1.MetricsViewSpec, lastUpdatedOn time.Time, attributes map[string]any) (*ResolvedMetricsViewSecurity, error) {
if mv.Security == nil {
return nil, nil
}
// if attributes is empty that means auth is disabled and also no user context is available
// since we are controlling the attributes we can safely return the open policy
if len(attributes) == 0 {
return openAccess, nil
}
cacheKey, err := computeCacheKey(instanceID, mv, lastUpdatedOn, attributes)
if err != nil {
return nil, fmt.Errorf("failed to compute cache key: %w", err)
}
p.lock.Lock()
defer p.lock.Unlock()
cached, ok := p.cache.Get(cacheKey)
if ok {
return cached.(*ResolvedMetricsViewSecurity), nil
}
resolved := &ResolvedMetricsViewSecurity{}
templateData := rillv1.TemplateData{
Environment: environment,
User: attributes,
}
if mv.Security.Access != "" {
access, err := rillv1.ResolveTemplate(mv.Security.Access, templateData)
if err != nil {
return nil, err
}
resolved.Access, err = rillv1.EvaluateBoolExpression(access)
if err != nil {
return nil, err
}
}
if mv.Security.RowFilter != "" {
filter, err := rillv1.ResolveTemplate(mv.Security.RowFilter, templateData)
if err != nil {
return nil, err
}
resolved.RowFilter = filter
}
seen := map[string]bool{}
for _, inc := range mv.Security.Include {
cond, err := rillv1.ResolveTemplate(inc.Condition, templateData)
if err != nil {
return nil, err
}
incCond, err := rillv1.EvaluateBoolExpression(cond)
if err != nil {
return nil, err
}
if incCond {
for _, name := range inc.Names {
if seen[name] {
continue
}
seen[name] = true
resolved.Include = append(resolved.Include, name)
}
}
}
// this is to handle the case where include filter was present but none of them evaluted to true
if len(mv.Security.Include) > 0 && len(resolved.Include) == 0 {
resolved.ExcludeAll = true
}
for _, exc := range mv.Security.Exclude {
cond, err := rillv1.ResolveTemplate(exc.Condition, templateData)
if err != nil {
return nil, err
}
excCond, err := rillv1.EvaluateBoolExpression(cond)
if err != nil {
return nil, err
}
if excCond {
for _, name := range exc.Names {
if seen[name] {
continue
}
seen[name] = true
resolved.Exclude = append(resolved.Exclude, name)
}
}
}
p.cache.Add(cacheKey, resolved)
return resolved, nil
}