2015-09-08 23:00:48 +00:00
|
|
|
package reference
|
|
|
|
|
|
|
|
import "regexp"
|
|
|
|
|
|
|
|
var (
|
2022-01-13 15:49:03 +00:00
|
|
|
// alphaNumeric defines the alpha numeric atom, typically a
|
2015-12-04 22:40:09 +00:00
|
|
|
// component of names. This only allows lower case characters and digits.
|
2022-01-13 15:49:03 +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
|
2015-12-04 22:40:09 +00:00
|
|
|
// components. This allow 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-01-13 15:49:03 +00:00
|
|
|
// nameComponent restricts registry path component names to start
|
2015-12-04 22:40:09 +00:00
|
|
|
// with at least one letter or number, with following parts able to be
|
|
|
|
// separated by one period, one or two underscore and multiple dashes.
|
2022-01-13 15:49:03 +00:00
|
|
|
nameComponent = expression(
|
|
|
|
alphaNumeric,
|
|
|
|
optional(repeated(separator, alphaNumeric)))
|
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-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.
|
|
|
|
ipv6address = expression(
|
|
|
|
literal(`[`), `(?:[a-fA-F0-9:]+)`, literal(`]`),
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
domainName = expression(
|
|
|
|
domainNameComponent,
|
|
|
|
optional(repeated(literal(`.`), domainNameComponent)),
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-01-13 15:49:03 +00:00
|
|
|
domain = expression(
|
2022-06-25 10:23:38 +00:00
|
|
|
host,
|
2022-01-13 15:49:03 +00:00
|
|
|
optional(literal(`:`), `[0-9]+`))
|
2022-06-25 10:23:38 +00:00
|
|
|
|
2017-02-08 01:43:28 +00:00
|
|
|
// DomainRegexp defines the structure of potential domain components
|
2015-12-04 22:40:09 +00:00
|
|
|
// 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.
|
2022-01-13 21:06:06 +00:00
|
|
|
DomainRegexp = regexp.MustCompile(domain)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
tag = `[\w][\w.-]{0,127}`
|
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
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
anchoredTag = anchored(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-01-13 21:06:06 +00:00
|
|
|
anchoredTagRegexp = regexp.MustCompile(anchoredTag)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
digestPat = `[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}`
|
2015-10-10 00:09:54 +00:00
|
|
|
// DigestRegexp matches valid digests.
|
2022-01-13 21:06:06 +00:00
|
|
|
DigestRegexp = regexp.MustCompile(digestPat)
|
2015-10-10 00:09:54 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
anchoredDigest = anchored(digestPat)
|
2015-10-10 00:09:54 +00:00
|
|
|
// anchoredDigestRegexp matches valid digests, anchored at the start and
|
|
|
|
// end of the matched string.
|
2022-01-13 21:06:06 +00:00
|
|
|
anchoredDigestRegexp = regexp.MustCompile(anchoredDigest)
|
2015-10-10 00:09:54 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
namePat = expression(
|
|
|
|
optional(domain, literal(`/`)),
|
|
|
|
nameComponent,
|
|
|
|
optional(repeated(literal(`/`), nameComponent)))
|
2015-09-08 23:00:48 +00:00
|
|
|
// NameRegexp is the format for the name component of references. The
|
2016-06-09 18:32:23 +00:00
|
|
|
// regexp has capturing groups for the domain and name part omitting
|
2016-02-11 00:26:29 +00:00
|
|
|
// the separating forward slash from either.
|
2022-01-13 21:06:06 +00:00
|
|
|
NameRegexp = regexp.MustCompile(namePat)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
anchoredName = anchored(
|
|
|
|
optional(capture(domain), literal(`/`)),
|
|
|
|
capture(nameComponent,
|
|
|
|
optional(repeated(literal(`/`), nameComponent))))
|
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-01-13 21:06:06 +00:00
|
|
|
anchoredNameRegexp = regexp.MustCompile(anchoredName)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
referencePat = anchored(capture(namePat),
|
|
|
|
optional(literal(":"), capture(tag)),
|
|
|
|
optional(literal("@"), capture(digestPat)))
|
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
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
identifier = `([a-f0-9]{64})`
|
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
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
shortIdentifier = `([a-f0-9]{6,64})`
|
2016-06-15 21:04:21 +00:00
|
|
|
// ShortIdentifierRegexp is the format used to represent a prefix
|
|
|
|
// of an identifier. A prefix may be used to match a sha256 identifier
|
|
|
|
// within a list of trusted identifiers.
|
2022-01-13 21:06:06 +00:00
|
|
|
ShortIdentifierRegexp = regexp.MustCompile(shortIdentifier)
|
2016-06-15 21:04:21 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
anchoredIdentifier = anchored(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-01-13 21:06:06 +00:00
|
|
|
anchoredIdentifierRegexp = regexp.MustCompile(anchoredIdentifier)
|
2016-06-15 21:04:21 +00:00
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
anchoredShortIdentifier = anchored(shortIdentifier)
|
2016-06-15 21:04:21 +00:00
|
|
|
// anchoredShortIdentifierRegexp is used to check if a value
|
|
|
|
// is a possible identifier prefix, anchored at start and end
|
|
|
|
// of string.
|
2022-01-13 21:06:06 +00:00
|
|
|
anchoredShortIdentifierRegexp = regexp.MustCompile(anchoredShortIdentifier)
|
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 {
|
2015-12-04 22:40:09 +00:00
|
|
|
var s string
|
|
|
|
for _, re := range res {
|
2022-01-13 15:49:03 +00:00
|
|
|
s += re
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 15:49:03 +00:00
|
|
|
return s
|
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 {
|
|
|
|
return group(expression(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 {
|
|
|
|
return group(expression(res...)) + `+`
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// group wraps the regexp in a non-capturing group.
|
2022-01-13 15:49:03 +00:00
|
|
|
func group(res ...string) string {
|
|
|
|
return `(?:` + expression(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 {
|
|
|
|
return `(` + expression(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 {
|
|
|
|
return `^` + expression(res...) + `$`
|
2015-12-04 22:40:09 +00:00
|
|
|
}
|