spiegel_podman/cmd/podman/images/trust_set.go
Daniel J Walsh 389dcb5c29
Remove some more excessive wrapping and stuttering
Stop over wrapping API Calls

The API calls will return an appropriate error, and this wrapping
just makes the error message look like it is stuttering and a
big mess.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2020-11-17 09:19:22 -05:00

65 lines
2.3 KiB
Go

package images
import (
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v2/cmd/podman/common"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/libpod/image"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
setTrustDescription = "Set default trust policy or add a new trust policy for a registry"
setTrustCommand = &cobra.Command{
Use: "set [options] REGISTRY",
Short: "Set default trust policy or a new trust policy for a registry",
Long: setTrustDescription,
Example: "",
RunE: setTrust,
Args: cobra.ExactArgs(1),
ValidArgsFunction: common.AutocompleteRegistries,
}
)
var (
setOptions entities.SetTrustOptions
)
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode},
Command: setTrustCommand,
Parent: trustCmd,
})
setFlags := setTrustCommand.Flags()
setFlags.StringVar(&setOptions.PolicyPath, "policypath", "", "")
_ = setFlags.MarkHidden("policypath")
pubkeysfileFlagName := "pubkeysfile"
setFlags.StringSliceVarP(&setOptions.PubKeysFile, pubkeysfileFlagName, "f", []string{}, `Path of installed public key(s) to trust for TARGET.
Absolute path to keys is added to policy.json. May
used multiple times to define multiple public keys.
File(s) must exist before using this command`)
_ = setTrustCommand.RegisterFlagCompletionFunc(pubkeysfileFlagName, completion.AutocompleteDefault)
typeFlagName := "type"
setFlags.StringVarP(&setOptions.Type, typeFlagName, "t", "signedBy", "Trust type, accept values: signedBy(default), accept, reject")
_ = setTrustCommand.RegisterFlagCompletionFunc(typeFlagName, common.AutocompleteTrustType)
}
func setTrust(cmd *cobra.Command, args []string) error {
validTrustTypes := []string{"accept", "insecureAcceptAnything", "reject", "signedBy"}
valid, err := image.IsValidImageURI(args[0])
if err != nil || !valid {
return err
}
if !util.StringInSlice(setOptions.Type, validTrustTypes) {
return errors.Errorf("invalid choice: %s (choose from 'accept', 'reject', 'signedBy')", setOptions.Type)
}
return registry.ImageEngine().SetTrust(registry.Context(), args, setOptions)
}