2016-12-05 20:02:26 -05:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
|
2023-08-30 18:36:58 -04:00
|
|
|
"github.com/distribution/reference"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/trust"
|
2016-12-05 20:02:26 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/registry"
|
2017-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-12-05 20:02:26 -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/tuf/data"
|
2016-12-05 20:02:26 -05:00
|
|
|
)
|
|
|
|
|
2017-05-11 05:07:35 -04:00
|
|
|
func resolveServiceImageDigestContentTrust(dockerCli command.Cli, service *swarm.ServiceSpec) error {
|
2018-03-08 14:56:56 -05:00
|
|
|
if !dockerCli.ContentTrustEnabled() {
|
2017-05-11 05:07:35 -04:00
|
|
|
// When not using content trust, digest resolution happens later when
|
|
|
|
// contacting the registry to retrieve image information.
|
2016-12-05 20:02:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
ref, err := reference.ParseAnyReference(service.TaskTemplate.ContainerSpec.Image)
|
2016-12-05 20:02:26 -05:00
|
|
|
if err != nil {
|
2017-01-11 16:54:52 -05:00
|
|
|
return errors.Wrapf(err, "invalid reference %s", service.TaskTemplate.ContainerSpec.Image)
|
2016-12-05 20:02:26 -05:00
|
|
|
}
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
// If reference does not have digest (is not canonical nor image id)
|
|
|
|
if _, ok := ref.(reference.Digested); !ok {
|
|
|
|
namedRef, ok := ref.(reference.Named)
|
2016-12-05 20:02:26 -05:00
|
|
|
if !ok {
|
2017-01-11 16:54:52 -05:00
|
|
|
return errors.New("failed to resolve image digest using content trust: reference is not named")
|
2016-12-05 20:02:26 -05:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
namedRef = reference.TagNameOnly(namedRef)
|
|
|
|
taggedRef, ok := namedRef.(reference.NamedTagged)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("failed to resolve image digest using content trust: reference is not tagged")
|
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
|
2023-07-10 11:24:07 -04:00
|
|
|
resolvedImage, err := trustedResolveDigest(dockerCli, taggedRef)
|
2016-12-05 20:02:26 -05:00
|
|
|
if err != nil {
|
2017-01-11 16:54:52 -05:00
|
|
|
return errors.Wrap(err, "failed to resolve image digest using content trust")
|
2016-12-05 20:02:26 -05:00
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
resolvedFamiliar := reference.FamiliarString(resolvedImage)
|
|
|
|
logrus.Debugf("resolved image tag to %s using content trust", resolvedFamiliar)
|
|
|
|
service.TaskTemplate.ContainerSpec.Image = resolvedFamiliar
|
2016-12-05 20:02:26 -05:00
|
|
|
}
|
2017-01-11 16:54:52 -05:00
|
|
|
|
2016-12-05 20:02:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-10 11:24:07 -04:00
|
|
|
func trustedResolveDigest(cli command.Cli, ref reference.NamedTagged) (reference.Canonical, error) {
|
2016-12-05 20:02:26 -05:00
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-10 11:24:07 -04:00
|
|
|
authConfig := command.ResolveAuthConfig(cli.ConfigFile(), repoInfo.Index)
|
2016-12-05 20:02:26 -05:00
|
|
|
|
2017-09-12 12:39:13 -04:00
|
|
|
notaryRepo, err := trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), repoInfo, &authConfig, "pull")
|
2016-12-05 20:02:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error establishing connection to trust repository")
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := notaryRepo.GetTargetByName(ref.Tag(), trust.ReleasesRole, data.CanonicalTargetsRole)
|
|
|
|
if err != nil {
|
2017-01-25 19:54:18 -05:00
|
|
|
return nil, trust.NotaryError(repoInfo.Name.Name(), err)
|
2016-12-05 20:02:26 -05:00
|
|
|
}
|
|
|
|
// 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 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, trust.NotaryError(repoInfo.Name.Name(), errors.Errorf("No trust data for %s", reference.FamiliarString(ref)))
|
2016-12-05 20:02:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("retrieving target for %s role\n", t.Role)
|
|
|
|
h, ok := t.Hashes["sha256"]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("no valid hash, expecting sha256")
|
|
|
|
}
|
|
|
|
|
|
|
|
dgst := digest.NewDigestFromHex("sha256", hex.EncodeToString(h))
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
// Allow returning canonical reference with tag
|
|
|
|
return reference.WithDigest(ref, dgst)
|
2016-12-05 20:02:26 -05:00
|
|
|
}
|