Class: DockerDistribution::Regexp

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_distribution/regexp.rb

Overview

Representation of regxexps we will use

Class Method Summary collapse

Class Method Details

.alpha_numericObject

alphaNumeric defines the alpha numeric atom, typically a component of names. This only allows lower case characters and digits.



10
11
12
# File 'lib/docker_distribution/regexp.rb', line 10

def alpha_numeric
  "[a-z0-9]+"
end

.anchored(*res) ⇒ Object

anchored anchors the regular expression by adding start and end delimiters.



210
211
212
# File 'lib/docker_distribution/regexp.rb', line 210

def anchored(*res)
  "^#{expression(*res)}$"
end

.anchored_digestObject



102
103
104
# File 'lib/docker_distribution/regexp.rb', line 102

def anchored_digest
  anchored(digest_pat)
end

.anchored_digest_regexpObject

anchoredDigestRegexp matches valid digests, anchored at the start and end of the matched string.



107
108
109
# File 'lib/docker_distribution/regexp.rb', line 107

def anchored_digest_regexp
  ::Regexp.new(anchored_digest)
end

.anchored_encoded_regexp(type) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/docker_distribution/regexp.rb', line 179

def anchored_encoded_regexp(type)
  map = {
    SHA256: ::Regexp.new("^[a-f0-9]{64}$"),
    SHA384: ::Regexp.new("^[a-f0-9]{96}$"),
    SHA512: ::Regexp.new("^[a-f0-9]{128}$")
  }
  map[type.to_sym]
end

.anchored_identifierObject



161
162
163
# File 'lib/docker_distribution/regexp.rb', line 161

def anchored_identifier
  anchored(identifier)
end

.anchored_identifier_regexpObject

anchoredIdentifierRegexp is used to check or match an # identifier value, anchored at start and end of string.



166
167
168
# File 'lib/docker_distribution/regexp.rb', line 166

def anchored_identifier_regexp
  ::Regexp.new(anchored_identifier)
end

.anchored_nameObject



122
123
124
# File 'lib/docker_distribution/regexp.rb', line 122

def anchored_name
  anchored(optional(capture(domain), literal("/")), capture(name_component, repeated(literal("/"), name_component)))
end

.anchored_name_regexpObject

anchoredNameRegexp is used to parse a name value, capturing the domain and trailing components.



127
128
129
# File 'lib/docker_distribution/regexp.rb', line 127

def anchored_name_regexp
  ::Regexp.new(anchored_name)
end

.anchored_short_identifierObject



170
171
172
# File 'lib/docker_distribution/regexp.rb', line 170

def anchored_short_identifier
  anchored(short_identifier)
end

.anchored_short_identifier_regexpObject

anchoredShortIdentifierRegexp is used to check if a value is a possible identifier prefix, anchored at start and end of string.



175
176
177
# File 'lib/docker_distribution/regexp.rb', line 175

def anchored_short_identifier_regexp
  ::Regexp.new(anchored_short_identifier)
end

.anchored_tagObject



84
85
86
# File 'lib/docker_distribution/regexp.rb', line 84

def anchored_tag
  anchored(tag)
end

.anchored_tag_regexpObject

anchoredTagRegexp matches valid tag names, anchored at the start and end of the matched string.



89
90
91
# File 'lib/docker_distribution/regexp.rb', line 89

def anchored_tag_regexp
  ::Regexp.new(anchored_tag)
end

.capture(*res) ⇒ Object

capture wraps the expression in a capturing group.



220
221
222
# File 'lib/docker_distribution/regexp.rb', line 220

def capture(*res)
  "(#{expression(*res)})"
end

.digest_patObject



93
94
95
# File 'lib/docker_distribution/regexp.rb', line 93

def digest_pat
  "[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}"
end

.digest_regexpObject

DigestRegexp matches valid digests.



98
99
100
# File 'lib/docker_distribution/regexp.rb', line 98

def digest_regexp
  ::Regexp.new(digest_pat)
end

.domainObject

allowed by the URI Host subcomponent on rfc3986 to ensure backwards compatibility with Docker image names.



65
66
67
# File 'lib/docker_distribution/regexp.rb', line 65

def domain
  expression(host, optional(literal(":"), "[0-9]+"))
end

.domain_nameObject

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.



50
51
52
# File 'lib/docker_distribution/regexp.rb', line 50

def domain_name
  expression(domain_name_component, repeated(literal("."), domain_name_component))
end

.domain_name_componentObject

domainNameComponent restricts the registry domain component of a repository name to start with a component as defined by DomainRegexp.



