-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathevaluator.go
More file actions
194 lines (166 loc) · 4.49 KB
/
Copy pathevaluator.go
File metadata and controls
194 lines (166 loc) · 4.49 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
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
package rbac
import (
"context"
"encoding/json"
"strings"
"github.com/openshift-online/rh-trex-ai/pkg/db"
"gorm.io/gorm"
)
type bindingRow struct {
RoleID string
RoleName string
Scope string
UserID *string
ProjectID *string
AgentID *string
SessionID *string
CredentialID *string
Permissions string
}
type Evaluator struct {
sessionFactory *db.SessionFactory
}
func NewEvaluator(sessionFactory *db.SessionFactory) *Evaluator {
return &Evaluator{sessionFactory: sessionFactory}
}
func (e *Evaluator) fetchBindings(g *gorm.DB, username string) ([]bindingRow, error) {
var rows []bindingRow
err := g.Raw(`
SELECT rb.role_id, r.name AS role_name, rb.scope,
rb.user_id, rb.project_id, rb.agent_id,
rb.session_id, rb.credential_id,
r.permissions
FROM role_bindings rb
JOIN roles r ON r.id = rb.role_id
WHERE rb.user_id = ?
AND rb.deleted_at IS NULL
AND r.deleted_at IS NULL
`, username).Scan(&rows).Error
return rows, err
}
func (e *Evaluator) Evaluate(ctx context.Context, username string, resource Resource, action Action, scope RequestScope) (bool, error) {
g := (*e.sessionFactory).New(ctx)
bindings, err := e.fetchBindings(g, username)
if err != nil {
return false, err
}
if len(bindings) == 0 {
return false, nil
}
requiredPerm := string(resource) + ":" + string(action)
if scope.SessionID != "" && scope.ProjectID == "" {
projectID, lookupErr := e.resolveSessionProject(g, scope.SessionID)
if lookupErr == nil && projectID != "" {
scope.ProjectID = projectID
}
}
for _, b := range bindings {
if !bindingMatchesPermission(b.Permissions, requiredPerm) {
continue
}
if bindingCoversScope(b, scope) {
return true, nil
}
}
return false, nil
}
func (e *Evaluator) AuthorizedProjectIDs(ctx context.Context, username string) (projectIDs []string, isGlobal bool, err error) {
g := (*e.sessionFactory).New(ctx)
bindings, fetchErr := e.fetchBindings(g, username)
if fetchErr != nil {
return nil, false, fetchErr
}
seen := map[string]bool{}
for _, b := range bindings {
if b.Scope == string(ScopeGlobal) {
return nil, true, nil
}
if b.Scope == string(ScopeProject) && b.ProjectID != nil {
if !seen[*b.ProjectID] {
seen[*b.ProjectID] = true
projectIDs = append(projectIDs, *b.ProjectID)
}
}
}
return projectIDs, false, nil
}
func (e *Evaluator) AuthorizedCredentialIDs(ctx context.Context, username string) (credentialIDs []string, isGlobal bool, err error) {
g := (*e.sessionFactory).New(ctx)
bindings, fetchErr := e.fetchBindings(g, username)
if fetchErr != nil {
return nil, false, fetchErr
}
seen := map[string]bool{}
for _, b := range bindings {
if b.Scope == string(ScopeGlobal) {
return nil, true, nil
}
if b.Scope == string(ScopeCredential) && b.CredentialID != nil {
if !seen[*b.CredentialID] {
seen[*b.CredentialID] = true
credentialIDs = append(credentialIDs, *b.CredentialID)
}
}
}
return credentialIDs, false, nil
}
func (e *Evaluator) resolveSessionProject(g *gorm.DB, sessionID string) (string, error) {
var projectID string
err := g.Raw(`SELECT COALESCE(project_id, '') FROM sessions WHERE id = ? AND deleted_at IS NULL`, sessionID).Scan(&projectID).Error
return projectID, err
}
func bindingMatchesPermission(permissionsJSON, required string) bool {
var perms []string
if err := json.Unmarshal([]byte(permissionsJSON), &perms); err != nil {
return false
}
reqParts := strings.SplitN(required, ":", 2)
if len(reqParts) != 2 {
return false
}
reqResource, reqAction := reqParts[0], reqParts[1]
for _, perm := range perms {
if perm == "*:*" {
return true
}
parts := strings.SplitN(perm, ":", 2)
if len(parts) != 2 {
continue
}
r, a := parts[0], parts[1]
resourceMatch := r == "*" || r == reqResource
actionMatch := a == "*" || a == reqAction
if resourceMatch && actionMatch {
return true
}
}
return false
}
func bindingCoversScope(b bindingRow, reqScope RequestScope) bool {
switch ScopeLevel(b.Scope) {
case ScopeGlobal:
return true
case ScopeProject:
if b.ProjectID == nil {
return false
}
return reqScope.ProjectID == *b.ProjectID
case ScopeAgent:
if b.AgentID == nil {
return false
}
return reqScope.AgentID == *b.AgentID
case ScopeSession:
if b.SessionID == nil {
return false
}
return reqScope.SessionID == *b.SessionID
case ScopeCredential:
if b.CredentialID == nil {
return false
}
return reqScope.CredentialID == *b.CredentialID
default:
return false
}
}