Skip to content
Closed
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
39 changes: 20 additions & 19 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,18 @@ type authScope struct {
actions string
}

// sendAuth determines whether we need authentication for v2 or v1 endpoint.
type sendAuth int
// SendAuth determines whether we need authentication for v2 or v1 endpoint.
type SendAuth int

const (
// v2 endpoint with authentication.
v2Auth sendAuth = iota
// V2Auth is v2 endpoint with authentication.
V2Auth SendAuth = iota
// v1 endpoint with authentication.
// TODO: Get v1Auth working
// v1Auth
// no authentication, works for both v1 and v2.
noAuth

// NoAuth is no authentication, works for both v1 and v2.
NoAuth
)

func newBearerTokenFromJSONBlob(blob []byte) (*bearerToken, error) {
Expand Down Expand Up @@ -182,9 +183,9 @@ func dockerCertDir(sys *types.SystemContext, hostPort string) (string, error) {
return fullCertDirPath, nil
}

// newDockerClientFromRef returns a new dockerClient instance for refHostname (a host a specified in the Docker image reference, not canonicalized to dockerRegistry)
// NewDockerClientFromRef returns a new dockerClient instance for refHostname (a host a specified in the Docker image reference, not canonicalized to dockerRegistry)
// “write” specifies whether the client will be used for "write" access (in particular passed to lookaside.go:toplevelFromSection)
func newDockerClientFromRef(sys *types.SystemContext, ref dockerReference, write bool, actions string) (*dockerClient, error) {
func NewDockerClientFromRef(sys *types.SystemContext, ref DockerReference, write bool, actions string) (*dockerClient, error) {
registry := reference.Domain(ref.ref)
username, password, err := config.GetAuthentication(sys, reference.Domain(ref.ref))
if err != nil {
Expand Down Expand Up @@ -247,7 +248,7 @@ func CheckAuth(ctx context.Context, sys *types.SystemContext, username, password
return errors.Wrapf(err, "error creating new docker client")
}

resp, err := newLoginClient.makeRequest(ctx, "GET", "/v2/", nil, nil, v2Auth)
resp, err := newLoginClient.MakeRequest(ctx, "GET", "/v2/", nil, nil, V2Auth)
if err != nil {
return err
}
Expand Down Expand Up @@ -323,7 +324,7 @@ func SearchRegistry(ctx context.Context, sys *types.SystemContext, registry, ima
u.RawQuery = q.Encode()

logrus.Debugf("trying to talk to v1 search endpoint\n")
resp, err := client.makeRequest(ctx, "GET", u.String(), nil, nil, noAuth)
resp, err := client.MakeRequest(ctx, "GET", u.String(), nil, nil, NoAuth)
if err != nil {
logrus.Debugf("error getting search results from v1 endpoint %q: %v", registry, err)
} else {
Expand All @@ -340,7 +341,7 @@ func SearchRegistry(ctx context.Context, sys *types.SystemContext, registry, ima
}

logrus.Debugf("trying to talk to v2 search endpoint\n")
resp, err := client.makeRequest(ctx, "GET", "/v2/_catalog", nil, nil, v2Auth)
resp, err := client.MakeRequest(ctx, "GET", "/v2/_catalog", nil, nil, V2Auth)
if err != nil {
logrus.Debugf("error getting search results from v2 endpoint %q: %v", registry, err)
} else {
Expand All @@ -367,9 +368,9 @@ func SearchRegistry(ctx context.Context, sys *types.SystemContext, registry, ima
return nil, errors.Wrapf(err, "couldn't search registry %q", registry)
}

// makeRequest creates and executes a http.Request with the specified parameters, adding authentication and TLS options for the Docker client.
// MakeRequest creates and executes a http.Request with the specified parameters, adding authentication and TLS options for the Docker client.
// The host name and schema is taken from the client or autodetected, and the path is relative to it, i.e. the path usually starts with /v2/.
func (c *dockerClient) makeRequest(ctx context.Context, method, path string, headers map[string][]string, stream io.Reader, auth sendAuth) (*http.Response, error) {
func (c *dockerClient) MakeRequest(ctx context.Context, method, path string, headers map[string][]string, stream io.Reader, auth SendAuth) (*http.Response, error) {
if err := c.detectProperties(ctx); err != nil {
return nil, err
}
Expand All @@ -382,7 +383,7 @@ func (c *dockerClient) makeRequest(ctx context.Context, method, path string, hea
// streamLen, if not -1, specifies the length of the data expected on stream.
// makeRequest should generally be preferred.
// TODO(runcom): too many arguments here, use a struct
func (c *dockerClient) makeRequestToResolvedURL(ctx context.Context, method, url string, headers map[string][]string, stream io.Reader, streamLen int64, auth sendAuth) (*http.Response, error) {
func (c *dockerClient) makeRequestToResolvedURL(ctx context.Context, method, url string, headers map[string][]string, stream io.Reader, streamLen int64, auth SendAuth) (*http.Response, error) {
req, err := http.NewRequest(method, url, stream)
if err != nil {
return nil, err
Expand All @@ -400,7 +401,7 @@ func (c *dockerClient) makeRequestToResolvedURL(ctx context.Context, method, url
if c.sys != nil && c.sys.DockerRegistryUserAgent != "" {
req.Header.Add("User-Agent", c.sys.DockerRegistryUserAgent)
}
if auth == v2Auth {
if auth == V2Auth {
if err := c.setupRequestAuth(req); err != nil {
return nil, err
}
Expand Down Expand Up @@ -514,7 +515,7 @@ func (c *dockerClient) detectProperties(ctx context.Context) error {

ping := func(scheme string) error {
url := fmt.Sprintf(resolvedPingV2URL, scheme, c.registry)
resp, err := c.makeRequestToResolvedURL(ctx, "GET", url, nil, nil, -1, noAuth)
resp, err := c.makeRequestToResolvedURL(ctx, "GET", url, nil, nil, -1, NoAuth)
if err != nil {
logrus.Debugf("Ping %s err %s (%#v)", url, err.Error(), err)
return err
Expand All @@ -541,7 +542,7 @@ func (c *dockerClient) detectProperties(ctx context.Context) error {
// best effort to understand if we're talking to a V1 registry
pingV1 := func(scheme string) bool {
url := fmt.Sprintf(resolvedPingV1URL, scheme, c.registry)
resp, err := c.makeRequestToResolvedURL(ctx, "GET", url, nil, nil, -1, noAuth)
resp, err := c.makeRequestToResolvedURL(ctx, "GET", url, nil, nil, -1, NoAuth)
if err != nil {
logrus.Debugf("Ping %s err %s (%#v)", url, err.Error(), err)
return false
Expand All @@ -566,9 +567,9 @@ func (c *dockerClient) detectProperties(ctx context.Context) error {

// getExtensionsSignatures returns signatures from the X-Registry-Supports-Signatures API extension,
// using the original data structures.
func (c *dockerClient) getExtensionsSignatures(ctx context.Context, ref dockerReference, manifestDigest digest.Digest) (*extensionSignatureList, error) {
func (c *dockerClient) getExtensionsSignatures(ctx context.Context, ref DockerReference, manifestDigest digest.Digest) (*extensionSignatureList, error) {
path := fmt.Sprintf(extensionsSignaturePath, reference.Path(ref.ref), manifestDigest)
res, err := c.makeRequest(ctx, "GET", path, nil, nil, v2Auth)
res, err := c.MakeRequest(ctx, "GET", path, nil, nil, V2Auth)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions docker/docker_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Image struct {
// newImage returns a new Image interface type after setting up
// a client to the registry hosting the given image.
// The caller must call .Close() on the returned Image.
func newImage(ctx context.Context, sys *types.SystemContext, ref dockerReference) (types.ImageCloser, error) {
func newImage(ctx context.Context, sys *types.SystemContext, ref DockerReference) (types.ImageCloser, error) {
s, err := newImageSource(sys, ref)
if err != nil {
return nil, err
Expand Down Expand Up @@ -52,21 +52,21 @@ func (i *Image) GetRepositoryTags(ctx context.Context) ([]string, error) {
// GetRepositoryTags list all tags available in the repository. The tag
// provided inside the ImageReference will be ignored.
func GetRepositoryTags(ctx context.Context, sys *types.SystemContext, ref types.ImageReference) ([]string, error) {
dr, ok := ref.(dockerReference)
dr, ok := ref.(DockerReference)
if !ok {
return nil, errors.Errorf("ref must be a dockerReference")
return nil, errors.Errorf("ref must be a DockerReference")
}

path := fmt.Sprintf(tagsPath, reference.Path(dr.ref))
client, err := newDockerClientFromRef(sys, dr, false, "pull")
client, err := NewDockerClientFromRef(sys, dr, false, "pull")
if err != nil {
return nil, errors.Wrap(err, "failed to create client")
}

tags := make([]string, 0)

for {
res, err := client.makeRequest(ctx, "GET", path, nil, nil, v2Auth)
res, err := client.MakeRequest(ctx, "GET", path, nil, nil, V2Auth)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions docker/docker_image_dest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import (
)

type dockerImageDestination struct {
ref dockerReference
ref DockerReference
c *dockerClient
// State
manifestDigest digest.Digest // or "" if not yet known.
}

// newImageDestination creates a new ImageDestination for the specified image reference.
func newImageDestination(sys *types.SystemContext, ref dockerReference) (types.ImageDestination, error) {
c, err := newDockerClientFromRef(sys, ref, true, "pull,push")
func newImageDestination(sys *types.SystemContext, ref DockerReference) (types.ImageDestination, error) {
c, err := NewDockerClientFromRef(sys, ref, true, "pull,push")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (d *dockerImageDestination) PutBlob(ctx context.Context, stream io.Reader,
// FIXME? Chunked upload, progress reporting, etc.
uploadPath := fmt.Sprintf(blobUploadPath, reference.Path(d.ref.ref))
logrus.Debugf("Uploading %s", uploadPath)
res, err := d.c.makeRequest(ctx, "POST", uploadPath, nil, nil, v2Auth)
res, err := d.c.MakeRequest(ctx, "POST", uploadPath, nil, nil, V2Auth)
if err != nil {
return types.BlobInfo{}, err
}
Expand All @@ -147,7 +147,7 @@ func (d *dockerImageDestination) PutBlob(ctx context.Context, stream io.Reader,
digester := digest.Canonical.Digester()
sizeCounter := &sizeCounter{}
tee := io.TeeReader(stream, io.MultiWriter(digester.Hash(), sizeCounter))
res, err = d.c.makeRequestToResolvedURL(ctx, "PATCH", uploadLocation.String(), map[string][]string{"Content-Type": {"application/octet-stream"}}, tee, inputInfo.Size, v2Auth)
res, err = d.c.makeRequestToResolvedURL(ctx, "PATCH", uploadLocation.String(), map[string][]string{"Content-Type": {"application/octet-stream"}}, tee, inputInfo.Size, V2Auth)
if err != nil {
logrus.Debugf("Error uploading layer chunked, response %#v", res)
return types.BlobInfo{}, err
Expand All @@ -166,7 +166,7 @@ func (d *dockerImageDestination) PutBlob(ctx context.Context, stream io.Reader,
// TODO: check inputInfo.Digest == computedDigest https://github.com/containers/image/pull/70#discussion_r77646717
locationQuery.Set("digest", computedDigest.String())
uploadLocation.RawQuery = locationQuery.Encode()
res, err = d.c.makeRequestToResolvedURL(ctx, "PUT", uploadLocation.String(), map[string][]string{"Content-Type": {"application/octet-stream"}}, nil, -1, v2Auth)
res, err = d.c.makeRequestToResolvedURL(ctx, "PUT", uploadLocation.String(), map[string][]string{"Content-Type": {"application/octet-stream"}}, nil, -1, V2Auth)
if err != nil {
return types.BlobInfo{}, err
}
Expand All @@ -191,7 +191,7 @@ func (d *dockerImageDestination) HasBlob(ctx context.Context, info types.BlobInf
checkPath := fmt.Sprintf(blobsPath, reference.Path(d.ref.ref), info.Digest.String())

logrus.Debugf("Checking %s", checkPath)
res, err := d.c.makeRequest(ctx, "HEAD", checkPath, nil, nil, v2Auth)
res, err := d.c.MakeRequest(ctx, "HEAD", checkPath, nil, nil, V2Auth)
if err != nil {
return false, -1, err
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (d *dockerImageDestination) PutManifest(ctx context.Context, m []byte) erro
}
d.manifestDigest = digest

refTail, err := d.ref.tagOrDigest()
refTail, err := d.ref.TagOrDigest()
if err != nil {
return err
}
Expand All @@ -237,7 +237,7 @@ func (d *dockerImageDestination) PutManifest(ctx context.Context, m []byte) erro
if mimeType != "" {
headers["Content-Type"] = []string{mimeType}
}
res, err := d.c.makeRequest(ctx, "PUT", path, headers, bytes.NewReader(m), v2Auth)
res, err := d.c.MakeRequest(ctx, "PUT", path, headers, bytes.NewReader(m), V2Auth)
if err != nil {
return err
}
Expand Down Expand Up @@ -442,7 +442,7 @@ sigExists:
}

path := fmt.Sprintf(extensionsSignaturePath, reference.Path(d.ref.ref), d.manifestDigest.String())
res, err := d.c.makeRequest(ctx, "PUT", path, nil, bytes.NewReader(body), v2Auth)
res, err := d.c.MakeRequest(ctx, "PUT", path, nil, bytes.NewReader(body), V2Auth)
if err != nil {
return err
}
Expand Down
24 changes: 12 additions & 12 deletions docker/docker_image_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type dockerImageSource struct {
ref dockerReference
ref DockerReference
c *dockerClient
// State
cachedManifest []byte // nil if not loaded yet
Expand All @@ -30,8 +30,8 @@ type dockerImageSource struct {

// newImageSource creates a new ImageSource for the specified image reference.
// The caller must call .Close() on the returned ImageSource.
func newImageSource(sys *types.SystemContext, ref dockerReference) (*dockerImageSource, error) {
c, err := newDockerClientFromRef(sys, ref, false, "pull")
func newImageSource(sys *types.SystemContext, ref DockerReference) (*dockerImageSource, error) {
c, err := NewDockerClientFromRef(sys, ref, false, "pull")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (s *dockerImageSource) fetchManifest(ctx context.Context, tagOrDigest strin
path := fmt.Sprintf(manifestPath, reference.Path(s.ref.ref), tagOrDigest)
headers := make(map[string][]string)
headers["Accept"] = manifest.DefaultRequestedManifestMIMETypes
res, err := s.c.makeRequest(ctx, "GET", path, headers, nil, v2Auth)
res, err := s.c.MakeRequest(ctx, "GET", path, headers, nil, V2Auth)
if err != nil {
return nil, "", err
}
Expand All @@ -116,7 +116,7 @@ func (s *dockerImageSource) ensureManifestIsLoaded(ctx context.Context) error {
return nil
}

reference, err := s.ref.tagOrDigest()
reference, err := s.ref.TagOrDigest()
if err != nil {
return err
}
Expand All @@ -137,7 +137,7 @@ func (s *dockerImageSource) getExternalBlob(ctx context.Context, urls []string)
err error
)
for _, url := range urls {
resp, err = s.c.makeRequestToResolvedURL(ctx, "GET", url, nil, nil, -1, noAuth)
resp, err = s.c.makeRequestToResolvedURL(ctx, "GET", url, nil, nil, -1, NoAuth)
if err == nil {
if resp.StatusCode != http.StatusOK {
err = errors.Errorf("error fetching external blob from %q: %d (%s)", url, resp.StatusCode, http.StatusText(resp.StatusCode))
Expand Down Expand Up @@ -169,7 +169,7 @@ func (s *dockerImageSource) GetBlob(ctx context.Context, info types.BlobInfo) (i

path := fmt.Sprintf(blobsPath, reference.Path(s.ref.ref), info.Digest.String())
logrus.Debugf("Downloading %s", path)
res, err := s.c.makeRequest(ctx, "GET", path, nil, nil, v2Auth)
res, err := s.c.MakeRequest(ctx, "GET", path, nil, nil, V2Auth)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -309,15 +309,15 @@ func (s *dockerImageSource) getSignaturesFromAPIExtension(ctx context.Context, i
}

// deleteImage deletes the named image from the registry, if supported.
func deleteImage(ctx context.Context, sys *types.SystemContext, ref dockerReference) error {
func deleteImage(ctx context.Context, sys *types.SystemContext, ref DockerReference) error {
// docker/distribution does not document what action should be used for deleting images.
//
// Current docker/distribution requires "pull" for reading the manifest and "delete" for deleting it.
// quay.io requires "push" (an explicit "pull" is unnecessary), does not grant any token (fails parsing the request) if "delete" is included.
// OpenShift ignores the action string (both the password and the token is an OpenShift API token identifying a user).
//
// We have to hard-code a single string, luckily both docker/distribution and quay.io support "*" to mean "everything".
c, err := newDockerClientFromRef(sys, ref, true, "*")
c, err := NewDockerClientFromRef(sys, ref, true, "*")
if err != nil {
return err
}
Expand All @@ -327,12 +327,12 @@ func deleteImage(ctx context.Context, sys *types.SystemContext, ref dockerRefere
headers := make(map[string][]string)
headers["Accept"] = []string{manifest.DockerV2Schema2MediaType}

refTail, err := ref.tagOrDigest()
refTail, err := ref.TagOrDigest()
if err != nil {
return err
}
getPath := fmt.Sprintf(manifestPath, reference.Path(ref.ref), refTail)
get, err := c.makeRequest(ctx, "GET", getPath, headers, nil, v2Auth)
get, err := c.MakeRequest(ctx, "GET", getPath, headers, nil, V2Auth)
if err != nil {
return err
}
Expand All @@ -354,7 +354,7 @@ func deleteImage(ctx context.Context, sys *types.SystemContext, ref dockerRefere

// When retrieving the digest from a registry >= 2.3 use the following header:
// "Accept": "application/vnd.docker.distribution.manifest.v2+json"
delete, err := c.makeRequest(ctx, "DELETE", deletePath, headers, nil, v2Auth)
delete, err := c.MakeRequest(ctx, "DELETE", deletePath, headers, nil, V2Auth)
if err != nil {
return err
}
Expand Down
Loading