Module: Pray::Registry

Defined in:
lib/pray/registry.rb

Class Method Summary collapse

Class Method Details

.cache_ready?(cache_directory, selected) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
243
244
245
246
247
# File 'lib/pray/registry.rb', line 239

def cache_ready?(cache_directory, selected)
  return false unless File.directory?(cache_directory)

  spec_path = Resolve.find_prayspec_file(cache_directory)
  spec = Pray.parse_package_spec(File.read(spec_path)).canonicalized
  spec.version == selected.version
rescue Errno::ENOENT, Errno::ENOTDIR, SystemCallError
  false
end

.compare_versions(left, right) ⇒ Object



176
177
178
# File 'lib/pray/registry.rb', line 176

def compare_versions(left, right)
  Gem::Version.new(left) <=> Gem::Version.new(right)
end

.fetch_package_metadata(source_url, package_name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/pray/registry.rb', line 117

def (source_url, package_name)
  if local_source?(source_url)
     = (source_url, package_name)
    return (File.read(, encoding: "UTF-8"))
  end

  PathSafety.reject_unsafe_package_name!(package_name)
  response = http_get(join_url(source_url, "v1/packages/#{package_name}.json"))
  (response)
end

.http_get(url) ⇒ Object



302
303
304
305
306
307
308
309
310
# File 'lib/pray/registry.rb', line 302

def http_get(url)
  uri = URI(url)
  response = http_request(uri) { |http| http.get(uri.request_uri) }
  unless response.is_a?(Net::HTTPSuccess)
    raise Error.resolution("HTTP request failed for #{url}: #{response.code}")
  end

  response.body
end

.http_post(url, content_type, body) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/pray/registry.rb', line 325

def http_post(url, content_type, body)
  uri = URI(url)
  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = content_type
  request.body = body
  response = http_request(uri) { |http| http.request(request) }
  unless response.is_a?(Net::HTTPSuccess)
    raise Error.resolution("HTTP request failed for #{url}: #{response.code}")
  end

  response.body
end

.http_put(url, content_type, body) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/pray/registry.rb', line 312

def http_put(url, content_type, body)
  uri = URI(url)
  request = Net::HTTP::Put.new(uri)
  request["Content-Type"] = content_type
  request.body = body
  response = http_request(uri) { |http| http.request(request) }
  unless response.is_a?(Net::HTTPSuccess)
    raise Error.resolution("HTTP upload failed for #{url}: #{response.code}")
  end

  response.body
end

.http_request(uri) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/pray/registry.rb', line 338

def http_request(uri)
  Net::HTTP.start(
    uri.hostname,
    uri.port,
    use_ssl: uri.scheme == "https",
    open_timeout: 10,
    read_timeout: 30
  ) do |http|
    yield http
  end
end

.join_url(base, path) ⇒ Object



298
299
300
# File 'lib/pray/registry.rb', line 298

def join_url(base, path)
  URI.join(base.end_with?("/") ? base : "#{base}/", path.delete_prefix("/")).to_s
end

.local_source?(source_url) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/pray/registry.rb', line 294

def local_source?(source_url)
  !source_url.start_with?("http://", "https://", "pray+ssh://", "ssh+pray://")
end

.offline_package_error(package_name, version) ⇒ Object



290
291
292
# File 'lib/pray/registry.rb', line 290

def offline_package_error(package_name, version)
  "package #{package_name} #{version} is not cached and offline mode is enabled"
end

.parse_metadata(text) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/pray/registry.rb', line 128

def (text)
  data = JSON.parse(text)
  RegistryPackageMetadata.new(
    name: data["name"],
    versions: Array(data["versions"]).map { |entry| version_from_hash(entry) }
  )
end

.read_artifact_bytes(source_url, artifact) ⇒ Object



233
234
235
236
237
# File 'lib/pray/registry.rb', line 233

def read_artifact_bytes(source_url, artifact)
  return read_local_artifact_bytes(source_url, artifact) if local_source?(source_url)

  http_get(join_url(source_url, artifact))
end

.read_local_artifact_bytes(source_root, artifact) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/pray/registry.rb', line 216

def read_local_artifact_bytes(source_root, artifact)
  if artifact.start_with?("http://", "https://")
    return http_get(artifact)
  end
  if artifact.start_with?("file://")
    path = PathSafety.join_under_root(source_root, artifact.delete_prefix("file://"))
    raise Error.resolution("package artifact path escapes distribution root") unless path
    return File.binread(path)
  end

  path = PathSafety.join_under_root(source_root, artifact)
  raise Error.resolution("package artifact path escapes distribution root") unless path
  raise Error.resolution("package artifact missing at #{path}") unless File.exist?(path)

  File.binread(path)
end

.registry_artifact_signature(artifact_bytes, tree_hash, signer) ⇒ Object



350
351
352
353
# File 'lib/pray/registry.rb', line 350

def registry_artifact_signature(artifact_bytes, tree_hash, signer)
  payload = artifact_bytes + "\0".b + tree_hash.b + "\0".b + signer.b
  Hashing.sha256_prefixed(payload)
end

.registry_cache_directory(project_root, source_key, package_name, version, artifact_hash) ⇒ Object



279
280
281
282
283
284
285
286
287
288
# File 'lib/pray/registry.rb', line 279

def registry_cache_directory(project_root, source_key, package_name, version, artifact_hash)
  identifier = [
    source_key,
    package_name,
    version,
    artifact_hash || "no-artifact-hash"
  ].join(":")
  digest = Hashing.sha256_hex(identifier)[0, 16]
  File.join(project_root, ".pray", "cache", "registry", package_name.tr("/", "-"), version, digest)
end

.registry_latest_version_label(metadata) ⇒ Object



171
172
173
174
# File 'lib/pray/registry.rb', line 171

def registry_latest_version_label()
  highest = .versions.reject(&:yanked).max_by { |entry| Gem::Version.new(entry.version) }
  highest&.version
end

.registry_metadata_path(source_root, package_name) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/pray/registry.rb', line 249

def (source_root, package_name)
  PathSafety.reject_unsafe_package_name!(package_name)
  packages_root = PathSafety.join_under_root(source_root, "v1", "packages")
  raise Error.resolution("invalid package name: #{package_name.inspect}") unless packages_root

   = Pathname.new(packages_root).join("#{package_name}.json").cleanpath.to_s
  unless PathSafety.path_under_root?(packages_root, )
    raise Error.resolution("invalid package name: #{package_name.inspect}")
  end

  
end

.resolve_local_registry_package_root(project_root, source_key, source_root, declaration, preferred_version: nil, offline: false) ⇒ Object



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
# File 'lib/pray/registry.rb', line 73

def resolve_local_registry_package_root(project_root, source_key, source_root, declaration, preferred_version: nil, offline: false)
   = (source_root, declaration.name)
  unless File.exist?()
    raise Error.resolution(
      "package #{declaration.name} not found in distribution #{source_root.inspect}. " \
      "Missing #{}. Check the package name, version constraint `#{declaration.constraint}`, " \
      "and that the source publishes registry metadata."
    )
  end

   = (File.read(, encoding: "UTF-8"))
  registry_latest_version = registry_latest_version_label()
  selected = select_package_version(, declaration.constraint, preferred_version)
  cache_directory = registry_cache_directory(
    project_root,
    source_key,
    declaration.name,
    selected.version,
    selected.artifact_hash
  )

  if cache_ready?(cache_directory, selected)
    return RegistryPackageResolution.new(
      root: cache_directory,
      signer_fingerprint: selected.signer_fingerprint,
      registry_latest_version: registry_latest_version
    )
  end

  raise Error.resolution(offline_package_error(declaration.name, selected.version)) if offline

  FileUtils.rm_rf(cache_directory) if File.exist?(cache_directory)
  FileUtils.mkdir_p(cache_directory)

  artifact_bytes = read_local_artifact_bytes(source_root, selected.artifact)
  validate_and_unpack(cache_directory, declaration, selected, artifact_bytes, source_url: source_root)

  RegistryPackageResolution.new(
    root: cache_directory,
    signer_fingerprint: selected.signer_fingerprint,
    registry_latest_version: registry_latest_version
  )
end

.resolve_registry_package_root(project_root, source_url, declaration, preferred_version: nil, offline: false) ⇒ Object



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
# File 'lib/pray/registry.rb', line 38

def resolve_registry_package_root(project_root, source_url, declaration, preferred_version: nil, offline: false)
   = (source_url, declaration.name)
  registry_latest_version = registry_latest_version_label()
  selected = select_package_version(, declaration.constraint, preferred_version)
  cache_directory = registry_cache_directory(
    project_root,
    source_url,
    declaration.name,
    selected.version,
    selected.artifact_hash
  )

  if cache_ready?(cache_directory, selected)
    return RegistryPackageResolution.new(
      root: cache_directory,
      signer_fingerprint: selected.signer_fingerprint,
      registry_latest_version: registry_latest_version
    )
  end

  raise Error.resolution(offline_package_error(declaration.name, selected.version)) if offline

  FileUtils.rm_rf(cache_directory) if File.exist?(cache_directory)
  FileUtils.mkdir_p(cache_directory)

  artifact_bytes = read_artifact_bytes(source_url, selected.artifact)
  validate_and_unpack(cache_directory, declaration, selected, artifact_bytes, source_url: source_url)

  RegistryPackageResolution.new(
    root: cache_directory,
    signer_fingerprint: selected.signer_fingerprint,
    registry_latest_version: registry_latest_version
  )
end

.select_package_version(metadata, constraint, preferred_version) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/pray/registry.rb', line 152

def select_package_version(, constraint, preferred_version)
  if preferred_version
    version = .versions.find { |entry| entry.version == preferred_version && !entry.yanked }
    return version if version && Constraint.version_satisfies(version.version, constraint)
  end

  selected = nil
  .versions.each do |version|
    next if version.yanked
    next unless Constraint.version_satisfies(version.version, constraint)

    selected = version if selected.nil? || compare_versions(version.version, selected.version).positive?
  end

  raise Error.resolution("no registry version for #{.name} satisfies #{constraint}") unless selected

  selected
end

.validate_and_unpack(cache_directory, declaration, selected, artifact_bytes, source_url: nil) ⇒ Object



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
# File 'lib/pray/registry.rb', line 180

def validate_and_unpack(cache_directory, declaration, selected, artifact_bytes, source_url: nil)
  if selected.artifact_hash
    artifact_hash = Hashing.sha256_prefixed(artifact_bytes)
    if artifact_hash != selected.artifact_hash
      raise Error.integrity(
        "package artifact hash mismatch for #{declaration.name} #{selected.version}"
      )
    end
  end

  verify_registry_signature!(declaration, selected, artifact_bytes)
  Trust.verify_publisher_fingerprint!(source_url, selected) if source_url

  Archive.unpack_praypkg(artifact_bytes, cache_directory)
  spec_path = Resolve.find_prayspec_file(cache_directory)
  spec = Pray.parse_package_spec(File.read(spec_path)).canonicalized
  if spec.name != declaration.name
    raise Error.resolution(
      "package path #{cache_directory.inspect} declares #{spec.name.inspect}, expected #{declaration.name.inspect}"
    )
  end
  if spec.version != selected.version
    raise Error.resolution(
      "package #{declaration.name} version #{spec.version} does not match registry version #{selected.version}"
    )
  end
  if selected.tree_hash
    actual_tree_hash = spec.tree_hash_for_root(cache_directory)
    if actual_tree_hash != selected.tree_hash
      raise Error.integrity(
        "package tree hash mismatch for #{declaration.name} #{selected.version}"
      )
    end
  end
end

.verify_registry_signature!(declaration, selected, artifact_bytes) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/pray/registry.rb', line 262

def verify_registry_signature!(declaration, selected, artifact_bytes)
  return unless selected.signature

  unless selected.signer && selected.tree_hash
    raise Error.integrity(
      "package signature metadata incomplete for #{declaration.name} #{selected.version}"
    )
  end

  expected = registry_artifact_signature(artifact_bytes, selected.tree_hash, selected.signer)
  return if expected == selected.signature

  raise Error.integrity(
    "package signature mismatch for #{declaration.name} #{selected.version}"
  )
end

.version_from_hash(entry) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pray/registry.rb', line 136

def version_from_hash(entry)
  RegistryPackageVersion.new(
    version: entry["version"],
    artifact: entry["artifact"],
    artifact_hash: entry["artifact_hash"],
    tree_hash: entry["tree_hash"],
    yanked: entry["yanked"] || false,
    targets: entry["targets"] || [],
    exports: entry["exports"] || [],
    signer: entry["signer"],
    signer_fingerprint: entry["signer_fingerprint"],
    published_at: entry["published_at"],
    signature: entry["signature"]
  )
end