2017-09-26 14:43:52 -04:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-10-23 05:23:39 -04:00
|
|
|
"github.com/docker/cli/cli/command/image"
|
2017-09-26 14:43:52 -04:00
|
|
|
"github.com/docker/cli/cli/trust"
|
2017-10-25 13:13:24 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-09-26 14:43:52 -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-09-26 14:43:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type signerRemoveOptions struct {
|
2017-10-10 13:16:01 -04:00
|
|
|
signer string
|
2017-10-25 13:45:10 -04:00
|
|
|
repos []string
|
2017-09-26 14:43:52 -04:00
|
|
|
forceYes bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSignerRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
|
|
|
options := signerRemoveOptions{}
|
|
|
|
cmd := &cobra.Command{
|
2017-10-25 13:45:10 -04:00
|
|
|
Use: "remove [OPTIONS] NAME REPOSITORY [REPOSITORY...]",
|
2017-09-26 14:43:52 -04:00
|
|
|
Short: "Remove a signer",
|
|
|
|
Args: cli.RequiresMinArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-10-10 13:16:01 -04:00
|
|
|
options.signer = args[0]
|
2017-10-25 13:45:10 -04:00
|
|
|
options.repos = args[1:]
|
2017-10-10 13:16:01 -04:00
|
|
|
return removeSigner(dockerCli, options)
|
2017-09-26 14:43:52 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2017-10-25 13:45:10 -04:00
|
|
|
flags.BoolVarP(&options.forceYes, "force", "f", false, "Do not prompt for confirmation before removing the most recent signer")
|
2017-09-26 14:43:52 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-10-10 13:16:01 -04:00
|
|
|
func removeSigner(cli command.Cli, options signerRemoveOptions) error {
|
2017-10-25 13:45:10 -04:00
|
|
|
var errRepos []string
|
|
|
|
for _, repo := range options.repos {
|
|
|
|
fmt.Fprintf(cli.Out(), "Removing signer \"%s\" from %s...\n", options.signer, repo)
|
|
|
|
if err := removeSingleSigner(cli, repo, options.signer, options.forceYes); err != nil {
|
|
|
|
fmt.Fprintln(cli.Err(), err.Error()+"\n")
|
|
|
|
errRepos = append(errRepos, repo)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(cli.Out(), "Successfully removed %s from %s\n\n", options.signer, repo)
|
2017-09-26 14:43:52 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-25 13:45:10 -04:00
|
|
|
if len(errRepos) > 0 {
|
|
|
|
return fmt.Errorf("Error removing signer from: %s", strings.Join(errRepos, ", "))
|
2017-09-26 14:43:52 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLastSignerForReleases(roleWithSig data.Role, allRoles []client.RoleWithSignatures) (bool, error) {
|
|
|
|
var releasesRoleWithSigs client.RoleWithSignatures
|
|
|
|
for _, role := range allRoles {
|
|
|
|
if role.Name == releasesRoleTUFName {
|
|
|
|
releasesRoleWithSigs = role
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
counter := len(releasesRoleWithSigs.Signatures)
|
|
|
|
if counter == 0 {
|
|
|
|
return false, fmt.Errorf("All signed tags are currently revoked, use docker trust sign to fix")
|
|
|
|
}
|
|
|
|
for _, signature := range releasesRoleWithSigs.Signatures {
|
|
|
|
for _, key := range roleWithSig.KeyIDs {
|
|
|
|
if signature.KeyID == key {
|
|
|
|
counter--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return counter < releasesRoleWithSigs.Threshold, nil
|
|
|
|
}
|
|
|
|
|
2017-10-25 13:45:10 -04:00
|
|
|
func removeSingleSigner(cli command.Cli, repoName, signerName string, forceYes bool) error {
|
2017-09-26 14:43:52 -04:00
|
|
|
ctx := context.Background()
|
2017-10-25 13:45:10 -04:00
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, image.AuthResolver(cli), repoName)
|
2017-09-26 14:43:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signerDelegation := data.RoleName("targets/" + signerName)
|
|
|
|
if signerDelegation == releasesRoleTUFName {
|
|
|
|
return fmt.Errorf("releases is a reserved keyword and cannot be removed")
|
|
|
|
}
|
2017-10-23 05:23:39 -04:00
|
|
|
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, trust.ActionsPushAndPull)
|
2017-09-26 14:43:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
|
|
|
|
}
|
|
|
|
delegationRoles, err := notaryRepo.GetDelegationRoles()
|
|
|
|
if err != nil {
|
2017-10-25 13:45:10 -04:00
|
|
|
return errors.Wrapf(err, "error retrieving signers for %s", repoName)
|
2017-09-26 14:43:52 -04:00
|
|
|
}
|
|
|
|
var role data.Role
|
|
|
|
for _, delRole := range delegationRoles {
|
|
|
|
if delRole.Name == signerDelegation {
|
|
|
|
role = delRole
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if role.Name == "" {
|
2017-10-25 13:45:10 -04:00
|
|
|
return fmt.Errorf("No signer %s for repository %s", signerName, repoName)
|
2017-09-26 14:43:52 -04:00
|
|
|
}
|
|
|
|
allRoles, err := notaryRepo.ListRoles()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok, err := isLastSignerForReleases(role, allRoles); ok && !forceYes {
|
|
|
|
removeSigner := command.PromptForConfirmation(os.Stdin, cli.Out(), fmt.Sprintf("The signer \"%s\" signed the last released version of %s. "+
|
|
|
|
"Removing this signer will make %s unpullable. "+
|
|
|
|
"Are you sure you want to continue?",
|
2017-10-25 13:45:10 -04:00
|
|
|
signerName, repoName, repoName,
|
2017-09-26 14:43:52 -04:00
|
|
|
))
|
|
|
|
|
|
|
|
if !removeSigner {
|
|
|
|
fmt.Fprintf(cli.Out(), "\nAborting action.\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2017-10-25 13:13:24 -04:00
|
|
|
return err
|
2017-09-26 14:43:52 -04:00
|
|
|
}
|
|
|
|
if err = notaryRepo.RemoveDelegationKeys(releasesRoleTUFName, role.KeyIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = notaryRepo.RemoveDelegationRole(signerDelegation); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = notaryRepo.Publish(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|