forked from apple/containerization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReference.swift
More file actions
285 lines (253 loc) · 10.8 KB
/
Reference.swift
File metadata and controls
285 lines (253 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//===----------------------------------------------------------------------===//
// Copyright © 2025-2026 Apple Inc. and the Containerization project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerizationError
import Foundation
// nameTotalLengthMax matches the OCI distribution spec which allows up to 255 bytes for the
// repository name component (domain + "/" + path).
private let nameTotalLengthMax = 255
// referenceTotalLengthMax is the upper bound for the full reference string: max name (255) +
// separator (1) + max tag length (128) = 384.
private let tagLengthMax = 128
private let referenceTotalLengthMax = nameTotalLengthMax + 1 + tagLengthMax
private let legacyDockerRegistryHost = "docker.io"
private let dockerRegistryHost = "registry-1.docker.io"
private let defaultDockerRegistryRepo = "library"
private let defaultTag = "latest"
/// A Reference is composed of the various parts of an OCI image reference.
/// For example:
/// let imageReference = "my-registry.com/repository/image:tag2"
/// let reference = Reference.parse(imageReference)
/// print(reference.domain!) // gives us "my-registry.com"
/// print(reference.name) // gives us "my-registry.com/repository/image"
/// print(reference.path) // gives us "repository/image"
/// print(reference.tag!) // gives us "tag2"
/// print(reference.digest) // gives us "nil"
public class Reference: CustomStringConvertible {
private var _domain: String?
public var domain: String? {
_domain
}
public var resolvedDomain: String? {
if let d = _domain {
return Self.resolveDomain(domain: d)
}
return nil
}
private var _path: String
public var path: String {
_path
}
private var _tag: String?
public var tag: String? {
_tag
}
private var _digest: String?
public var digest: String? {
_digest
}
public var name: String {
if let domain, !domain.isEmpty {
return "\(domain)/\(path)"
}
return path
}
public var description: String {
if let tag {
return "\(name):\(tag)"
}
if let digest {
return "\(name)@\(digest)"
}
return name
}
static let identifierPattern = "([a-f0-9]{64})"
static let domainPattern = {
let domainNameComponent = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])"
let optionalPort = "(?::[0-9]+)?"
let ipv6address = "\\[(?:[a-fA-F0-9:]+)\\]"
let domainName = "\(domainNameComponent)(?:\\.\(domainNameComponent))*"
let host = "(?:\(domainName)|\(ipv6address))"
let domainAndPort = "\(host)\(optionalPort)"
return domainAndPort
}()
static let pathPattern = "(?<path>(?:[a-z0-9]+(?:[._]|__|-|/)?)*[a-z0-9]+)"
static let tagPattern = "(?::(?<tag>[\\w][\\w.-]{0,127}))?(?:@(?<digest>sha256:[0-9a-fA-F]{64}))?"
static let pathTagPattern = "\(pathPattern)\(tagPattern)"
public init(path: String, domain: String? = nil, tag: String? = nil, digest: String? = nil) throws {
if let domain, !domain.isEmpty {
self._domain = domain
}
self._path = path
self._tag = tag
self._digest = digest
}
public static func parse(_ s: String) throws -> Reference {
if s.count > referenceTotalLengthMax {
throw ContainerizationError(.invalidArgument, message: "reference length \(s.count) greater than \(referenceTotalLengthMax)")
}
let identifierRegex = try Regex(Self.identifierPattern)
guard try identifierRegex.wholeMatch(in: s) == nil else {
throw ContainerizationError(.invalidArgument, message: "cannot specify 64 byte hex string as reference")
}
let (domain, remainder) = try Self.parseDomain(from: s)
let constructedRawReference: String = remainder
if let domain {
let domainRegex = try Regex(domainPattern)
guard try domainRegex.wholeMatch(in: domain) != nil else {
throw ContainerizationError(.invalidArgument, message: "invalid domain \(domain) for reference \(s)")
}
}
let fields = try constructedRawReference.matches(regex: pathTagPattern)
guard let path = fields["path"] else {
throw ContainerizationError(.invalidArgument, message: "cannot parse path for reference \(s)")
}
let ref = try Reference(path: path, domain: domain)
if ref.name.count > nameTotalLengthMax {
throw ContainerizationError(.invalidArgument, message: "repo length \(ref.name.count) greater than \(nameTotalLengthMax)")
}
// Extract tag and digest
let tag = fields["tag"] ?? ""
let digest = fields["digest"] ?? ""
if !digest.isEmpty {
return try ref.withDigest(digest)
} else if !tag.isEmpty {
return try ref.withTag(tag)
}
return ref
}
private static func parseDomain(from s: String) throws -> (domain: String?, remainder: String) {
var domain: String? = nil
var path: String = s
let charset = CharacterSet(charactersIn: ".:")
let splits = s.split(separator: "/", maxSplits: 1)
guard splits.count == 2 else {
if s.starts(with: "localhost") {
return (s, "")
}
return (nil, s)
}
let _domain = String(splits[0])
let _path = String(splits[1])
if _domain.starts(with: "localhost") || _domain.rangeOfCharacter(from: charset) != nil {
domain = _domain
path = _path
}
return (domain, path)
}
public static func withName(_ name: String) throws -> Reference {
if name.count > nameTotalLengthMax {
throw ContainerizationError(.invalidArgument, message: "name length \(name.count) greater than \(nameTotalLengthMax)")
}
let fields = try name.matches(regex: Self.domainPattern)
// Extract domain and path
let domain = fields["domain"] ?? ""
let path = fields["path"] ?? ""
if domain.isEmpty || path.isEmpty {
throw ContainerizationError(.invalidArgument, message: "image reference domain or path is empty")
}
return try Reference(path: path, domain: domain)
}
public func withTag(_ tag: String) throws -> Reference {
var tag = tag
if !tag.starts(with: ":") {
tag = ":" + tag
}
let fields = try tag.matches(regex: Self.tagPattern)
tag = fields["tag"] ?? ""
if tag.isEmpty {
throw ContainerizationError(.invalidArgument, message: "invalid format for image reference, missing tag")
}
return try Reference(path: self.path, domain: self.domain, tag: tag)
}
public func withDigest(_ digest: String) throws -> Reference {
var digest = digest
if !digest.starts(with: "@") {
digest = "@" + digest
}
let fields = try digest.matches(regex: Self.tagPattern)
digest = fields["digest"] ?? ""
if digest.isEmpty {
throw ContainerizationError(.invalidArgument, message: "invalid format for image reference, missing digest")
}
return try Reference(path: self.path, domain: self.domain, digest: digest)
}
private static func splitDomain(_ name: String) -> (domain: String, path: String) {
let parts = name.split(separator: "/")
guard parts.count == 2 else {
return ("", name)
}
return (String(parts[0]), String(parts[1]))
}
/// Normalize the reference object.
/// Normalization is useful in cases where the reference object is to be used to
/// fetch/push an image from/to a remote registry.
/// It does the following:
/// - Adds a default tag of "latest" if the reference had no tag/digest set.
/// - If the domain is "registry-1.docker.io" or "docker.io" and the path has no repository set,
/// it adds a default "library/" repository name.
public func normalize() {
if let domain = self.domain, domain == dockerRegistryHost || domain == legacyDockerRegistryHost {
// Check if the image is being referenced by a named tag.
// If it is, and a repository is not specified, prefix it with "library/".
// This needs to be done only if we are using the Docker registry.
if !self.path.contains("/") {
self._path = "\(defaultDockerRegistryRepo)/\(self._path)"
}
}
let identifier = self._tag ?? self._digest
if identifier == nil {
// If the user did not specify a tag or a digest for the reference, set the tag to "latest".
self._tag = defaultTag
}
}
public static func resolveDomain(domain: String) -> String {
if domain == legacyDockerRegistryHost {
return dockerRegistryHost
}
return domain
}
}
extension String {
func matches(regex: String) throws -> [String: String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsRange = NSRange(self.startIndex..<self.endIndex, in: self)
guard let match = regex.firstMatch(in: self, options: [], range: nsRange), match.range == nsRange else {
throw ContainerizationError(.invalidArgument, message: "invalid format for image reference")
}
var results = [String: String]()
for name in try regex.captureGroupNames() {
if let range = Range(match.range(withName: name), in: self) {
results[name] = String(self[range])
}
}
return results
} catch {
throw error
}
}
}
extension NSRegularExpression {
func captureGroupNames() throws -> [String] {
let pattern = self.pattern
let regex = try NSRegularExpression(pattern: "\\(\\?<(\\w+)>", options: [])
let nsRange = NSRange(pattern.startIndex..<pattern.endIndex, in: pattern)
let matches = regex.matches(in: pattern, options: [], range: nsRange)
return matches.map {
String(pattern[Range($0.range(at: 1), in: pattern)!])
}
}
}