34
35
36
# File 'lib/docker_distribution/regexp.rb', line 34

def domain_name_component
  "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])"
end

.domain_regexpObject

DomainRegexp 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.



71
72
73
# File 'lib/docker_distribution/regexp.rb', line 71

def domain_regexp
  @domain_regexp ||= ::Regexp.new(domain)
end

.expression(*res) ⇒ Object

expression defines a full expression, where each regular expression must follow the previous.



195
196
197
# File 'lib/docker_distribution/regexp.rb', line 195

def expression(*res)
  res.join("")
end

.group(*res) ⇒ Object

group wraps the regexp in a non-capturing group.



200
201
202
# File 'lib/docker_distribution/regexp.rb', line 200

def group(*res)
  "(?:#{expression(*res)})"
end

.hostObject

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).



59
60
61
# File 'lib/docker_distribution/regexp.rb', line 59

def host
  "(?:#{domain_name}|#{ipv6address})"
end

.identifierObject



141
142
143
# File 'lib/docker_distribution/regexp.rb', line 141

def identifier
  "([a-f0-9]{64})"
end

.identifier_regexpObject

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.



147
148
149
# File 'lib/docker_distribution/regexp.rb', line 147

def identifier_regexp
  ::Regexp.new(identifier)
end

.ipv6addressObject

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.



42
43
44
# File 'lib/docker_distribution/regexp.rb', line 42

def ipv6address
  expression(literal("["), "(?:[a-fA-F0-9:]+)", literal("]"))
end

.literal(str) ⇒ Object

literal compiles s into a literal regular expression, escaping any regexp reserved characters.



189
190
191
192
# File 'lib/docker_distribution/regexp.rb', line 189

def literal(str)
  re = ::Regexp.new(::Regexp.quote(str))
  re.source
end

.name_componentObject

nameComponent restricts registry path component names 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.



28
29
30
# File 'lib/docker_distribution/regexp.rb', line 28

def name_component
  expression(alpha_numeric, repeated(separator, alpha_numeric))
end

.name_patObject



111
112
113
# File 'lib/docker_distribution/regexp.rb', line 111

def name_pat
  expression(optional(domain, literal("/")), name_component, repeated(literal("/"), name_component))
end

.name_regexpObject

NameRegexp is the format for the name component of references. The regexp has capturing groups for the domain and name part omitting the separating forward slash from either.



118
119
120
# File 'lib/docker_distribution/regexp.rb', line 118

def name_regexp
  ::Regexp.new(name_pat)
end

.optional(*res) ⇒ Object

optional wraps the expression in a non-capturing group and makes the production optional.



205
206
207
# File 'lib/docker_distribution/regexp.rb', line 205

def optional(*res)
  "#{group(expression(*res))}?"
end

.reference_patObject



131
132
133
# File 'lib/docker_distribution/regexp.rb', line 131

def reference_pat
  anchored(capture(name_pat), optional(literal(":"), capture(tag)), optional(literal("@"), capture(digest_pat)))
end

.reference_regexpObject

ReferenceRegexp is the full supported format of a reference. The regexp is anchored and has capturing groups for name, tag, and digest components.



137
138
139
# File 'lib/docker_distribution/regexp.rb', line 137

def reference_regexp
  ::Regexp.new(reference_pat)
end

.repeated(*res) ⇒ Object

repeated wraps the regexp in a non-capturing group to get zero or more matches.



215
216
217
# File 'lib/docker_distribution/regexp.rb', line 215

def repeated(*res)
  group("#{group(expression(*res))}*")
end

.separatorObject

separator defines the separators allowed to be embedded in name components. This allow one period, one or two underscore and multiple 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.



21
22
23
# File 'lib/docker_distribution/regexp.rb', line 21

def separator
  "(?:[._]|__|[-]*)"
end

.short_identifierObject



151
152
153
# File 'lib/docker_distribution/regexp.rb', line 151

def short_identifier
  "([a-f0-9]{6,64})"
end

.short_identifier_regexpObject

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.



157
158
159
# File 'lib/docker_distribution/regexp.rb', line 157

def short_identifier_regexp
  ::Regexp.new(short_identifier)
end

.tagObject



75
76
77
# File 'lib/docker_distribution/regexp.rb', line 75

def tag
  "[\\w][\\w.-]{0,127}"
end

.tag_regexpObject

TagRegexp matches valid tag names



80
81
82
# File 'lib/docker_distribution/regexp.rb', line 80

def tag_regexp
  ::Regexp.new(tag)
end