diff --git a/pkg/buildpack/locator_type.go b/pkg/buildpack/locator_type.go index 0019cc900..abbeb5202 100644 --- a/pkg/buildpack/locator_type.go +++ b/pkg/buildpack/locator_type.go @@ -36,7 +36,7 @@ const ( var ( // https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string semverPattern = `(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?` - registryPattern = regexp.MustCompile(`^[a-z0-9\-\.]+\/[a-z0-9\-\.]+(?:@` + semverPattern + `)?$`) + registryPattern = regexp.MustCompile(`^[a-z0-9\-\.]+(?::[0-9]+)?\/[a-z0-9\-\.\/]+(?:@` + semverPattern + `)?$`) ) func (l LocatorType) String() string { @@ -120,7 +120,25 @@ func canBePackageRef(locator string) bool { } func canBeRegistryRef(locator string) bool { - return registryPattern.MatchString(locator) + if !registryPattern.MatchString(locator) { + return false + } + // If the segment before the first "/" contains a dot, it looks like + // a Docker registry hostname (e.g. registry.com/path/name) rather + // than a Buildpack Registry ID (e.g. example/foo@1.0.0). + // Similarly, "localhost" with a port is a Docker registry. + // Let canBePackageRef handle these instead. + if i := strings.Index(locator, "/"); i > 0 { + host := locator[:i] + if strings.Contains(host, ".") { + return false + } + // localhost:port is a Docker registry, not a Buildpack Registry + if strings.HasPrefix(host, "localhost:") { + return false + } + } + return true } func isFoundInBuilder(locator string, candidates []dist.ModuleInfo) bool { diff --git a/pkg/buildpack/locator_type_test.go b/pkg/buildpack/locator_type_test.go index bdc045373..1b77b9337 100644 --- a/pkg/buildpack/locator_type_test.go +++ b/pkg/buildpack/locator_type_test.go @@ -119,6 +119,14 @@ func testGetLocatorType(t *testing.T, when spec.G, it spec.S) { locator: "docker://registry.com/cnbs/some-bp:some-tag@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", expectedType: buildpack.PackageLocator, }, + { + locator: "localhost:5000/example/registry-cnb", + expectedType: buildpack.PackageLocator, + }, + { + locator: "registry.example.com:5000/example/foo@1.0.0", + expectedType: buildpack.InvalidLocator, + }, { locator: "cnbs/some-bp@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", expectedType: buildpack.PackageLocator, @@ -155,6 +163,14 @@ func testGetLocatorType(t *testing.T, when spec.G, it spec.S) { locator: "example/registry-cnb", expectedType: buildpack.RegistryLocator, }, + { + locator: "localhost:5000/example/registry-cnb", + expectedType: buildpack.PackageLocator, + }, + { + locator: "registry.example.com:5000/example/foo@1.0.0", + expectedType: buildpack.InvalidLocator, + }, { locator: "cnbs/sample-package@hello-universe", expectedType: buildpack.InvalidLocator,