|
| 1 | +import { exec } from "@actions/exec"; |
| 2 | +import * as core from "@actions/core"; |
| 3 | +import * as toolCache from "@actions/tool-cache"; |
| 4 | + |
| 5 | +export async function setupKeys() { |
| 6 | + core.debug("Fetching verification keys"); |
| 7 | + let path = await toolCache.downloadTool( |
| 8 | + "https://swift.org/keys/all-keys.asc" |
| 9 | + ); |
| 10 | + |
| 11 | + core.debug("Importing verification keys"); |
| 12 | + await exec(`gpg --import "${path}"`); |
| 13 | + |
| 14 | + core.debug("Refreshing keys"); |
| 15 | + await refreshKeys(); |
| 16 | +} |
| 17 | + |
| 18 | +export async function verify(signaturePath: string, packagePath: string) { |
| 19 | + core.debug("Verifying signature"); |
| 20 | + await exec("gpg", ["--verify", signaturePath, packagePath]); |
| 21 | +} |
| 22 | + |
| 23 | +export async function refreshKeys() { |
| 24 | + const pool = [ |
| 25 | + "hkp://pool.sks-keyservers.net", |
| 26 | + "ha.pool.sks-keyservers.net", |
| 27 | + "keyserver.ubuntu.com", |
| 28 | + "hkp://keyserver.ubuntu.com", |
| 29 | + "pgp.mit.edu", |
| 30 | + ]; |
| 31 | + |
| 32 | + for (const server of pool) { |
| 33 | + core.debug(`Refreshing keys from ${server}`); |
| 34 | + if (await refreshKeysFromServer(server)) { |
| 35 | + core.debug(`Refresh successful`); |
| 36 | + return; |
| 37 | + } |
| 38 | + core.debug(`Refresh failed`); |
| 39 | + } |
| 40 | + |
| 41 | + throw new Error("Failed to refresh keys from any server in the pool."); |
| 42 | +} |
| 43 | + |
| 44 | +function refreshKeysFromServer(server: string): Promise<boolean> { |
| 45 | + return exec(`gpg --keyserver ${server} --refresh-keys Swift`) |
| 46 | + .then((code) => code === 0) |
| 47 | + .catch((error) => { |
| 48 | + core.warning( |
| 49 | + `An error occurred when trying to refresh keys from ${server}: ${error}` |
| 50 | + ); |
| 51 | + return false; |
| 52 | + }); |
| 53 | +} |
0 commit comments