|
| 1 | +package storage |
| 2 | + |
| 3 | +// TODO(runcom): explain why we need this here!!! |
| 4 | + |
| 5 | +import "regexp" |
| 6 | + |
| 7 | +var ( |
| 8 | + // alphaNumericRegexp defines the alpha numeric atom, typically a |
| 9 | + // component of names. This only allows lower case characters and digits. |
| 10 | + alphaNumericRegexp = match(`[a-z0-9]+`) |
| 11 | + |
| 12 | + // separatorRegexp defines the separators allowed to be embedded in name |
| 13 | + // components. This allow one period, one or two underscore and multiple |
| 14 | + // dashes. |
| 15 | + separatorRegexp = match(`(?:[._]|__|[-]*)`) |
| 16 | + |
| 17 | + // nameComponentRegexp restricts registry path component names to start |
| 18 | + // with at least one letter or number, with following parts able to be |
| 19 | + // separated by one period, one or two underscore and multiple dashes. |
| 20 | + nameComponentRegexp = expression( |
| 21 | + alphaNumericRegexp, |
| 22 | + optional(repeated(separatorRegexp, alphaNumericRegexp))) |
| 23 | + |
| 24 | + // domainComponentRegexp restricts the registry domain component of a |
| 25 | + // repository name to start with a component as defined by domainRegexp |
| 26 | + // and followed by an optional port. |
| 27 | + domainComponentRegexp = match(`(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])`) |
| 28 | + |
| 29 | + // domainRegexp defines the structure of potential domain components |
| 30 | + // that may be part of image names. This is purposely a subset of what is |
| 31 | + // allowed by DNS to ensure backwards compatibility with Docker image |
| 32 | + // names. |
| 33 | + domainRegexp = expression( |
| 34 | + domainComponentRegexp, |
| 35 | + optional(repeated(literal(`.`), domainComponentRegexp)), |
| 36 | + optional(literal(`:`), match(`[0-9]+`))) |
| 37 | + |
| 38 | + // NameRegexp is the format for the name component of references. The |
| 39 | + // regexp has capturing groups for the domain and name part omitting |
| 40 | + // the separating forward slash from either. |
| 41 | + NameRegexp = expression( |
| 42 | + optional(domainRegexp, literal(`/`)), |
| 43 | + nameComponentRegexp, |
| 44 | + optional(repeated(literal(`/`), nameComponentRegexp))) |
| 45 | + |
| 46 | + // anchoredNameRegexp is used to parse a name value, capturing the |
| 47 | + // domain and trailing components. |
| 48 | + anchoredNameRegexp = anchored( |
| 49 | + optional(capture(domainRegexp), literal(`/`)), |
| 50 | + capture(nameComponentRegexp, |
| 51 | + optional(repeated(literal(`/`), nameComponentRegexp)))) |
| 52 | + |
| 53 | + // IdentifierRegexp is the format for string identifier used as a |
| 54 | + // content addressable identifier using sha256. These identifiers |
| 55 | + // are like digests without the algorithm, since sha256 is used. |
| 56 | + IdentifierRegexp = match(`([a-f0-9]{64})`) |
| 57 | + |
| 58 | + // ShortIdentifierRegexp is the format used to represent a prefix |
| 59 | + // of an identifier. A prefix may be used to match a sha256 identifier |
| 60 | + // within a list of trusted identifiers. |
| 61 | + ShortIdentifierRegexp = match(`([a-f0-9]{6,64})`) |
| 62 | +) |
| 63 | + |
| 64 | +// match compiles the string to a regular expression. |
| 65 | +var match = regexp.MustCompile |
| 66 | + |
| 67 | +// literal compiles s into a literal regular expression, escaping any regexp |
| 68 | +// reserved characters. |
| 69 | +func literal(s string) *regexp.Regexp { |
| 70 | + re := match(regexp.QuoteMeta(s)) |
| 71 | + |
| 72 | + if _, complete := re.LiteralPrefix(); !complete { |
| 73 | + panic("must be a literal") |
| 74 | + } |
| 75 | + |
| 76 | + return re |
| 77 | +} |
| 78 | + |
| 79 | +func splitDomain(name string) (string, string) { |
| 80 | + match := anchoredNameRegexp.FindStringSubmatch(name) |
| 81 | + if len(match) != 3 { |
| 82 | + return "", name |
| 83 | + } |
| 84 | + return match[1], match[2] |
| 85 | +} |
| 86 | + |
| 87 | +// expression defines a full expression, where each regular expression must |
| 88 | +// follow the previous. |
| 89 | +func expression(res ...*regexp.Regexp) *regexp.Regexp { |
| 90 | + var s string |
| 91 | + for _, re := range res { |
| 92 | + s += re.String() |
| 93 | + } |
| 94 | + |
| 95 | + return match(s) |
| 96 | +} |
| 97 | + |
| 98 | +// optional wraps the expression in a non-capturing group and makes the |
| 99 | +// production optional. |
| 100 | +func optional(res ...*regexp.Regexp) *regexp.Regexp { |
| 101 | + return match(group(expression(res...)).String() + `?`) |
| 102 | +} |
| 103 | + |
| 104 | +// repeated wraps the regexp in a non-capturing group to get one or more |
| 105 | +// matches. |
| 106 | +func repeated(res ...*regexp.Regexp) *regexp.Regexp { |
| 107 | + return match(group(expression(res...)).String() + `+`) |
| 108 | +} |
| 109 | + |
| 110 | +// group wraps the regexp in a non-capturing group. |
| 111 | +func group(res ...*regexp.Regexp) *regexp.Regexp { |
| 112 | + return match(`(?:` + expression(res...).String() + `)`) |
| 113 | +} |
| 114 | + |
| 115 | +// capture wraps the expression in a capturing group. |
| 116 | +func capture(res ...*regexp.Regexp) *regexp.Regexp { |
| 117 | + return match(`(` + expression(res...).String() + `)`) |
| 118 | +} |
| 119 | + |
| 120 | +// anchored anchors the regular expression by adding start and end delimiters. |
| 121 | +func anchored(res ...*regexp.Regexp) *regexp.Regexp { |
| 122 | + return match(`^` + expression(res...).String() + `$`) |
| 123 | +} |
0 commit comments