Skip to content

Commit

Permalink
fix(sync): added bearer client for sync
Browse files Browse the repository at this point in the history
fixed ping function taking too much time

closes: #2213 #2212

Signed-off-by: Petu Eusebiu <[email protected]>
  • Loading branch information
eusebiu-constantin-petu-dbk committed Feb 2, 2024
1 parent a60d389 commit 94f6d12
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 121 deletions.
2 changes: 1 addition & 1 deletion pkg/api/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func bearerAuthHandler(ctlr *Controller) mux.MiddlewareFunc {
if err != nil {
ctlr.Log.Error().Err(err).Msg("failed to parse Authorization header")
response.Header().Set("Content-Type", "application/json")
zcommon.WriteJSON(response, http.StatusInternalServerError, apiErr.NewError(apiErr.UNSUPPORTED))
zcommon.WriteJSON(response, http.StatusUnauthorized, apiErr.NewError(apiErr.UNSUPPORTED))

return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
httpTimeout = 5 * time.Minute
httpTimeout = 30 * time.Second
certsPath = "/etc/containers/certs.d"
homeCertsDir = ".config/containers/certs.d"
clientCertFilename = "client.cert"
Expand Down
56 changes: 0 additions & 56 deletions pkg/common/http_client.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package common

import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path"
"path/filepath"

"zotregistry.dev/zot/pkg/log"
)

func GetTLSConfig(certsPath string, caCertPool *x509.CertPool) (*tls.Config, error) {
Expand Down Expand Up @@ -111,53 +105,3 @@ func CreateHTTPClient(verifyTLS bool, host string, certDir string) (*http.Client
Transport: htr,
}, nil
}

func MakeHTTPGetRequest(ctx context.Context, httpClient *http.Client,
username string, password string, resultPtr interface{},
blobURL string, mediaType string, log log.Logger,
) ([]byte, string, int, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, blobURL, nil) //nolint
if err != nil {
return nil, "", 0, err
}

if mediaType != "" {
req.Header.Set("Accept", mediaType)
}

if username != "" && password != "" {
req.SetBasicAuth(username, password)
}

resp, err := httpClient.Do(req)
if err != nil {
log.Error().Str("errorType", TypeOf(err)).
Err(err).Str("blobURL", blobURL).Msg("couldn't get blob")

return nil, "", -1, err
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Str("errorType", TypeOf(err)).
Err(err).Str("blobURL", blobURL).Msg("couldn't get blob")

return nil, "", resp.StatusCode, err
}

if resp.StatusCode != http.StatusOK {
return nil, "", resp.StatusCode, errors.New(string(body)) //nolint:goerr113
}

// read blob
if len(body) > 0 {
err = json.Unmarshal(body, &resultPtr)
if err != nil {
return body, "", resp.StatusCode, err
}
}

return body, resp.Header.Get("Content-Type"), resp.StatusCode, err
}
53 changes: 22 additions & 31 deletions pkg/common/http_client_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package common_test

import (
"context"
"crypto/x509"
"os"
"path"
"testing"

ispec "github.com/opencontainers/image-spec/specs-go/v1"
. "github.com/smartystreets/goconvey/convey"

"zotregistry.dev/zot/pkg/api"
"zotregistry.dev/zot/pkg/api/config"
"zotregistry.dev/zot/pkg/common"
"zotregistry.dev/zot/pkg/log"
test "zotregistry.dev/zot/pkg/test/common"
)

Expand Down Expand Up @@ -54,30 +49,26 @@ func TestHTTPClient(t *testing.T) {
_, err = common.CreateHTTPClient(true, "localhost", tempDir)
So(err, ShouldNotBeNil)
})

Convey("test MakeHTTPGetRequest() no permissions on key", t, func() {
port := test.GetFreePort()
baseURL := test.GetBaseURL(port)

conf := config.New()
conf.HTTP.Port = port

ctlr := api.NewController(conf)
tempDir := t.TempDir()
err := test.CopyTestKeysAndCerts(tempDir)
So(err, ShouldBeNil)
ctlr.Config.Storage.RootDirectory = tempDir

cm := test.NewControllerManager(ctlr)
cm.StartServer()
defer cm.StopServer()
test.WaitTillServerReady(baseURL)

var resultPtr interface{}
httpClient, err := common.CreateHTTPClient(true, "localhost", tempDir)
So(err, ShouldBeNil)
_, _, _, err = common.MakeHTTPGetRequest(context.Background(), httpClient, "", "",
resultPtr, baseURL+"/v2/", ispec.MediaTypeImageManifest, log.NewLogger("", ""))
So(err, ShouldBeNil)
})
}

// Convey("test MakeHTTPGetRequest() no permissions on key", t, func() {
// port := test.GetFreePort()
// baseURL := test.GetBaseURL(port)
// conf := config.New()
// conf.HTTP.Port = port
// ctlr := api.NewController(conf)
// tempDir := t.TempDir()
// err := test.CopyTestKeysAndCerts(tempDir)
// So(err, ShouldBeNil)
// ctlr.Config.Storage.RootDirectory = tempDir
// cm := test.NewControllerManager(ctlr)
// cm.StartServer()
// defer cm.StopServer()
// test.WaitTillServerReady(baseURL)
// var resultPtr interface{}
// httpClient, err := common.CreateHTTPClient(true, "localhost", tempDir)
// So(err, ShouldBeNil)
// _, _, _, err = common.MakeHTTPGetRequest(context.Background(), httpClient, "", "",
// resultPtr, baseURL+"/v2/", ispec.MediaTypeImageManifest, log.NewLogger("", ""))
// So(err, ShouldBeNil)
// }).
Loading

0 comments on commit 94f6d12

Please sign in to comment.