-
Notifications
You must be signed in to change notification settings - Fork 3
/
templates.go
365 lines (320 loc) · 9.62 KB
/
templates.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
358
359
360
361
362
363
364
365
package explore
import (
"fmt"
"text/template"
v1 "github.com/google/go-containerregistry/pkg/v1"
)
var (
headerTmpl *template.Template
bodyTmpl *template.Template
oauthTmpl *template.Template
)
func init() {
headerTmpl = template.Must(template.New("headerTemplate").Parse(headerTemplate))
bodyTmpl = template.Must(template.New("bodyTemplate").Parse(bodyTemplate))
oauthTmpl = template.Must(template.New("oauthTemplate").Parse(oauthTemplate))
}
const (
gcrane = `<a class="mt" href="https://github.com/google/go-containerregistry/blob/main/cmd/gcrane/README.md">gcrane</a>`
craneLink = `<a class="mt" href="https://github.com/google/go-containerregistry/blob/main/cmd/crane/README.md">crane</a>`
subLinkFmt = `<a class="mt" href="https://github.com/google/go-containerregistry/blob/main/cmd/crane/doc/crane_%s.md">%s</a>`
)
func crane(sub string) string {
if sub == "" {
return craneLink
}
subLink := fmt.Sprintf(subLinkFmt, sub, sub)
return craneLink + " " + subLink
}
const (
landingPage = `
<html>
<body>
<head>
<title>Registry Explorer</title>
<link rel="icon" href="/favicon.svg">
<style>
.mt:hover {
text-decoration: underline;
}
.mt {
color: inherit;
text-decoration: inherit;
}
.link {
position: relative;
bottom: .125em;
}
.crane {
height: 1em;
width: 1em;
}
.top {
color: inherit;
text-decoration: inherit;
}
:root {
color-scheme: light dark;
}
body {
font-family: monospace;
width: fit-content;
overflow-wrap: anywhere;
padding: 12px;
}
</style>
</head>
<h1><a class="top" href="/"><img class="crane" src="/favicon.svg"/> <span class="link">Registry Explorer</span></a></h1>
<p>
This beautiful tool allows you to <em>explore</em> the contents of a registry interactively.
</p>
<p>
You can even drill down into layers to explore an image's filesystem.
</p>
<p>
Enter a <strong>public</strong> image, e.g. <tt>"ubuntu:latest"</tt>:
</p>
<form action="/" method="GET" autocomplete="off" spellcheck="false">
<input size="100" type="text" name="image" value="ubuntu:latest"/>
<input type="submit" />
</form>
<p>
<p>
Enter a <strong>public</strong> repository, e.g. <tt>"ubuntu"</tt>:
</p>
<form action="/" method="GET" autocomplete="off" spellcheck="false">
<input size="100" type="text" name="repo" value="ubuntu"/>
<input type="submit" />
</form>
<p>
<h4>Interesting examples</h4>
<ul>
<li><a href="/?image=cgr.dev/chainguard/static:latest-glibc">cgr.dev/chainguard/static:latest-glibc</a></li>
<li><a href="/?image=gcr.io/distroless/static">gcr.io/distroless/static:latest</a></li>
<li><a href="/?repo=ghcr.io/homebrew/core/crane">ghcr.io/homebrew/core/crane</a></li>
<li><a href="/?repo=registry.k8s.io">registry.k8s.io</a></li>
<li><a href="/?image=registry.k8s.io/bom/bom:sha256-499bdf4cc0498bbfb2395f8bbaf3b7e9e407cca605aecc46b2ef1b390a0bc4c4.sig">registry.k8s.io/bom/bom:sha256-499bdf4cc0498bbfb2395f8bbaf3b7e9e407cca605aecc46b2ef1b390a0bc4c4.sig</a></li>
<li><a href="/?image=docker/dockerfile:1.5.1">docker/dockerfile:1.5.1</a></li>
<li><a href="/?image=pengfeizhou/test-oci:sha256-04eaff953b0066d7e4ea2e822eb5c31be0742fca494561336f0912fabc246760">pengfeizhou/test-oci:sha256-04eaff953b0066d7e4ea2e822eb5c31be0742fca494561336f0912fabc246760</a></li>
<li><a href="/?image=tianon/true:oci">tianon/true:oci</a></li>
<li><a href="/?image=ghcr.io/stargz-containers/node:13.13.0-esgz">ghcr.io/stargz-containers/node:13.13.0-esgz</a></li>
</ul>
</p>
<h3>FAQ</h3>
<h4>How does this work?</h4>
<p>
This service lives on <a href="https://cloud.run">Cloud Run</a> and uses <a href="https://github.com/google/go-containerregistry">google/go-containerregistry</a> for registry interactions.
</p>
<h4>Isn't this expensive for the registry?</h4>
<p>
Not really! The first time a layer is accessed, I download and index it. Browsing the filesystem just uses that index, and opening a file uses Range requests to load small chunks of the layer as needed.
</p>
<h4>That can't be true, gzip doesn't support random access!</h4>
<p>
That's not a question.
</p>
<h4>Okay then, how does random access work if the layers are gzipped tarballs?</h4>
<p>Great question! See <a href="https://github.com/madler/zlib/blob/master/examples/zran.c">here</a>.</p>
<p>Tl;dr, you can seek to an arbitrary position in a gzip stream if you know the 32KiB of uncompressed data that comes just before it, so by storing ~1% of the uncompressed layer size, I can jump ahead to predetermined locations and start reading from there rather than reading the entire layer.</p>
<p>Thanks <a href="https://github.com/aidansteele">@aidansteele</a>!</p>
<h4>Is this open source?</h4>
<p>Yes! See <a href="https://github.com/jonjohnsonjr/dagdotdev">here</a>.</p>
</body>
</html>
`
oauthTemplate = `
<html>
<body>
<head>
<title>Registry Explorer</title>
<link rel="icon" href="/favicon.svg">
<style>
.mt:hover {
text-decoration: underline;
}
.mt {
color: inherit;
text-decoration: inherit;
}
.link {
position: relative;
bottom: .125em;
}
.crane {
height: 1em;
width: 1em;
}
.top {
color: inherit;
text-decoration: inherit;
}
:root {
color-scheme: light dark;
}
body {
font-family: monospace;
width: fit-content;
overflow-wrap: anywhere;
padding: 12px;
}
</style>
</head>
<h1><a class="top" href="/"><img class="crane" src="/favicon.svg"/> <span class="link">Registry Explorer</span></a></h1>
<p>
It looks like we encountered an auth error:
</p>
<code>
{{.Error}}
</code>
<p>
If you trust <a class="mt" href="https://github.com/jonjohnsonjr">me</a>, click <a href="{{.Redirect}}">here</a> for oauth to use your own credentials.
</p>
</body>
</html>
`
headerTemplate = `
<html>
<head>
<title>{{.Title}}</title>
<link rel="icon" href="/favicon.svg">
<style>
.mt:hover {
text-decoration: underline;
}
.mt {
color: inherit;
text-decoration: inherit;
}
.link {
position: relative;
bottom: .125em;
}
.crane {
height: 1em;
width: 1em;
}
.top {
color: inherit;
text-decoration: inherit;
}
:root {
color-scheme: light dark;
}
body {
font-family: monospace;
width: fit-content;
overflow-wrap: anywhere;
padding: 12px;
}
pre {
white-space: pre-wrap;
}
.indent {
margin-left: 2em;
}
.noselect {
user-select: none;
-webkit-user-select: none;
width: fit-content;
overflow-wrap: none;
padding-right: 1em;
text-align: right;
white-space: nowrap;
}
td {
vertical-align: top;
}
// Adapted from https://medium.com/allenhwkim/how-to-build-tabs-only-with-css-844718d7de2f
input + label { display: inline-block } /* show labels in line */
input { display: none; } /* hide radio buttons */
input ~ .tab { display: none } /* hide contents */
/* show contents only for selected tab */
#tab1:checked ~ .tab.content1,
#tab2:checked ~ .tab.content2 { display: block; }
input + label { /* box with rounded corner */
display: inline-block;
border: 1px solid #999;
padding: 4px 12px;
border-radius: 4px 4px 0 0;
position: relative;
top: 1px;
opacity: 50%;
}
input:checked + label { /* white background for selected tab */
opacity: 100%;
}
input ~ .tab { /* grey line between tab and contents */
border-top: 1px solid #999;
padding-top: 0.5em;
}
</style>
</head>
`
bodyTemplate = `
<body>
<div>
<h1><a class="top" href="/"><img class="crane" src="/favicon.svg"/> <span class="link">Registry Explorer</span></a></h1>
{{ if .Up }}
<h2>{{ if and (ne .Up.Parent "docker.io") (ne .Up.Parent "index.docker.io") }}<a class="mt" href="/?repo={{.Up.Parent}}">{{.Up.Parent}}</a>{{else}}{{.Up.Parent}}{{end}}{{.Up.Separator}}{{if .Handler }}<a class="mt" href="/{{.Handler}}{{.Reference}}{{if .EscapedMediaType}}&mt={{.EscapedMediaType}}{{end}}">{{.Up.Child}}</a>{{else}}{{.Up.Child}}{{end}}{{ range .CosignTags }} (<a href="/?image={{$.Repo}}:{{.Tag}}">{{.Short}}</a>){{end}}{{if .Referrers}} <a href="/?referrers={{$.Repo}}@{{$.Descriptor.Digest}}">(referrers)</a>{{end}}</h2>
{{ else }}
<h2>{{.Reference}}{{ range .CosignTags }} (<a href="/?image={{$.Repo}}:{{.Tag}}">{{.Short}}</a>){{end}}{{if .Referrers}} <a href="/?referrers={{$.Repo}}@{{$.Descriptor.Digest}}">(referrers)</a>{{end}}</h2>
{{ end }}
{{ if .Descriptor }}
<input type="radio" name="tabs" id="tab1" checked />
<label for="tab1">HTTP</label>
<input type="radio" name="tabs" id="tab2" />
<label for="tab2">OCI</label>
<div class="tab content1">
Content-Type: {{if .MediaTypeLink}}<a class="mt" href="{{.MediaTypeLink}}">{{.Descriptor.MediaType}}</a>{{else}}{{.Descriptor.MediaType}}{{end}}<br>
Docker-Content-Digest: <a class="mt" href="/{{.Handler}}{{$.Repo}}@{{.Descriptor.Digest}}{{if .EscapedMediaType}}&mt={{.EscapedMediaType}}{{end}}&size={{.Descriptor.Size}}">{{.Descriptor.Digest}}</a><br>
Content-Length: {{if .SizeLink}}<a class="mt" href="{{.SizeLink}}">{{.Descriptor.Size}}</a>{{else}}{{.Descriptor.Size}}{{end}}<br>
{{if $.Subject}}OCI-Subject: <a class="mt" href="/?image={{$.Repo}}@{{.Subject}}">{{.Subject}}</a><br>{{end}}
</div>
<div class="tab content2">
{<br>
"mediaType": "{{.Descriptor.MediaType}}",<br>
"digest": "{{.Descriptor.Digest}}",<br>
"size": {{.Descriptor.Size}}<br>
}<br>
</div>
{{end}}
</div>
{{ if .JQ }}
<h4><span class="noselect">$</span>{{.JQ}}</h4>
{{ end }}
`
footer = `
</body>
</html>
`
)
type RepoParent struct {
Parent string
Child string
Separator string
}
type OauthData struct {
Error string
Redirect string
}
type TitleData struct {
Title string
}
type CosignTag struct {
Tag string
Short string
}
type HeaderData struct {
Repo string
CosignTags []CosignTag
JQ string
Reference string
Up *RepoParent
Descriptor *v1.Descriptor
Handler string
EscapedMediaType string
MediaTypeLink string
SizeLink string
Referrers bool
Subject string
}