Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
71 lines (58 loc) · 1.69 KB
/
Copy pathexample_test.go
File metadata and controls
71 lines (58 loc) · 1.69 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
package cmd_test
import (
"fmt"
"log"
"strings"
"github.com/1set/starlet"
"github.com/starpkg/cmd"
"go.starlark.net/starlark"
)
// runScript loads the given cmd module into a starlet machine, runs the script,
// and returns its captured print output plus any run error.
func runScript(module *cmd.Module, script string) (string, error) {
env := starlet.NewDefault()
env.SetScriptContent([]byte(script))
var printOutput strings.Builder
env.SetPrintFunc(func(_ *starlark.Thread, msg string) {
printOutput.WriteString(msg)
printOutput.WriteString("\n")
})
env.SetLazyloadModules(map[string]starlet.ModuleLoader{"cmd": module.LoadModule()})
_, err := env.Run()
return printOutput.String(), err
}
// Example shows the happy path: the host enables the module with an allowlist,
// and a permitted command runs via argv (no shell).
func Example() {
// Enable the module and permit only the "go" tool.
module := cmd.NewModuleWithAllow("go")
script := `
load("cmd", "run")
def main():
result = run("go version")
print("succeeded:", result.success)
print("exit code:", result.exit_code)
main()
`
out, err := runScript(module, script)
if err != nil {
log.Fatalf("Failed to run script: %v", err)
}
fmt.Print(out)
// Output:
// succeeded: True
// exit code: 0
}
// ExampleModule_disabled shows the secure default: a module created with
// NewModule is disabled, so run() refuses until the host opts in.
func ExampleModule_disabled() {
module := cmd.NewModule() // disabled by default
script := `
load("cmd", "run")
run("go version")
`
_, err := runScript(module, script)
fmt.Println("disabled module refuses to run:", err != nil)
// Output:
// disabled module refuses to run: true
}