This repository was archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
284 lines (253 loc) · 7.96 KB
/
main.go
File metadata and controls
284 lines (253 loc) · 7.96 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
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
package main
import (
_ "embed"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"sync"
"time"
"github.com/chainguard-dev/tlogistry/internal/rekor"
authchallenge "github.com/docker/distribution/registry/client/auth/challenge"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/google/go-containerregistry/pkg/name"
"github.com/kelseyhightower/envconfig"
)
func main() {
var env struct {
Port int64 `envconfig:"PORT" default:"8080"`
}
if err := envconfig.Process("", &env); err != nil {
log.Fatalf("envconfig: %v", err)
}
http.HandleFunc("/", handleHome)
http.HandleFunc("/style.css", handleStyle)
http.HandleFunc("/v2/", handler)
log.Printf("Listening on port %d", env.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", env.Port), nil))
}
//go:embed README.md
var readmeMD []byte
var readmeHTML []byte
var homeOnce sync.Once
//go:embed style.css
var style []byte
func handleHome(w http.ResponseWriter, _ *http.Request) {
homeOnce.Do(func() {
readmeHTML = markdown.ToHTML(readmeMD,
parser.NewWithExtensions(parser.CommonExtensions),
html.NewRenderer(html.RendererOptions{
CSS: "style.css",
Title: "tlogistry.dev",
Flags: html.CommonFlags | html.CompletePage | html.HrefTargetBlank,
}))
})
if _, err := w.Write(readmeHTML); err != nil {
log.Printf("!!! ERROR WRITING HTML: %v", err)
}
}
func handleStyle(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/css")
if _, err := w.Write(style); err != nil {
log.Printf("!!! ERROR WRITING STYLE: %v", err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
log.Println("handler:", r.Method, r.URL)
if r.Method != http.MethodGet && r.Method != http.MethodHead {
serveError(w, regError{status: http.StatusMethodNotAllowed, Code: "DENIED", Message: "tlogistry is read-only"})
return
}
switch r.URL.Path {
case "/v2/", "/v2":
w.Header().Set("Docker-Distribution-API-Version", "registry/2.0")
default:
proxy(w, r)
}
}
func proxy(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
parts := strings.Split(r.URL.Path, "/")
// /v2/ubuntu/manifests/latest -> ubuntu
// /v2/example.biz/foo/bar/manifests/latest -> example.biz/foo/bar
repostr := strings.Join(parts[2:len(parts)-2], "/")
repo, err := name.NewRepository(repostr)
if err != nil {
serveError(w, regError{status: http.StatusBadRequest, Code: "NAME_INVALID", Message: fmt.Sprintf("parsing repository name: %v", err)})
return
}
url := fmt.Sprintf("https://%s/v2/%s/%s", repo.RegistryStr(), repo.RepositoryStr(), strings.Join(parts[len(parts)-2:], "/"))
log.Println("-->", r.Method, r.URL)
req, _ := http.NewRequest(r.Method, url, nil)
for k, v := range r.Header {
for _, vv := range v {
req.Header.Add(k, vv)
if k == "Authorization" {
vv = "REDACTED"
}
log.Printf("--> %s: %s", k, vv)
}
}
isManifestTagRequest := parts[len(parts)-2] == "manifests" &&
!strings.HasPrefix(parts[len(parts)-1], "sha256:")
// If this is a request for manifest by tag, check Rekor to see if we have a digest for it.
var tag name.Tag
var wantDigest string
var info *rekor.Info
if isManifestTagRequest {
tagstr := parts[len(parts)-1]
var err error
tag, err = name.NewTag(fmt.Sprintf("%s:%s", repo.String(), tagstr))
if err != nil {
serveError(w, regError{status: http.StatusBadRequest, Code: "NAME_INVALID", Message: fmt.Sprintf("parsing tag: %v", err)})
return
}
wantDigest, info, err = rekor.Get(ctx, tag)
if err != nil {
serveError(w, newRegError(fmt.Errorf("looking up digest for tag %q: %v", tag, err)))
return
}
log.Println("=== REKOR: found digest for tag", tag, wantDigest)
}
// If the request is coming in without auth, get some auth.
//
// It's unlikely the request comes in with auth already attached, since
// that would have required /v2 to point to /token and for /token to
// have generated some creds.
if req.Header.Get("Authorization") == "" {
log.Println(" Getting token...")
t, err := getToken(repo)
if err != nil {
serveError(w, newRegError(fmt.Errorf("getting token: %v", err)))
return
}
req.Header.Set("Authorization", "Bearer "+t)
}
resp, err := http.DefaultTransport.RoundTrip(req) // Transport doesn't follow redirects.
if err != nil {
serveError(w, newRegError(fmt.Errorf("fetching %q: %v", url, err)))
return
}
defer resp.Body.Close()
gotDigest := resp.Header.Get("Docker-Content-Digest")
if wantDigest != "" && gotDigest != wantDigest {
serveError(w, digestMismatch(tag.String(), gotDigest, wantDigest))
return
}
log.Println("<--", resp.StatusCode)
for k, v := range resp.Header {
for _, vv := range v {
log.Printf("<-- %s: %s", k, vv)
w.Header().Add(k, vv)
}
}
if isManifestTagRequest && // If this is a request for manifest by tag,
gotDigest != "" && // and we have the digest now,
wantDigest == "" { // and we didn't have one before --> record it in Rekor.
log.Println("=== REKOR: writing digest for tag", tag, gotDigest)
if info, err = rekor.Put(ctx, tag, gotDigest); err != nil {
log.Println("!!! ERROR WRITING TO REKOR:", err)
}
// This request made us write an entry for the first time.
w.Header().Set("TLog-First-Seen", "true")
}
if info != nil {
w.Header().Set("TLog-UUID", info.UUID)
w.Header().Set("TLog-LogIndex", fmt.Sprintf("%d", info.LogIndex))
w.Header().Set("TLog-IntegratedTime", info.IntegratedTime.Format(time.RFC3339))
}
w.WriteHeader(resp.StatusCode)
if parts[len(parts)-2] != "blobs" { // Never proxy blobs.
if _, err := io.Copy(w, resp.Body); err != nil {
log.Println("!!! ERROR COPYING RESPONSE BODY:", err)
}
}
}
func getToken(repo name.Repository) (string, error) {
// Ping /v2/, determine the registry's auth scheme.
url := fmt.Sprintf("https://%s/v2/", repo.RegistryStr())
log.Println(" --> GET", url)
resp, err := http.Get(url)
if err != nil {
return "", err
}
log.Println(" <--", resp.StatusCode)
for k, v := range resp.Header {
for _, vv := range v {
log.Printf(" <-- %s: %s", k, vv)
}
}
if resp.StatusCode == http.StatusOK {
return "", nil // Registry doesn't require auth.
}
if resp.StatusCode != http.StatusUnauthorized {
return "", fmt.Errorf("unexpected status code (%s): %d", url, resp.StatusCode)
}
chs := authchallenge.ResponseChallenges(resp)
if len(chs) == 0 {
return "", nil // Registry doesn't require auth.
}
if strings.ToLower(chs[0].Scheme) != "bearer" {
return "", fmt.Errorf("unsupported auth scheme: %s", chs[0].Scheme)
}
// Ping token endpoint, get a token.
service := chs[0].Parameters["service"]
realm := chs[0].Parameters["realm"]
url = fmt.Sprintf("%s?scope=repository:%s:pull&service=%s", realm, repo.RepositoryStr(), service)
log.Println(" --> GET", url)
resp, err = http.Get(url)
if err != nil {
return "", err
}
log.Println(" <--", resp.StatusCode)
for k, v := range resp.Header {
for _, vv := range v {
log.Printf(" <-- %s: %s", k, vv)
}
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code (%s): %d", url, resp.StatusCode)
}
defer resp.Body.Close()
var tokenResp struct {
Token string `json:"token"`
}
if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
return "", err
}
return tokenResp.Token, nil
}
func serveError(w http.ResponseWriter, re regError) {
http.Error(w, "", re.status)
if err := json.NewEncoder(w).Encode(&resp{
Errors: []regError{re},
}); err != nil {
log.Printf("!!! ERROR WRITING ERROR BODY (%+v): %v", re, err)
}
}
type resp struct {
Errors []regError `json:"errors"`
}
type regError struct {
status int
Code string `json:"code"`
Message string `json:"message"`
}
func digestMismatch(tag, got, want string) regError {
return regError{
status: http.StatusBadRequest,
Code: "TAG_INVALID",
Message: fmt.Sprintf("tag %q mismatch; got %q, want %q", tag, got, want),
}
}
func newRegError(err error) regError {
return regError{
status: http.StatusInternalServerError,
Code: "INTERNAL_ERROR",
Message: err.Error(),
}
}