/
api.go
325 lines (272 loc) · 8.13 KB
/
api.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
package api
import (
"context"
"fmt"
"net"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
containerdarchive "github.com/containerd/containerd/archive"
"github.com/docker/go-units"
"github.com/lthibault/jitterbug"
"github.com/moby/moby/api/types"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/labstack/echo/v4"
"github.com/mudler/containerbay/store"
)
// API returns a new containerbay instance
type API struct {
listenAddr string
dnsTXTKey string
magicDNS string
standaloneImage string
defaultImage string
whitelist []string
whitergx []*regexp.Regexp
cacheStore *store.Store
maxSize int64
poolSize, workers int
pool chan workPackage
cleanupInterval time.Duration
auth *types.AuthConfig
mu sync.Mutex
}
type workPackage struct {
img, dst string
}
func (a *API) downloadImage(image, dst string) error {
// Let just one of the routine go and handle the download
a.mu.Lock()
if _, err := os.Stat(dst); err == nil {
a.mu.Unlock()
return nil
}
os.MkdirAll(dst, os.ModePerm)
a.mu.Unlock()
os.MkdirAll(fmt.Sprintf("%s.lock", dst), 0600)
defer os.RemoveAll(fmt.Sprintf("%s.lock", dst))
pterm.Info.Printfln("Downloading %s to %s", image, dst)
ref, err := name.ParseReference(image)
if err != nil {
return err
}
var img v1.Image
if a.auth != nil {
img, err = remote.Image(ref, remote.WithAuth(staticAuth{a.auth}))
if err != nil {
return err
}
} else {
img, err = remote.Image(ref)
if err != nil {
return err
}
}
reader := mutate.Extract(img)
defer reader.Close()
_, err = containerdarchive.Apply(context.Background(), dst, reader)
if err != nil {
return err
}
pterm.Info.Printfln("Downloaded %s to %s", image, dst)
return nil
}
func imageSize(img v1.Image) (size int64) {
lyrs, _ := img.Layers()
for _, l := range lyrs {
s, _ := l.Size()
size += s
}
return
}
type errorMessage struct {
Error string `json:"error"`
}
func retError(c echo.Context, template string, i ...interface{}) error {
return c.JSON(http.StatusInternalServerError, errorMessage{Error: fmt.Sprintf(template, i...)})
}
func (a *API) startWorkers() {
pterm.Info.Printfln("Starting '%d' workers", a.workers)
for i := 0; i < a.workers; i++ {
go func() {
for f := range a.pool {
err := a.downloadImage(f.img, f.dst)
if err != nil {
pterm.Error.Printfln("Download failed: %s", err.Error())
os.RemoveAll(f.dst)
}
}
}()
}
}
func (a *API) cleanupWorker(ctx context.Context) {
if a.cleanupInterval == 0 {
pterm.Info.Println("Cleanup disabled")
return
}
pterm.Info.Println("Cleanups enabled")
go func() {
t := jitterbug.New(
a.cleanupInterval,
&jitterbug.Norm{Stdev: time.Second * 10},
)
for {
select {
case <-t.C:
err := a.cacheStore.Clean()
if err != nil {
pterm.Error.Println("error while cleaning up:", err)
}
case <-ctx.Done():
return
}
}
}()
}
func (a *API) renderImage(c echo.Context, image, strip string) error {
if len(a.whitergx) > 0 {
ok := false
for _, r := range a.whitergx {
if r.MatchString(image) {
ok = true
}
}
if !ok {
return retError(c, "forbidden image '%s'", image)
}
}
ref, err := name.ParseReference(image)
if err != nil {
return retError(c, "while parsing image reference '%s'", err.Error())
}
img, err := remote.Image(ref)
if err != nil {
return retError(c, "while fetching remote image reference '%s'", err.Error())
}
size := imageSize(img)
if a.maxSize != 0 && size > a.maxSize {
pterm.Warning.Printfln("Refusing to serve image '%s' (size: %s)", image, units.HumanSize(float64(size)))
return retError(c, "max size exceeded: image %d, threshold %d", size, a.maxSize)
}
pterm.Info.Printfln("Serving image: %s Size: %s", image, units.HumanSize(float64(size)))
h, err := img.Digest()
if err != nil {
return retError(c, "while getting image digest: %s", err.Error())
}
// If doesn't exist in cache we have to download it
// We let the worker download them, and handle the request separately
if !a.cacheStore.Exists(h.Hex) {
pterm.Info.Printfln("Not present in cache %s: %s Size: %s", h.Hex, image, units.HumanSize(float64(size)))
a.pool <- workPackage{img: image, dst: a.cacheStore.Path(h.Hex)}
return c.HTML(202, "Processing the request, try again soon.")
}
if _, err := os.Stat(fmt.Sprintf("%s.lock", a.cacheStore.Path(h.Hex))); err == nil {
return c.HTML(202, "Still processing, try again soon.")
}
pterm.Info.Printfln("Render from cache %s: %s Size: %s", h.Hex, image, units.HumanSize(float64(size)))
return echo.WrapHandler(
http.StripPrefix(strip, http.FileServer(http.Dir(a.cacheStore.Path(h.Hex)))))(c)
}
func (a *API) containerFromDomain(domain string) (string, error) {
pterm.Debug.Printfln("(magicDNS) Querying TXT records for domain '%s'", domain)
txtrecords, err := net.LookupTXT(domain)
if err != nil {
return "", err
}
pterm.Debug.Printfln("(magicDNS) Found '%v' for domain '%s'", txtrecords, domain)
for _, txt := range txtrecords {
dat := strings.Split(txt, " ")
pterm.Debug.Printfln("(magicDNS) txt record '%v' for domain '%s'", txt, domain)
if len(dat) > 0 {
record := dat[0]
// dd := dat[1]
// if dd != domain { continue }
r := strings.Split(record, "=")
if len(r) > 0 {
if r[0] == a.dnsTXTKey {
return r[1], nil
}
}
}
}
return "", errors.New("record not found")
}
// EchoOption is a generic handler which mutates the underlying Echo instance
type EchoOption func(e *echo.Echo) error
// Start starts the API with the given EchoOption
func (a *API) Start(opts ...EchoOption) error {
a.pool = make(chan workPackage, a.poolSize)
a.startWorkers()
a.cleanupWorker(context.Background())
a.cacheStore.CleanAll()
for _, w := range a.whitelist {
r, err := regexp.Compile(w)
if err != nil {
return err
}
a.whitergx = append(a.whitergx, r)
pterm.Info.Printfln("Whitelist '%s'", w)
}
ec := echo.New()
for _, o := range opts {
o(ec)
}
pterm.Info.Printfln("Cachestore dir at '%s'", a.cacheStore)
pterm.Info.Printfln("Max image size '%s'", units.HumanSize(float64(a.maxSize)))
pterm.Info.Printfln("Default image '%s'", a.defaultImage)
if a.standaloneImage != "" {
ec.GET("/*", func(c echo.Context) error {
return a.renderImage(c, a.standaloneImage, "/")
})
} else {
ec.GET("/*", func(c echo.Context) error {
req := c.Request()
host := req.Host
if a.magicDNS != "" {
dns := a.magicDNS
if !strings.HasPrefix(dns, ".") {
dns = "." + dns
}
hostdots := strings.ReplaceAll(host, dns, "")
fields := strings.Split(hostdots, ".")
pterm.Info.Printfln("Trying to resolve magicDNS(%s) for '%s'", dns, host)
// A host can be: registry.org.container.tag
if len(fields) >= 4 {
var tag, org, container, registry string
// pop the required fields
tag, fields = fields[len(fields)-1], fields[:len(fields)-1]
container, fields = fields[len(fields)-1], fields[:len(fields)-1]
org, fields = fields[len(fields)-1], fields[:len(fields)-1]
registry = strings.Join(fields, ".") // recompose registry
// compose image name
image := fmt.Sprintf("%s/%s/%s:%s", registry, org, container, tag)
pterm.Info.Printfln("magicDNS resolved '%s'", image)
return a.renderImage(c, image, "/")
}
}
if container, err := a.containerFromDomain(host); err == nil {
pterm.Info.Printfln("magicDNS from dns domain resolved '%s'", container)
return a.renderImage(c, container, "/")
} else {
pterm.Debug.Printfln("(magicDNS) failed getting records from TXT '%s'", err.Error())
}
return a.renderImage(c, a.defaultImage, "/")
})
ec.GET("/:registry/:org/:container/*", func(c echo.Context) error {
org := c.Param("org")
container := c.Param("container")
registry := c.Param("registry")
image := fmt.Sprintf("%s/%s/%s", registry, org, container)
return a.renderImage(c, image, fmt.Sprintf("/%s/", image))
})
}
return ec.Start(a.listenAddr)
}