-
Notifications
You must be signed in to change notification settings - Fork 82
/
image.go
357 lines (342 loc) · 9.24 KB
/
image.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
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
package sandbox
import (
"context"
"encoding/json"
"os"
"time"
"github.com/sirupsen/logrus"
lua "github.com/yuin/gopher-lua"
"github.com/regclient/regclient"
"github.com/regclient/regclient/cmd/regbot/internal/go2lua"
"github.com/regclient/regclient/types/blob"
"github.com/regclient/regclient/types/manifest"
v1 "github.com/regclient/regclient/types/oci/v1"
"github.com/regclient/regclient/types/ref"
)
type config struct {
m manifest.Manifest
r ref.Ref
conf blob.OCIConfig
}
func setupImage(s *Sandbox) {
s.setupMod(
luaImageName,
map[string]lua.LGFunction{
"config": s.configGet,
"copy": s.imageCopy,
"exportTar": s.imageExportTar,
"importTar": s.imageImportTar,
"manifest": s.manifestGet,
"manifestHead": s.manifestHead,
"manifestList": s.manifestGetList,
"ratelimitWait": s.imageRateLimitWait,
},
map[string]map[string]lua.LGFunction{
"__index": {
// "inspect": s.imageInspect,
},
},
)
s.setupMod(
luaImageConfigName,
map[string]lua.LGFunction{
"__tostring": s.configJSON,
},
map[string]map[string]lua.LGFunction{
"__index": {
"export": s.configExport,
},
},
)
}
func (s *Sandbox) checkConfig(ls *lua.LState, i int) *config {
var c *config
switch ls.Get(i).Type() {
case lua.LTUserData:
ud := ls.CheckUserData(i)
udc, ok := ud.Value.(*config)
if !ok {
ls.ArgError(i, "config expected")
}
c = udc
default:
ls.ArgError(i, "config expected")
}
return c
}
func (s *Sandbox) configGet(ls *lua.LState) int {
err := s.ctx.Err()
if err != nil {
ls.RaiseError("Context error: %v", err)
}
m := s.checkManifest(ls, 1, false, false)
if s.throttleC != nil {
err = s.throttleC.Acquire(s.ctx)
if err != nil {
ls.RaiseError("Failed to acquire throttle: %v", err)
}
defer s.throttleC.Release(s.ctx)
}
s.log.WithFields(logrus.Fields{
"script": s.name,
"image": m.r.CommonName(),
}).Debug("Retrieve image config")
mi, ok := m.m.(manifest.Imager)
if !ok {
ls.RaiseError("Image methods are not available for manifest")
}
confDesc, err := mi.GetConfig()
if err != nil {
ls.RaiseError("Failed looking up \"%s\" config digest: %v", m.r.CommonName(), err)
}
confBlob, err := s.rc.BlobGetOCIConfig(s.ctx, m.r, confDesc)
if err != nil {
ls.RaiseError("Failed retrieving \"%s\" config: %v", m.r.CommonName(), err)
}
ud, err := wrapUserData(ls, &config{conf: confBlob, m: m.m, r: m.r}, confBlob.GetConfig(), luaImageConfigName)
if err != nil {
ls.RaiseError("Failed packaging \"%s\" config: %v", m.r.CommonName(), err)
}
ls.Push(ud)
return 1
}
// configExport recreates a new config object based on any user changes to the lua object
func (s *Sandbox) configExport(ls *lua.LState) int {
err := s.ctx.Err()
if err != nil {
ls.RaiseError("Context error: %v", err)
}
var newC *config
i := 1
switch ls.Get(i).Type() {
case lua.LTUserData:
// unpack existing config from user data
ud := ls.CheckUserData(i)
origC, ok := ud.Value.(*config)
if !ok {
ls.ArgError(i, "config expected")
}
// unwrap extracts lua table that user may have modified
utab, err := unwrapUserData(ls, ud)
if err != nil {
ls.RaiseError("failed exporting config (unwrap): %v", err)
}
// get the original config object, used to set fields that can be extracted from lua table
origOCIConf := origC.conf.GetConfig()
// build a new oci image from the lua table
var ociImage v1.Image
err = go2lua.Import(ls, utab, &ociImage, &origOCIConf)
if err != nil {
ls.RaiseError("Failed exporting config (go2lua): %v", err)
}
// save image to a new config
bc := blob.NewOCIConfig(
blob.WithRef(origC.r),
blob.WithImage(ociImage),
)
newC = &config{
conf: bc,
m: origC.m,
r: origC.r,
}
default:
ls.ArgError(i, "Config expected")
}
// wrap config to send back to lua
ud, err := wrapUserData(ls, newC, newC.conf.GetConfig(), luaImageConfigName)
if err != nil {
ls.RaiseError("Failed packaging config: %v", err)
}
ls.Push(ud)
return 1
}
func (s *Sandbox) configJSON(ls *lua.LState) int {
c := s.checkConfig(ls, 1)
cJSON, err := json.MarshalIndent(c.conf, "", " ")
if err != nil {
ls.RaiseError("Failed outputing config: %v", err)
}
ls.Push(lua.LString(string(cJSON)))
return 1
}
func (s *Sandbox) imageCopy(ls *lua.LState) int {
err := s.ctx.Err()
if err != nil {
ls.RaiseError("Context error: %v", err)
}
src := s.checkReference(ls, 1)
tgt := s.checkReference(ls, 2)
opts := []regclient.ImageOpts{}
lOpts := struct {
DigestTags bool `json:"digestTags"`
ForceRecursive bool `json:"forceRecursive"`
IncludeExternal bool `json:"includeExternal"`
Platforms []string `json:"platforms"`
}{}
if ls.GetTop() == 3 {
err := go2lua.Import(ls, ls.Get(3), &lOpts, lOpts)
if err != nil {
ls.RaiseError("Failed to parse options: %v", err)
}
if lOpts.DigestTags {
opts = append(opts, regclient.ImageWithDigestTags())
}
if lOpts.ForceRecursive {
opts = append(opts, regclient.ImageWithForceRecursive())
}
if lOpts.IncludeExternal {
opts = append(opts, regclient.ImageWithIncludeExternal())
}
if len(lOpts.Platforms) > 0 {
opts = append(opts, regclient.ImageWithPlatforms(lOpts.Platforms))
}
}
if s.throttleC != nil {
err = s.throttleC.Acquire(s.ctx)
if err != nil {
ls.RaiseError("Failed to acquire throttle: %v", err)
}
defer s.throttleC.Release(s.ctx)
}
s.log.WithFields(logrus.Fields{
"script": s.name,
"source": src.r.CommonName(),
"target": tgt.r.CommonName(),
"digestTags": lOpts.DigestTags,
"forceRecursive": lOpts.ForceRecursive,
"includeExternal": lOpts.IncludeExternal,
"dry-run": s.dryRun,
}).Info("Copy image")
if s.dryRun {
return 0
}
err = s.rc.ImageCopy(s.ctx, src.r, tgt.r, opts...)
if err != nil {
ls.RaiseError("Failed copying \"%s\" to \"%s\": %v", src.r.CommonName(), tgt.r.CommonName(), err)
}
err = s.rc.Close(s.ctx, tgt.r)
if err != nil {
ls.RaiseError("Failed closing reference \"%s\": %v", tgt.r.CommonName(), err)
}
return 0
}
func (s *Sandbox) imageExportTar(ls *lua.LState) int {
err := s.ctx.Err()
if err != nil {
ls.RaiseError("Context error: %v", err)
}
src := s.checkReference(ls, 1)
file := ls.CheckString(2)
if s.throttleC != nil {
err = s.throttleC.Acquire(s.ctx)
if err != nil {
ls.RaiseError("Failed to acquire throttle: %v", err)
}
defer s.throttleC.Release(s.ctx)
}
//#nosec G304 command is run by a user accessing their own files
fh, err := os.Create(file)
if err != nil {
ls.RaiseError("Failed to open \"%s\": %v", file, err)
}
err = s.rc.ImageExport(s.ctx, src.r, fh)
if err != nil {
ls.RaiseError("Failed to export image \"%s\" to \"%s\": %v", src.r.CommonName(), file, err)
}
return 0
}
func (s *Sandbox) imageImportTar(ls *lua.LState) int {
err := s.ctx.Err()
if err != nil {
ls.RaiseError("Context error: %v", err)
}
tgt := s.checkReference(ls, 1)
file := ls.CheckString(2)
if s.throttleC != nil {
err = s.throttleC.Acquire(s.ctx)
if err != nil {
ls.RaiseError("Failed to acquire throttle: %v", err)
}
defer s.throttleC.Release(s.ctx)
}
//#nosec G304 command is run by a user accessing their own files
rs, err := os.Open(file)
if err != nil {
ls.RaiseError("Failed to read from \"%s\": %v", file, err)
}
err = s.rc.ImageImport(s.ctx, tgt.r, rs)
if err != nil {
ls.RaiseError("Failed to import image \"%s\" from \"%s\": %v", tgt.r.CommonName(), file, err)
}
return 0
}
func (s *Sandbox) imageRateLimit(ls *lua.LState) int {
err := s.ctx.Err()
if err != nil {
ls.RaiseError("Context error: %v", err)
}
m := s.checkManifest(ls, 1, false, true)
rl := go2lua.Export(ls, manifest.GetRateLimit(m.m))
ls.Push(rl)
return 1
}
// imageRateLimitWait takes a ref, limit, poll freq, timeout, returns a bool for success
func (s *Sandbox) imageRateLimitWait(ls *lua.LState) int {
r := s.checkReference(ls, 1)
limit := ls.CheckInt(2)
top := ls.GetTop()
var freq time.Duration
if top >= 3 {
freqStr := ls.CheckString(3)
freqParsed, err := time.ParseDuration(freqStr)
if err != nil {
ls.RaiseError("Failed parsing rate limit frequency %s: %v", freqStr, err)
return 0
}
freq = freqParsed
} else {
freq, _ = time.ParseDuration("5m")
}
var timeout time.Duration
if top >= 4 {
timeoutStr := ls.CheckString(4)
timeoutParsed, err := time.ParseDuration(timeoutStr)
if err != nil {
ls.RaiseError("Failed parsing timeout %s: %v", timeoutStr, err)
return 0
}
timeout = timeoutParsed
} else {
timeout, _ = time.ParseDuration("6h")
}
ctx, cancel := context.WithTimeout(s.ctx, timeout)
defer cancel()
for {
// check the current manifest head
mh, err := s.rc.ManifestHead(ctx, r.r)
if err != nil {
ls.RaiseError("Failed checking \"%s\" manifest: %v", r.r.CommonName(), err)
return 0
}
// success if rate limit not set or remaining is above our limit
rl := manifest.GetRateLimit(mh)
if !rl.Set || rl.Remain >= limit {
ls.Push(lua.LBool(true))
return 1
}
// delay for freq (until timeout reached), and then retry
s.log.WithFields(logrus.Fields{
"script": s.name,
"image": r.r.CommonName(),
"current": rl.Remain,
"target": limit,
"delay": freq.String(),
}).Info("Delaying for ratelimit")
select {
case <-ctx.Done():
ls.Push(lua.LBool(false))
return 1
case <-time.After(freq):
}
}
}