Repository navigation
Expand file tree
/
Copy pathctor.go
More file actions
566 lines (498 loc) · 20 KB
/
Copy pathctor.go
File metadata and controls
566 lines (498 loc) · 20 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package starbox
import (
"fmt"
"io/fs"
"net/http"
"strings"
"sync"
"time"
"github.com/1set/starlet"
"github.com/1set/starlet/dataconv"
libhttp "github.com/1set/starlet/lib/http"
"go.starlark.net/starlark"
"go.uber.org/zap"
)
// DoNotCompare prevents == and != comparisons on the containing struct.
type DoNotCompare [0]func()
// StarlarkFunc is a function that can be called from Starlark.
type StarlarkFunc func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)
// FuncMap is a map of Starlark functions.
type FuncMap map[string]StarlarkFunc
// DynamicModuleLoader is a function type that takes a module name as input and returns a corresponding module loader.
// It is invoked before execution to dynamically load modules as needed, and serves as a complement to Starlet's built-in modules and custom-added modules.
// For given module names, if the module is not a built-in module or a custom-added module, this function is called to look it up.
// If the module is not found or fails to initialize, an error is returned.
// For non-existent modules, it should return (nil, nil) or (nil, error).
type DynamicModuleLoader func(string) (starlet.ModuleLoader, error)
// Starbox is a wrapper of starlet.Machine with additional features.
type Starbox struct {
_ DoNotCompare
mac *starlet.Machine
mu sync.RWMutex
hasExec bool
execTimes uint
name string
structTag string
printFunc starlet.PrintFunc
globals starlet.StringAnyMap
modSet ModuleSetName
namedMods []string
loadMods starlet.ModuleLoaderMap
modMembers map[string][]string
scriptMods map[string]string
modFS fs.FS // host-provided filesystem configuration
scriptFS fs.FS // filesystem prepared for the current machine
modNames []string
dynMods DynamicModuleLoader
userLog *zap.SugaredLogger
result starlark.Value
resultSet bool
maxOutputEntries uint
maxSteps uint64
scriptCache starlet.ByteCache
scriptCacheSet bool
policy *Policy
console *Console
}
// New creates a new Starbox instance with default settings.
func New(name string) *Starbox {
return &Starbox{mac: newStarMachine(name), name: name}
}
// NewWithPolicy creates a Starbox whose loadable modules are constrained by a
// host-side, default-deny Policy (the A4 load gate). The policy is deep-copied
// in (the caller cannot mutate grants afterwards) and only ever TIGHTENS what
// SetModuleSet/AddNamedModules/custom/dynamic modules would otherwise load — a
// module loads iff it is both requested AND permitted by the policy. Modules
// the policy withholds raise a ModuleWithheldError (for builtins) or are simply
// absent (custom/dynamic). New (no policy) behaviour is unchanged.
func NewWithPolicy(name string, p Policy) *Starbox {
s := New(name)
cp := p.clone()
s.policy = &cp
return s
}
func newStarMachine(name string) *starlet.Machine {
m := starlet.NewDefault()
m.EnableGlobalReassign()
m.SetScriptCacheEnabled(true)
// m.SetInputConversionEnabled(false)
// m.SetOutputConversionEnabled(true)
m.SetPrintFunc(func(thread *starlark.Thread, msg string) {
prefix := fmt.Sprintf("[⭐|%s](%s)", name, time.Now().UTC().Format(`15:04:05.000`))
eprintln(prefix, msg)
})
return m
}
// deniedAfterExec reports whether a configuration change must be rejected
// because the Box has already executed. It logs the misuse through the package
// logger (which panics under a development logger, per the setters' contract)
// and returns true so the caller bails out WITHOUT applying the change. The
// return is what enforces the contract fail-closed: the default logger is a
// no-op zap logger whose DPanic neither panics nor logs, so before this the
// guard was silent and the change landed anyway. The caller holds s.mu.
func (s *Starbox) deniedAfterExec(action string) bool {
if s.hasExec {
log.DPanic("cannot " + action + " after execution")
return true
}
return false
}
// String returns the name of the Starbox instance.
func (s *Starbox) String() string {
return fmt.Sprintf("🥡Box{name:%s,run:%d}", s.name, s.execTimes)
}
// Reset replaces the underlying Starlet machine with a fresh one while keeping
// the Box's configuration (name, globals, module set, script modules, policy,
// limits, ...), so the same Box can be run again from a clean per-run state.
// It clears the captured result and loaded-module list, and regenerates script
// modules on the next run. Undrained console output and the lifetime run count
// are retained.
//
// Reset is for SERIAL reuse of a single Box. There is deliberately no Clone and
// no Box pool: per-run state (globals injected during a run, the Starlark step
// counter) lives on the machine, so sharing or hand-pooling a Box would leak
// that state between runs. For a hot path or concurrent workload, construct a
// fresh New(...) per run and share only the compiled-program cache across them
// via SetScriptCache - that keeps compilation shared while keeping per-run state
// isolated.
func (s *Starbox) Reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.mac = newStarMachine(s.name)
s.hasExec = false
s.scriptFS = nil
s.modNames = nil
s.result = nil
s.resultSet = false
// Re-apply the Box-level limits/cache to the fresh machine eagerly, not
// lazily at the next run: newStarMachine defaults to no step budget and an
// enabled cache, so without this a caller that reaches for the raw machine
// via GetMachine right after Reset would see the guard dropped. This keeps
// Reset's "keeps the Box's limits" contract true for the machine itself.
s.mac.SetMaxExecutionSteps(s.maxSteps)
s.applyScriptCache()
}
// GetMachine returns the underlying starlet.Machine instance.
func (s *Starbox) GetMachine() *starlet.Machine {
s.mu.RLock()
defer s.mu.RUnlock()
return s.mac
}
// GetSteps returns the computation steps executed by the underlying Starlark thread.
func (s *Starbox) GetSteps() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
if m := s.mac; m != nil {
if t := m.GetStarlarkThread(); t != nil {
return t.Steps
}
}
return 0
}
// SetMaxExecutionSteps sets the per-run budget of Starlark computation steps;
// 0 (the default) means unlimited. When a run exceeds the budget, Run/Call fail
// with a starlet.MaxStepsExceededError reachable via errors.As — the standard
// guard against a runaway loop that a wall-clock timeout cannot stop. The step
// counter resets at the start of every run. Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) SetMaxExecutionSteps(steps uint64) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set max execution steps") {
return
}
// Store on the Box, not only on the current machine: Reset() swaps in a
// fresh machine (which defaults to unlimited), so a machine-only budget
// vanished after the first run — dropping the CPU-DoS guard on exactly the
// serial-reuse path Reset exists for. Reset re-applies s.maxSteps to the
// fresh machine, the same way maxOutputEntries survives Reset.
s.maxSteps = steps
s.mac.SetMaxExecutionSteps(steps)
}
// SetMaxOutputEntries sets the maximum number of top-level entries a run's
// result may contain; 0 (the default) means unlimited. A run that produces more
// is aborted with an OutputLimitExceededError (reachable via errors.As) and its
// result is withheld. This is a post-hoc policy gate on result size, not a
// memory guard - use SetMaxExecutionSteps to bound resource use. Calling it
// after execution is rejected: the change is ignored, and it panics under a
// development logger.
func (s *Starbox) SetMaxOutputEntries(n uint) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set max output entries") {
return
}
s.maxOutputEntries = n
}
// GetModuleNames returns the names of the modules loaded after execution.
func (s *Starbox) GetModuleNames() []string {
s.mu.RLock()
defer s.mu.RUnlock()
return s.modNames
}
// SetLogger sets the logger for user-defined log output.
func (s *Starbox) SetLogger(sl *zap.SugaredLogger) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set logger") {
return
}
s.userLog = sl
}
// SetStructTag sets the custom tag of Go struct fields for Starlark.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) SetStructTag(tag string) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set tag") {
return
}
s.structTag = tag
}
// SetPrintFunc sets the print function for Starlark.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) SetPrintFunc(printFunc starlet.PrintFunc) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set print function") {
return
}
s.printFunc = printFunc
}
// SetFS sets the virtual filesystem for module scripts.
// If it's not nil, it'll override all the scripts added by AddModuleScript().
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) SetFS(hfs fs.FS) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set filesystem") {
return
}
s.modFS = hfs
}
// SetScriptCache sets a custom cache provider for compiled script content; a
// nil provider disables the script cache. Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
//
// Hot-path / concurrency recommendation: a single starlet.MemoryCache
// (NewMemoryCache, which is safe for concurrent use) shared across many per-run
// New(...) boxes lets each distinct script compile once and be reused, while
// every Box keeps its own isolated per-run state. Prefer this over reusing or
// pooling a single Box (see Reset).
func (s *Starbox) SetScriptCache(cache starlet.ByteCache) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set script cache") {
return
}
// Store on the Box too, so the choice survives Reset: newStarMachine always
// enables the cache, so a machine-only SetScriptCache(nil) was re-enabled
// after Reset. Reset re-applies the stored choice to the fresh machine.
s.scriptCache = cache
s.scriptCacheSet = true
s.applyScriptCache()
}
// applyScriptCache applies the Box's script-cache choice to the current
// machine. The caller holds s.mu.
func (s *Starbox) applyScriptCache() {
if !s.scriptCacheSet {
return
}
if s.scriptCache == nil {
s.mac.SetScriptCacheEnabled(false)
} else {
s.mac.SetScriptCache(s.scriptCache)
}
}
// SetDynamicModuleLoader sets the dynamic module loader for preload and lazyload modules.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) SetDynamicModuleLoader(loader DynamicModuleLoader) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set dynamic module loader") {
return
}
s.dynMods = loader
}
// SetModuleSet sets the module set to be loaded before execution.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) SetModuleSet(modSet ModuleSetName) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("set module set") {
return
}
s.modSet = modSet
}
// AddKeyValue adds a key-value pair to the global environment before execution.
// If the key already exists, it will be overwritten.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddKeyValue(key string, value interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add key-value pair") {
return
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
s.globals[key] = value
}
// AddKeyStarlarkValue adds a key-value pair to the global environment before execution, the value is a Starlark value.
// If the key already exists, it will be overwritten.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddKeyStarlarkValue(key string, value starlark.Value) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add key-value pair") {
return
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
s.globals[key] = value
}
// AddKeyValues adds key-value pairs to the global environment before execution. Usually for output of Run()*.
// For each key-value pair, if the key already exists, it will be overwritten.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddKeyValues(keyValues starlet.StringAnyMap) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add key-value pairs") {
return
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
s.globals.Merge(keyValues)
}
// AddStarlarkValues adds key-value pairs to the global environment before execution, the values are already converted to Starlark values.
// For each key-value pair, if the key already exists, it will be overwritten.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddStarlarkValues(keyValues starlark.StringDict) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add key-value pairs") {
return
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
for key, value := range keyValues {
s.globals[key] = value
}
}
// AddBuiltin adds a builtin function with name to the global environment before execution.
// If the name already exists, it will be overwritten.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddBuiltin(name string, starFunc StarlarkFunc) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add builtin") {
return
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
sb := starlark.NewBuiltin(name, starFunc)
s.globals[name] = sb
}
// AddNamedModules adds builtin and custom modules by name to the preload and lazyload registry.
// It will not load the modules until the first run.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddNamedModules(moduleNames ...string) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add named modules") {
return
}
s.namedMods = append(s.namedMods, moduleNames...)
}
// AddModulesByName is an alias of AddNamedModules().
func (s *Starbox) AddModulesByName(moduleNames ...string) {
s.AddNamedModules(moduleNames...)
}
// AddModuleLoader adds a custom module loader to the preload and lazyload registry.
// It will not load the module until the first run, and load result can be accessed in script via load("module_name", "key1") or key1 directly.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddModuleLoader(moduleName string, moduleLoader starlet.ModuleLoader) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add module loader") {
return
}
if s.loadMods == nil {
s.loadMods = make(map[string]starlet.ModuleLoader)
}
s.loadMods[moduleName] = moduleLoader
}
// AddModuleFunctions adds a module with the given module functions along with a module loader, and adds it to the preload and lazyload registry.
// The given module function can be accessed in script via load("module_name", "func1") or module_name.func1.
// It works like AddModuleData() but allows only functions as values.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddModuleFunctions(name string, funcs FuncMap) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add module function") {
return
}
if s.loadMods == nil {
s.loadMods = make(map[string]starlet.ModuleLoader)
}
sfd := starlark.StringDict{}
for fn, fv := range funcs {
sfd[fn] = starlark.NewBuiltin(name+"."+fn, fv)
}
s.loadMods[name] = dataconv.WrapModuleData(name, sfd)
s.recordModMembers(name, sfd)
}
// AddModuleData creates a module for the given module data along with a module loader, and adds it to the preload and lazyload registry.
// The given module data can be accessed in script via load("module_name", "key1") or module_name.key1.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddModuleData(moduleName string, moduleData starlark.StringDict) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add module data") {
return
}
if s.loadMods == nil {
s.loadMods = make(map[string]starlet.ModuleLoader)
}
s.loadMods[moduleName] = dataconv.WrapModuleData(moduleName, moduleData)
s.recordModMembers(moduleName, moduleData)
}
// AddStructFunctions adds a module with the given struct functions along with a module loader, and adds it to the preload and lazyload registry.
// The given struct function can be accessed in script via load("struct_name", "func1") or struct_name.func1.
// It works like AddStructData() but allows only functions as values.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddStructFunctions(name string, funcs FuncMap) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add struct function") {
return
}
if s.loadMods == nil {
s.loadMods = make(map[string]starlet.ModuleLoader)
}
sfd := starlark.StringDict{}
for fn, fv := range funcs {
sfd[fn] = starlark.NewBuiltin(name+"."+fn, fv)
}
s.loadMods[name] = dataconv.WrapStructData(name, sfd)
s.recordModMembers(name, sfd)
}
// AddStructData creates a module for the given struct data along with a module loader, and adds it to the preload and lazyload registry.
// The given struct data can be accessed in script via load("struct_name", "key1") or struct_name.key1.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddStructData(structName string, structData starlark.StringDict) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add struct data") {
return
}
if s.loadMods == nil {
s.loadMods = make(map[string]starlet.ModuleLoader)
}
s.loadMods[structName] = dataconv.WrapStructData(structName, structData)
s.recordModMembers(structName, structData)
}
// AddModuleScript creates a module with given module script in virtual filesystem, and adds it to the preload and lazyload registry.
// The given module script can be accessed in script via load("module_name", "key1") or load("module_name.star", "key1") if module name has no ".star" suffix.
// All the module scripts added by this method would be overridden by SetFS() if it's not nil.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddModuleScript(moduleName, moduleScript string) {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add module script") {
return
}
if s.scriptMods == nil {
s.scriptMods = make(map[string]string)
}
name := strings.TrimSpace(moduleName)
if !strings.HasSuffix(name, ".star") {
name += ".star"
}
s.scriptMods[name] = moduleScript
}
// AddHTTPContext adds HTTP request and response data wrapper to the global environment before execution.
// It takes an HTTP request and returns the response data wrapper for setting response headers and body.
// Calling it after execution is rejected: the change is ignored, and it panics under a development logger.
func (s *Starbox) AddHTTPContext(req *http.Request) *libhttp.ServerResponse {
s.mu.Lock()
defer s.mu.Unlock()
if s.deniedAfterExec("add HTTP context") {
return nil
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
// add request to globals
if sr := libhttp.ConvertServerRequest(req); sr != nil {
s.globals["request"] = sr
} else {
s.globals["request"] = starlark.None
}
// add response to globals
resp := libhttp.NewServerResponse()
s.globals["response"] = resp.Struct()
return resp
}