0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

compliance: "Location" header can be absolute or relative

Pls. see: https://github.com/opencontainers/distribution-spec/issues/79
This commit is contained in:
Ramkumar Chinchani 2020-01-04 19:05:39 -08:00
parent 915c994c6c
commit 87bad57345

View file

@ -19,18 +19,18 @@ import (
"gopkg.in/resty.v1" "gopkg.in/resty.v1"
) )
func Location(baseURL string, resp *resty.Response, config *compliance.Config) string { func Location(baseURL string, resp *resty.Response) string {
// For some API responses, the Location header is set and is supposed to // For some API responses, the Location header is set and is supposed to
// indicate an opaque value. However, it is not clear if this value is an // indicate an opaque value. However, it is not clear if this value is an
// absolute URL (https://server:port/v2/...) or just a path (/v2/...) // absolute URL (https://server:port/v2/...) or just a path (/v2/...)
// zot implements the latter as per the spec, but some registries appear to // zot implements the latter as per the spec, but some registries appear to
// return the former - this needs to be clarified // return the former - this needs to be clarified
loc := resp.Header().Get("Location") loc := resp.Header().Get("Location")
if config.Compliance { if loc[0] == '/' {
return loc return baseURL + loc
} }
return loc
return baseURL + loc
} }
func CheckWorkflows(t *testing.T, config *compliance.Config) { func CheckWorkflows(t *testing.T, config *compliance.Config) {
@ -119,7 +119,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
resp, err := resty.R().Post(baseURL + "/v2/repo2/blobs/uploads/") resp, err := resty.R().Post(baseURL + "/v2/repo2/blobs/uploads/")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202) So(resp.StatusCode(), ShouldEqual, 202)
loc := Location(baseURL, resp, config) loc := Location(baseURL, resp)
So(loc, ShouldNotBeEmpty) So(loc, ShouldNotBeEmpty)
resp, err = resty.R().Get(loc) resp, err = resty.R().Get(loc)
@ -155,7 +155,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc) SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 201) So(resp.StatusCode(), ShouldEqual, 201)
blobLoc := Location(baseURL, resp, config) blobLoc := Location(baseURL, resp)
So(blobLoc, ShouldNotBeEmpty) So(blobLoc, ShouldNotBeEmpty)
So(resp.Header().Get("Content-Length"), ShouldEqual, "0") So(resp.Header().Get("Content-Length"), ShouldEqual, "0")
So(resp.Header().Get(api.DistContentDigestKey), ShouldNotBeEmpty) So(resp.Header().Get(api.DistContentDigestKey), ShouldNotBeEmpty)
@ -174,7 +174,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
resp, err := resty.R().Post(baseURL + "/v2/repo10/repo20/repo30/blobs/uploads/") resp, err := resty.R().Post(baseURL + "/v2/repo10/repo20/repo30/blobs/uploads/")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202) So(resp.StatusCode(), ShouldEqual, 202)
loc := Location(baseURL, resp, config) loc := Location(baseURL, resp)
So(loc, ShouldNotBeEmpty) So(loc, ShouldNotBeEmpty)
resp, err = resty.R().Get(loc) resp, err = resty.R().Get(loc)
@ -210,7 +210,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc) SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 201) So(resp.StatusCode(), ShouldEqual, 201)
blobLoc := Location(baseURL, resp, config) blobLoc := Location(baseURL, resp)
So(blobLoc, ShouldNotBeEmpty) So(blobLoc, ShouldNotBeEmpty)
So(resp.Header().Get("Content-Length"), ShouldEqual, "0") So(resp.Header().Get("Content-Length"), ShouldEqual, "0")
So(resp.Header().Get(api.DistContentDigestKey), ShouldNotBeEmpty) So(resp.Header().Get(api.DistContentDigestKey), ShouldNotBeEmpty)
@ -229,7 +229,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
resp, err := resty.R().Post(baseURL + "/v2/repo3/blobs/uploads/") resp, err := resty.R().Post(baseURL + "/v2/repo3/blobs/uploads/")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202) So(resp.StatusCode(), ShouldEqual, 202)
loc := Location(baseURL, resp, config) loc := Location(baseURL, resp)
So(loc, ShouldNotBeEmpty) So(loc, ShouldNotBeEmpty)
var buf bytes.Buffer var buf bytes.Buffer
@ -276,7 +276,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
SetHeader("Content-Type", "application/octet-stream").SetBody(chunk2).Put(loc) SetHeader("Content-Type", "application/octet-stream").SetBody(chunk2).Put(loc)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 201) So(resp.StatusCode(), ShouldEqual, 201)
blobLoc := Location(baseURL, resp, config) blobLoc := Location(baseURL, resp)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 201) So(resp.StatusCode(), ShouldEqual, 201)
So(blobLoc, ShouldNotBeEmpty) So(blobLoc, ShouldNotBeEmpty)
@ -297,7 +297,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
resp, err := resty.R().Post(baseURL + "/v2/repo40/repo50/repo60/blobs/uploads/") resp, err := resty.R().Post(baseURL + "/v2/repo40/repo50/repo60/blobs/uploads/")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202) So(resp.StatusCode(), ShouldEqual, 202)
loc := Location(baseURL, resp, config) loc := Location(baseURL, resp)
So(loc, ShouldNotBeEmpty) So(loc, ShouldNotBeEmpty)
var buf bytes.Buffer var buf bytes.Buffer
@ -344,7 +344,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
SetHeader("Content-Type", "application/octet-stream").SetBody(chunk2).Put(loc) SetHeader("Content-Type", "application/octet-stream").SetBody(chunk2).Put(loc)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 201) So(resp.StatusCode(), ShouldEqual, 201)
blobLoc := Location(baseURL, resp, config) blobLoc := Location(baseURL, resp)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 201) So(resp.StatusCode(), ShouldEqual, 201)
So(blobLoc, ShouldNotBeEmpty) So(blobLoc, ShouldNotBeEmpty)
@ -366,7 +366,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
resp, err := resty.R().Post(baseURL + "/v2/repo4/blobs/uploads/") resp, err := resty.R().Post(baseURL + "/v2/repo4/blobs/uploads/")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202) So(resp.StatusCode(), ShouldEqual, 202)
loc := Location(baseURL, resp, config) loc := Location(baseURL, resp)
So(loc, ShouldNotBeEmpty) So(loc, ShouldNotBeEmpty)
// delete this upload // delete this upload
@ -381,7 +381,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
resp, err := resty.R().Post(baseURL + "/v2/repo5/blobs/uploads/") resp, err := resty.R().Post(baseURL + "/v2/repo5/blobs/uploads/")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202) So(resp.StatusCode(), ShouldEqual, 202)
loc := Location(baseURL, resp, config) loc := Location(baseURL, resp)
So(loc, ShouldNotBeEmpty) So(loc, ShouldNotBeEmpty)
content := []byte("this is a blob") content := []byte("this is a blob")
@ -392,7 +392,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc) SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 201) So(resp.StatusCode(), ShouldEqual, 201)
blobLoc := Location(baseURL, resp, config) blobLoc := Location(baseURL, resp)
So(blobLoc, ShouldNotBeEmpty) So(blobLoc, ShouldNotBeEmpty)
So(resp.Header().Get(api.DistContentDigestKey), ShouldNotBeEmpty) So(resp.Header().Get(api.DistContentDigestKey), ShouldNotBeEmpty)
@ -417,7 +417,7 @@ func CheckWorkflows(t *testing.T, config *compliance.Config) {
resp, err := resty.R().Post(baseURL + "/v2/repo7/blobs/uploads/") resp, err := resty.R().Post(baseURL + "/v2/repo7/blobs/uploads/")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202) So(resp.StatusCode(), ShouldEqual, 202)
loc := Location(baseURL, resp, config) loc := Location(baseURL, resp)
So(loc, ShouldNotBeEmpty) So(loc, ShouldNotBeEmpty)
resp, err = resty.R().Get(loc) resp, err = resty.R().Get(loc)