2015-09-08 23:00:48 +00:00
|
|
|
package reference
|
|
|
|
|
2022-11-09 17:59:49 +00:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-11-08 15:52:02 +00:00
|
|
|
const (
|
|
|
|
// alphanumeric defines the alphanumeric atom, typically a
|
2015-12-04 22:40:09 +00:00
|
|
|
// component of names. This only allows lower case characters and digits.
|
2022-11-08 15:52:02 +00:00
|
|
|
alphanumeric = `[a-z0-9]+`
|
2015-10-09 23:01:01 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
// separator defines the separators allowed to be embedded in name
|
2022-11-08 15:52:02 +00:00
|
|
|
// components. This allows one period, one or two underscore and multiple
|
2020-07-09 14:43:10 +00:00
|
|
|
// dashes. Repeated dashes and underscores are intentionally treated
|
|
|
|
// differently. In order to support valid hostnames as name components,
|
|
|
|
// supporting repeated dash was added. Additionally double underscore is
|
|
|
|
// now allowed as a separator to loosen the restriction for previously
|
|
|
|
// supported names.
|
2022-01-13 15:49:03 +00:00
|
|
|
separator = `(?:[._]|__|[-]*)`
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-06-25 10:23:38 +00:00
|
|
|
// domainNameComponent restricts the registry domain component of a
|
|
|
|
// repository name to start with a component as defined by DomainRegexp.
|
|
|
|
domainNameComponent = `(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])`
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-11-08 15:52:02 +00:00
|
|
|
// tag matches valid tag names. From docker/docker:graph/tags.go.
|
|
|
|
tag = `[\w][\w.-]{0,127}`
|
|
|
|
|
|
|
|
// digestPat matches well-formed digests, including algorithm (e.g. "sha256:<encoded>").
|
|
|
|
//
|
|
|
|
// TODO(thaJeztah): this should follow the same rules as https://pkg.go.dev/github.com/opencontainers/go-digest@v1.0.0#DigestRegexp
|
|
|
|
// so that go-digest defines the canonical format. Note that the go-digest is
|
|
|
|
// more relaxed:
|
|
|
|
// - it allows multiple algorithms (e.g. "sha256+b64:<encoded>") to allow
|
|
|
|
// future expansion of supported algorithms.
|
|
|
|
// - it allows the "<encoded>" value to use urlsafe base64 encoding as defined
|
|
|
|
// in [rfc4648, section 5].
|
|
|
|
//
|
|
|
|
// [rfc4648, section 5]: https://www.rfc-editor.org/rfc/rfc4648#section-5.
|
|
|
|
digestPat = `[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}`
|
|
|
|
|
|
|
|
// identifier is the format for a content addressable identifier using sha256.
|
|
|
|
// These identifiers are like digests without the algorithm, since sha256 is used.
|
|
|
|
identifier = `([a-f0-9]{64})`
|
|
|
|
|
2022-06-25 10:23:38 +00:00
|
|
|
// ipv6address are enclosed between square brackets and may be represented
|
|
|
|
// in many ways, see rfc5952. Only IPv6 in compressed or uncompressed format
|
|
|
|
// are allowed, IPv6 zone identifiers (rfc6874) or Special addresses such as
|
|
|
|
// IPv4-Mapped are deliberately excluded.
|
2022-11-08 15:52:02 +00:00
|
|
|
ipv6address = `\[(?:[a-fA-F0-9:]+)\]`
|
|
|
|
)
|
2022-06-25 10:23:38 +00:00
|
|
|
|
2022-11-08 15:52:02 +00:00
|
|
|
var (
|
2022-06-25 10:23:38 +00:00
|
|
|
// domainName defines the structure of potential domain components
|
|
|
|
// that may be part of image names. This is purposely a subset of what is
|
|
|
|
// allowed by DNS to ensure backwards compatibility with Docker image
|
|
|
|
// names. This includes IPv4 addresses on decimal format.
|
2022-11-08 15:52:02 +00:00
|
|
|
domainName = expression(domainNameComponent, optional(repeated(literal(`.`), domainNameComponent)))
|
2022-06-25 10:23:38 +00:00
|
|
|
|
|
|
|
// host defines the structure of potential domains based on the URI
|
|
|
|
// Host subcomponent on rfc3986. It may be a subset of DNS domain name,
|
|
|
|
// or an IPv4 address in decimal format, or an IPv6 address between square
|
|
|
|
// brackets (excluding zone identifiers as defined by rfc6874 or special
|
|
|
|
// addresses such as IPv4-Mapped).
|
|
|
|
host = `(?:` + domainName + `|` + ipv6address + `)`
|
|
|
|
|
|
|
|
// allowed by the URI Host subcomponent on rfc3986 to ensure backwards
|
|
|
|
// compatibility with Docker image names.
|
2022-11-08 15:52:02 +00:00
|
|
|
domain = expression(host, optional(literal(`:`), `[0-9]+`))
|
|
|
|
|
|
|
|
// DomainRegexp matches hostname or IP-addresses, optionally including a port
|
|
|
|
// number. It defines the structure of potential domain components that may be
|
|
|
|
// part of image names. This is purposely a subset of what is allowed by DNS to
|
|
|
|
// ensure backwards compatibility with Docker image names. It may be a subset of
|
|
|
|
// DNS domain name, an IPv4 address in decimal format, or an IPv6 address between
|
|
|
|
// square brackets (excluding zone identifiers as defined by [rfc6874] or special
|
|
|
|
// addresses such as IPv4-Mapped).
|
|
|
|
//
|
|
|
|
// [rfc6874]: https://www.rfc-editor.org/rfc/rfc6874.
|
2022-01-13 21:06:06 +00:00
|
|
|
DomainRegexp = regexp.MustCompile(domain)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
|
|
|
// TagRegexp matches valid tag names. From docker/docker:graph/tags.go.
|
2022-01-13 21:06:06 +00:00
|
|
|
TagRegexp = regexp.MustCompile(tag)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
|
|
|
// anchoredTagRegexp matches valid tag names, anchored at the start and
|
|
|
|
// end of the matched string.
|
2022-11-08 15:52:02 +00:00
|
|
|
anchoredTagRegexp = regexp.MustCompile(anchored(tag))
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-11-08 15:52:02 +00:00
|
|
|
// DigestRegexp matches well-formed digests, including algorithm (e.g. "sha256:<encoded>").
|
2022-01-13 21:06:06 +00:00
|
|
|
DigestRegexp = regexp.MustCompile(digestPat)
|
2015-10-10 00:09:54 +00:00
|
|
|
|
|
|
|
// anchoredDigestRegexp matches valid digests, anchored at the start and
|
|
|
|
// end of the matched string.
|
2022-11-08 15:52:02 +00:00
|
|
|
anchoredDigestRegexp = regexp.MustCompile(anchored(digestPat))
|
|
|
|
|
2022-11-11 10:19:43 +00:00
|
|
|
// pathComponent restricts registry path-components to start with at least
|
|
|
|
// one letter or number, with following parts able to be separated by one
|
|
|
|
// period, one or two underscore and multiple dashes.
|
|
|
|
pathComponent = expression(alphanumeric, optional(repeated(separator, alphanumeric)))
|
|
|
|
namePat = expression(optional(domain, literal(`/`)), pathComponent, optional(repeated(literal(`/`), pathComponent)))
|
2015-10-10 00:09:54 +00:00
|
|
|
|
2022-11-10 22:28:37 +00:00
|
|
|
// NameRegexp is the format for the name component of references, including
|
|
|
|
// an optional domain and port, but without tag or digest suffix.
|
2022-01-13 21:06:06 +00:00
|
|
|
NameRegexp = regexp.MustCompile(namePat)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2015-12-04 22:40:09 +00:00
|
|
|
// anchoredNameRegexp is used to parse a name value, capturing the
|
2016-06-09 18:32:23 +00:00
|
|
|
// domain and trailing components.
|
2022-11-11 10:19:43 +00:00
|
|
|
anchoredNameRegexp = regexp.MustCompile(anchored(optional(capture(domain), literal(`/`)), capture(pathComponent, optional(repeated(literal(`/`), pathComponent)))))
|
2022-11-08 15:52:02 +00:00
|
|
|
|
|
|
|
referencePat = anchored(capture(namePat), optional(literal(":"), capture(tag)), optional(literal("@"), capture(digestPat)))
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2015-12-04 22:40:09 +00:00
|
|
|
// ReferenceRegexp is the full supported format of a reference. The regexp
|
|
|
|
// is anchored and has capturing groups for name, tag, and digest
|
|
|
|
// components.
|
2022-01-13 21:06:06 +00:00
|
|
|
ReferenceRegexp = regexp.MustCompile(referencePat)
|
2016-06-15 21:04:21 +00:00
|
|
|
|
|
|
|
// IdentifierRegexp is the format for string identifier used as a
|
|
|
|
// content addressable identifier using sha256. These identifiers
|
|
|
|
// are like digests without the algorithm, since sha256 is used.
|
2022-01-13 21:06:06 +00:00
|
|
|
IdentifierRegexp = regexp.MustCompile(identifier)
|
2016-06-15 21:04:21 +00:00
|
|
|
|
|
|
|
// anchoredIdentifierRegexp is used to check or match an
|
|
|
|
// identifier value, anchored at start and end of string.
|
2022-11-08 15:52:02 +00:00
|
|
|
anchoredIdentifierRegexp = regexp.MustCompile(anchored(identifier))
|
2015-09-08 23:00:48 +00:00
|
|
|
)
|
2015-12-04 22:40:09 +00:00
|
|
|
|
|
|
|
// literal compiles s into a literal regular expression, escaping any regexp
|
|
|
|
// reserved characters.
|
2022-01-13 15:49:03 +00:00
|
|
|
func literal(s string) string {
|
2022-01-13 21:06:06 +00:00
|
|
|
re := regexp.MustCompile(regexp.QuoteMeta(s))
|
2015-12-04 22:40:09 +00:00
|
|
|
|
|
|
|
if _, complete := re.LiteralPrefix(); !complete {
|
|
|
|
panic("must be a literal")
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
return re.String()
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// expression defines a full expression, where each regular expression must
|
|
|
|
// follow the previous.
|
2022-01-13 15:49:03 +00:00
|
|
|
func expression(res ...string) string {
|
2022-11-09 17:59:49 +00:00
|
|
|
return strings.Join(res, "")
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// optional wraps the expression in a non-capturing group and makes the
|
|
|
|
// production optional.
|
2022-01-13 15:49:03 +00:00
|
|
|
func optional(res ...string) string {
|
2022-11-09 18:02:30 +00:00
|
|
|
return `(?:` + strings.Join(res, "") + `)?`
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// repeated wraps the regexp in a non-capturing group to get one or more
|
|
|
|
// matches.
|
2022-01-13 15:49:03 +00:00
|
|
|
func repeated(res ...string) string {
|
2022-11-09 18:02:30 +00:00
|
|
|
return `(?:` + strings.Join(res, "") + `)+`
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// capture wraps the expression in a capturing group.
|
2022-01-13 15:49:03 +00:00
|
|
|
func capture(res ...string) string {
|
2022-11-09 17:59:49 +00:00
|
|
|
return `(` + strings.Join(res, "") + `)`
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// anchored anchors the regular expression by adding start and end delimiters.
|
2022-01-13 15:49:03 +00:00
|
|
|
func anchored(res ...string) string {
|
2022-11-09 17:59:49 +00:00
|
|
|
return `^` + strings.Join(res, "") + `$`
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|