Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions pkg/buildpack/locator_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/buildpack/locator_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading