feat: added support for providing optional checksums for ZIP validation (#107)

Signed-off-by: Kim <kim.peter.wagner@gmail.com>
This commit is contained in:
Kim Wagner authored and GitHub committed 2026-04-13 16:46:38 -03:00
1 parent 03a9076efe
commit 4bcfe8cded
6 files changed
+153 -7

No files matched your search

+20 -3
View File
@@ -16,6 +16,7 @@ import {
error as logError,
exportVariable,
getInput,
getMultilineInput,
warning,
addPath
} from '@actions/core';
@@ -23,6 +24,7 @@ import { downloadTool, extractZip, find, cacheDir } from '@actions/tool-cache';
import { mv, cp, mkdirP } from '@actions/io';
import { getRelease } from './releases.js';
import { getErrorMessage } from './error-utils.js';
import { fileSHA256 } from './util.js';
// __dirname is not available in ES modules, so we need to construct it ourselves
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -46,7 +48,7 @@ function mapOS (os) {
return os;
}
async function downloadAndExtractCLI (url) {
async function downloadAndExtractCLI (url, checksums) {
debug(`Downloading OpenTofu CLI from ${url}`);
let pathToCLIZip;
try {
@@ -56,6 +58,20 @@ async function downloadAndExtractCLI (url) {
throw new Error(`Failed to download OpenTofu from ${url}: ${cause}`);
}
if (checksums.length > 0) {
let checksum;
try {
checksum = await fileSHA256(pathToCLIZip);
} catch (error) {
const cause = getErrorMessage(error);
throw new Error(`Failed to calculate the checksum for the OpenTofu CLI zip: ${cause}`);
}
if (!checksums.includes(checksum)) {
throw new Error(`Failed to validate OpenTofu CLI zip checksum. Received: ${checksum}. Valid checksums: ${checksums.join(', ')}`);
}
}
if (!pathToCLIZip) {
throw new Error(`Unable to download OpenTofu from ${url}`);
}
@@ -162,6 +178,7 @@ async function run () {
const credentialsToken = getInput('cli_config_credentials_token');
const wrapper = getInput('tofu_wrapper') === 'true';
const useCache = getInput('cache') === 'true';
const checksums = getMultilineInput('checksums');
let githubToken = getInput('github_token');
if (
githubToken === '' &&
@@ -217,12 +234,12 @@ async function run () {
pathToCLI = cachedPath;
} else {
debug(`OpenTofu version ${release.version} not found in cache, downloading...`);
const extractedPath = await downloadAndExtractCLI(build.url);
const extractedPath = await downloadAndExtractCLI(build.url, checksums);
debug(`Caching OpenTofu version ${release.version} to tool cache`);
pathToCLI = await cacheDir(extractedPath, 'tofu', release.version, buildArch);
}
} else {
pathToCLI = await downloadAndExtractCLI(build.url);
pathToCLI = await downloadAndExtractCLI(build.url, checksums);
}
// Install our wrapper