fix(terraform): Adjust terraform ziphash fetching to mitigate caching issue (#25626)

This commit is contained in:
Daniel de Cloet 2023-11-07 10:08:17 +01:00 committed by GitHub
parent 6d48407d13
commit a61e45b55d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 61 deletions

View file

@ -426,18 +426,9 @@ describe('modules/datasource/terraform-provider/index', () => {
'fbdb892d9822ed0e4cb60f2fedbdbb556e4da0d88d3b942ae963ed6ff091e48f terraform-provider-azurerm_2.56.0_manifest.json'
);
const res = await terraformProviderDatasource.getZipHashes([
{
name: 'azurerm',
version: '2.56.0',
os: 'linux',
arch: 'amd64',
filename: 'terraform-provider-azurerm_2.56.0_linux_amd64.zip',
url: 'https://releases.hashicorp.com/terraform-provider-azurerm/2.56.0/terraform-provider-azurerm_2.56.0_linux_amd64.zip',
shasums_url:
'https://releases.hashicorp.com/terraform-provider-azurerm/2.56.0/terraform-provider-azurerm_2.56.0_SHA256SUMS',
},
]);
const res = await terraformProviderDatasource.getZipHashes(
'https://releases.hashicorp.com/terraform-provider-azurerm/2.56.0/terraform-provider-azurerm_2.56.0_SHA256SUMS'
);
expect(res).toMatchObject([
'500d4e787bf046bbe64c4853530aff3dfddee2fdbff0087d7b1e7a8c24388628',
@ -446,27 +437,6 @@ describe('modules/datasource/terraform-provider/index', () => {
]);
});
it('does not fetch anything when there are no builds passed in', async () => {
const res = await terraformProviderDatasource.getZipHashes([]);
expect(res).toBeEmptyArray();
});
it('does not fetch anything when there is no shasums_url defined', async () => {
const res = await terraformProviderDatasource.getZipHashes([
{
name: 'azurerm',
version: '2.56.0',
os: 'linux',
arch: 'amd64',
filename: 'terraform-provider-azurerm_2.56.0_linux_amd64.zip',
url: 'https://releases.hashicorp.com/terraform-provider-azurerm/2.56.0/terraform-provider-azurerm_2.56.0_linux_amd64.zip',
},
]);
expect(res).toBeEmptyArray();
});
it('does not hard fail when the ziphashes endpoint is not available', async () => {
httpMock
.scope(secondaryUrl)
@ -475,18 +445,9 @@ describe('modules/datasource/terraform-provider/index', () => {
)
.reply(404);
const res = await terraformProviderDatasource.getZipHashes([
{
name: 'azurerm',
version: '2.56.0',
os: 'linux',
arch: 'amd64',
filename: 'terraform-provider-azurerm_2.56.0_linux_amd64.zip',
url: 'https://releases.hashicorp.com/terraform-provider-azurerm/2.56.0/terraform-provider-azurerm_2.56.0_linux_amd64.zip',
shasums_url:
'https://releases.hashicorp.com/terraform-provider-azurerm/2.56.0/terraform-provider-azurerm_2.56.0_SHA256SUMS',
},
]);
const res = await terraformProviderDatasource.getZipHashes(
'https://releases.hashicorp.com/terraform-provider-azurerm/2.56.0/terraform-provider-azurerm_2.56.0_SHA256SUMS'
);
expect(res).toBeUndefined();
});

View file

@ -284,20 +284,9 @@ export class TerraformProviderDatasource extends TerraformDatasource {
@cache({
namespace: `datasource-${TerraformProviderDatasource.id}-zip-hashes`,
key: (registryURL: string, repository: string, version: string) =>
`${registryURL}/${repository}/${version}`,
key: (zipHashUrl: string) => zipHashUrl,
})
async getZipHashes(builds: TerraformBuild[]): Promise<string[] | undefined> {
if (builds.length === 0) {
return [];
}
const zipHashUrl = builds[0].shasums_url;
if (!zipHashUrl) {
return [];
}
async getZipHashes(zipHashUrl: string): Promise<string[] | undefined> {
// The hashes are formatted as the result of sha256sum in plain text, each line: <hash>\t<filename>
let rawHashData: string;
try {

View file

@ -115,9 +115,14 @@ export class TerraformProviderHash {
return null;
}
const zhHashes =
(await TerraformProviderHash.terraformDatasource.getZipHashes(builds)) ??
[];
let zhHashes: string[] = [];
if (builds.length > 0 && builds[0].shasums_url) {
zhHashes =
(await TerraformProviderHash.terraformDatasource.getZipHashes(
builds[0].shasums_url
)) ?? [];
}
const h1Hashes = await TerraformProviderHash.calculateHashScheme1Hashes(
builds
);