| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package ociserver |
| 21 |
|
| 22 |
import ( |
| 23 |
"context" |
| 24 |
"errors" |
| 25 |
"fmt" |
| 26 |
"log" |
| 27 |
"net/http" |
| 28 |
"sync/atomic" |
| 29 |
|
| 30 |
"cuelabs.dev/go/oci/ociregistry" |
| 31 |
"cuelabs.dev/go/oci/ociregistry/internal/ocirequest" |
| 32 |
ocispecroot "github.com/opencontainers/image-spec/specs-go" |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
const debug = false |
| 37 |
|
| 38 |
var v2 = ocispecroot.Versioned{ |
| 39 |
SchemaVersion: 2, |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
type Options struct { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
WriteError func(w http.ResponseWriter, req *http.Request, err error) |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
DisableReferrersAPI bool |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
DisableSinglePostUpload bool |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
MaxListPageSize int |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
OmitDigestFromTagGetResponse bool |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
OmitLinkHeaderFromResponses bool |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
LocationForUploadID func(string) (string, error) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
LocationsForDescriptor func(isManifest bool, desc ociregistry.Descriptor) ([]string, error) |
| 110 |
|
| 111 |
DebugID string |
| 112 |
} |
| 113 |
|
| 114 |
var debugID int32 |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func New(backend ociregistry.Interface, opts *Options) http.Handler { |
| 134 |
if opts == nil { |
| 135 |
opts = new(Options) |
| 136 |
} |
| 137 |
r := ®istry{ |
| 138 |
opts: *opts, |
| 139 |
backend: backend, |
| 140 |
} |
| 141 |
if r.opts.DebugID == "" { |
| 142 |
r.opts.DebugID = fmt.Sprintf("ociserver%d", atomic.AddInt32(&debugID, 1)) |
| 143 |
} |
| 144 |
if r.opts.WriteError == nil { |
| 145 |
r.opts.WriteError = func(w http.ResponseWriter, _ *http.Request, err error) { |
| 146 |
ociregistry.WriteError(w, err) |
| 147 |
} |
| 148 |
} |
| 149 |
return r |
| 150 |
} |
| 151 |
|
| 152 |
func (r *registry) logf(f string, a ...any) { |
| 153 |
log.Printf("ociserver %s: %s", r.opts.DebugID, fmt.Sprintf(f, a...)) |
| 154 |
} |
| 155 |
|
| 156 |
type registry struct { |
| 157 |
opts Options |
| 158 |
backend ociregistry.Interface |
| 159 |
} |
| 160 |
|
| 161 |
var handlers = []func(r *registry, ctx context.Context, w http.ResponseWriter, req *http.Request, rreq *ocirequest.Request) error{ |
| 162 |
ocirequest.ReqPing: (*registry).handlePing, |
| 163 |
ocirequest.ReqBlobGet: (*registry).handleBlobGet, |
| 164 |
ocirequest.ReqBlobHead: (*registry).handleBlobHead, |
| 165 |
ocirequest.ReqBlobDelete: (*registry).handleBlobDelete, |
| 166 |
ocirequest.ReqBlobStartUpload: (*registry).handleBlobStartUpload, |
| 167 |
ocirequest.ReqBlobUploadBlob: (*registry).handleBlobUploadBlob, |
| 168 |
ocirequest.ReqBlobMount: (*registry).handleBlobMount, |
| 169 |
ocirequest.ReqBlobUploadInfo: (*registry).handleBlobUploadInfo, |
| 170 |
ocirequest.ReqBlobUploadChunk: (*registry).handleBlobUploadChunk, |
| 171 |
ocirequest.ReqBlobCompleteUpload: (*registry).handleBlobCompleteUpload, |
| 172 |
ocirequest.ReqManifestGet: (*registry).handleManifestGet, |
| 173 |
ocirequest.ReqManifestHead: (*registry).handleManifestHead, |
| 174 |
ocirequest.ReqManifestPut: (*registry).handleManifestPut, |
| 175 |
ocirequest.ReqManifestDelete: (*registry).handleManifestDelete, |
| 176 |
ocirequest.ReqTagsList: (*registry).handleTagsList, |
| 177 |
ocirequest.ReqReferrersList: (*registry).handleReferrersList, |
| 178 |
ocirequest.ReqCatalogList: (*registry).handleCatalogList, |
| 179 |
} |
| 180 |
|
| 181 |
func (r *registry) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 182 |
if rerr := r.v2(resp, req); rerr != nil { |
| 183 |
r.opts.WriteError(resp, req, rerr) |
| 184 |
return |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
func (r *registry) v2(resp http.ResponseWriter, req *http.Request) (_err error) { |
| 191 |
if debug { |
| 192 |
r.logf("registry.v2 %v %s {", req.Method, req.URL) |
| 193 |
defer func() { |
| 194 |
if _err != nil { |
| 195 |
r.logf("} -> %v", _err) |
| 196 |
} else { |
| 197 |
r.logf("}") |
| 198 |
} |
| 199 |
}() |
| 200 |
} |
| 201 |
|
| 202 |
rreq, err := ocirequest.Parse(req.Method, req.URL) |
| 203 |
if err != nil { |
| 204 |
resp.Header().Set("Docker-Distribution-API-Version", "registry/2.0") |
| 205 |
return handlerErrorForRequestParseError(err) |
| 206 |
} |
| 207 |
handle := handlers[rreq.Kind] |
| 208 |
return handle(r, req.Context(), resp, req, rreq) |
| 209 |
} |
| 210 |
|
| 211 |
func (r *registry) handlePing(ctx context.Context, resp http.ResponseWriter, req *http.Request, rreq *ocirequest.Request) error { |
| 212 |
resp.Header().Set("Docker-Distribution-API-Version", "registry/2.0") |
| 213 |
return nil |
| 214 |
} |
| 215 |
|
| 216 |
func (r *registry) setLocationHeader(resp http.ResponseWriter, isManifest bool, desc ociregistry.Descriptor, defaultLocation string) error { |
| 217 |
loc := defaultLocation |
| 218 |
if r.opts.LocationsForDescriptor != nil { |
| 219 |
locs, err := r.opts.LocationsForDescriptor(isManifest, desc) |
| 220 |
if err != nil { |
| 221 |
what := "blob" |
| 222 |
if isManifest { |
| 223 |
what = "manifest" |
| 224 |
} |
| 225 |
return fmt.Errorf("cannot determine location for %s: %v", what, err) |
| 226 |
} |
| 227 |
if len(locs) > 0 { |
| 228 |
loc = locs[0] |
| 229 |
} |
| 230 |
} |
| 231 |
resp.Header().Set("Location", loc) |
| 232 |
resp.Header().Set("Docker-Content-Digest", string(desc.Digest)) |
| 233 |
return nil |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
type ParseError struct { |
| 239 |
error |
| 240 |
} |
| 241 |
|
| 242 |
func handlerErrorForRequestParseError(err error) error { |
| 243 |
if err == nil { |
| 244 |
return nil |
| 245 |
} |
| 246 |
var perr *ocirequest.ParseError |
| 247 |
if !errors.As(err, &perr) { |
| 248 |
return err |
| 249 |
} |
| 250 |
switch perr.Err { |
| 251 |
case ocirequest.ErrNotFound: |
| 252 |
return withHTTPCode(http.StatusNotFound, err) |
| 253 |
case ocirequest.ErrBadlyFormedDigest: |
| 254 |
return withHTTPCode(http.StatusBadRequest, err) |
| 255 |
case ocirequest.ErrMethodNotAllowed: |
| 256 |
return withHTTPCode(http.StatusMethodNotAllowed, err) |
| 257 |
case ocirequest.ErrBadRequest: |
| 258 |
return withHTTPCode(http.StatusBadRequest, err) |
| 259 |
} |
| 260 |
return err |
| 261 |
} |