|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execSync } from "node:child_process"; |
| 3 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 4 | + |
| 5 | +const JF_URL = "https://a0us.jfrog.io"; |
| 6 | +const OIDC_PROVIDER_NAME = "vercel"; |
| 7 | +const ARTIFACTORY_NPM_HOST = "a0us.jfrog.io/artifactory/api/npm/npm"; |
| 8 | +const PUBLIC_NPM_HOST = "registry.npmjs.org"; |
| 9 | +const BUILD_NPMRC_PATH = ".npmrc.oidc"; |
| 10 | + |
| 11 | +async function exchangeVercelOidcTokenForArtifactoryToken() { |
| 12 | + const oidcToken = process.env.VERCEL_OIDC_TOKEN; |
| 13 | + if (!oidcToken) { |
| 14 | + throw new Error( |
| 15 | + "VERCEL_OIDC_TOKEN is not set. Enable OIDC Federation for this " + |
| 16 | + "environment in Vercel Project Settings > Security > OIDC Federation." |
| 17 | + ); |
| 18 | + } |
| 19 | + |
| 20 | + const res = await fetch(`${JF_URL}/access/api/v1/oidc/token`, { |
| 21 | + method: "POST", |
| 22 | + headers: { "Content-Type": "application/json" }, |
| 23 | + body: JSON.stringify({ |
| 24 | + grant_type: "urn:ietf:params:oauth:grant-type:token-exchange", |
| 25 | + subject_token_type: "urn:ietf:params:oauth:token-type:id_token", |
| 26 | + subject_token: oidcToken, |
| 27 | + provider_name: OIDC_PROVIDER_NAME, |
| 28 | + }), |
| 29 | + }); |
| 30 | + |
| 31 | + if (!res.ok) { |
| 32 | + throw new Error( |
| 33 | + `JFrog OIDC token exchange failed (${res.status}): ${await res.text()}` |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + const { access_token } = await res.json(); |
| 38 | + if (!access_token) { |
| 39 | + throw new Error("JFrog OIDC token exchange returned no access_token."); |
| 40 | + } |
| 41 | + |
| 42 | + return access_token; |
| 43 | +} |
| 44 | + |
| 45 | +function pointLockfileAtArtifactory() { |
| 46 | + const lockfile = readFileSync("package-lock.json", "utf8"); |
| 47 | + const rewritten = lockfile.replaceAll( |
| 48 | + `https://${PUBLIC_NPM_HOST}/`, |
| 49 | + `https://${ARTIFACTORY_NPM_HOST}/` |
| 50 | + ); |
| 51 | + writeFileSync("package-lock.json", rewritten); |
| 52 | +} |
| 53 | + |
| 54 | +// Written fresh for this build only, under --userconfig, so the Artifactory |
| 55 | +// registry override never leaks into a plain `npm ci` elsewhere (e.g. the |
| 56 | +// GitHub Actions test workflows, which install straight from the public |
| 57 | +// registry and have no access to this token). |
| 58 | +function writeBuildScopedNpmrc(npmToken) { |
| 59 | + writeFileSync( |
| 60 | + BUILD_NPMRC_PATH, |
| 61 | + `registry=https://${ARTIFACTORY_NPM_HOST}/\n` + |
| 62 | + `//${ARTIFACTORY_NPM_HOST}/:_authToken=${npmToken}\n` |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +const npmToken = await exchangeVercelOidcTokenForArtifactoryToken(); |
| 67 | + |
| 68 | +// Only this ephemeral Vercel build checkout is rewritten to fetch through |
| 69 | +// Artifactory; the committed lockfile (used by GitHub Actions' npm ci) is |
| 70 | +// untouched and keeps resolving against the public registry. |
| 71 | +pointLockfileAtArtifactory(); |
| 72 | +writeBuildScopedNpmrc(npmToken); |
| 73 | + |
| 74 | +execSync(`npm ci --userconfig ${BUILD_NPMRC_PATH}`, { stdio: "inherit" }); |
0 commit comments