From a61e45b55da9799c792179ed7836638b874b7be5 Mon Sep 17 00:00:00 2001 From: Daniel de Cloet <34800550+Daniel-I-Am@users.noreply.github.com> Date: Tue, 7 Nov 2023 10:08:17 +0100 Subject: [PATCH] fix(terraform): Adjust terraform ziphash fetching to mitigate caching issue (#25626) --- .../terraform-provider/index.spec.ts | 51 +++---------------- .../datasource/terraform-provider/index.ts | 15 +----- .../manager/terraform/lockfile/hash.ts | 11 ++-- 3 files changed, 16 insertions(+), 61 deletions(-) diff --git a/lib/modules/datasource/terraform-provider/index.spec.ts b/lib/modules/datasource/terraform-provider/index.spec.ts index e9108a2b2b..531861d27d 100644 --- a/lib/modules/datasource/terraform-provider/index.spec.ts +++ b/lib/modules/datasource/terraform-provider/index.spec.ts @@ -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(); }); diff --git a/lib/modules/datasource/terraform-provider/index.ts b/lib/modules/datasource/terraform-provider/index.ts index 1ebe37c869..5e66240a50 100644 --- a/lib/modules/datasource/terraform-provider/index.ts +++ b/lib/modules/datasource/terraform-provider/index.ts @@ -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 { - if (builds.length === 0) { - return []; - } - - const zipHashUrl = builds[0].shasums_url; - - if (!zipHashUrl) { - return []; - } - + async getZipHashes(zipHashUrl: string): Promise { // The hashes are formatted as the result of sha256sum in plain text, each line: \t let rawHashData: string; try { diff --git a/lib/modules/manager/terraform/lockfile/hash.ts b/lib/modules/manager/terraform/lockfile/hash.ts index f9d37eaf3a..08ebab3967 100644 --- a/lib/modules/manager/terraform/lockfile/hash.ts +++ b/lib/modules/manager/terraform/lockfile/hash.ts @@ -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 );