From 1d43bcfa6d4090109bfabef06b49a4069120dd33 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 11 Jul 2022 13:49:25 +0200 Subject: [PATCH] auth-backend: add script for re-publishing openid-client Co-authored-by: blam Signed-off-by: Patrik Oldsberg --- .../scripts/republish-openid-client.js | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100755 plugins/auth-backend/scripts/republish-openid-client.js diff --git a/plugins/auth-backend/scripts/republish-openid-client.js b/plugins/auth-backend/scripts/republish-openid-client.js new file mode 100755 index 0000000000..9d4497e590 --- /dev/null +++ b/plugins/auth-backend/scripts/republish-openid-client.js @@ -0,0 +1,109 @@ +#!/usr/bin/env node +/* eslint-disable import/no-extraneous-dependencies */ +/* + * Copyright 2022 The Backstage 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 + * + * http://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. + */ + +const fetch = require('node-fetch'); +const zlib = require('zlib'); + +// eslint-disable-next-line import/no-extraneous-dependencies +const libpub = require('libnpmpublish'); +const concatStream = require('concat-stream'); +const tar = require('tar-stream'); + +/* + +This works around an incompatibility in node versioning policies between Backstage and +the openid-client package. This script downloads a target version of the openid-client +package and re-publishes it as a new version of the openid-client-any-engine package. + +Usage: ./republish-openid-client.js + +Environment Variables: + +NPM_TOKEN: The NPM auth token to use for publishing. +NPM_OTP: The NPM OTP (2FA one time password) to use for publishing. + +*/ + +async function main(args) { + const [version] = args; + if (!version) { + throw new Error('No version provided, Usage: $0 '); + } + + const res = await fetch( + `https://registry.npmjs.org/openid-client/-/openid-client-${version}.tgz`, + ); + if (!res.ok) { + throw new Error(`Failed to fetch openid-client: ${res.status}`); + } + + const { data, manifest } = await new Promise((resolve, reject) => { + const extract = tar.extract(); + const pack = tar.pack(); + let foundPackageJson = undefined; + + res.body.pipe(zlib.createGunzip()).pipe(extract).on('error', reject); + + extract.on('entry', (header, stream, callback) => { + if (header.name === 'package/package.json') { + stream.pipe( + concatStream(fileContents => { + const packageJson = JSON.parse(fileContents.toString('utf8')); + packageJson.name = 'openid-client-any-engine'; + packageJson.description = + 'Re-publish of openid-client that allows any Node.js version above 12.19.0'; + packageJson.engines.node = '>=12.19.0'; + + foundPackageJson = packageJson; + + pack.entry( + { name: 'package/package.json' }, + JSON.stringify(packageJson, null, 2), + ); + callback(); + }), + ); + } else { + stream.pipe(pack.entry(header, callback)); + } + }); + + extract.on('finish', () => { + pack.finalize(); + }); + + pack + .pipe(zlib.createGzip()) + .pipe( + concatStream(d => { + resolve({ data: d, manifest: foundPackageJson }); + }), + ) + .on('error', reject); + }); + + await libpub.publish(manifest, data, { + token: process.env.NPM_TOKEN, + otp: process.env.NPM_OTP, + }); +} + +main(process.argv.slice(2)).catch(error => { + console.error(error); + process.exit(1); +});