From 98202c86adb4ee697bb2ff5c6d72ab373ab2a0f6 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 26 Feb 2022 13:04:15 +0100 Subject: [PATCH] registry: remove ErrInvalidRepositoryName This error was only returned in a single location, and not used anywhere as a specific type. The error returned by `validateNoScheme()` also appeared to only be used in one case; in all other cases, the error itself was ignored, and replaced with a custom error. Because of this, this patch also replace `validateNoScheme()` with a `hasScheme()` function that returns a boolean, to better match how it's used. Signed-off-by: Sebastiaan van Stijn --- registry/config.go | 17 ++++------------- registry/service.go | 4 ++-- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/registry/config.go b/registry/config.go index 9cf39452f5..fae73bb4e3 100644 --- a/registry/config.go +++ b/registry/config.go @@ -10,7 +10,6 @@ import ( "github.com/docker/distribution/reference" registrytypes "github.com/docker/docker/api/types/registry" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -58,10 +57,6 @@ var ( Host: DefaultRegistryHost, } - // ErrInvalidRepositoryName is an error returned if the repository name did - // not have the correct form - ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")") - emptyServiceConfig, _ = newServiceConfig(ServiceOptions{}) validHostPortRegex = regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`) @@ -101,7 +96,7 @@ func (config *serviceConfig) loadAllowNondistributableArtifacts(registries []str if _, err := ValidateIndexName(r); err != nil { return err } - if validateNoScheme(r) != nil { + if hasScheme(r) { return fmt.Errorf("allow-nondistributable-artifacts registry %s should not contain '://'", r) } @@ -188,7 +183,7 @@ skip: } else if strings.HasPrefix(strings.ToLower(r), "https://") { logrus.Warnf("insecure registry %s should not contain 'https://' and 'https://' has been removed from the insecure registry config", r) r = r[8:] - } else if validateNoScheme(r) != nil { + } else if hasScheme(r) { // Insecure registry should not contain '://' // before returning err, roll back to original data config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs @@ -343,12 +338,8 @@ func ValidateIndexName(val string) (string, error) { return val, nil } -func validateNoScheme(reposName string) error { - if strings.Contains(reposName, "://") { - // It cannot contain a scheme! - return ErrInvalidRepositoryName - } - return nil +func hasScheme(reposName string) bool { + return strings.Contains(reposName, "://") } func validateHostPort(s string) error { diff --git a/registry/service.go b/registry/service.go index a8ea446e37..ea2fb35c14 100644 --- a/registry/service.go +++ b/registry/service.go @@ -161,8 +161,8 @@ func splitReposSearchTerm(reposName string) (string, string) { // search terms, and returns the results. func (s *defaultService) Search(ctx context.Context, term string, limit int, authConfig *types.AuthConfig, userAgent string, headers map[string][]string) (*registrytypes.SearchResults, error) { // TODO Use ctx when searching for repositories - if err := validateNoScheme(term); err != nil { - return nil, err + if hasScheme(term) { + return nil, errors.New(`invalid repository name (ex: "registry.domain.tld/myrepos")`) } indexName, remoteName := splitReposSearchTerm(term)