Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing endpoints and fixed delete endpoints /w auth #4

Merged
merged 1 commit into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
./certs
./parachute
.cache
./parachute.yaml
.idea/
tests
12 changes: 6 additions & 6 deletions cache/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func (ds *dataStore) ResolveManifestRef(namespace, ref string) (string, error) {
}

for _, c := range md.Manifest.Config {
mdRef := c.Reference
mdDigest := c.Digest
if ref == mdRef || ref == mdDigest {
return c.SkynetLink, nil
mdRef := c.Reference
mdDigest := c.Digest
if ref == mdRef || ref == mdDigest {
return c.SkynetLink, nil
}
}
}

return "", fmt.Errorf("ref not found")
}
Expand All @@ -177,7 +177,7 @@ const layerDigestNamespace = "layers/digests"
func (ds *dataStore) SetDigest(digest, skylink string) error {
key := fmt.Sprintf("%s/%s", layerDigestNamespace, digest)
value := types.LayerRef{
Digest: digest,
Digest: digest,
Skylink: skylink,
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v20.10.6+incompatible
github.com/fatih/color v1.7.0
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible
github.com/google/go-containerregistry v0.5.1
github.com/labstack/echo-contrib v0.11.0
github.com/labstack/echo/v4 v4.3.0
Expand All @@ -21,7 +21,7 @@ require (
github.com/spf13/viper v1.7.1
github.com/whyrusleeping/tar-utils v0.0.0-20201201191210-20a61371de5b
gitlab.com/NebulousLabs/errors v0.0.0-20200929122200-06c536cf6975 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect
golang.org/x/time v0.0.0-20210608053304-ed9ce3a009e4 // indirect
google.golang.org/genproto v0.0.0-20210521181308-5ccab8a35a9a // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/Microsoft/hcsshim v0.8.16/go.mod h1:o5/SZqmR7x9JNKsW3pu+nqHm0MF8vbA+V
github.com/Microsoft/hcsshim/test v0.0.0-20201218223536-d3e5debf77da/go.mod h1:5hlzMzRKMLyo42nCZ9oml8AdTlq/0cvIaBv6tK1RehU=
github.com/Microsoft/hcsshim/test v0.0.0-20210227013316-43a75bb4edd3/go.mod h1:mw7qgWloBUl75W/gVH3cQszUg1+gUITj7D6NY7ywVnY=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/NebulousLabs/go-skynet/v2 v2.0.1 h1:c5x/onhWwap9p9QgcGZtep580vUYdcIdCgAHueyksyM=
github.com/NebulousLabs/go-skynet/v2 v2.0.1/go.mod h1:uhGD1M7Qe1nXsnqRD90B5CJVgjqSqwJUmDosgAn4CdQ=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
Expand Down
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func main() {
// GET GET /v2/<name>/blobs/uploads/<uuid>
router.Add(http.MethodGet, "/blobs/uploads/:uuid", reg.UploadProgress)

// router.Add(http.MethodGet, "/blobs/:digest", reg.DownloadBlob)
router.Add(http.MethodDelete, "/blobs/:digest", reg.DeleteLayer).Name = "DeleteLayer"
router.Add(http.MethodDelete, "/manifests/:digest", reg.DeleteImage).Name = "DeleteImage"

e.Add(http.MethodGet, "/v2/", reg.ApiVersion, BasicAuth(authSvc.BasicAuth))

Expand Down Expand Up @@ -148,17 +149,13 @@ Strict-Transport-Security: max-age=31536000

func BasicAuth(authfn func(string, string) (map[string]interface{}, error)) echo.MiddlewareFunc {
return middleware.BasicAuth(func(username string, password string, ctx echo.Context) (bool, error) {

if ctx.Request().RequestURI != "/v2/" {
if ctx.Request().Method == http.MethodHead || ctx.Request().Method == http.MethodGet {
return true, nil
}
}

color.Red("request uri %s", ctx.Request().RequestURI)

if ctx.Request().RequestURI == "/v2/" {
color.Blue("username %s password %s\n", username, password)
_, err := authfn(username, password)
if err != nil {
return false, ctx.NoContent(http.StatusUnauthorized)
Expand Down
2 changes: 1 addition & 1 deletion server/registry/v2/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (b *blobs) UploadBlob(ctx echo.Context) error {

buf := bytes.NewBuffer(b.uploads[uuid]) // 90
io.Copy(buf, ctx.Request().Body) // 10
defer ctx.Request().Body.Close()
ctx.Request().Body.Close()

b.uploads[uuid] = buf.Bytes()
locationHeader := fmt.Sprintf("/v2/%s/blobs/uploads/%s", namespace, uuid)
Expand Down
42 changes: 37 additions & 5 deletions server/registry/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ func NewRegistry(skynetClient *skynet.Client, logger zerolog.Logger, c cache.Sto
}

func (r *registry) DeleteLayer(ctx echo.Context) error {
return nil
dig := ctx.Param("digest")

_, err := r.localCache.GetDigest(dig)
if err != nil {
errMsg := r.errorResponse(RegistryErrorCodeBlobUnknown, err.Error(), nil)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
}

return ctx.NoContent(http.StatusOK)
}

// PUT /v2/<name>/blobs/uploads/<uuid>?digest=<digest>
Expand All @@ -56,7 +64,7 @@ func (r *registry) MonolithicUpload(ctx echo.Context) error {
errMsg := r.errorResponse(RegistryErrorCodeBlobUploadInvalid, err.Error(), nil)
return ctx.JSONBlob(http.StatusBadRequest, errMsg)
}
defer ctx.Request().Body.Close()
ctx.Request().Body.Close()

link, err := r.skynet.Upload(namespace, digest, bz)
if err != nil {
Expand Down Expand Up @@ -89,6 +97,7 @@ func (r *registry) CompleteUpload(ctx echo.Context) error {
errMsg := r.errorResponse(RegistryErrorCodeDigestInvalid, err.Error(), nil)
return ctx.JSONBlob(http.StatusBadRequest, errMsg)
}
ctx.Request().Body.Close()

buf := bytes.NewBuffer(r.b.uploads[uuid])
buf.Write(bz)
Expand Down Expand Up @@ -131,7 +140,11 @@ func (r *registry) CompleteUpload(ctx echo.Context) error {
Manifest: types.ImageManifest{
SchemaVersion: 2,
MediaType: "",
Layers: []*types.Layer{{MediaType: "", Size: len(bz), Digest: dig, SkynetLink: skylink, UUID: uuid}},
Layers: []*types.Layer{
{
MediaType: "", Size: len(bz), Digest: dig, SkynetLink: skylink, UUID: uuid,
},
},
},
}

Expand Down Expand Up @@ -301,7 +314,7 @@ func (r *registry) PushManifest(ctx echo.Context) error {
errMsg := r.errorResponse(RegistryErrorCodeManifestInvalid, err.Error(), nil)
return ctx.JSONBlob(http.StatusBadRequest, errMsg)
}
defer ctx.Request().Body.Close()
ctx.Request().Body.Close()

dig := digest(bz)

Expand Down Expand Up @@ -406,7 +419,26 @@ func (r *registry) PushManifest(ctx echo.Context) error {
}

func (r *registry) DeleteImage(ctx echo.Context) error {
return nil
clientDigest := ctx.Param("digest")
if clientDigest == "" {
path := strings.Split(ctx.Request().RequestURI, "/")
if len(path) == 6 {
clientDigest = path[5]
}
}
namespace := ctx.Param("username") + "/" + ctx.Param("imagename")

if d, err := r.localCache.ResolveManifestRef(namespace, clientDigest); err != nil {
details := map[string]interface{}{
"namespace": namespace,
"digest": clientDigest,
"data": d,
}
errMsg := r.errorResponse(RegistryErrorCodeManifestUnknown, err.Error(), details)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
}

return ctx.NoContent(http.StatusOK)
}

// GET /v2/<name>/blobs/<digest>
Expand Down