2016-08-29 14:45:29 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-08-29 14:45:29 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2019-01-28 08:30:31 -05:00
|
|
|
"github.com/docker/cli/cli/streams"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/trust"
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-08-29 14:45:29 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-09-26 12:53:21 -04:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2016-08-29 14:45:29 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"github.com/docker/docker/registry"
|
2022-03-04 08:43:34 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-08-07 05:52:40 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-10-30 12:21:41 -04:00
|
|
|
"github.com/theupdateframework/notary/client"
|
|
|
|
"github.com/theupdateframework/notary/tuf/data"
|
2016-08-29 14:45:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type target struct {
|
2016-12-06 14:27:27 -05:00
|
|
|
name string
|
|
|
|
digest digest.Digest
|
|
|
|
size int64
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:42:21 -04:00
|
|
|
// TrustedPush handles content trust pushing of an image
|
implement docker push -a/--all-tags
The `docker push` command up until [v0.9.1](https://github.com/moby/moby/blob/v0.9.1/api/client.go#L998)
always pushed all tags of a given image, so `docker push foo/bar` would push (e.g.)
all of `foo/bar:latest`, `foo:/bar:v1`, `foo/bar:v1.0.0`.
Pushing all tags of an image was not desirable in many case, so docker v0.10.0
enhanced `docker push` to optionally specify a tag to push (`docker push foo/bar:v1`)
(see https://github.com/moby/moby/issues/3411 and the pull request that implemented
this: https://github.com/moby/moby/pull/4948).
This behavior exists up until today, and is confusing, because unlike other commands,
`docker push` does not default to use the `:latest` tag when omitted, but instead
makes it push "all tags of the image"
For example, in the following situation;
```
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
thajeztah/myimage latest b534869c81f0 41 hours ago 1.22MB
```
Running `docker push thajeztah/myimage` seemingly does the expected behavior (it
pushes `thajeztah/myimage:latest` to Docker Hub), however, it does not so for the
reason expected (`:latest` being the default tag), but because `:latest` happens
to be the only tag present for the `thajeztah/myimage` image.
If another tag exists for the image:
```
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
thajeztah/myimage latest b534869c81f0 41 hours ago 1.22MB
thajeztah/myimage v1.0.0 b534869c81f0 41 hours ago 1.22MB
```
Running the same command (`docker push thajeztah/myimage`) will push _both_ images
to Docker Hub.
> Note that the behavior described above is currently not (clearly) documented;
> the `docker push` reference documentation (https://docs.docker.com/engine/reference/commandline/push/)
does not mention that omitting the tag will push all tags
This patch changes the default behavior, and if no tag is specified, `:latest` is
assumed. To push _all_ tags, a new flag (`-a` / `--all-tags`) is added, similar
to the flag that's present on `docker pull`.
With this change:
- `docker push myname/myimage` will be the equivalent of `docker push myname/myimage:latest`
- to push all images, the user needs to set a flag (`--all-tags`), so `docker push --all-tags myname/myimage:latest`
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-09 08:48:42 -05:00
|
|
|
func TrustedPush(ctx context.Context, cli command.Cli, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig types.AuthConfig, options types.ImagePushOptions) error {
|
|
|
|
responseBody, err := cli.Client().ImagePush(ctx, reference.FamiliarString(ref), options)
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
2016-12-27 15:51:00 -05:00
|
|
|
return PushTrustedReference(cli, repoInfo, ref, authConfig, responseBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushTrustedReference pushes a canonical reference to the trust server.
|
2017-05-02 16:14:43 -04:00
|
|
|
// nolint: gocyclo
|
2017-05-03 17:58:52 -04:00
|
|
|
func PushTrustedReference(streams command.Streams, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig types.AuthConfig, in io.Reader) error {
|
2016-08-29 14:45:29 -04:00
|
|
|
// If it is a trusted push we would like to find the target entry which match the
|
|
|
|
// tag provided in the function and then do an AddTarget later.
|
|
|
|
target := &client.Target{}
|
|
|
|
// Count the times of calling for handleTarget,
|
|
|
|
// if it is called more that once, that should be considered an error in a trusted push.
|
|
|
|
cnt := 0
|
2018-04-19 13:07:27 -04:00
|
|
|
handleTarget := func(msg jsonmessage.JSONMessage) {
|
2016-08-29 14:45:29 -04:00
|
|
|
cnt++
|
|
|
|
if cnt > 1 {
|
2018-05-17 11:09:10 -04:00
|
|
|
// handleTarget should only be called once. This will be treated as an error.
|
2016-08-29 14:45:29 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-22 14:44:09 -05:00
|
|
|
var pushResult types.PushResult
|
2018-04-19 13:07:27 -04:00
|
|
|
err := json.Unmarshal(*msg.Aux, &pushResult)
|
2016-12-22 14:44:09 -05:00
|
|
|
if err == nil && pushResult.Tag != "" {
|
2017-01-06 20:23:18 -05:00
|
|
|
if dgst, err := digest.Parse(pushResult.Digest); err == nil {
|
2016-12-22 14:44:09 -05:00
|
|
|
h, err := hex.DecodeString(dgst.Hex())
|
|
|
|
if err != nil {
|
|
|
|
target = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
target.Name = pushResult.Tag
|
|
|
|
target.Hashes = data.Hashes{string(dgst.Algorithm()): h}
|
|
|
|
target.Length = int64(pushResult.Size)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var tag string
|
|
|
|
switch x := ref.(type) {
|
|
|
|
case reference.Canonical:
|
|
|
|
return errors.New("cannot push a digest reference")
|
|
|
|
case reference.NamedTagged:
|
|
|
|
tag = x.Tag()
|
2016-12-06 14:27:27 -05:00
|
|
|
default:
|
|
|
|
// We want trust signatures to always take an explicit tag,
|
|
|
|
// otherwise it will act as an untrusted push.
|
2017-05-03 17:58:52 -04:00
|
|
|
if err := jsonmessage.DisplayJSONMessagesToStream(in, streams.Out(), nil); err != nil {
|
2016-08-29 14:45:29 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-10-23 13:41:52 -04:00
|
|
|
fmt.Fprintln(streams.Err(), "No tag specified, skipping trust metadata push")
|
2016-08-29 14:45:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
if err := jsonmessage.DisplayJSONMessagesToStream(in, streams.Out(), handleTarget); err != nil {
|
2016-08-29 14:45:29 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cnt > 1 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("internal error: only one call to handleTarget expected")
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if target == nil {
|
2017-10-23 13:41:52 -04:00
|
|
|
return errors.Errorf("no targets found, please provide a specific tag in order to sign it")
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
fmt.Fprintln(streams.Out(), "Signing and pushing trust metadata")
|
2016-08-29 14:45:29 -04:00
|
|
|
|
2017-09-12 12:39:13 -04:00
|
|
|
repo, err := trust.GetNotaryRepository(streams.In(), streams.Out(), command.UserAgent(), repoInfo, &authConfig, "push", "pull")
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
2017-10-23 13:41:52 -04:00
|
|
|
return errors.Wrap(err, "error establishing connection to trust repository")
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the latest repository metadata so we can figure out which roles to sign
|
2017-09-11 17:07:00 -04:00
|
|
|
_, err = repo.ListTargets()
|
2016-08-29 14:45:29 -04:00
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case client.ErrRepoNotInitialized, client.ErrRepositoryNotExist:
|
2017-09-11 17:07:00 -04:00
|
|
|
keys := repo.GetCryptoService().ListKeys(data.CanonicalRootRole)
|
2016-08-29 14:45:29 -04:00
|
|
|
var rootKeyID string
|
|
|
|
// always select the first root key
|
|
|
|
if len(keys) > 0 {
|
|
|
|
sort.Strings(keys)
|
|
|
|
rootKeyID = keys[0]
|
|
|
|
} else {
|
2017-09-11 17:07:00 -04:00
|
|
|
rootPublicKey, err := repo.GetCryptoService().Create(data.CanonicalRootRole, "", data.ECDSAKey)
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rootKeyID = rootPublicKey.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the notary repository with a remotely managed snapshot key
|
2016-09-28 15:49:47 -04:00
|
|
|
if err := repo.Initialize([]string{rootKeyID}, data.CanonicalSnapshotRole); err != nil {
|
2017-01-25 19:54:18 -05:00
|
|
|
return trust.NotaryError(repoInfo.Name.Name(), err)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
2017-05-03 17:58:52 -04:00
|
|
|
fmt.Fprintf(streams.Out(), "Finished initializing %q\n", repoInfo.Name.Name())
|
2016-08-29 14:45:29 -04:00
|
|
|
err = repo.AddTarget(target, data.CanonicalTargetsRole)
|
|
|
|
case nil:
|
|
|
|
// already initialized and we have successfully downloaded the latest metadata
|
2017-08-24 18:42:21 -04:00
|
|
|
err = AddTargetToAllSignableRoles(repo, target)
|
2016-08-29 14:45:29 -04:00
|
|
|
default:
|
2017-01-25 19:54:18 -05:00
|
|
|
return trust.NotaryError(repoInfo.Name.Name(), err)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = repo.Publish()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-23 13:41:52 -04:00
|
|
|
err = errors.Wrapf(err, "failed to sign %s:%s", repoInfo.Name.Name(), tag)
|
2017-01-25 19:54:18 -05:00
|
|
|
return trust.NotaryError(repoInfo.Name.Name(), err)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 13:22:10 -04:00
|
|
|
fmt.Fprintf(streams.Out(), "Successfully signed %s:%s\n", repoInfo.Name.Name(), tag)
|
2016-08-29 14:45:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 18:42:21 -04:00
|
|
|
// AddTargetToAllSignableRoles attempts to add the image target to all the top level delegation roles we can
|
|
|
|
// (based on whether we have the signing key and whether the role's path allows
|
|
|
|
// us to).
|
|
|
|
// If there are no delegation roles, we add to the targets role.
|
2017-09-11 17:07:00 -04:00
|
|
|
func AddTargetToAllSignableRoles(repo client.Repository, target *client.Target) error {
|
2017-08-25 17:49:40 -04:00
|
|
|
signableRoles, err := trust.GetSignableRoles(repo, target)
|
2017-08-24 18:42:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return repo.AddTarget(target, signableRoles...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// trustedPull handles content trust pulling of an image
|
2018-12-19 07:48:41 -05:00
|
|
|
func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, opts PullOptions) error {
|
2017-09-26 12:53:21 -04:00
|
|
|
refs, err := getTrustedPullTargets(cli, imgRefAndAuth)
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
ref := imgRefAndAuth.Reference()
|
2016-08-29 14:45:29 -04:00
|
|
|
for i, r := range refs {
|
2016-12-06 14:27:27 -05:00
|
|
|
displayTag := r.name
|
2016-08-29 14:45:29 -04:00
|
|
|
if displayTag != "" {
|
|
|
|
displayTag = ":" + displayTag
|
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
fmt.Fprintf(cli.Out(), "Pull (%d of %d): %s%s@%s\n", i+1, len(refs), reference.FamiliarName(ref), displayTag, r.digest)
|
2016-08-29 14:45:29 -04:00
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
trustedRef, err := reference.WithDigest(reference.TrimNamed(ref), r.digest)
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-06 05:15:18 -05:00
|
|
|
updatedImgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, nil, AuthResolver(cli), trustedRef.String())
|
2017-10-06 17:46:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-19 07:48:41 -05:00
|
|
|
if err := imagePullPrivileged(ctx, cli, updatedImgRefAndAuth, PullOptions{
|
|
|
|
all: false,
|
|
|
|
platform: opts.platform,
|
|
|
|
quiet: opts.quiet,
|
|
|
|
remote: opts.remote,
|
|
|
|
}); err != nil {
|
2016-08-29 14:45:29 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
tagged, err := reference.WithTag(reference.TrimNamed(ref), r.name)
|
2016-12-06 14:27:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
|
2016-12-06 14:27:27 -05:00
|
|
|
if err := TagTrusted(ctx, cli, trustedRef, tagged); err != nil {
|
|
|
|
return err
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
func getTrustedPullTargets(cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth) ([]target, error) {
|
|
|
|
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, trust.ActionsPullOnly)
|
|
|
|
if err != nil {
|
2017-10-23 13:41:52 -04:00
|
|
|
return nil, errors.Wrap(err, "error establishing connection to trust repository")
|
2017-09-26 12:53:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ref := imgRefAndAuth.Reference()
|
|
|
|
tagged, isTagged := ref.(reference.NamedTagged)
|
|
|
|
if !isTagged {
|
|
|
|
// List all targets
|
|
|
|
targets, err := notaryRepo.ListTargets(trust.ReleasesRole, data.CanonicalTargetsRole)
|
|
|
|
if err != nil {
|
|
|
|
return nil, trust.NotaryError(ref.Name(), err)
|
|
|
|
}
|
|
|
|
var refs []target
|
|
|
|
for _, tgt := range targets {
|
|
|
|
t, err := convertTarget(tgt.Target)
|
|
|
|
if err != nil {
|
2017-10-23 13:41:52 -04:00
|
|
|
fmt.Fprintf(cli.Err(), "Skipping target for %q\n", reference.FamiliarName(ref))
|
2017-09-26 12:53:21 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Only list tags in the top level targets role or the releases delegation role - ignore
|
|
|
|
// all other delegation roles
|
|
|
|
if tgt.Role != trust.ReleasesRole && tgt.Role != data.CanonicalTargetsRole {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
refs = append(refs, t)
|
|
|
|
}
|
|
|
|
if len(refs) == 0 {
|
|
|
|
return nil, trust.NotaryError(ref.Name(), errors.Errorf("No trusted tags for %s", ref.Name()))
|
|
|
|
}
|
|
|
|
return refs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := notaryRepo.GetTargetByName(tagged.Tag(), trust.ReleasesRole, data.CanonicalTargetsRole)
|
|
|
|
if err != nil {
|
|
|
|
return nil, trust.NotaryError(ref.Name(), err)
|
|
|
|
}
|
|
|
|
// Only get the tag if it's in the top level targets role or the releases delegation role
|
|
|
|
// ignore it if it's in any other delegation roles
|
|
|
|
if t.Role != trust.ReleasesRole && t.Role != data.CanonicalTargetsRole {
|
|
|
|
return nil, trust.NotaryError(ref.Name(), errors.Errorf("No trust data for %s", tagged.Tag()))
|
|
|
|
}
|
|
|
|
|
2017-10-23 13:41:52 -04:00
|
|
|
logrus.Debugf("retrieving target for %s role", t.Role)
|
2017-09-26 12:53:21 -04:00
|
|
|
r, err := convertTarget(t.Target)
|
|
|
|
return []target{r}, err
|
|
|
|
}
|
|
|
|
|
2016-08-29 14:45:29 -04:00
|
|
|
// imagePullPrivileged pulls the image and displays it to the output
|
2018-12-19 07:48:41 -05:00
|
|
|
func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, opts PullOptions) error {
|
2017-09-26 12:53:21 -04:00
|
|
|
ref := reference.FamiliarString(imgRefAndAuth.Reference())
|
2016-08-29 14:45:29 -04:00
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
encodedAuth, err := command.EncodeAuthToBase64(*imgRefAndAuth.AuthConfig())
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-26 12:53:21 -04:00
|
|
|
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(cli, imgRefAndAuth.RepoInfo().Index, "pull")
|
2016-08-29 14:45:29 -04:00
|
|
|
options := types.ImagePullOptions{
|
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
PrivilegeFunc: requestPrivilege,
|
2018-12-19 07:48:41 -05:00
|
|
|
All: opts.all,
|
|
|
|
Platform: opts.platform,
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
responseBody, err := cli.Client().ImagePull(ctx, ref, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
2018-12-19 07:48:41 -05:00
|
|
|
out := cli.Out()
|
|
|
|
if opts.quiet {
|
2022-02-25 07:10:34 -05:00
|
|
|
out = streams.NewOut(io.Discard)
|
2018-12-19 07:48:41 -05:00
|
|
|
}
|
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, out, nil)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TrustedReference returns the canonical trusted reference for an image reference
|
2017-03-30 20:21:14 -04:00
|
|
|
func TrustedReference(ctx context.Context, cli command.Cli, ref reference.NamedTagged, rs registry.Service) (reference.Canonical, error) {
|
2018-03-06 05:15:18 -05:00
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, rs, AuthResolver(cli), ref.String())
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, []string{"pull"})
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
2017-10-23 13:41:52 -04:00
|
|
|
return nil, errors.Wrap(err, "error establishing connection to trust repository")
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
2016-12-05 19:06:29 -05:00
|
|
|
t, err := notaryRepo.GetTargetByName(ref.Tag(), trust.ReleasesRole, data.CanonicalTargetsRole)
|
2016-08-29 14:45:29 -04:00
|
|
|
if err != nil {
|
2018-03-06 05:15:18 -05:00
|
|
|
return nil, trust.NotaryError(imgRefAndAuth.RepoInfo().Name.Name(), err)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
// Only list tags in the top level targets role or the releases delegation role - ignore
|
|
|
|
// all other delegation roles
|
2016-12-05 19:06:29 -05:00
|
|
|
if t.Role != trust.ReleasesRole && t.Role != data.CanonicalTargetsRole {
|
2018-03-06 05:15:18 -05:00
|
|
|
return nil, trust.NotaryError(imgRefAndAuth.RepoInfo().Name.Name(), client.ErrNoSuchTarget(ref.Tag()))
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
r, err := convertTarget(t.Target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
2016-11-10 18:59:02 -05:00
|
|
|
return reference.WithDigest(reference.TrimNamed(ref), r.digest)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func convertTarget(t client.Target) (target, error) {
|
|
|
|
h, ok := t.Hashes["sha256"]
|
|
|
|
if !ok {
|
|
|
|
return target{}, errors.New("no valid hash, expecting sha256")
|
|
|
|
}
|
|
|
|
return target{
|
2016-12-06 14:27:27 -05:00
|
|
|
name: t.Name,
|
|
|
|
digest: digest.NewDigestFromHex("sha256", hex.EncodeToString(h)),
|
|
|
|
size: t.Length,
|
2016-08-29 14:45:29 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TagTrusted tags a trusted ref
|
2017-03-30 20:21:14 -04:00
|
|
|
func TagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canonical, ref reference.NamedTagged) error {
|
2017-01-11 16:54:52 -05:00
|
|
|
// Use familiar references when interacting with client and output
|
|
|
|
familiarRef := reference.FamiliarString(ref)
|
|
|
|
trustedFamiliarRef := reference.FamiliarString(trustedRef)
|
|
|
|
|
2017-10-23 13:41:52 -04:00
|
|
|
fmt.Fprintf(cli.Err(), "Tagging %s as %s\n", trustedFamiliarRef, familiarRef)
|
2016-08-29 14:45:29 -04:00
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
return cli.Client().ImageTag(ctx, trustedFamiliarRef, familiarRef)
|
2016-08-29 14:45:29 -04:00
|
|
|
}
|
2017-09-26 12:53:21 -04:00
|
|
|
|
|
|
|
// AuthResolver returns an auth resolver function from a command.Cli
|
|
|
|
func AuthResolver(cli command.Cli) func(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
|
|
|
|
return func(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
|
|
|
|
return command.ResolveAuthConfig(ctx, cli, index)
|
|
|
|
}
|
|
|
|
}
|