diff --git a/README.md b/README.md index 526738d..9b82927 100644 --- a/README.md +++ b/README.md @@ -289,3 +289,54 @@ Bundles use Maven coordinate names and include all artifacts of the selected pub The selected publication's module metadata and adjacent signature are cleared before metadata generation, including plain `publishToMavenLocal`, publishing to other repositories, and snapshots. Metadata regenerates or restores from cache. Deleting its signature forces the publication signing task to re-sign every artifact; gpg-agent/pinentry may prompt on every publish even when signing previously stayed up to date. Disable generation with `enabled = false` to omit metadata. Skipping it with `onlyIf` while the POM advertises metadata fails before upload. The publishing-script harness runs as part of `check` when Bash is available. Local builds without Bash skip it with a reason; CI requires it with `-PrequireShellHarness=true`. + +### Upload a verified combined bundle + +Run planning and `bundleAll` separately from the upload invocation. After testing and +reviewing the saved ZIP, configure the upload task with the ZIP and manifest SHA-256 values +from retained, reviewed evidence: + +```groovy +releaseAll { + useExistingBundle = providers.gradleProperty('existing').isPresent() + expectedBundleSha256 = providers.gradleProperty('verifiedBundleSha256') + expectedManifestSha256 = providers.gradleProperty('verifiedManifestSha256') +} +``` + +Then run: + +```sh +./gradlew releaseAll -Pexisting \ + -PverifiedBundleSha256="$REVIEWED_BUNDLE_SHA256" \ + -PverifiedManifestSha256="$REVIEWED_MANIFEST_SHA256" +``` + +This submits without rebuilding, signing or staging again. Existing-bundle mode requires +both digests; do not derive them from the mutable receipt at upload time. Each must match +both its receipt entry and the corresponding file. The verified manifest controls the +coordinates probed before upload and during polling in that upload invocation, so review +and retain it with the ZIP. A later resume trusts the manifest copy in `deployment.json`; +it ignores the reviewed digests and does not authenticate that journal copy. Protect the +retained journal from modification. + +Digest presence and syntax are validated before any task runs. Digests are accepted only +in existing-bundle mode: normal uploads rebuild and re-sign artifacts, so an earlier ZIP's +digest generally cannot match. Digest properties also come from project/user +`gradle.properties` and `ORG_GRADLE_PROJECT_*` environment variables; remove or clear them +before a normal release. Upload evidence is ignored when `releaseAll` is absent from the +task graph, including plain checks, signing checks and bundle-only runs. Source/version +and saved manifest hashes are rechecked. + +The uploader hashes and parses the same manifest bytes, and sends a private ZIP snapshot +checked against the verified digest. The bundle directory must be writable for snapshot +creation. Snapshots live beside the bundle, independently of the deployment journal +location. Cleanup failures emit a warning naming the path for manual removal after the +upload has stopped; after a process crash, remove leftover `.verified-upload-*` +directories there once no upload is running. + +Requesting `bundleAll` and `releaseAll` together in existing-bundle mode is rejected, +including through task dependencies. `useExistingBundle` defaults to false, including +when assigned a provider with no value. When `resumeId` is set, resume takes priority: +`useExistingBundle`, `expectedBundleSha256` and `expectedManifestSha256` are ignored. +Resume remains status-only and does not require current build outputs. diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy index 15ec5c9..3ece01d 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -2,15 +2,47 @@ package se.alipsa.gradle.plugin.release /** One-attempt upload journaling and status-only recovery. */ class CombinedDeployment { + private static final org.gradle.api.logging.Logger LOGGER = org.gradle.api.logging.Logging.getLogger(CombinedDeployment) + /** Uploads once, persisting intent before HTTP and ID immediately after it. */ - static Map submit(File record, File bundle, Map manifest, PublicationStatus availability, CentralPortalClient client) { + static Map submit(File record, File bundle, Map manifest, PublicationStatus availability, CentralPortalClient client, String verifiedBundleHash = '') { if (record.exists()) throw new IllegalStateException('Deployment record exists; resume or reconcile it before a new upload') manifest.selected.each { Map component -> if (availability.published(new ReleaseCoordinate(component.groupId, component.artifactId, component.version), true)) { throw new IllegalStateException("Component was published since planning: ${component.artifactId}") } } - Map state = [state: 'SUBMITTING', bundleHash: ReleaseChecksums.checksum(bundle, 'SHA-256'), + // Upload a private snapshot: later writes to the original ZIP cannot change request bytes. + File uploadDirectory + try { + uploadDirectory = java.nio.file.Files.createTempDirectory(bundle.absoluteFile.parentFile.toPath(), '.verified-upload-').toFile() + } catch (IOException failure) { + throw new IllegalStateException("Cannot create a verified upload snapshot beside ${bundle.absolutePath}; place the reviewed ZIP in a writable directory and point bundleFile there. No upload attempted", failure) + } + File snapshot = new File(uploadDirectory, bundle.name) + try { + String uploadHash + try { + java.nio.file.Files.copy(bundle.toPath(), snapshot.toPath()) + uploadHash = ReleaseChecksums.checksum(snapshot, 'SHA-256') + } catch (IOException failure) { + throw new IllegalStateException("Cannot copy or read the verified upload snapshot for ${bundle.absolutePath}; check that the ZIP is readable and its directory has sufficient free space and write access. No upload attempted", failure) + } + if (verifiedBundleHash && uploadHash != verifiedBundleHash) { + throw new IllegalStateException('Bundle changed after verification; no upload attempted') + } + return uploadSnapshot(record, snapshot, manifest, client, uploadHash) + } finally { + [snapshot, uploadDirectory].each { File temporary -> + if (temporary.exists() && !temporary.delete()) { + LOGGER.warn("Could not remove verified upload temporary path {}; remove it manually after the upload has stopped", temporary.absolutePath) + } + } + } + } + + private static Map uploadSnapshot(File record, File bundle, Map manifest, CentralPortalClient client, String uploadHash) { + Map state = [state: 'SUBMITTING', bundleHash: uploadHash, manifest: manifest, created: java.time.Instant.now().toString()] // Reserve the active journal atomically: two local invocations must not both submit. record.parentFile.mkdirs() diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy index 5aa457b..ca5deaf 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy @@ -67,16 +67,35 @@ final class CombinedReleaseRegistrar { task.password.convention(shared.password) task.portalFixture.convention(shared.portalFixture) task.resumeId.convention('') + task.useExistingBundle.convention(false) + task.expectedBundleSha256.convention('') + task.expectedManifestSha256.convention('') } // Inspect resolved paths without realizing tasks. Reject before any task actions, // including when planning is scheduled through dependencies or abbreviations. // Rejected graphs cannot be reused to bypass validation on the next invocation. String planPath = project.absoluteProjectPath('releaseAllPlan') String releasePath = project.absoluteProjectPath('releaseAll') + String bundlePath = project.absoluteProjectPath('bundleAll') project.gradle.taskGraph.whenReady { TaskExecutionGraph graph -> if (graph.hasTask(planPath) && graph.hasTask(releasePath)) { throw new GradleException('Do not request releaseAllPlan and releaseAll in the same invocation; review the saved manifest, then run :releaseAll separately') } + if (graph.hasTask(releasePath)) { + ReleaseAllTask release = releaseAllTask.get() + release.validateDigestInputs() + if (!release.resumeId.getOrElse('') && release.useExistingBundle.getOrElse(false)) { + // Bind upload evidence only for an upload graph, never plain checks/bundling. + // This input is retained when the configuration cache stores the resolved graph. + guardTask.get().expectedManifestSha256.set(release.expectedManifestSha256) + } + } + if (graph.hasTask(releasePath) && graph.hasTask(bundlePath)) { + ReleaseAllTask release = releaseAllTask.get() + if (!release.resumeId.getOrElse('') && release.useExistingBundle.getOrElse(false)) { + throw new GradleException('Do not request bundleAll and releaseAll in the same invocation in existing-bundle mode; verify the saved ZIP, then run releaseAll separately') + } + } } def providers = new CombinedReleaseTasks(planTask, signingCheckTask, guardTask, prepareMavenTask, bundleAllTask, releaseAllTask) CombinedReleaseExtension extension = objects.newInstance(CombinedReleaseExtension, shared, providers) diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy index 853490d..ce51451 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy @@ -82,7 +82,9 @@ final class ModuleReleaseConfigurer { task.wiredSelection.set(staging.map { it.selection }) } extension.releaseAllTask.configure { task -> - if (!task.resumeId.getOrElse('')) task.dependsOn(extension.bundleAllTask) + if (!task.resumeId.getOrElse('')) { + task.dependsOn(task.useExistingBundle.getOrElse(false) ? extension.guardTask : extension.bundleAllTask) + } } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy index 33881de..385e319 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy @@ -21,6 +21,31 @@ abstract class ReleaseAllTask extends DefaultTask { @Input abstract Property getPortalUrl() @Input abstract Property getRepositoryUrl() @Input abstract Property getResumeId() + /** Upload the saved ZIP without bundling; ignored when resumeId is set. */ + @Input @Optional abstract Property getUseExistingBundle() + /** Reviewed SHA-256 anchor, required in existing-bundle mode; ignored during resume. */ + @Input @Optional abstract Property getExpectedBundleSha256() + + /** Reviewed manifest anchor, required in existing-bundle mode; ignored during resume. */ + @Input @Optional abstract Property getExpectedManifestSha256() + + /** Validates reviewed evidence inputs before any build tasks execute. */ + void validateDigestInputs() { + if (resumeId.getOrElse('')) return + boolean existing = useExistingBundle.getOrElse(false) + [expectedBundleSha256: expectedBundleSha256.getOrElse(''), + expectedManifestSha256: expectedManifestSha256.getOrElse('')].each { String name, String value -> + if (value && !(value ==~ /[0-9a-fA-F]{64}/)) { + throw new IllegalStateException("${name} must be a 64-character hexadecimal SHA-256") + } + if (existing && !value) { + throw new IllegalStateException("Existing-bundle mode requires ${name} from reviewed evidence") + } + if (!existing && value) { + throw new IllegalStateException("${name} is supported only in existing-bundle mode; set useExistingBundle or remove the digest") + } + } + } /** Checks source and availability before submitting, or resumes without building. */ @TaskAction @@ -29,14 +54,35 @@ abstract class ReleaseAllTask extends DefaultTask { String id = resumeId.getOrElse('') File record = recordFile.get().asFile if (!id) { - Map manifest = ReleaseManifest.read(manifestFile.get().asFile) - ReleaseManifest.guard(sourceDirectory.get().asFile, manifest) + validateDigestInputs() + String expected = expectedBundleSha256.getOrElse('').toLowerCase(Locale.ROOT) + String expectedManifest = expectedManifestSha256.getOrElse('').toLowerCase(Locale.ROOT) + [manifestFile.get().asFile, receiptFile.get().asFile, bundleFile.get().asFile].each { File required -> + if (!required.isFile()) { + String guidance = useExistingBundle.getOrElse(false) ? + 'run releaseAllPlan and bundleAll separately before existing-bundle upload' : + 'check that releaseAll and bundleAll use the same manifest, receipt and bundle paths' + throw new IllegalStateException("Prepared release file missing: ${required.name}; ${guidance}") + } + } + // Hash and parse the same bytes, so the probed graph is the reviewed manifest. + byte[] manifestBytes = manifestFile.get().asFile.bytes + String manifestHash = ReleaseChecksums.checksum(manifestBytes, 'SHA-256') Map receipt = ReleaseManifest.read(receiptFile.get().asFile) - if (receipt.bundleHash != ReleaseChecksums.checksum(bundleFile.get().asFile, 'SHA-256') || receipt.manifestHash != ReleaseChecksums.checksum(manifestFile.get().asFile, 'SHA-256')) { + if (expectedManifest && (receipt.manifestHash != expectedManifest || manifestHash != expectedManifest)) { + throw new IllegalStateException('Manifest or receipt differs from expectedManifestSha256; no upload attempted') + } + Map manifest = new groovy.json.JsonSlurper().parse(manifestBytes) as Map + ReleaseManifest.guard(sourceDirectory.get().asFile, manifest) + String verifiedHash = ReleaseChecksums.checksum(bundleFile.get().asFile, 'SHA-256') + if (expected && (receipt.bundleHash != expected || verifiedHash != expected)) { + throw new IllegalStateException('Bundle or receipt differs from expectedBundleSha256; no upload attempted') + } + if (receipt.bundleHash != verifiedHash || receipt.manifestHash != manifestHash) { throw new IllegalStateException('Bundle or manifest changed since validation; rebuild the bundle') } def status = new PublicationStatus(repositoryUrl: repositoryUrl.get(), portalUrl: portalUrl.get(), userName: userName.getOrElse(''), password: password.getOrElse(''), requireFixture: true, fixtureFile: portalFixture.get().asFile) - id = CombinedDeployment.submit(record, bundleFile.get().asFile, manifest, status, client).deploymentId + id = CombinedDeployment.submit(record, bundleFile.get().asFile, manifest, status, client, verifiedHash).deploymentId } CombinedDeployment.resume(record, id, client, { Map manifest -> manifest.selected.every { Map p -> diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy index b1e5728..3bd048b 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy @@ -2,7 +2,7 @@ package se.alipsa.gradle.plugin.release import java.security.MessageDigest -/** Shared streaming checksums for single and combined release archives. */ +/** Shared checksums for single and combined release archives and records. */ class ReleaseChecksums { /** Computes a Maven checksum without loading the artifact into memory. */ static String checksum(File file, String algorithm) { @@ -14,4 +14,8 @@ class ReleaseChecksums { } digest.digest().encodeHex().toString() } + /** Computes a checksum of already-read evidence bytes in the same lowercase hex format. */ + static String checksum(byte[] bytes, String algorithm) { + MessageDigest.getInstance(algorithm).digest(bytes).encodeHex().toString() + } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy index 30ddd56..b6a943b 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy @@ -14,10 +14,13 @@ abstract class ReleaseSourceGuardTask extends DefaultTask { @InputFile @PathSensitive(PathSensitivity.NONE) abstract RegularFileProperty getManifestFile() @Internal abstract DirectoryProperty getSourceDirectory() @Input abstract Property getVersionsJson() + /** Optional reviewed manifest anchor, checked before source and selection diagnostics. */ + @Input @Optional abstract Property getExpectedManifestSha256() @Input abstract org.gradle.api.provider.ListProperty getModulePaths() @Input abstract org.gradle.api.provider.ListProperty getUnpublishedProjectPaths() ReleaseSourceGuardTask() { + expectedManifestSha256.convention('') modulePaths.convention([]) unpublishedProjectPaths.convention([]) outputs.upToDateWhen { false } @@ -26,7 +29,12 @@ abstract class ReleaseSourceGuardTask extends DefaultTask { /** Rejects stale manifests before any preparation task can run. */ @TaskAction void guard() { - Map manifest = ReleaseManifest.read(manifestFile.get().asFile) + byte[] manifestBytes = manifestFile.get().asFile.bytes + String expected = expectedManifestSha256.getOrElse('').toLowerCase(Locale.ROOT) + if (expected && ReleaseChecksums.checksum(manifestBytes, 'SHA-256') != expected) { + throw new IllegalStateException('Manifest differs from expectedManifestSha256; no upload attempted') + } + Map manifest = new JsonSlurper().parse(manifestBytes) as Map if (!modulePaths.get().empty && manifest.modulePaths != modulePaths.get()) { throw new IllegalStateException('Configured modules changed; run releaseAllPlan again before bundling') } diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy index 3190e48..95bb68a 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -292,6 +292,96 @@ class CombinedReleaseTest { assertEquals(1, uploads) } + @Test void changedBundleAfterVerificationCannotUpload() { + File record = new File(directory, 'deployment.json') + File bundle = new File(directory, 'bundle.zip'); bundle.text = 'verified' + String verifiedHash = ReleaseChecksums.checksum(bundle, 'SHA-256') + def availability = new PublicationStatus() { + @Override boolean published(ReleaseCoordinate coordinate, boolean both) { + bundle.text = 'changed during availability probe' + false + } + } + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { fail('Changed bytes must never upload'); '' } + } + Map manifest = [selected: [[groupId: 'g', artifactId: 'a', version: '1']]] + def failure = assertThrows(IllegalStateException) { + CombinedDeployment.submit(record, bundle, manifest, availability, client, verifiedHash) + } + assertTrue(failure.message.contains('Bundle changed after verification')) + assertFalse(record.exists()) + assertFalse(directory.listFiles().any { it.name.startsWith('.verified-upload-') }) + } + + @Test void uploadUsesVerifiedSnapshotEvenIfOriginalIsReplaced() { + File record = new File(directory, 'journal/deployment.json') + File bundle = new File(directory, 'bundles/bundle.zip'); bundle.parentFile.mkdirs(); bundle.text = 'verified' + String verifiedHash = ReleaseChecksums.checksum(bundle, 'SHA-256') + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { + assertNotEquals(bundle.canonicalPath, file.canonicalPath) + assertEquals(bundle.name, file.name) + assertEquals(bundle.parentFile.canonicalPath, file.parentFile.parentFile.canonicalPath) + assertNotEquals(record.parentFile.canonicalPath, file.parentFile.parentFile.canonicalPath) + bundle.text = 'replaced before multipart reads' + assertEquals('verified', file.text) + assertEquals(verifiedHash, ReleaseChecksums.checksum(file, 'SHA-256')) + 'saved-id' + } + } + Map state = CombinedDeployment.submit(record, bundle, [selected: []], new PublicationStatus(), client, verifiedHash) + assertEquals(verifiedHash, state.bundleHash) + assertEquals(verifiedHash, ReleaseManifest.read(record).bundleHash) + assertFalse(bundle.parentFile.listFiles().any { it.name.startsWith('.verified-upload-') }) + assertFalse(record.parentFile.listFiles().any { it.name.startsWith('.verified-upload-') }) + assertFalse(directory.listFiles().any { it.name.startsWith('.verified-upload-') }) + } + + @Test void snapshotCreationFailureExplainsWritableDirectoryRequirement() { + File record = new File(directory, 'deployment.json') + // A file as parent fails on every OS, including root-run tests where permissions do not. + // This exercises snapshot creation error handling without relying on a read-only directory. + File blockedDirectory = new File(directory, 'not-a-directory'); blockedDirectory.text = 'blocked' + File bundle = new File(blockedDirectory, 'bundle.zip') + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { fail('Snapshot creation failure must never upload'); '' } + } + def failure = assertThrows(IllegalStateException) { + CombinedDeployment.submit(record, bundle, [selected: []], new PublicationStatus(), client) + } + assertTrue(failure.message.contains('place the reviewed ZIP in a writable directory')) + assertTrue(failure.message.contains('bundleFile')) + assertInstanceOf(IOException, failure.cause) + assertFalse(record.exists()) + } + + @Test void snapshotCopyFailureExplainsNoUploadAndCleansUp() { + File record = new File(directory, 'deployment.json') + File bundle = new File(directory, 'missing-bundle.zip') + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { fail('Copy failure must never upload'); '' } + } + def failure = assertThrows(IllegalStateException) { + CombinedDeployment.submit(record, bundle, [selected: []], new PublicationStatus(), client) + } + assertTrue(failure.message.contains('ZIP is readable')) + assertTrue(failure.message.contains('sufficient free space and write access')) + assertTrue(failure.message.contains('No upload attempted')) + assertInstanceOf(IOException, failure.cause) + assertFalse(record.exists()) + assertFalse(directory.listFiles().any { it.name.startsWith('.verified-upload-') }) + } + + @Test void byteAndFileChecksumsUseTheSameHexFormat() { + File file = new File(directory, 'evidence') + file.bytes = [0, 127, -128, -1] as byte[] + ['SHA-256', 'SHA-1', 'MD5'].each { String algorithm -> + assertEquals(ReleaseChecksums.checksum(file, algorithm), ReleaseChecksums.checksum(file.bytes, algorithm)) + assertTrue(ReleaseChecksums.checksum(file.bytes, algorithm) ==~ /[0-9a-f]+/) + } + } + @Test void failedDeploymentReportsEveryCoordinate() { File record = new File(directory, 'deployment.json') ReleaseManifest.write(record, [deploymentId: 'id', manifest: [selected: [[groupId: 'g', artifactId: 'a', version: '1']]]]) diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy index ffd0967..a250ae4 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy @@ -265,7 +265,7 @@ if (gradle.startParameter.taskNames == ['help']) { @Test @Timeout(120) void consumerConfiguredBundleFileOverridesTheModuleModeDefault() { - withServer(Collections.synchronizedSet([] as Set), ['example:core-artifact:1.0'] as Set) { String base, List requests, List bodies -> + withServer(Collections.synchronizedSet([] as Set), ['example:core-artifact:1.0'] as Set) { String base, List requests, List rawBodies -> fixture(base, "modules = [':core']; bundleFile = layout.buildDirectory.file('custom/my-bundle.zip')") run('releaseAllPlan') run(':bundleAll') @@ -273,8 +273,8 @@ if (gradle.startParameter.taskNames == ['help']) { assertFalse(new File(root, BUNDLE).exists()) run(':releaseAll') assertTrue(requests.any { it.contains('/publisher/upload') }) - assertTrue(bodies.any { it.contains('filename="my-bundle.zip"') }, - { -> "upload should use my-bundle.zip; Content-Disposition lines: ${bodies*.readLines().flatten().findAll { it.startsWith('Content-Disposition') }}" } as java.util.function.Supplier) + assertTrue(rawBodies.any { new String(it as byte[], 'UTF-8').contains('filename="my-bundle.zip"') }, + { -> "upload should use my-bundle.zip; Content-Disposition lines: ${rawBodies.collect { new String(it as byte[], 'UTF-8') }*.readLines().flatten().findAll { it.startsWith('Content-Disposition') }}" } as java.util.function.Supplier) } } @@ -506,7 +506,7 @@ nexusReleasePlugin { void resumeOnlyUsesSavedDeploymentWithoutModulePreparation(boolean configureOnDemand) { withServer(['example:api-artifact:1.0'] as Set) { String base, List requests -> fixture(base, "modules = [':missing']") - new File(root, 'build.gradle') << "\nreleaseAll { resumeId = 'saved-id' }\n" + new File(root, 'build.gradle') << "\nreleaseAll { resumeId = 'saved-id'; useExistingBundle = true; expectedBundleSha256 = 'ignored-during-resume'; expectedManifestSha256 = 'also-ignored' }\n" new File(root, 'build.gradle') << '\ntasks.all { }\n' if (configureOnDemand) new File(root, 'unused/build.gradle').text = "throw new GradleException('Resume configured an unrelated project')" ReleaseManifest.write(new File(root, 'build/releaseAll/deployment.json'), [deploymentId: 'saved-id', state: 'UPLOADED', @@ -523,6 +523,221 @@ nexusReleasePlugin { new File(root, 'core/build.gradle').text = "plugins { id 'java-library' }\ngroup = 'internal'; version = '1.0'\n" } + @ParameterizedTest + @ValueSource(booleans = [false, true]) + void existingBundleUploadsWithoutRebuilding(boolean uppercase) { + withServer([] as Set, ['example:core-artifact:1.0'] as Set) { String base, List requests, List rawBodies -> + fixture(base, "modules = [':core']") + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent(); expectedBundleSha256 = providers.gradleProperty('expected'); expectedManifestSha256 = providers.gradleProperty('expectedManifest') }\n" + commit() + run('releaseAllPlan') + run('bundleAll') + byte[] original = new File(root, BUNDLE).bytes + String bundleHash = ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256') + String manifestHash = ReleaseChecksums.checksum(new File(root, 'build/releaseAll/manifest.json'), 'SHA-256') + def result = run('releaseAll', '-Pexisting', '-Pexpected=' + (uppercase ? bundleHash.toUpperCase(Locale.ROOT) : bundleHash), + '-PexpectedManifest=' + (uppercase ? manifestHash.toUpperCase(Locale.ROOT) : manifestHash)) + assertNull(result.task(':bundleAll')) + assertNull(result.task(':core:jar')) + assertNull(result.task(':core:check')) + assertNotNull(result.task(':releaseAllGuard')) + assertArrayEquals(original, new File(root, BUNDLE).bytes) + assertTrue(requests.any { it.contains('/publisher/upload') }) + assertTrue(rawBodies.findAll { it.length > 0 }.any { byte[] body -> Arrays.equals(multipartFile(body), original) }) + } + } + + @Test void existingBundleRejectsTamperingWithoutUpload() { + withServer { String base, List requests -> + fixture(base, "modules = [':core']") + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent(); expectedBundleSha256 = providers.gradleProperty('expected'); expectedManifestSha256 = providers.gradleProperty('expectedManifest') }\n" + commit() + run('releaseAllPlan') + run('bundleAll') + String expected = ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256') + String expectedManifest = ReleaseChecksums.checksum(new File(root, 'build/releaseAll/manifest.json'), 'SHA-256') + new File(root, BUNDLE) << 'changed' + def failed = runner('releaseAll', '-Pexisting', '-Pexpected=' + expected, '-PexpectedManifest=' + expectedManifest).buildAndFail() + assertNull(failed.task(':bundleAll')) + assertTrue(failed.output.contains('differs from expectedBundleSha256')) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + + /** The fixture server receives one file part; ISO-8859-1 preserves byte offsets. */ + private static byte[] multipartFile(byte[] body) { + String text = new String(body, java.nio.charset.StandardCharsets.ISO_8859_1) + int headerEnd = text.indexOf('\r\n\r\n') + assertTrue(headerEnd >= 0) + String boundary = text.substring(0, text.indexOf('\r\n')) + int end = text.lastIndexOf('\r\n' + boundary + '--\r\n') + assertTrue(end >= headerEnd + 4) + Arrays.copyOfRange(body, headerEnd + 4, end) + } + + @ParameterizedTest + @ValueSource(strings = ['dirty', 'commit', 'manifest', 'replan', 'missing-receipt', 'missing-zip', 'rewritten-receipt', 'rewritten-manifest-receipt', 'guard-manifest']) + void existingBundleRejectsDriftAndMissingFiles(String change) { + withServer { String base, List requests -> + existingFixture(base) + run('releaseAllPlan') + run('bundleAll') + String expected = ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256') + String expectedManifest = ReleaseChecksums.checksum(new File(root, 'build/releaseAll/manifest.json'), 'SHA-256') + switch (change) { + case 'dirty': new File(root, 'core/src/main/java/Core.java') << '\n// changed'; break + case 'commit': + new File(root, 'core/src/main/java/Core.java') << '\n// changed' + commit(); break + case 'manifest': new File(root, 'build/releaseAll/manifest.json') << '\n'; break + case 'replan': + new File(root, 'core/src/main/java/Core.java') << '\n// changed' + commit() + run('releaseAllPlan'); break + case 'missing-receipt': assertTrue(new File(root, 'build/releaseAll/bundle-receipt.json').delete()); break + case 'missing-zip': assertTrue(new File(root, BUNDLE).delete()); break + case 'guard-manifest': + File guardManifestFile = new File(root, 'build/releaseAll/manifest.json') + Map guardManifest = ReleaseManifest.read(guardManifestFile) + guardManifest.modulePaths = [':replaced'] + guardManifest.revision = 'replaced' + ReleaseManifest.write(guardManifestFile, guardManifest) + break + case 'rewritten-manifest-receipt': + // Bypass the mutable source/selection guard and update its mutable receipt. + new File(root, 'core/src/main/java/Core.java') << '\n// changed' + commit() + File manifestFile = new File(root, 'build/releaseAll/manifest.json') + Map manifest = ReleaseManifest.read(manifestFile) + manifest.revision = ReleaseManifest.git(root, 'rev-parse', 'HEAD') + manifest.selected = [] + ReleaseManifest.write(manifestFile, manifest) + File manifestReceiptFile = new File(root, 'build/releaseAll/bundle-receipt.json') + Map manifestReceipt = ReleaseManifest.read(manifestReceiptFile) + manifestReceipt.manifestHash = ReleaseChecksums.checksum(manifestFile, 'SHA-256') + ReleaseManifest.write(manifestReceiptFile, manifestReceipt) + break + case 'rewritten-receipt': + new File(root, BUNDLE) << 'changed' + File receiptFile = new File(root, 'build/releaseAll/bundle-receipt.json') + Map receipt = ReleaseManifest.read(receiptFile) + receipt.bundleHash = ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256') + ReleaseManifest.write(receiptFile, receipt) + } + def failed = runner('releaseAll', '-Pexisting', '-Pexpected=' + expected, '-PexpectedManifest=' + expectedManifest).buildAndFail() + String message = change in ['dirty', 'commit'] ? 'Stale manifest' : + change.startsWith('missing-') ? 'Prepared release file missing' : + change == 'rewritten-receipt' ? 'differs from expectedBundleSha256' : 'differs from expectedManifestSha256' + assertTrue(failed.output.contains(message), failed.output) + assertNull(failed.task(':bundleAll')) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + + @Test void existingBundleRequiresReviewedDigestAndPriorBundle() { + withServer { String base, List requests -> + existingFixture(base) + run('releaseAllPlan') + def missingDigest = runner('releaseAll', '-Pexisting').buildAndFail() + assertTrue(missingDigest.output.contains('requires expectedBundleSha256')) + def missingBundle = runner('releaseAll', '-Pexisting', '-Pexpected=' + ('0' * 64), + '-PexpectedManifest=' + ReleaseChecksums.checksum(new File(root, 'build/releaseAll/manifest.json'), 'SHA-256')).buildAndFail() + assertTrue(missingBundle.output.contains('Prepared release file missing')) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + + @ParameterizedTest + @ValueSource(strings = ['bundleAll', 'releaseAll', 'dependency']) + void existingBundleRejectsBundlingInTheUploadGraph(String first) { + withServer { String base, List requests -> + existingFixture(base) + run('releaseAllPlan') + if (first == 'dependency') { + new File(root, 'build.gradle') << "\ntasks.register('consumer') { dependsOn bundleAll }\n" + commit() + } + String second = first == 'bundleAll' ? 'releaseAll' : first == 'dependency' ? 'releaseAll' : 'bundleAll' + if (first == 'dependency') first = 'consumer' + def failed = runner(first, second, '-Pexisting', '-Pexpected=' + ('0' * 64), '-PexpectedManifest=' + ('0' * 64)).buildAndFail() + assertTrue(failed.output.contains('Do not request bundleAll and releaseAll')) + assertTrue(failed.tasks.empty) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + + @ParameterizedTest + @ValueSource(strings = ['bundle-malformed', 'manifest-malformed', 'manifest-missing', 'bundle-normal', 'manifest-normal', 'malformed-normal']) + void evidenceInputErrorsFailBeforeAnyTask(String scenario) { + withServer { String base, List requests -> + existingFixture(base) + String valid = 'a' * 64 + List args = ['-Pexpected=' + valid, '-PexpectedManifest=' + valid] + if (scenario.endsWith('normal')) { + args = scenario == 'manifest-normal' ? ['-PexpectedManifest=' + valid] : + ['-Pexpected=' + (scenario == 'malformed-normal' ? 'oops' : valid)] + } else { + args.add('-Pexisting') + if (scenario == 'manifest-missing') args.remove('-PexpectedManifest=' + valid) + else if (scenario == 'bundle-malformed') args[0] = '-Pexpected=' + ('z' * 64) + else args[1] = '-PexpectedManifest=123' + } + // No plan/bundle exists: input validation must win before preparation or source guards. + def failed = runner('releaseAll', *args).buildAndFail() + String message = scenario == 'manifest-missing' ? 'requires expectedManifestSha256' : + scenario in ['bundle-normal', 'manifest-normal'] ? 'supported only in existing-bundle mode' : + 'must be a 64-character hexadecimal SHA-256' + assertTrue(failed.output.contains(message), failed.output) + assertTrue(failed.tasks.empty) + assertTrue(requests.empty) + } + } + + @Test void normalUploadMissingFileExplainsPathMismatch() { + withServer { String base, List requests -> + fixture(base, "modules = [':core']") + new File(root, 'build.gradle') << "\nreleaseAll { receiptFile = layout.buildDirectory.file('missing-receipt.json') }\n" + commit() + run('releaseAllPlan') + def failed = runner('releaseAll').buildAndFail() + assertTrue(failed.output.contains('check that releaseAll and bundleAll use the same')) + assertFalse(failed.output.contains('before existing-bundle upload')) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + + @ParameterizedTest + @ValueSource(strings = ['bundleAll', 'releaseAllSigningCheck', 'releaseAllGuard', ':core:check']) + void nonUploadGraphsIgnoreExportedUploadEvidence(String task) { + withServer { String base, List requests -> + existingFixture(base) + run('releaseAllPlan') + String[] args = ['-Pexisting', '-Pexpected=not-an-upload-digest', '-PexpectedManifest=not-a-manifest-digest'] + def first = run(task, *args) + assertNull(first.task(':releaseAll')) + def cached = run(task, *args) + assertNull(cached.task(':releaseAll')) + assertTrue(cached.output.contains('Reusing configuration cache.')) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + + @Test void absentExistingBundleProviderDefaultsToNormalBuild() { + withServer([] as Set, ['example:core-artifact:1.0'] as Set) { String base, List requests, List rawBodies -> + fixture(base, "modules = [':core']") + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').map { it.toBoolean() } }\n" + commit() + run('releaseAllPlan') + assertNotNull(run('releaseAll').task(':bundleAll')) + } + } + + private void existingFixture(String base) { + fixture(base, "modules = [':core']") + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent(); expectedBundleSha256 = providers.gradleProperty('expected'); expectedManifestSha256 = providers.gradleProperty('expectedManifest') }\n" + commit() + } + private void fixture(String base, String selection, boolean compileOnly = false, boolean signingKeys = true) { new File(root, 'settings.gradle').text = "rootProject.name='module-release-fixture'\ninclude 'core', 'api', 'unused'\n" new File(root, '.gitignore').text = 'build/\n.gradle/\n' @@ -579,7 +794,7 @@ ${name == 'api' ? "dependencies { ${compileOnly ? 'compileOnly' : 'api'} project } private void withServer(Set published = [] as Set, Closure action) { - withServer(published, false, [] as Set) { String base, List requests, List bodies -> action(base, requests) } + withServer(published, false, [] as Set) { String base, List requests, List rawBodies -> action(base, requests) } } /** Variant for full upload flows: captures bodies, answers uploads, and marks the given coordinates @@ -590,7 +805,7 @@ ${name == 'api' ? "dependencies { ${compileOnly ? 'compileOnly' : 'api'} project private void withServer(Set published, boolean captureBodies, Set deployedOnUpload, Closure action) { List requests = Collections.synchronizedList([]) - List bodies = Collections.synchronizedList([]) + List rawBodies = Collections.synchronizedList([]) def server = HttpServer.create(new InetSocketAddress('127.0.0.1', 0), 0) server.createContext('/') { exchange -> requests.add(exchange.requestURI.toString()) @@ -602,14 +817,20 @@ ${name == 'api' ? "dependencies { ${compileOnly ? 'compileOnly' : 'api'} project exchange.requestURI.path.endsWith("/${group.replace('.', '/')}/${name}/${version}/${name}-${version}.pom") || exchange.requestURI.path.endsWith("/${group.replace('.', '/')}/${name}/${version}/${name}-${version}.jar") } - if (captureBodies) bodies.add(new String(exchange.requestBody.readAllBytes(), 'UTF-8')) + if (captureBodies) { + byte[] raw = exchange.requestBody.readAllBytes() + rawBodies.add(raw) + } if (upload) published.addAll(deployedOnUpload) byte[] body = (status ? '{"deploymentState":"PUBLISHED"}' : portal ? '{"published":false}' : upload ? 'upload-deployment-id' : '').getBytes('UTF-8') exchange.sendResponseHeaders(portal || status || upload || released ? 200 : 404, exchange.requestMethod == 'HEAD' ? -1 : body.length) exchange.responseBody.withCloseable { it.write(body) } } server.start() - try { action("http://127.0.0.1:${server.address.port}".toString(), requests, bodies) } + try { + String base = "http://127.0.0.1:${server.address.port}".toString() + action(base, requests, rawBodies) + } finally { server.stop(0) } }