2015-09-09 01:00:48 +02:00
|
|
|
package reference
|
|
|
|
|
|
|
|
import "regexp"
|
|
|
|
|
|
|
|
var (
|
2022-01-13 16:49:03 +01:00
|
|
|
// alphaNumeric defines the alpha numeric atom, typically a
|
2015-12-04 23:40:09 +01:00
|
|
|
// component of names. This only allows lower case characters and digits.
|
2022-01-13 16:49:03 +01:00
|
|
|
alphaNumeric = `[a-z0-9]+`
|
2015-10-10 01:01:01 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
// separator defines the separators allowed to be embedded in name
|
2015-12-04 23:40:09 +01:00
|
|
|
// components. This allow one period, one or two underscore and multiple
|
2020-07-09 16:43:10 +02: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 16:49:03 +01:00
|
|
|
separator = `(?:[._]|__|[-]*)`
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
// nameComponent restricts registry path component names to start
|
2015-12-04 23:40:09 +01: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 16:49:03 +01:00
|
|
|
nameComponent = expression(
|
|
|
|
alphaNumeric,
|
|
|
|
optional(repeated(separator, alphaNumeric)))
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
// domainComponent restricts the registry domain component of a
|
2017-02-08 02:43:28 +01:00
|
|
|
// repository name to start with a component as defined by DomainRegexp
|
2015-12-04 23:40:09 +01:00
|
|
|
// and followed by an optional port.
|
2022-01-13 16:49:03 +01:00
|
|
|
domainComponent = `(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])`
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
domain = expression(
|
|
|
|
domainComponent,
|
|
|
|
optional(repeated(literal(`.`), domainComponent)),
|
|
|
|
optional(literal(`:`), `[0-9]+`))
|
2017-02-08 02:43:28 +01:00
|
|
|
// DomainRegexp defines the structure of potential domain components
|
2015-12-04 23:40:09 +01: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 16:49:03 +01:00
|
|
|
DomainRegexp = re(domain)
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
tag = `[\w][\w.-]{0,127}`
|
2015-09-09 01:00:48 +02:00
|
|
|
// TagRegexp matches valid tag names. From docker/docker:graph/tags.go.
|
2022-01-13 16:49:03 +01:00
|
|
|
TagRegexp = re(tag)
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredTag = anchored(tag)
|
2015-09-09 01:00:48 +02:00
|
|
|
// anchoredTagRegexp matches valid tag names, anchored at the start and
|
|
|
|
// end of the matched string.
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredTagRegexp = re(anchoredTag)
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
digestPat = `[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}`
|
2015-10-10 02:09:54 +02:00
|
|
|
// DigestRegexp matches valid digests.
|
2022-01-13 16:49:03 +01:00
|
|
|
DigestRegexp = re(digestPat)
|
2015-10-10 02:09:54 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredDigest = anchored(digestPat)
|
2015-10-10 02:09:54 +02:00
|
|
|
// anchoredDigestRegexp matches valid digests, anchored at the start and
|
|
|
|
// end of the matched string.
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredDigestRegexp = re(anchoredDigest)
|
2015-10-10 02:09:54 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
namePat = expression(
|
|
|
|
optional(domain, literal(`/`)),
|
|
|
|
nameComponent,
|
|
|
|
optional(repeated(literal(`/`), nameComponent)))
|
2015-09-09 01:00:48 +02:00
|
|
|
// NameRegexp is the format for the name component of references. The
|
2016-06-09 20:32:23 +02:00
|
|
|
// regexp has capturing groups for the domain and name part omitting
|
2016-02-11 01:26:29 +01:00
|
|
|
// the separating forward slash from either.
|
2022-01-13 16:49:03 +01:00
|
|
|
NameRegexp = re(namePat)
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredName = anchored(
|
|
|
|
optional(capture(domain), literal(`/`)),
|
|
|
|
capture(nameComponent,
|
|
|
|
optional(repeated(literal(`/`), nameComponent))))
|
2015-12-04 23:40:09 +01:00
|
|
|
// anchoredNameRegexp is used to parse a name value, capturing the
|
2016-06-09 20:32:23 +02:00
|
|
|
// domain and trailing components.
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredNameRegexp = re(anchoredName)
|
2015-09-09 01:00:48 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
referencePat = anchored(capture(namePat),
|
|
|
|
optional(literal(":"), capture(tag)),
|
|
|
|
optional(literal("@"), capture(digestPat)))
|
2015-12-04 23:40:09 +01: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 16:49:03 +01:00
|
|
|
ReferenceRegexp = re(referencePat)
|
2016-06-15 23:04:21 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
identifier = `([a-f0-9]{64})`
|
2016-06-15 23:04:21 +02: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 16:49:03 +01:00
|
|
|
IdentifierRegexp = re(identifier)
|
2016-06-15 23:04:21 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
shortIdentifier = `([a-f0-9]{6,64})`
|
2016-06-15 23:04:21 +02: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 16:49:03 +01:00
|
|
|
ShortIdentifierRegexp = re(shortIdentifier)
|
2016-06-15 23:04:21 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredIdentifier = anchored(identifier)
|
2016-06-15 23:04:21 +02:00
|
|
|
// anchoredIdentifierRegexp is used to check or match an
|
|
|
|
// identifier value, anchored at start and end of string.
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredIdentifierRegexp = re(anchoredIdentifier)
|
2016-06-15 23:04:21 +02:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredShortIdentifier = anchored(shortIdentifier)
|
2016-06-15 23:04:21 +02:00
|
|
|
// anchoredShortIdentifierRegexp is used to check if a value
|
|
|
|
// is a possible identifier prefix, anchored at start and end
|
|
|
|
// of string.
|
2022-01-13 16:49:03 +01:00
|
|
|
anchoredShortIdentifierRegexp = re(anchoredShortIdentifier)
|
2015-09-09 01:00:48 +02:00
|
|
|
)
|
2015-12-04 23:40:09 +01:00
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
// re compiles the string to a regular expression.
|
|
|
|
var re = regexp.MustCompile
|
2015-12-04 23:40:09 +01:00
|
|
|
|
|
|
|
// literal compiles s into a literal regular expression, escaping any regexp
|
|
|
|
// reserved characters.
|
2022-01-13 16:49:03 +01:00
|
|
|
func literal(s string) string {
|
|
|
|
re := re(regexp.QuoteMeta(s))
|
2015-12-04 23:40:09 +01:00
|
|
|
|
|
|
|
if _, complete := re.LiteralPrefix(); !complete {
|
|
|
|
panic("must be a literal")
|
|
|
|
}
|
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
return re.String()
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// expression defines a full expression, where each regular expression must
|
|
|
|
// follow the previous.
|
2022-01-13 16:49:03 +01:00
|
|
|
func expression(res ...string) string {
|
2015-12-04 23:40:09 +01:00
|
|
|
var s string
|
|
|
|
for _, re := range res {
|
2022-01-13 16:49:03 +01:00
|
|
|
s += re
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|
|
|
|
|
2022-01-13 16:49:03 +01:00
|
|
|
return s
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// optional wraps the expression in a non-capturing group and makes the
|
|
|
|
// production optional.
|
2022-01-13 16:49:03 +01:00
|
|
|
func optional(res ...string) string {
|
|
|
|
return group(expression(res...)) + `?`
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// repeated wraps the regexp in a non-capturing group to get one or more
|
|
|
|
// matches.
|
2022-01-13 16:49:03 +01:00
|
|
|
func repeated(res ...string) string {
|
|
|
|
return group(expression(res...)) + `+`
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// group wraps the regexp in a non-capturing group.
|
2022-01-13 16:49:03 +01:00
|
|
|
func group(res ...string) string {
|
|
|
|
return `(?:` + expression(res...) + `)`
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// capture wraps the expression in a capturing group.
|
2022-01-13 16:49:03 +01:00
|
|
|
func capture(res ...string) string {
|
|
|
|
return `(` + expression(res...) + `)`
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// anchored anchors the regular expression by adding start and end delimiters.
|
2022-01-13 16:49:03 +01:00
|
|
|
func anchored(res ...string) string {
|
|
|
|
return `^` + expression(res...) + `$`
|
2015-12-04 23:40:09 +01:00
|
|
|
}
|