From 47f7a360da406cbd163fe66fc9c872fb474b1e26 Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sat, 14 Nov 2026 22:49:40 +0100 Subject: [PATCH 1/5] Allow uploading an existing verified combined bundle --- README.md | 8 ++++ .../release/CombinedReleaseRegistrar.groovy | 1 + .../release/ModuleReleaseConfigurer.groovy | 4 +- .../plugin/release/ReleaseAllTask.groovy | 2 + .../plugin/release/ModuleReleaseTest.groovy | 47 ++++++++++++++++++- 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 526738d..7223bd1 100644 --- a/README.md +++ b/README.md @@ -289,3 +289,11 @@ 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 + +After planning and `bundleAll`, configure `releaseAll { useExistingBundle = true }` to +submit the saved ZIP without rebuilding, signing or staging it again. Source/version and +saved manifest/receipt hashes are rechecked before availability probes and upload. Consumers +must bind additional integration evidence to this exact ZIP digest before invoking the task. +Resume remains status-only and does not require current build outputs. 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..dc19596 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy @@ -67,6 +67,7 @@ final class CombinedReleaseRegistrar { task.password.convention(shared.password) task.portalFixture.convention(shared.portalFixture) task.resumeId.convention('') + task.useExistingBundle.convention(false) } // Inspect resolved paths without realizing tasks. Reject before any task actions, // including when planning is scheduled through dependencies or abbreviations. 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..69eb89e 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.get() ? 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..ddcc67b 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,8 @@ abstract class ReleaseAllTask extends DefaultTask { @Input abstract Property getPortalUrl() @Input abstract Property getRepositoryUrl() @Input abstract Property getResumeId() + /** Upload the saved, verified ZIP without scheduling bundleAll. */ + @Input abstract Property getUseExistingBundle() /** Checks source and availability before submitting, or resumes without building. */ @TaskAction 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..8b0ae58 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy @@ -523,6 +523,40 @@ nexusReleasePlugin { new File(root, 'core/build.gradle').text = "plugins { id 'java-library' }\ngroup = 'internal'; version = '1.0'\n" } + @Test void existingBundleUploadsWithoutRebuilding() { + withServer([] as Set, ['example:core-artifact:1.0'] as Set) { String base, List requests, List bodies, List rawBodies -> + fixture(base, "modules = [':core']") + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent() }\n" + commit() + run('releaseAllPlan') + run('bundleAll') + byte[] original = new File(root, BUNDLE).bytes + def result = run('releaseAll', '-Pexisting') + 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.any { byte[] body -> Collections.indexOfSubList(body.toList(), original.toList()) >= 0 }) + } + } + + @Test void existingBundleRejectsTamperingWithoutUpload() { + withServer { String base, List requests -> + fixture(base, "modules = [':core']") + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent() }\n" + commit() + run('releaseAllPlan') + run('bundleAll') + new File(root, BUNDLE) << 'changed' + def failed = runner('releaseAll', '-Pexisting').buildAndFail() + assertNull(failed.task(':bundleAll')) + assertTrue(failed.output.contains('Bundle or manifest changed since validation')) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + 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' @@ -591,6 +625,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 +637,22 @@ ${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) + bodies.add(new String(raw, 'UTF-8')) + } 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() + if (action.maximumNumberOfParameters == 4) action(base, requests, bodies, rawBodies) + else action(base, requests, bodies) + } finally { server.stop(0) } } From 113450c8b6fa977619aa28f4a4087686619324ca Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sun, 15 Nov 2026 16:02:33 +0100 Subject: [PATCH 2/5] Bind existing bundle uploads to reviewed digests and immutable snapshots --- README.md | 28 +++- .../plugin/release/CombinedDeployment.groovy | 22 +++- .../release/CombinedReleaseRegistrar.groovy | 8 ++ .../release/ModuleReleaseConfigurer.groovy | 2 +- .../plugin/release/ReleaseAllTask.groovy | 27 +++- .../plugin/release/CombinedReleaseTest.groovy | 42 ++++++ .../plugin/release/ModuleReleaseTest.groovy | 120 +++++++++++++++--- 7 files changed, 221 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 7223bd1..8a1acb6 100644 --- a/README.md +++ b/README.md @@ -292,8 +292,26 @@ The publishing-script harness runs as part of `check` when Bash is available. Lo ### Upload a verified combined bundle -After planning and `bundleAll`, configure `releaseAll { useExistingBundle = true }` to -submit the saved ZIP without rebuilding, signing or staging it again. Source/version and -saved manifest/receipt hashes are rechecked before availability probes and upload. Consumers -must bind additional integration evidence to this exact ZIP digest before invoking the task. -Resume remains status-only and does not require current build outputs. +Run planning and `bundleAll` separately from the upload invocation. After testing and +reviewing the saved ZIP, configure the upload task with its SHA-256 from retained, +reviewed evidence: + +```groovy +releaseAll { + useExistingBundle = providers.gradleProperty('existing').isPresent() + expectedBundleSha256 = providers.gradleProperty('verifiedBundleSha256') +} +``` + +Then run `releaseAll -Pexisting -PverifiedBundleSha256=` to submit +without rebuilding, signing or staging again. Existing-bundle mode requires this digest; +do not derive it from the mutable receipt beside the ZIP at upload time. It must match +both the receipt and the actual ZIP. The optional digest also applies to normal uploads +when supplied. Source/version and saved manifest hashes are rechecked before upload. +The uploader checks a private snapshot against the verified digest and sends that snapshot. + +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: +both `useExistingBundle` and `expectedBundleSha256` 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..3c6c89b 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -3,14 +3,32 @@ package se.alipsa.gradle.plugin.release /** One-attempt upload journaling and status-only recovery. */ class 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. + record.absoluteFile.parentFile.mkdirs() + File uploadDirectory = java.nio.file.Files.createTempDirectory(record.absoluteFile.parentFile.toPath(), '.verified-upload-').toFile() + File snapshot = new File(uploadDirectory, bundle.name) + try { + java.nio.file.Files.copy(bundle.toPath(), snapshot.toPath()) + String uploadHash = ReleaseChecksums.checksum(snapshot, 'SHA-256') + if (verifiedBundleHash && uploadHash != verifiedBundleHash) { + throw new IllegalStateException('Bundle changed after verification; no upload attempted') + } + return uploadSnapshot(record, snapshot, manifest, client, uploadHash) + } finally { + snapshot.delete() + uploadDirectory.delete() + } + } + + 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 dc19596..3b16005 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy @@ -68,16 +68,24 @@ final class CombinedReleaseRegistrar { task.portalFixture.convention(shared.portalFixture) task.resumeId.convention('') task.useExistingBundle.convention(false) + task.expectedBundleSha256.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) && 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 69eb89e..ce51451 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy @@ -83,7 +83,7 @@ final class ModuleReleaseConfigurer { } extension.releaseAllTask.configure { task -> if (!task.resumeId.getOrElse('')) { - task.dependsOn(task.useExistingBundle.get() ? extension.guardTask : extension.bundleAllTask) + 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 ddcc67b..42172ec 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy @@ -21,8 +21,10 @@ abstract class ReleaseAllTask extends DefaultTask { @Input abstract Property getPortalUrl() @Input abstract Property getRepositoryUrl() @Input abstract Property getResumeId() - /** Upload the saved, verified ZIP without scheduling bundleAll. */ - @Input abstract Property getUseExistingBundle() + /** 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() /** Checks source and availability before submitting, or resumes without building. */ @TaskAction @@ -31,14 +33,31 @@ abstract class ReleaseAllTask extends DefaultTask { String id = resumeId.getOrElse('') File record = recordFile.get().asFile if (!id) { + String expected = expectedBundleSha256.getOrElse('') + if (useExistingBundle.getOrElse(false) && !expected) { + throw new IllegalStateException('Existing-bundle mode requires expectedBundleSha256 from reviewed evidence') + } + if (expected && !(expected ==~ /[0-9a-fA-F]{64}/)) { + throw new IllegalStateException('expectedBundleSha256 must be a 64-character hexadecimal SHA-256') + } + expected = expected.toLowerCase(Locale.ROOT) + [manifestFile.get().asFile, receiptFile.get().asFile, bundleFile.get().asFile].each { File required -> + if (!required.isFile()) { + throw new IllegalStateException("Prepared release file missing: ${required.name}; run releaseAllPlan and bundleAll separately before existing-bundle upload") + } + } Map manifest = ReleaseManifest.read(manifestFile.get().asFile) ReleaseManifest.guard(sourceDirectory.get().asFile, manifest) 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')) { + 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 != ReleaseChecksums.checksum(manifestFile.get().asFile, 'SHA-256')) { 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/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy index 3190e48..e256ecd 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,48 @@ 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, 'deployment.json') + File bundle = new File(directory, 'bundle.zip'); 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) + 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(directory.listFiles().any { it.name.startsWith('.verified-upload-') }) + } + @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 8b0ae58..966fb18 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' }\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', @@ -524,39 +524,130 @@ nexusReleasePlugin { } @Test void existingBundleUploadsWithoutRebuilding() { - withServer([] as Set, ['example:core-artifact:1.0'] as Set) { String base, List requests, List bodies, List rawBodies -> + 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() }\n" + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent(); expectedBundleSha256 = providers.gradleProperty('expected') }\n" commit() run('releaseAllPlan') run('bundleAll') byte[] original = new File(root, BUNDLE).bytes - def result = run('releaseAll', '-Pexisting') + def result = run('releaseAll', '-Pexisting', '-Pexpected=' + ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256')) 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.any { byte[] body -> Collections.indexOfSubList(body.toList(), original.toList()) >= 0 }) + 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() }\n" + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent(); expectedBundleSha256 = providers.gradleProperty('expected') }\n" commit() run('releaseAllPlan') run('bundleAll') + String expected = ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256') new File(root, BUNDLE) << 'changed' - def failed = runner('releaseAll', '-Pexisting').buildAndFail() + def failed = runner('releaseAll', '-Pexisting', '-Pexpected=' + expected).buildAndFail() assertNull(failed.task(':bundleAll')) - assertTrue(failed.output.contains('Bundle or manifest changed since validation')) + 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']) + 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') + 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 '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).buildAndFail() + String message = change in ['dirty', 'commit'] ? 'Stale manifest' : + change.startsWith('missing-') ? 'Prepared release file missing' : + change == 'rewritten-receipt' ? 'differs from expectedBundleSha256' : 'Bundle or manifest changed since validation' + 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)).buildAndFail() + assertTrue(missingBundle.output.contains('Prepared release file missing')) + assertFalse(requests.any { it.contains('/publisher/upload') }) + } + } + + @ParameterizedTest + @ValueSource(strings = ['bundleAll', 'releaseAll']) + void existingBundleRejectsBundlingInTheUploadGraph(String first) { + withServer { String base, List requests -> + existingFixture(base) + run('releaseAllPlan') + String second = first == 'bundleAll' ? 'releaseAll' : 'bundleAll' + def failed = runner(first, second, '-Pexisting', '-Pexpected=' + ('0' * 64)).buildAndFail() + assertTrue(failed.output.contains('Do not request bundleAll and releaseAll')) + assertTrue(failed.tasks.empty) + 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') }\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' @@ -613,7 +704,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 @@ -624,7 +715,6 @@ ${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 -> @@ -640,7 +730,6 @@ ${name == 'api' ? "dependencies { ${compileOnly ? 'compileOnly' : 'api'} project if (captureBodies) { byte[] raw = exchange.requestBody.readAllBytes() rawBodies.add(raw) - bodies.add(new String(raw, 'UTF-8')) } if (upload) published.addAll(deployedOnUpload) byte[] body = (status ? '{"deploymentState":"PUBLISHED"}' : portal ? '{"published":false}' : upload ? 'upload-deployment-id' : '').getBytes('UTF-8') @@ -650,8 +739,7 @@ ${name == 'api' ? "dependencies { ${compileOnly ? 'compileOnly' : 'api'} project server.start() try { String base = "http://127.0.0.1:${server.address.port}".toString() - if (action.maximumNumberOfParameters == 4) action(base, requests, bodies, rawBodies) - else action(base, requests, bodies) + action(base, requests, rawBodies) } finally { server.stop(0) } } From 0548e32b7684d2b9a99fdcc2587847740e3254bd Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sun, 15 Nov 2026 16:09:31 +0100 Subject: [PATCH 3/5] Bind reviewed manifests and validate upload evidence before task execution --- README.md | 35 +++++-- .../plugin/release/CombinedDeployment.groovy | 12 ++- .../release/CombinedReleaseRegistrar.groovy | 4 + .../plugin/release/ReleaseAllTask.groovy | 49 +++++++--- .../plugin/release/CombinedReleaseTest.groovy | 8 +- .../plugin/release/ModuleReleaseTest.groovy | 94 ++++++++++++++++--- 6 files changed, 160 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 8a1acb6..90734eb 100644 --- a/README.md +++ b/README.md @@ -293,25 +293,40 @@ The publishing-script harness runs as part of `check` when Bash is available. Lo ### 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 its SHA-256 from retained, -reviewed evidence: +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 `releaseAll -Pexisting -PverifiedBundleSha256=` to submit -without rebuilding, signing or staging again. Existing-bundle mode requires this digest; -do not derive it from the mutable receipt beside the ZIP at upload time. It must match -both the receipt and the actual ZIP. The optional digest also applies to normal uploads -when supplied. Source/version and saved manifest hashes are rechecked before upload. -The uploader checks a private snapshot against the verified digest and sends that snapshot. +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 manifest controls the coordinates +probed before upload and during publication polling, so review and retain it with the ZIP. +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. 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. 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: -both `useExistingBundle` and `expectedBundleSha256` are ignored. Resume remains -status-only and does not require current build outputs. +`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 3c6c89b..b3c36f8 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -2,6 +2,8 @@ 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, String verifiedBundleHash = '') { if (record.exists()) throw new IllegalStateException('Deployment record exists; resume or reconcile it before a new upload') @@ -11,8 +13,7 @@ class CombinedDeployment { } } // Upload a private snapshot: later writes to the original ZIP cannot change request bytes. - record.absoluteFile.parentFile.mkdirs() - File uploadDirectory = java.nio.file.Files.createTempDirectory(record.absoluteFile.parentFile.toPath(), '.verified-upload-').toFile() + File uploadDirectory = java.nio.file.Files.createTempDirectory(bundle.absoluteFile.parentFile.toPath(), '.verified-upload-').toFile() File snapshot = new File(uploadDirectory, bundle.name) try { java.nio.file.Files.copy(bundle.toPath(), snapshot.toPath()) @@ -22,8 +23,11 @@ class CombinedDeployment { } return uploadSnapshot(record, snapshot, manifest, client, uploadHash) } finally { - snapshot.delete() - uploadDirectory.delete() + [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) + } + } } } 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 3b16005..2e090ac 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy @@ -69,6 +69,7 @@ final class CombinedReleaseRegistrar { 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. @@ -80,6 +81,9 @@ final class CombinedReleaseRegistrar { 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.get().validateDigestInputs() + } if (graph.hasTask(releasePath) && graph.hasTask(bundlePath)) { ReleaseAllTask release = releaseAllTask.get() if (!release.resumeId.getOrElse('') && release.useExistingBundle.getOrElse(false)) { 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 42172ec..f9d6123 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy @@ -26,6 +26,27 @@ abstract class ReleaseAllTask extends DefaultTask { /** 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 void release() { @@ -33,27 +54,31 @@ abstract class ReleaseAllTask extends DefaultTask { String id = resumeId.getOrElse('') File record = recordFile.get().asFile if (!id) { - String expected = expectedBundleSha256.getOrElse('') - if (useExistingBundle.getOrElse(false) && !expected) { - throw new IllegalStateException('Existing-bundle mode requires expectedBundleSha256 from reviewed evidence') - } - if (expected && !(expected ==~ /[0-9a-fA-F]{64}/)) { - throw new IllegalStateException('expectedBundleSha256 must be a 64-character hexadecimal SHA-256') - } - expected = expected.toLowerCase(Locale.ROOT) + 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()) { - throw new IllegalStateException("Prepared release file missing: ${required.name}; run releaseAllPlan and bundleAll separately before existing-bundle upload") + 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}") } } - Map manifest = ReleaseManifest.read(manifestFile.get().asFile) - ReleaseManifest.guard(sourceDirectory.get().asFile, manifest) + // Hash and parse the same bytes, so the probed graph is the reviewed manifest. + byte[] manifestBytes = manifestFile.get().asFile.bytes + String manifestHash = java.security.MessageDigest.getInstance('SHA-256').digest(manifestBytes).encodeHex().toString() Map receipt = ReleaseManifest.read(receiptFile.get().asFile) + 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 != ReleaseChecksums.checksum(manifestFile.get().asFile, 'SHA-256')) { + 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) 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 e256ecd..bdf98f2 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -315,13 +315,15 @@ class CombinedReleaseTest { } @Test void uploadUsesVerifiedSnapshotEvenIfOriginalIsReplaced() { - File record = new File(directory, 'deployment.json') - File bundle = new File(directory, 'bundle.zip'); bundle.text = 'verified' + 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')) @@ -331,6 +333,8 @@ class CombinedReleaseTest { 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-') }) } 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 966fb18..3b700d0 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy @@ -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'; useExistingBundle = true; expectedBundleSha256 = 'ignored-during-resume' }\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,15 +523,20 @@ nexusReleasePlugin { new File(root, 'core/build.gradle').text = "plugins { id 'java-library' }\ngroup = 'internal'; version = '1.0'\n" } - @Test void existingBundleUploadsWithoutRebuilding() { + @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') }\n" + 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 - def result = run('releaseAll', '-Pexisting', '-Pexpected=' + ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256')) + 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')) @@ -545,13 +550,14 @@ nexusReleasePlugin { @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') }\n" + 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).buildAndFail() + 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') }) @@ -570,13 +576,14 @@ nexusReleasePlugin { } @ParameterizedTest - @ValueSource(strings = ['dirty', 'commit', 'manifest', 'replan', 'missing-receipt', 'missing-zip', 'rewritten-receipt']) + @ValueSource(strings = ['dirty', 'commit', 'manifest', 'replan', 'missing-receipt', 'missing-zip', 'rewritten-receipt', 'rewritten-manifest-receipt']) 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': @@ -589,6 +596,20 @@ nexusReleasePlugin { 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 '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') @@ -596,10 +617,10 @@ nexusReleasePlugin { receipt.bundleHash = ReleaseChecksums.checksum(new File(root, BUNDLE), 'SHA-256') ReleaseManifest.write(receiptFile, receipt) } - def failed = runner('releaseAll', '-Pexisting', '-Pexpected=' + expected).buildAndFail() + 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' : 'Bundle or manifest changed since validation' + 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') }) @@ -612,26 +633,71 @@ nexusReleasePlugin { run('releaseAllPlan') def missingDigest = runner('releaseAll', '-Pexisting').buildAndFail() assertTrue(missingDigest.output.contains('requires expectedBundleSha256')) - def missingBundle = runner('releaseAll', '-Pexisting', '-Pexpected=' + ('0' * 64)).buildAndFail() + def missingBundle = runner('releaseAll', '-Pexisting', '-Pexpected=' + ('0' * 64), '-PexpectedManifest=' + ('0' * 64)).buildAndFail() assertTrue(missingBundle.output.contains('Prepared release file missing')) assertFalse(requests.any { it.contains('/publisher/upload') }) } } @ParameterizedTest - @ValueSource(strings = ['bundleAll', 'releaseAll']) + @ValueSource(strings = ['bundleAll', 'releaseAll', 'dependency']) void existingBundleRejectsBundlingInTheUploadGraph(String first) { withServer { String base, List requests -> existingFixture(base) run('releaseAllPlan') - String second = first == 'bundleAll' ? 'releaseAll' : 'bundleAll' - def failed = runner(first, second, '-Pexisting', '-Pexpected=' + ('0' * 64)).buildAndFail() + 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') }) + } + } + @Test void absentExistingBundleProviderDefaultsToNormalBuild() { withServer([] as Set, ['example:core-artifact:1.0'] as Set) { String base, List requests, List rawBodies -> fixture(base, "modules = [':core']") @@ -644,7 +710,7 @@ nexusReleasePlugin { private void existingFixture(String base) { fixture(base, "modules = [':core']") - new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent(); expectedBundleSha256 = providers.gradleProperty('expected') }\n" + new File(root, 'build.gradle') << "\nreleaseAll { useExistingBundle = providers.gradleProperty('existing').isPresent(); expectedBundleSha256 = providers.gradleProperty('expected'); expectedManifestSha256 = providers.gradleProperty('expectedManifest') }\n" commit() } From 485c0aca5fa85d5e51459a0bb394e6c8ee2f0382 Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sun, 15 Nov 2026 16:26:06 +0100 Subject: [PATCH 4/5] Clarify resume trust and improve verified upload diagnostics --- README.md | 18 +++++++++---- .../plugin/release/CombinedDeployment.groovy | 7 +++++- .../release/ModuleReleaseConfigurer.groovy | 5 ++++ .../plugin/release/ReleaseAllTask.groovy | 2 +- .../plugin/release/ReleaseChecksums.groovy | 6 ++++- .../release/ReleaseSourceGuardTask.groovy | 10 +++++++- .../plugin/release/CombinedReleaseTest.groovy | 25 +++++++++++++++++++ .../plugin/release/ModuleReleaseTest.groovy | 12 +++++++-- 8 files changed, 74 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 90734eb..2ad2180 100644 --- a/README.md +++ b/README.md @@ -314,14 +314,22 @@ Then run: 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 manifest controls the coordinates -probed before upload and during publication polling, so review and retain it with the ZIP. +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. Source/version and saved manifest hashes are rechecked. +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. 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. Snapshots live beside the bundle, independently of -the deployment journal location. Cleanup failures emit a warning naming the path for +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. 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 b3c36f8..435cb20 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -13,7 +13,12 @@ class CombinedDeployment { } } // Upload a private snapshot: later writes to the original ZIP cannot change request bytes. - File uploadDirectory = java.nio.file.Files.createTempDirectory(bundle.absoluteFile.parentFile.toPath(), '.verified-upload-').toFile() + 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 { java.nio.file.Files.copy(bundle.toPath(), snapshot.toPath()) 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 ce51451..73bb549 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy @@ -65,6 +65,11 @@ final class ModuleReleaseConfigurer { task.dependsOn(extension.guardTask) } extension.guardTask.configure { task -> + task.expectedManifestSha256.set(project.providers.provider { + ReleaseAllTask release = extension.releaseAllTask.get() + !release.resumeId.getOrElse('') && release.useExistingBundle.getOrElse(false) ? + release.expectedManifestSha256.getOrElse('') : '' + }) task.versionsJson.convention(model.call().versionsJson) task.modulePaths.set(paths) task.unpublishedProjectPaths.set(unpublishedPaths) 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 f9d6123..385e319 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy @@ -67,7 +67,7 @@ abstract class ReleaseAllTask extends DefaultTask { } // Hash and parse the same bytes, so the probed graph is the reviewed manifest. byte[] manifestBytes = manifestFile.get().asFile.bytes - String manifestHash = java.security.MessageDigest.getInstance('SHA-256').digest(manifestBytes).encodeHex().toString() + String manifestHash = ReleaseChecksums.checksum(manifestBytes, 'SHA-256') Map receipt = ReleaseManifest.read(receiptFile.get().asFile) if (expectedManifest && (receipt.manifestHash != expectedManifest || manifestHash != expectedManifest)) { throw new IllegalStateException('Manifest or receipt differs from expectedManifestSha256; no upload attempted') 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 bdf98f2..e770999 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -338,6 +338,31 @@ class CombinedReleaseTest { assertFalse(directory.listFiles().any { it.name.startsWith('.verified-upload-') }) } + @Test void snapshotCreationFailureExplainsWritableDirectoryRequirement() { + File record = new File(directory, 'deployment.json') + 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 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 3b700d0..adc1e0d 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy @@ -576,7 +576,7 @@ nexusReleasePlugin { } @ParameterizedTest - @ValueSource(strings = ['dirty', 'commit', 'manifest', 'replan', 'missing-receipt', 'missing-zip', 'rewritten-receipt', 'rewritten-manifest-receipt']) + @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) @@ -596,6 +596,13 @@ nexusReleasePlugin { 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' @@ -633,7 +640,8 @@ nexusReleasePlugin { run('releaseAllPlan') def missingDigest = runner('releaseAll', '-Pexisting').buildAndFail() assertTrue(missingDigest.output.contains('requires expectedBundleSha256')) - def missingBundle = runner('releaseAll', '-Pexisting', '-Pexpected=' + ('0' * 64), '-PexpectedManifest=' + ('0' * 64)).buildAndFail() + 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') }) } From 94d72fa5bf0034ed66911d473e7dd4a16dd95b3a Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sun, 15 Nov 2026 17:33:18 +0100 Subject: [PATCH 5/5] Scope manifest evidence to upload graphs and explain snapshot copy failures --- README.md | 16 +++++++++------- .../plugin/release/CombinedDeployment.groovy | 9 +++++++-- .../release/CombinedReleaseRegistrar.groovy | 8 +++++++- .../release/ModuleReleaseConfigurer.groovy | 5 ----- .../plugin/release/CombinedReleaseTest.groovy | 19 +++++++++++++++++++ .../plugin/release/ModuleReleaseTest.groovy | 16 ++++++++++++++++ 6 files changed, 58 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2ad2180..9b82927 100644 --- a/README.md +++ b/README.md @@ -316,22 +316,24 @@ This submits without rebuilding, signing or staging again. Existing-bundle mode 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. +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. Source/version and saved manifest hashes are rechecked. +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. +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 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 435cb20..3ece01d 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -21,8 +21,13 @@ class CombinedDeployment { } File snapshot = new File(uploadDirectory, bundle.name) try { - java.nio.file.Files.copy(bundle.toPath(), snapshot.toPath()) - String uploadHash = ReleaseChecksums.checksum(snapshot, 'SHA-256') + 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') } 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 2e090ac..ca5deaf 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseRegistrar.groovy @@ -82,7 +82,13 @@ final class CombinedReleaseRegistrar { 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.get().validateDigestInputs() + 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() 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 73bb549..ce51451 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseConfigurer.groovy @@ -65,11 +65,6 @@ final class ModuleReleaseConfigurer { task.dependsOn(extension.guardTask) } extension.guardTask.configure { task -> - task.expectedManifestSha256.set(project.providers.provider { - ReleaseAllTask release = extension.releaseAllTask.get() - !release.resumeId.getOrElse('') && release.useExistingBundle.getOrElse(false) ? - release.expectedManifestSha256.getOrElse('') : '' - }) task.versionsJson.convention(model.call().versionsJson) task.modulePaths.set(paths) task.unpublishedProjectPaths.set(unpublishedPaths) 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 e770999..95bb68a 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -340,6 +340,8 @@ class CombinedReleaseTest { @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') { @@ -354,6 +356,23 @@ class CombinedReleaseTest { 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[] 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 adc1e0d..a250ae4 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/ModuleReleaseTest.groovy @@ -706,6 +706,22 @@ nexusReleasePlugin { } } + @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']")