2016-12-05 20:02:26 -05:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/trust"
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-12-05 20:02:26 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/docker/notary/tuf/data"
|
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"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func resolveServiceImageDigest(dockerCli command.Cli, service *swarm.ServiceSpec) error {
|
2016-12-05 20:02:26 -05:00
|
|
|
if !command.IsTrusted() {
|
|
|
|
// Digests are resolved by the daemon when not using content
|
|
|
|
// trust.
|
|
|
|
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
|
|
|
|
2016-12-05 20:02:26 -05:00
|
|
|
resolvedImage, err := trustedResolveDigest(context.Background(), dockerCli, taggedRef)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func trustedResolveDigest(ctx context.Context, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfig := command.ResolveAuthConfig(ctx, cli, repoInfo.Index)
|
|
|
|
|
|
|
|
notaryRepo, err := trust.GetNotaryRepository(cli, repoInfo, authConfig, "pull")
|
|
|
|
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
|
|
|
}
|