(output: Log, platformInfo: PlatformInfo, name: string)
| 318 | } |
| 319 | |
| 320 | export async function inspectImageInRegistry(output: Log, platformInfo: PlatformInfo, name: string): Promise<ImageDetails> { |
| 321 | const resourceAndVersion = qualifyImageName(name); |
| 322 | const params = { output, env: process.env }; |
| 323 | const ref = getRef(output, resourceAndVersion); |
| 324 | if (!ref) { |
| 325 | throw new Error(`Could not parse image name '${name}'`); |
| 326 | } |
| 327 | |
| 328 | const registryServer = ref.registry === 'docker.io' ? 'registry-1.docker.io' : ref.registry; |
| 329 | const manifestUrl = `https://${registryServer}/v2/${ref.path}/manifests/${ref.version}`; |
| 330 | output.write(`manifest url: ${manifestUrl}`, LogLevel.Trace); |
| 331 | |
| 332 | let targetDigest: string | undefined = undefined; |
| 333 | const manifest = await getManifest(params, manifestUrl, ref, 'application/vnd.docker.distribution.manifest.v2+json'); |
| 334 | if (manifest?.manifestObj.config) { // Checking for config because the above mime type sometimes returns an image index. |
| 335 | targetDigest = manifest.manifestObj.config.digest; |
| 336 | } else { |
| 337 | // If we couldn't fetch the manifest, perhaps the registry supports querying for the 'Image Index' |
| 338 | // Spec: https://github.com/opencontainers/image-spec/blob/main/image-index.md |
| 339 | const imageIndexEntry = await getImageIndexEntryForPlatform(params, manifestUrl, ref, platformInfo); |
| 340 | if (imageIndexEntry) { |
| 341 | const manifestUrl = `https://${registryServer}/v2/${ref.path}/manifests/${imageIndexEntry.digest}`; |
| 342 | const a = await getManifest(params, manifestUrl, ref); |
| 343 | if (a) { |
| 344 | targetDigest = a.manifestObj.config.digest; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (!targetDigest) { |
| 350 | throw new Error(`No manifest found for ${resourceAndVersion}.`); |
| 351 | } |
| 352 | |
| 353 | const blobUrl = `https://${registryServer}/v2/${ref.path}/blobs/${targetDigest}`; |
| 354 | output.write(`blob url: ${blobUrl}`, LogLevel.Trace); |
| 355 | |
| 356 | const httpOptions = { |
| 357 | type: 'GET', |
| 358 | url: blobUrl, |
| 359 | headers: {} |
| 360 | }; |
| 361 | |
| 362 | const res = await requestEnsureAuthenticated(params, httpOptions, ref); |
| 363 | if (!res) { |
| 364 | throw new Error(`Failed to fetch blob for ${resourceAndVersion}.`); |
| 365 | } |
| 366 | const blob = res.resBody.toString(); |
| 367 | const obj = JSON.parse(blob); |
| 368 | return { |
| 369 | Id: targetDigest, |
| 370 | Config: obj.config, |
| 371 | Os: platformInfo.os, |
| 372 | Variant: platformInfo.variant, |
| 373 | Architecture: platformInfo.arch, |
| 374 | }; |
| 375 | } |
| 376 | |
| 377 | export function qualifyImageName(name: string) { |
no test coverage detected