-
Notifications
You must be signed in to change notification settings - Fork 1
/
executor.go
289 lines (235 loc) · 7.22 KB
/
executor.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package executor
import (
"fmt"
"reflect"
"strings"
"github.com/subsilent/kappa/common"
"github.com/subsilent/kappa/datamodel"
"github.com/subsilent/kappa/skl"
)
func NewSession(ns string, user datamodel.User) Session {
return Session{ns, user}
}
func NewExecutor(session Session, term common.Terminal, sys datamodel.System) *Executor {
return &Executor{session, term, sys}
}
// Session provides session and connection related information
type Session struct {
namespace string
user datamodel.User
}
// Executor executes successfully parsed queries
type Executor struct {
session Session
terminal common.Terminal
system datamodel.System
}
// Execute processes each statement
func (e *Executor) Execute(w *common.ResponseWriter, stmt skl.Statement) {
// Verify session has a user
if e.session.user == nil {
w.Fail(common.InternalServerError, "could not determine session user")
return
}
switch stmt.NodeType() {
case skl.UseNamespaceType:
e.handleUseStatement(w, stmt)
case skl.CreateNamespaceType:
e.handleCreateNamespace(w, stmt)
case skl.ShowNamespaceType:
e.handleShowNamespace(w, stmt)
}
}
func (e *Executor) handleUseStatement(w *common.ResponseWriter, stmt skl.Statement) {
use, ok := stmt.(*skl.UseStatement)
if !ok {
w.Fail(common.InvalidStatementType, "expected *UseStatement, got %s instead", reflect.TypeOf(stmt))
return
}
// Get user from session
user := e.session.user
// Get namespace store
namespaceStore, err := e.system.Namespaces()
if err != nil {
w.Fail(common.InternalServerError, "could not access namespace data")
return
}
// Get namespace
name := use.Namespace()
// Verify namespace existence
_, err = namespaceStore.Get(name)
if err == datamodel.ErrNamespaceDoesNotExist {
w.Fail(common.NamespaceDoesNotExist, name)
return
} else if err != nil {
w.Fail(common.InternalServerError, "could not access namespace data")
return
}
// If the user is an admin, grant access
if user.IsAdmin() {
e.session.namespace = name
e.terminal.SetPrompt(fmt.Sprintf("kappa: %s> ", name))
w.Success(common.OK, "")
return
}
// Verify user has access to the namespace or is admin
// If user has access, update session namespace and terminal
// Otherwise, return access denied error
for _, namespace := range user.Namespaces() {
if namespace == name {
e.session.namespace = name
e.terminal.SetPrompt(fmt.Sprintf("kappa: %s> ", name))
w.Success(common.OK, "")
return
}
}
// Otherwise, the user is not authorized
w.Fail(common.Unauthorized, "")
}
// Only the admin can create root namespaces.
// Admin can also create sub-namespaces for any existing namespace.
// If the user is not the admin, they must have the 'create.namespace'
// permission for the parent namespace.
// Root namespaces don't have any periods.
func (e *Executor) handleCreateNamespace(w *common.ResponseWriter, stmt skl.Statement) {
createStatement, ok := stmt.(*skl.CreateNamespaceStatement)
if !ok {
w.Fail(common.InvalidStatementType, "expected *CreateNamespaceStatement, got %s instead", reflect.TypeOf(stmt))
return
}
// Get namespace store
namespaceStore, err := e.system.Namespaces()
if err != nil {
w.Fail(common.InternalServerError, "could not access namespace data")
return
}
// Get session user
user := e.session.user
// Get namespace
namespace := createStatement.Namespace()
// If err == nil, the namespace already existed
if e.namespaceAlreadyExists(namespace, namespaceStore) {
w.Success(common.NamespaceAlreadyExists, namespace)
return
}
// If root namespace
if createStatement.IsRootNamespace() {
e.handleCreateRootNamespace(w, createStatement, namespaceStore)
return
}
// Admin user is granted access by default
access := user.IsAdmin()
// Get parent namespace
var parent datamodel.Namespace
index := strings.LastIndex(namespace, ".")
parentNamespace := namespace[:index]
// If the user is not an admin check their permissions for the parent namespace
if !access {
// Get user roles for parent namespace
roles := user.Roles(parentNamespace)
// Determine if parent namespace exists
ns, err := namespaceStore.Get(parentNamespace)
if err == datamodel.ErrNamespaceDoesNotExist {
w.Fail(common.NamespaceDoesNotExist, parentNamespace)
return
} else if err != nil {
w.Fail(common.InternalServerError, "")
return
}
// Memoize parent namespace
parent = ns
// Scan roles for permissions
for _, role := range roles {
if ns.HasPermission(role, createStatement.RequiredPermissions()) {
access = true
}
}
// Return error if not authorized
if !access {
w.Fail(common.Unauthorized, "cannot create namespace '%s'", namespace)
return
}
}
// If we've gotten this far, the user has permission to create the namespace
// Get parent namespace
if parent == nil {
// Verify namespace existance
parent, err = namespaceStore.Get(parentNamespace)
if err != nil {
w.Fail(common.InternalServerError, "parent namespace does not exist")
return
}
}
// Create child namespace
if _, err = parent.CreateChild(namespace); err != nil {
w.Fail(common.CreateNamespaceError, "cannot create namespace '%s'", namespace)
return
}
w.Success(common.OK, "namespace created")
}
func (e *Executor) handleShowNamespace(w *common.ResponseWriter, stmt skl.Statement) {
_, ok := stmt.(*skl.ShowNamespacesStatement)
if !ok {
w.Fail(common.InvalidStatementType, "expected *ShowNamespacesStatement, got %s instead", reflect.TypeOf(stmt))
return
}
// Get session user
user := e.session.user
if user.IsAdmin() {
// Get namespace store
namespaceStore, err := e.system.Namespaces()
if err != nil {
w.Fail(common.InternalServerError, "could not access namespace data")
return
}
// Stream namespaces
w.Write(w.Colors.LightYellow)
namespaces := namespaceStore.Stream()
for name := range namespaces {
w.Write([]byte(" " + name + "\r\n"))
}
w.Write(w.Colors.Reset)
} else {
// List namespaces
w.Write(w.Colors.Yellow)
for _, name := range user.Namespaces() {
w.Write([]byte(" " + name + "\r\n"))
}
w.Write(w.Colors.Reset)
}
w.Success(common.OK, "")
}
// namespaceAlreadyExists determines if a namespace already exists...
func (e *Executor) namespaceAlreadyExists(namespace string, store datamodel.NamespaceStore) bool {
_, err := store.Get(namespace)
return err == nil
}
// If the namespace being created is a root namespace, only the admin account can create it
func (e *Executor) handleCreateRootNamespace(w *common.ResponseWriter, stmt *skl.CreateNamespaceStatement, store datamodel.NamespaceStore) {
// Get namespace
name := stmt.Namespace()
// Verify namespace existance
_, err := store.Get(name)
// If err == nil, the namespace already exists
if err == nil {
w.Success(common.NamespaceAlreadyExists, name)
return
}
// Get session user
user := e.session.user
if user.IsAdmin() {
// Create new namespace
_, err := store.Create(name)
// If err !+ nil, namespace could not be created
if err != nil {
w.Fail(common.CreateNamespaceError, "could not create namespace '%s'", name)
return
}
// No error == success
w.Success(common.OK, "namespace created")
return
}
// Otherwise fail creation
w.Fail(common.Unauthorized, "root namespaces can only be created by the admin account")
return
}