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
+63 -1
View File
@@ -14,6 +14,7 @@ import { join } from 'path';
// Set up mocks BEFORE dynamic imports
jest.unstable_mockModule('@actions/core', () => ({
getInput: jest.fn(),
getMultilineInput: jest.fn(),
setOutput: jest.fn(),
setFailed: jest.fn(),
debug: jest.fn(),
@@ -41,12 +42,17 @@ jest.unstable_mockModule('../releases.js', () => ({
Release: jest.fn()
}));
jest.unstable_mockModule('../util.js', () => ({
fileSHA256: jest.fn()
}));
// Dynamic imports pick up the mocks
const { getInput, warning } = await import('@actions/core');
const { getInput, getMultilineInput, warning } = await import('@actions/core');
const tc = await import('@actions/tool-cache');
const io = await import('@actions/io');
const { getRelease } = await import('../releases.js');
const { default: setup } = await import('../setup-tofu.js');
const { fileSHA256 } = await import('../util.js');
// Set up global test fixtures
const fallbackVersion = 'latest';
@@ -94,6 +100,13 @@ describe('setup-tofu', () => {
return defaults[name] || '';
});
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: []
};
return defaults[name] || [];
});
// Mock environment variables
process.env.GITHUB_TOKEN = 'mock-github-token';
});
@@ -278,4 +291,53 @@ describe('setup-tofu', () => {
expect(tc.cacheDir).not.toHaveBeenCalled();
});
});
describe('checksum validation', () => {
const mockChecksum = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6';
const mockChecksum2 = 'fcfac0a865b57e0711cb8bae47c8fc70ad93198fa6146a40020ea459715fda83';
const mockInvalidChecksum = 'invalidchecksuminvalidchecksuminvalidchecksuminvalidchecksum';
fileSHA256.mockResolvedValue(mockChecksum);
it('should validate checksum when checksums are provided', async () => {
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: [mockInvalidChecksum, mockChecksum]
};
return defaults[name] ?? [];
});
await setup();
expect(fileSHA256).toHaveBeenCalled();
});
it('should not validate checksum when no checksums are provided', async () => {
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: []
};
return defaults[name] ?? [];
});
await setup();
expect(fileSHA256).not.toHaveBeenCalled();
});
it('should throw error when checksum validation fails', async () => {
fileSHA256.mockResolvedValue(mockInvalidChecksum);
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: [mockChecksum, mockChecksum2]
};
return defaults[name] ?? [];
});
await expect(setup()).rejects.toMatchObject(new Error(
`Failed to validate OpenTofu CLI zip checksum. Received: ${mockInvalidChecksum}. Valid checksums: ${mockChecksum}, ${mockChecksum2}`));
expect(fileSHA256).toHaveBeenCalled();
});
});
});
+16
View File
@@ -0,0 +1,16 @@
/**
* Copyright (c) OpenTofu
* SPDX-License-Identifier: MPL-2.0
*/
import { createHash } from 'crypto';
import { createReadStream } from 'fs';
import { pipeline } from 'stream/promises';
export async function fileSHA256 (filePath) {
const hash = createHash('sha256');
const fileStream = createReadStream(filePath);
await pipeline(fileStream, hash);
return hash.digest('hex');
}