1
0
mirror of https://github.com/distribution/distribution synced 2024-11-06 19:35:52 +01:00

reference: check for prefix instead of splitting, and use consts

- use strings.HasPrefix() to check for the prefix we're interested in instead
  of doing a strings.Split() without limits. This makes the code both easier
  to read, and prevents potential situations where we end up with a long slice.
- use consts for defaults; these should never be modified, so better to use
  consts for them to indicate they're fixed values.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-09 15:20:23 +01:00
parent 9d38ed78d2
commit 552b1526c6
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

@ -7,10 +7,10 @@ import (
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
) )
var ( const (
legacyDefaultDomain = "index.docker.io" legacyDefaultDomain = "index.docker.io"
defaultDomain = "docker.io" defaultDomain = "docker.io"
officialRepoName = "library" officialRepoPrefix = "library/"
defaultTag = "latest" defaultTag = "latest"
) )
@ -96,8 +96,13 @@ func splitDockerDomain(name string) (domain, remainder string) {
if domain == legacyDefaultDomain { if domain == legacyDefaultDomain {
domain = defaultDomain domain = defaultDomain
} }
// TODO(thaJeztah): this check may be too strict, as it assumes the
// "library/" namespace does not have nested namespaces. While this
// is true (currently), technically it would be possible for Docker
// Hub to use those (e.g. "library/distros/ubuntu:latest").
// See https://github.com/distribution/distribution/pull/3769#issuecomment-1302031785.
if domain == defaultDomain && !strings.ContainsRune(remainder, '/') { if domain == defaultDomain && !strings.ContainsRune(remainder, '/') {
remainder = officialRepoName + "/" + remainder remainder = officialRepoPrefix + remainder
} }
return return
} }
@ -117,8 +122,15 @@ func familiarizeName(named namedRepository) repository {
if repo.domain == defaultDomain { if repo.domain == defaultDomain {
repo.domain = "" repo.domain = ""
// Handle official repositories which have the pattern "library/<official repo name>" // Handle official repositories which have the pattern "library/<official repo name>"
if split := strings.Split(repo.path, "/"); len(split) == 2 && split[0] == officialRepoName { if strings.HasPrefix(repo.path, officialRepoPrefix) {
repo.path = split[1] // TODO(thaJeztah): this check may be too strict, as it assumes the
// "library/" namespace does not have nested namespaces. While this
// is true (currently), technically it would be possible for Docker
// Hub to use those (e.g. "library/distros/ubuntu:latest").
// See https://github.com/distribution/distribution/pull/3769#issuecomment-1302031785.
if remainder := strings.TrimPrefix(repo.path, officialRepoPrefix); !strings.ContainsRune(remainder, '/') {
repo.path = remainder
}
} }
} }
return repo return repo