Skip to content

Commit a18977c

Browse files
committed
allow app-id as context for secret decryption
1 parent f03e6bf commit a18977c

38 files changed

+124
-82
lines changed

cmd/community/loadapp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func LoadApp(cmd *cobra.Command, args []string) error {
4343
})
4444

4545
applet := runtime.Applet{}
46-
err = applet.LoadWithInitializers(script, src, nil, initializers...)
46+
err = applet.LoadWithInitializers("", script, src, nil, initializers...)
4747
if err != nil {
4848
return fmt.Errorf("failed to load applet: %w", err)
4949
}

cmd/community/validateicons.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func ValidateIcons(cmd *cobra.Command, args []string) error {
4747
})
4848

4949
applet := runtime.Applet{}
50-
err = applet.LoadWithInitializers(args[0], src, nil, initializers...)
50+
err = applet.LoadWithInitializers("", args[0], src, nil, initializers...)
5151
if err != nil {
5252
return fmt.Errorf("failed to load applet: %w", err)
5353
}

cmd/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func ProfileApp(script string, config map[string]string) (*pprof_profile.Profile
114114
})
115115

116116
applet := runtime.Applet{}
117-
err = applet.LoadWithInitializers(script, src, nil, initializers...)
117+
err = applet.LoadWithInitializers("", script, src, nil, initializers...)
118118
if err != nil {
119119
return nil, fmt.Errorf("failed to load applet: %w", err)
120120
}

cmd/render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func render(cmd *cobra.Command, args []string) error {
139139
runtime.InitCache(cache)
140140

141141
applet := runtime.Applet{}
142-
err = applet.LoadWithInitializers(script, src, nil, initializers...)
142+
err = applet.LoadWithInitializers("", script, src, nil, initializers...)
143143
if err != nil {
144144
return fmt.Errorf("failed to load applet: %w", err)
145145
}

docs/gen_widget_imgs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def main():
6868
`, snippet)
6969

7070
app := runtime.Applet{}
71-
err = app.Load(name, []byte(src), nil)
71+
err = app.Load(fmt.Sprintf("id-%s", name), name, []byte(src), nil)
7272
if err != nil {
7373
panic(err)
7474
}

encode/encode_bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def main(config):
7272

7373
func BenchmarkRunAndRender(b *testing.B) {
7474
app := &runtime.Applet{}
75-
err := app.Load("benchmark.star", []byte(BenchmarkDotStar), nil)
75+
err := app.Load("benchid", "benchmark.star", []byte(BenchmarkDotStar), nil)
7676
if err != nil {
7777
b.Error(err)
7878
}

encode/encode_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def main():
145145

146146
func TestFile(t *testing.T) {
147147
app := runtime.Applet{}
148-
err := app.Load("test.star", []byte(TestDotStar), nil)
148+
err := app.Load("testid", "test.star", []byte(TestDotStar), nil)
149149
assert.NoError(t, err)
150150

151151
roots, err := app.Run(map[string]string{})
@@ -158,7 +158,7 @@ func TestFile(t *testing.T) {
158158

159159
func TestHash(t *testing.T) {
160160
app := runtime.Applet{}
161-
err := app.Load("test.star", []byte(TestDotStar), nil)
161+
err := app.Load("testid", "test.star", []byte(TestDotStar), nil)
162162
require.NoError(t, err)
163163

164164
roots, err := app.Run(map[string]string{})
@@ -179,7 +179,7 @@ func TestHash(t *testing.T) {
179179
// change the app slightly
180180
modifiedSource := strings.Replace(TestDotStar, "foo bar", "bar foo", 1)
181181
app2 := runtime.Applet{}
182-
err = app2.Load("test.star", []byte(modifiedSource), nil)
182+
err = app2.Load("testid2", "test.star", []byte(modifiedSource), nil)
183183
require.NoError(t, err)
184184

185185
roots2, err := app2.Run(map[string]string{})
@@ -238,7 +238,7 @@ def main():
238238
return render.Root(show_full_animation=True, child=render.Box())
239239
`
240240
app := runtime.Applet{}
241-
require.NoError(t, app.Load("test.star", []byte(requestFull), nil))
241+
require.NoError(t, app.Load("testid", "test.star", []byte(requestFull), nil))
242242
roots, err := app.Run(map[string]string{})
243243
assert.NoError(t, err)
244244
assert.True(t, ScreensFromRoots(roots).ShowFullAnimation)
@@ -249,7 +249,7 @@ def main():
249249
return render.Root(child=render.Box())
250250
`
251251
app = runtime.Applet{}
252-
require.NoError(t, app.Load("test.star", []byte(dontRequestFull), nil))
252+
require.NoError(t, app.Load("testid", "test.star", []byte(dontRequestFull), nil))
253253
roots, err = app.Run(map[string]string{})
254254
assert.NoError(t, err)
255255
assert.False(t, ScreensFromRoots(roots).ShowFullAnimation)
@@ -276,7 +276,7 @@ def main():
276276
`)
277277

278278
app := runtime.Applet{}
279-
err := app.Load("test.star", src, nil)
279+
err := app.Load("testid", "test.star", src, nil)
280280
assert.NoError(t, err)
281281

282282
roots, err := app.Run(map[string]string{})

runtime/applet.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func init() {
5353

5454
type Applet struct {
5555
Filename string
56-
Id string
56+
ThreadID string
57+
AppID string
5758
Globals starlark.StringDict
5859
SecretDecryptionKey *SecretDecryptionKey
5960

@@ -69,7 +70,7 @@ type Applet struct {
6970

7071
func (a *Applet) thread(initializers ...ThreadInitializer) *starlark.Thread {
7172
t := &starlark.Thread{
72-
Name: a.Id,
73+
Name: a.ThreadID,
7374
Load: a.loadModule,
7475
Print: func(thread *starlark.Thread, msg string) {
7576
fmt.Printf("[%s] %s\n", a.Filename, msg)
@@ -92,23 +93,24 @@ func (a *Applet) thread(initializers ...ThreadInitializer) *starlark.Thread {
9293
// Loads an applet. The script filename is used as a descriptor only,
9394
// and the actual code should be passed in src. Optionally also pass
9495
// loader to make additional starlark modules available to the script.
95-
func (a *Applet) Load(filename string, src []byte, loader ModuleLoader) (err error) {
96-
return a.LoadWithInitializers(filename, src, loader)
96+
func (a *Applet) Load(appID string, filename string, src []byte, loader ModuleLoader) (err error) {
97+
return a.LoadWithInitializers(appID, filename, src, loader)
9798
}
9899

99-
func (a *Applet) LoadWithInitializers(filename string, src []byte, loader ModuleLoader, initializers ...ThreadInitializer) (err error) {
100+
func (a *Applet) LoadWithInitializers(appID string, filename string, src []byte, loader ModuleLoader, initializers ...ThreadInitializer) (err error) {
100101
defer func() {
101102
if r := recover(); r != nil {
102103
err = fmt.Errorf("panic while executing %s: %v", a.Filename, r)
103104
}
104105
}()
105106

107+
a.AppID = appID
106108
a.Filename = filename
107109
a.loader = loader
108110

109111
a.src = src
110112

111-
a.Id = fmt.Sprintf("%s/%x", filename, md5.Sum(src))
113+
a.ThreadID = fmt.Sprintf("%s/%x", filename, md5.Sum(src))
112114

113115
if a.SecretDecryptionKey != nil {
114116
a.decrypter, err = a.SecretDecryptionKey.decrypterForApp(a)

runtime/applet_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414

1515
func TestLoadEmptySrc(t *testing.T) {
1616
app := &Applet{}
17-
err := app.Load("test.star", []byte{}, nil)
17+
err := app.Load("testid", "test.star", []byte{}, nil)
1818
assert.Error(t, err)
1919
}
2020

2121
func TestLoadMalformed(t *testing.T) {
2222
src := "this is not valid starlark"
2323
app := &Applet{}
24-
err := app.Load("test.star", []byte(src), nil)
24+
err := app.Load("testid", "test.star", []byte(src), nil)
2525
assert.Error(t, err)
2626
}
2727

@@ -33,7 +33,7 @@ def main():
3333
return render.Root(child=render.Box())
3434
`
3535
app := &Applet{}
36-
err := app.Load("test.star", []byte(src), nil)
36+
err := app.Load("testid", "test.star", []byte(src), nil)
3737
assert.NoError(t, err)
3838

3939
// As is this
@@ -45,7 +45,7 @@ def main2():
4545
main = main2
4646
`
4747
app = &Applet{}
48-
err = app.Load("test.star", []byte(src), nil)
48+
err = app.Load("testid", "test.star", []byte(src), nil)
4949
assert.NoError(t, err)
5050

5151
// And this (a lambda is a function)
@@ -57,7 +57,7 @@ def main2():
5757
main = lambda: main2()
5858
`
5959
app = &Applet{}
60-
err = app.Load("test.star", []byte(src), nil)
60+
err = app.Load("testid", "test.star", []byte(src), nil)
6161
assert.NoError(t, err)
6262

6363
// But not this, because a string is not a function
@@ -69,7 +69,7 @@ def main2():
6969
main = "main2"
7070
`
7171
app = &Applet{}
72-
err = app.Load("test.star", []byte(src), nil)
72+
err = app.Load("testid", "test.star", []byte(src), nil)
7373
assert.Error(t, err)
7474

7575
// And not this either, because here main is gone
@@ -79,7 +79,7 @@ def main2():
7979
return render.Root(child=render.Box())
8080
`
8181
app = &Applet{}
82-
err = app.Load("test.star", []byte(src), nil)
82+
err = app.Load("testid", "test.star", []byte(src), nil)
8383
assert.Error(t, err)
8484

8585
}
@@ -92,7 +92,7 @@ def main():
9292
return [render.Box()]
9393
`
9494
app := &Applet{}
95-
err := app.Load("test.star", []byte(src), nil)
95+
err := app.Load("testid", "test.star", []byte(src), nil)
9696
assert.NoError(t, err)
9797
screens, err := app.Run(map[string]string{})
9898
assert.Error(t, err)
@@ -106,7 +106,7 @@ def main():
106106
`
107107

108108
app = &Applet{}
109-
err = app.Load("test.star", []byte(src), nil)
109+
err = app.Load("testid", "test.star", []byte(src), nil)
110110
assert.NoError(t, err)
111111
screens, err = app.Run(map[string]string{})
112112
assert.NoError(t, err)
@@ -119,7 +119,7 @@ def main():
119119
return [render.Root(child=render.Box()), render.Root(child=render.Text("hi"))]
120120
`
121121
app = &Applet{}
122-
err = app.Load("test.star", []byte(src), nil)
122+
err = app.Load("testid", "test.star", []byte(src), nil)
123123
assert.NoError(t, err)
124124
screens, err = app.Run(map[string]string{})
125125
assert.NoError(t, err)
@@ -141,7 +141,7 @@ def main():
141141
return render.Root(child=render.Box())
142142
`
143143
app := &Applet{}
144-
err := app.Load("test.star", []byte(src), nil)
144+
err := app.Load("testid", "test.star", []byte(src), nil)
145145
assert.NoError(t, err)
146146
roots, err := app.Run(config)
147147
assert.NoError(t, err)
@@ -170,7 +170,7 @@ def main(config):
170170
return [render.Root(child=render.Box()) for _ in range(int(config["one"]) + int(config["two"]))]
171171
`
172172
app = &Applet{}
173-
err = app.Load("test.star", []byte(src), nil)
173+
err = app.Load("testid", "test.star", []byte(src), nil)
174174
require.NoError(t, err)
175175
roots, err = app.Run(config)
176176
require.NoError(t, err)
@@ -208,7 +208,7 @@ def main():
208208
return render.Root(child=render.Box())
209209
`
210210
app := &Applet{}
211-
err := app.Load("test.star", []byte(src), nil)
211+
err := app.Load("testid", "test.star", []byte(src), nil)
212212
assert.NoError(t, err)
213213
roots, err := app.Run(map[string]string{})
214214
assert.NoError(t, err)
@@ -230,7 +230,7 @@ def main():
230230
return render.Root(child=render.Box())
231231
`
232232
app = &Applet{}
233-
err = app.Load("test.star", []byte(src), loader)
233+
err = app.Load("testid", "test.star", []byte(src), loader)
234234
assert.NoError(t, err)
235235
roots, err = app.Run(map[string]string{})
236236
assert.NoError(t, err)
@@ -255,7 +255,7 @@ def main():
255255
}
256256

257257
app := &Applet{}
258-
err := app.Load("test.star", []byte(src), nil)
258+
err := app.Load("testid", "test.star", []byte(src), nil)
259259
assert.NoError(t, err)
260260
_, err = app.Run(map[string]string{}, initializer)
261261
assert.NoError(t, err)
@@ -275,7 +275,7 @@ def main():
275275
`
276276

277277
app := &Applet{}
278-
err := app.Load("test.star", []byte(src), nil)
278+
err := app.Load("testid", "test.star", []byte(src), nil)
279279
assert.NoError(t, err)
280280
_, err = app.Run(map[string]string{})
281281
assert.NoError(t, err)
@@ -323,7 +323,7 @@ def main(config):
323323
`
324324

325325
app := &Applet{}
326-
err = app.Load("test.star", []byte(src), nil)
326+
err = app.Load("testid", "test.star", []byte(src), nil)
327327
_, err = app.Run(map[string]string{"ZIP_BYTES": buf.String()}, initializer)
328328
assert.NoError(t, err)
329329

runtime/cache_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main():
3333
`
3434
InitCache(NewInMemoryCache())
3535
app := &Applet{}
36-
err := app.Load("test.star", []byte(src), nil)
36+
err := app.Load("testid", "test.star", []byte(src), nil)
3737
assert.NoError(t, err)
3838
roots, err := app.Run(map[string]string{})
3939
assert.NoError(t, err)
@@ -54,7 +54,7 @@ def main():
5454
`
5555
InitCache(NewInMemoryCache())
5656
app := &Applet{}
57-
err := app.Load("test.star", []byte(src), nil)
57+
err := app.Load("testid", "test.star", []byte(src), nil)
5858
assert.NoError(t, err)
5959

6060
// first time, i == 1
@@ -72,7 +72,7 @@ def main():
7272
// but run the same code using different filename, and cached
7373
// data ends up in a different namespace
7474
app = &Applet{}
75-
err = app.Load("test2.star", []byte(src), nil)
75+
err = app.Load("testid", "test2.star", []byte(src), nil)
7676
assert.NoError(t, err)
7777

7878
roots, _ = app.Run(map[string]string{})
@@ -103,7 +103,7 @@ def main():
103103
`
104104
InitCache(nil)
105105
app := &Applet{}
106-
err := app.Load("test.star", []byte(src), nil)
106+
err := app.Load("testid", "test.star", []byte(src), nil)
107107
assert.NoError(t, err)
108108
screens, err := app.Run(map[string]string{})
109109
assert.NoError(t, err)
@@ -121,7 +121,7 @@ def main():
121121
`
122122
InitCache(nil)
123123
app := &Applet{}
124-
err := app.Load("test.star", []byte(src), nil)
124+
err := app.Load("testid", "test.star", []byte(src), nil)
125125
assert.NoError(t, err)
126126
screens, err := app.Run(map[string]string{})
127127
assert.Error(t, err)

runtime/httpcache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestInitHTTP(t *testing.T) {
1919
assert.NoError(t, err)
2020

2121
app := &Applet{}
22-
err = app.Load("httpcache.star", b, nil)
22+
err = app.Load("httpid", "httpcache.star", b, nil)
2323
assert.NoError(t, err)
2424

2525
screens, err := app.Run(map[string]string{})

runtime/modules/hmac/hmac_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def main():
2626

2727
func TestHmac(t *testing.T) {
2828
app := &runtime.Applet{}
29-
err := app.Load("hmac_test.star", []byte(hmacSource), nil)
29+
err := app.Load("hmacid", "hmac_test.star", []byte(hmacSource), nil)
3030
assert.NoError(t, err)
3131

3232
screens, err := app.Run(map[string]string{})

runtime/modules/humanize/humanize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def main():
8080

8181
func TestHumanize(t *testing.T) {
8282
app := &runtime.Applet{}
83-
err := app.Load("human.star", []byte(humanSource), nil)
83+
err := app.Load("humanid", "human.star", []byte(humanSource), nil)
8484
assert.NoError(t, err)
8585

8686
screens, err := app.Run(map[string]string{})

runtime/modules/qrcode/qrcode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def main():
2929

3030
func TestQRCode(t *testing.T) {
3131
app := &runtime.Applet{}
32-
err := app.Load("test.star", []byte(qrCodeSource), nil)
32+
err := app.Load("testid", "test.star", []byte(qrCodeSource), nil)
3333
assert.NoError(t, err)
3434

3535
screens, err := app.Run(map[string]string{})

runtime/modules/random/random_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def main():
4848

4949
func TestRandom(t *testing.T) {
5050
app := &runtime.Applet{}
51-
err := app.Load("random_test.star", []byte(randomSrc), nil)
51+
err := app.Load("randomid", "random_test.star", []byte(randomSrc), nil)
5252
require.NoError(t, err)
5353

5454
screens, err := app.Run(map[string]string{})

0 commit comments

Comments
 (0)