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

docker auth config support #186

Merged
merged 1 commit into from
Dec 19, 2023
Merged
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
38 changes: 26 additions & 12 deletions internal/extractor/image/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/containers/image/v5/copy"
"github.com/containers/image/v5/docker"
"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/pkg/docker/config"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/transports/alltransports"
"github.com/containers/image/v5/types"
Expand All @@ -20,22 +22,33 @@ import (
)

func (c *Client) cacheSource(src string) ([]byte, string, error) {
srcCtx, srcObj, err := c.srcCtx(src, c.cli.Insecure)
if err != nil {
return nil, "", errors.Wrap(err, "cannot create source context")
}
srcObj := NewSource(src)
srcRef, err := srcObj.Reference()
if err != nil {
return nil, "", errors.Wrapf(err, "cannot parse reference '%s'", srcObj.String())
}

var cacheDigest string
switch srcObj.Scheme() {
case "docker":
dockerRef, err := image.DockerReference(strings.TrimPrefix(src, "docker://"))
var dockerRef types.ImageReference
var dockerAuth types.DockerAuthConfig
if srcObj.Scheme() == "docker" {
dockerRef, err = image.DockerReference(strings.TrimPrefix(src, "docker://"))
if err != nil {
return nil, "", errors.Wrap(err, "cannot parse docker reference")
}
dockerAuth, err = config.GetCredentials(nil, reference.Domain(dockerRef.DockerReference()))
if err != nil {
c.logger.Warn().Msg("cannot retrieve Docker credentials")
}
}

srcCtx, err := c.srcCtx(&dockerAuth, c.cli.Insecure)
if err != nil {
return nil, "", errors.Wrap(err, "cannot create source context")
}

var cacheDigest string
switch srcObj.Scheme() {
case "docker":
if dgst, err := docker.GetDigest(c.ctx, srcCtx, dockerRef); err == nil {
cacheDigest = srcObj.Scheme() + "-" + dgst.Encoded()
} else {
Expand Down Expand Up @@ -92,8 +105,9 @@ func (c *Client) cacheSource(src string) ([]byte, string, error) {
return manblob, cachedir, err
}

func (c *Client) srcCtx(name string, insecure bool) (*types.SystemContext, *Source, error) {
func (c *Client) srcCtx(auth *types.DockerAuthConfig, insecure bool) (*types.SystemContext, error) {
sysCtx := &types.SystemContext{
DockerAuthConfig: auth,
DockerDaemonInsecureSkipTLSVerify: insecure,
DockerInsecureSkipTLSVerify: types.NewOptionalBool(insecure),
DockerRegistryUserAgent: c.meta.UserAgent,
Expand All @@ -102,13 +116,13 @@ func (c *Client) srcCtx(name string, insecure bool) (*types.SystemContext, *Sour
VariantChoice: c.platform.Variant,
BlobInfoCacheDir: filepath.Join(c.cli.CacheDir, "blobs"),
}
return sysCtx, NewSource(name), nil
return sysCtx, nil
}

func (c *Client) dstCtx(cacheDir string) (*types.SystemContext, error) {
func (c *Client) dstCtx(_ string) (*types.SystemContext, error) {
return &types.SystemContext{
DirForceDecompress: true,
BlobInfoCacheDir: filepath.Join(cacheDir, "blobs"),
BlobInfoCacheDir: filepath.Join(c.cli.CacheDir, "blobs"),
}, nil
}

Expand Down