Skip to content

Commit

Permalink
Verify wrappers for distribution-snapshots
Browse files Browse the repository at this point in the history
By slurping the checksum URLs from https://services.gradle.org/distributions-snapshots/
we can include these unpublished wrapper checksums in validation.

Fixes #281
  • Loading branch information
bigdaz committed Jul 31, 2024
1 parent f31c203 commit ca81ef2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
30 changes: 29 additions & 1 deletion sources/src/wrapper-validation/checksums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as httpm from 'typed-rest-client/HttpClient'
import * as cheerio from 'cheerio'

import fileWrapperChecksums from './wrapper-checksums.json'

Expand Down Expand Up @@ -54,7 +55,15 @@ export async function fetchUnknownChecksums(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(entry: any) => entry.wrapperChecksumUrl as string
)
const checksums = await Promise.all(checksumUrls.map(async (url: string) => httpGetText(url)))
console.log(`Fetching checksums for ${checksumUrls.length} versions`)

Check failure on line 58 in sources/src/wrapper-validation/checksums.ts

View workflow job for this annotation

GitHub Actions / build-distribution

Unexpected console statement
if (allowSnapshots) {
await addDistributionSnapshotChecksums(checksumUrls)
}
console.log(`Fetching checksums for ${checksumUrls.length} versions after snapshot check`)

Check failure on line 62 in sources/src/wrapper-validation/checksums.ts

View workflow job for this annotation

GitHub Actions / build-distribution

Unexpected console statement
const checksums = await Promise.all(checksumUrls.map(async (url: string) => {
// console.log(`Fetching checksum from ${url}`)
return httpGetText(url)
}))
return new Set(checksums)
}

Expand All @@ -66,3 +75,22 @@ async function httpGetText(url: string): Promise<string> {
const response = await httpc.get(url)
return await response.readBody()
}

// Public for testing
export async function addDistributionSnapshotChecksums(checksumUrls: string[]): Promise<void> {
// Load the index page of the distribution snapshot repository
const indexPage = await httpGetText('https://services.gradle.org/distributions-snapshots/')

// // Extract all wrapper checksum from the index page. These end in -wrapper.jar.sha256
// // Load the HTML into cheerio
const $ = cheerio.load(indexPage);

// // Find all links ending with '-wrapper.jar.sha256'
const wrapperChecksumLinks = $('a[href$="-wrapper.jar.sha256"]');

// build the absolute URL for each wrapper checksum
wrapperChecksumLinks.each((index, element) => {
const url = $(element).attr('href')
checksumUrls.push(`https://services.gradle.org${url}`)
})
}
16 changes: 16 additions & 0 deletions sources/test/jest/wrapper-validation/checksums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ test('fetches wrapper jars checksums', async () => {
).toBe(true)
})

test('fetches wrapper jar checksums for snapshots', async () => {
const nonSnapshotChecksums = await checksums.fetchUnknownChecksums(false, new checksums.WrapperChecksums)
const validChecksums = await checksums.fetchUnknownChecksums(true, new checksums.WrapperChecksums)

// Expect that at least one snapshot checksum is different from the non-snapshot checksums
expect(validChecksums.size).toBeGreaterThan(nonSnapshotChecksums.size)
})

test('fetches all wrapper checksum URLS for snapshots', async () => {
const checksumUrls: string[] = []
await checksums.addDistributionSnapshotChecksums(checksumUrls)

expect(checksumUrls.length).toBeGreaterThan(100) // May only be a few unique checksums
console.log(checksumUrls)
})

describe('retry', () => {
afterEach(() => {
nock.cleanAll()
Expand Down

0 comments on commit ca81ef2

Please sign in to comment.