diff options
Diffstat (limited to 'cmd/podman/images')
-rw-r--r-- | cmd/podman/images/trust_set.go | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/cmd/podman/images/trust_set.go b/cmd/podman/images/trust_set.go index 6333512d9..c192669a9 100644 --- a/cmd/podman/images/trust_set.go +++ b/cmd/podman/images/trust_set.go @@ -1,10 +1,12 @@ package images import ( + "net/url" + "regexp" + "github.com/containers/common/pkg/completion" "github.com/containers/podman/v3/cmd/podman/common" "github.com/containers/podman/v3/cmd/podman/registry" - "github.com/containers/podman/v3/libpod/image" "github.com/containers/podman/v3/pkg/domain/entities" "github.com/containers/podman/v3/pkg/util" "github.com/pkg/errors" @@ -53,7 +55,7 @@ File(s) must exist before using this command`) func setTrust(cmd *cobra.Command, args []string) error { validTrustTypes := []string{"accept", "insecureAcceptAnything", "reject", "signedBy"} - valid, err := image.IsValidImageURI(args[0]) + valid, err := isValidImageURI(args[0]) if err != nil || !valid { return err } @@ -63,3 +65,23 @@ func setTrust(cmd *cobra.Command, args []string) error { } return registry.ImageEngine().SetTrust(registry.Context(), args, setOptions) } + +// isValidImageURI checks if image name has valid format +func isValidImageURI(imguri string) (bool, error) { + uri := "http://" + imguri + u, err := url.Parse(uri) + if err != nil { + return false, errors.Wrapf(err, "invalid image uri: %s", imguri) + } + reg := regexp.MustCompile(`^[a-zA-Z0-9-_\.]+\/?:?[0-9]*[a-z0-9-\/:]*$`) + ret := reg.FindAllString(u.Host, -1) + if len(ret) == 0 { + return false, errors.Wrapf(err, "invalid image uri: %s", imguri) + } + reg = regexp.MustCompile(`^[a-z0-9-:\./]*$`) + ret = reg.FindAllString(u.Fragment, -1) + if len(ret) == 0 { + return false, errors.Wrapf(err, "invalid image uri: %s", imguri) + } + return true, nil +} |