2017-08-24 18:45:20 -04:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
2017-08-25 17:49:40 -04:00
|
|
|
"context"
|
2017-08-24 18:45:20 -04:00
|
|
|
"fmt"
|
2017-09-26 12:53:21 -04:00
|
|
|
"io"
|
2017-08-24 18:45:20 -04:00
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/image"
|
|
|
|
"github.com/docker/cli/cli/trust"
|
2017-08-25 17:49:40 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-08-24 18:45:20 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-10-30 12:21:41 -04:00
|
|
|
"github.com/theupdateframework/notary/client"
|
|
|
|
"github.com/theupdateframework/notary/tuf/data"
|
2017-08-24 18:45:20 -04:00
|
|
|
)
|
|
|
|
|
2017-09-29 12:51:45 -04:00
|
|
|
type signOptions struct {
|
|
|
|
local bool
|
|
|
|
imageName string
|
|
|
|
}
|
|
|
|
|
2017-08-24 18:45:20 -04:00
|
|
|
func newSignCommand(dockerCli command.Cli) *cobra.Command {
|
2017-09-29 12:51:45 -04:00
|
|
|
options := signOptions{}
|
2017-08-24 18:45:20 -04:00
|
|
|
cmd := &cobra.Command{
|
2017-09-26 19:15:45 -04:00
|
|
|
Use: "sign IMAGE:TAG",
|
2017-08-24 18:45:20 -04:00
|
|
|
Short: "Sign an image",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-09-29 12:51:45 -04:00
|
|
|
options.imageName = args[0]
|
|
|
|
return runSignImage(dockerCli, options)
|
2017-08-24 18:45:20 -04:00
|
|
|
},
|
|
|
|
}
|
2017-09-29 12:51:45 -04:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVar(&options.local, "local", false, "Sign a locally tagged image")
|
2017-08-24 18:45:20 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-09-29 12:51:45 -04:00
|
|
|
func runSignImage(cli command.Cli, options signOptions) error {
|
|
|
|
imageName := options.imageName
|
2017-08-25 17:49:40 -04:00
|
|
|
ctx := context.Background()
|
2018-03-06 05:15:18 -05:00
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, nil, image.AuthResolver(cli), imageName)
|
2017-08-24 18:45:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-26 12:53:21 -04:00
|
|
|
if err := validateTag(imgRefAndAuth); err != nil {
|
|
|
|
return err
|
2017-08-25 17:49:40 -04:00
|
|
|
}
|
2017-08-24 18:45:20 -04:00
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, trust.ActionsPushAndPull)
|
2017-08-24 18:45:20 -04:00
|
|
|
if err != nil {
|
2017-08-25 17:49:40 -04:00
|
|
|
return trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
if err = clearChangeList(notaryRepo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer clearChangeList(notaryRepo)
|
|
|
|
|
|
|
|
// get the latest repository metadata so we can figure out which roles to sign
|
2017-09-11 17:07:00 -04:00
|
|
|
if _, err = notaryRepo.ListTargets(); err != nil {
|
2017-08-24 18:45:20 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case client.ErrRepoNotInitialized, client.ErrRepositoryNotExist:
|
|
|
|
// before initializing a new repo, check that the image exists locally:
|
|
|
|
if err := checkLocalImageExistence(ctx, cli, imageName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-25 17:49:40 -04:00
|
|
|
userRole := data.RoleName(path.Join(data.CanonicalTargetsRole.String(), imgRefAndAuth.AuthConfig().Username))
|
2017-08-24 18:45:20 -04:00
|
|
|
if err := initNotaryRepoWithSigners(notaryRepo, userRole); err != nil {
|
2017-08-25 17:49:40 -04:00
|
|
|
return trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
2017-08-25 17:49:40 -04:00
|
|
|
fmt.Fprintf(cli.Out(), "Created signer: %s\n", imgRefAndAuth.AuthConfig().Username)
|
|
|
|
fmt.Fprintf(cli.Out(), "Finished initializing signed repository for %s\n", imageName)
|
2017-08-24 18:45:20 -04:00
|
|
|
default:
|
2017-08-25 17:49:40 -04:00
|
|
|
return trust.NotaryError(imgRefAndAuth.RepoInfo().Name.Name(), err)
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-25 17:49:40 -04:00
|
|
|
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(cli, imgRefAndAuth.RepoInfo().Index, "push")
|
2017-09-26 12:53:21 -04:00
|
|
|
target, err := createTarget(notaryRepo, imgRefAndAuth.Tag())
|
2017-09-29 12:51:45 -04:00
|
|
|
if err != nil || options.local {
|
2017-08-24 18:45:20 -04:00
|
|
|
switch err := err.(type) {
|
2017-09-29 12:51:45 -04:00
|
|
|
// If the error is nil then the local flag is set
|
|
|
|
case client.ErrNoSuchTarget, client.ErrRepositoryNotExist, nil:
|
2017-08-24 18:45:20 -04:00
|
|
|
// Fail fast if the image doesn't exist locally
|
|
|
|
if err := checkLocalImageExistence(ctx, cli, imageName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-29 12:51:45 -04:00
|
|
|
fmt.Fprintf(cli.Err(), "Signing and pushing trust data for local image %s, may overwrite remote trust data\n", imageName)
|
2017-08-25 17:49:40 -04:00
|
|
|
return image.TrustedPush(ctx, cli, imgRefAndAuth.RepoInfo(), imgRefAndAuth.Reference(), *imgRefAndAuth.AuthConfig(), requestPrivilege)
|
2017-08-24 18:45:20 -04:00
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 12:53:21 -04:00
|
|
|
return signAndPublishToTarget(cli.Out(), imgRefAndAuth, notaryRepo, target)
|
|
|
|
}
|
2017-08-24 18:45:20 -04:00
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
func signAndPublishToTarget(out io.Writer, imgRefAndAuth trust.ImageRefAndAuth, notaryRepo client.Repository, target client.Target) error {
|
|
|
|
tag := imgRefAndAuth.Tag()
|
|
|
|
fmt.Fprintf(out, "Signing and pushing trust metadata for %s\n", imgRefAndAuth.Name())
|
2017-08-24 18:45:20 -04:00
|
|
|
existingSigInfo, err := getExistingSignatureInfoForReleasedTag(notaryRepo, tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = image.AddTargetToAllSignableRoles(notaryRepo, &target)
|
|
|
|
if err == nil {
|
2017-09-26 12:53:21 -04:00
|
|
|
prettyPrintExistingSignatureInfo(out, existingSigInfo)
|
2017-08-24 18:45:20 -04:00
|
|
|
err = notaryRepo.Publish()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-10-30 13:22:10 -04:00
|
|
|
return errors.Wrapf(err, "failed to sign %s:%s", imgRefAndAuth.RepoInfo().Name.Name(), tag)
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
2017-10-30 13:22:10 -04:00
|
|
|
fmt.Fprintf(out, "Successfully signed %s:%s\n", imgRefAndAuth.RepoInfo().Name.Name(), tag)
|
2017-09-26 12:53:21 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateTag(imgRefAndAuth trust.ImageRefAndAuth) error {
|
|
|
|
tag := imgRefAndAuth.Tag()
|
|
|
|
if tag == "" {
|
|
|
|
if imgRefAndAuth.Digest() != "" {
|
|
|
|
return fmt.Errorf("cannot use a digest reference for IMAGE:TAG")
|
|
|
|
}
|
|
|
|
return fmt.Errorf("No tag specified for %s", imgRefAndAuth.Name())
|
|
|
|
}
|
2017-08-24 18:45:20 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-25 17:49:40 -04:00
|
|
|
func checkLocalImageExistence(ctx context.Context, cli command.Cli, imageName string) error {
|
|
|
|
_, _, err := cli.Client().ImageInspectWithRaw(ctx, imageName)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
func createTarget(notaryRepo client.Repository, tag string) (client.Target, error) {
|
2017-08-24 18:45:20 -04:00
|
|
|
target := &client.Target{}
|
|
|
|
var err error
|
|
|
|
if tag == "" {
|
|
|
|
return *target, fmt.Errorf("No tag specified")
|
|
|
|
}
|
|
|
|
target.Name = tag
|
|
|
|
target.Hashes, target.Length, err = getSignedManifestHashAndSize(notaryRepo, tag)
|
|
|
|
return *target, err
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
func getSignedManifestHashAndSize(notaryRepo client.Repository, tag string) (data.Hashes, int64, error) {
|
2017-08-24 18:45:20 -04:00
|
|
|
targets, err := notaryRepo.GetAllTargetMetadataByName(tag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return getReleasedTargetHashAndSize(targets, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getReleasedTargetHashAndSize(targets []client.TargetSignedStruct, tag string) (data.Hashes, int64, error) {
|
|
|
|
for _, tgt := range targets {
|
|
|
|
if isReleasedTarget(tgt.Role.Name) {
|
|
|
|
return tgt.Target.Hashes, tgt.Target.Length, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, 0, client.ErrNoSuchTarget(tag)
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
func getExistingSignatureInfoForReleasedTag(notaryRepo client.Repository, tag string) (trustTagRow, error) {
|
2017-08-24 18:45:20 -04:00
|
|
|
targets, err := notaryRepo.GetAllTargetMetadataByName(tag)
|
|
|
|
if err != nil {
|
|
|
|
return trustTagRow{}, err
|
|
|
|
}
|
|
|
|
releasedTargetInfoList := matchReleasedSignatures(targets)
|
|
|
|
if len(releasedTargetInfoList) == 0 {
|
|
|
|
return trustTagRow{}, nil
|
|
|
|
}
|
|
|
|
return releasedTargetInfoList[0], nil
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
func prettyPrintExistingSignatureInfo(out io.Writer, existingSigInfo trustTagRow) {
|
2017-08-24 18:45:20 -04:00
|
|
|
sort.Strings(existingSigInfo.Signers)
|
|
|
|
joinedSigners := strings.Join(existingSigInfo.Signers, ", ")
|
2017-11-15 14:50:33 -05:00
|
|
|
fmt.Fprintf(out, "Existing signatures for tag %s digest %s from:\n%s\n", existingSigInfo.SignedTag, existingSigInfo.Digest, joinedSigners)
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
func initNotaryRepoWithSigners(notaryRepo client.Repository, newSigner data.RoleName) error {
|
2017-08-24 18:45:20 -04:00
|
|
|
rootKey, err := getOrGenerateNotaryKey(notaryRepo, data.CanonicalRootRole)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rootKeyID := rootKey.ID()
|
|
|
|
|
|
|
|
// Initialize the notary repository with a remotely managed snapshot key
|
|
|
|
if err := notaryRepo.Initialize([]string{rootKeyID}, data.CanonicalSnapshotRole); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signerKey, err := getOrGenerateNotaryKey(notaryRepo, newSigner)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-10 13:16:01 -04:00
|
|
|
if err := addStagedSigner(notaryRepo, newSigner, []data.PublicKey{signerKey}); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not add signer to repo: %s", strings.TrimPrefix(newSigner.String(), "targets/"))
|
|
|
|
}
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
return notaryRepo.Publish()
|
|
|
|
}
|
|
|
|
|
|
|
|
// generates an ECDSA key without a GUN for the specified role
|
2017-09-11 17:07:00 -04:00
|
|
|
func getOrGenerateNotaryKey(notaryRepo client.Repository, role data.RoleName) (data.PublicKey, error) {
|
2017-08-24 18:45:20 -04:00
|
|
|
// use the signer name in the PEM headers if this is a delegation key
|
|
|
|
if data.IsDelegation(role) {
|
|
|
|
role = data.RoleName(notaryRoleToSigner(role))
|
|
|
|
}
|
2017-09-11 17:07:00 -04:00
|
|
|
keys := notaryRepo.GetCryptoService().ListKeys(role)
|
2017-08-24 18:45:20 -04:00
|
|
|
var err error
|
|
|
|
var key data.PublicKey
|
|
|
|
// always select the first key by ID
|
|
|
|
if len(keys) > 0 {
|
|
|
|
sort.Strings(keys)
|
|
|
|
keyID := keys[0]
|
2017-09-11 17:07:00 -04:00
|
|
|
privKey, _, err := notaryRepo.GetCryptoService().GetPrivateKey(keyID)
|
2017-08-24 18:45:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key = data.PublicKeyFromPrivate(privKey)
|
|
|
|
} else {
|
2017-09-11 17:07:00 -04:00
|
|
|
key, err = notaryRepo.GetCryptoService().Create(role, "", data.ECDSAKey)
|
2017-08-24 18:45:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stages changes to add a signer with the specified name and key(s). Adds to targets/<name> and targets/releases
|
2017-10-10 13:16:01 -04:00
|
|
|
func addStagedSigner(notaryRepo client.Repository, newSigner data.RoleName, signerKeys []data.PublicKey) error {
|
2017-08-24 18:45:20 -04:00
|
|
|
// create targets/<username>
|
2017-10-10 13:16:01 -04:00
|
|
|
if err := notaryRepo.AddDelegationRoleAndKeys(newSigner, signerKeys); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := notaryRepo.AddDelegationPaths(newSigner, []string{""}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// create targets/releases
|
2017-10-10 13:16:01 -04:00
|
|
|
if err := notaryRepo.AddDelegationRoleAndKeys(trust.ReleasesRole, signerKeys); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-13 07:16:34 -05:00
|
|
|
return notaryRepo.AddDelegationPaths(trust.ReleasesRole, []string{""})
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|