2016-12-05 20:02:26 -05:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
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/cli/command"
|
|
|
|
"github.com/docker/docker/cli/trust"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resolveServiceImageDigest(dockerCli *command.DockerCli, service *swarm.ServiceSpec) error {
|
|
|
|
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-11 16:54:52 -05:00
|
|
|
taggedRef := reference.EnsureTagged(namedRef)
|
|
|
|
|
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-01-11 16:54:52 -05:00
|
|
|
func trustedResolveDigest(ctx context.Context, cli *command.DockerCli, 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 {
|
|
|
|
return nil, trust.NotaryError(repoInfo.FullName(), 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 {
|
2017-01-11 16:54:52 -05:00
|
|
|
return nil, trust.NotaryError(repoInfo.FullName(), fmt.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
|
|
|
}
|