diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java index e6b388c983..9ad97fa76e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java @@ -1003,12 +1003,19 @@ public class CodeArtifactService implements Resettable { digest.update(buffer, 0, read); } return expectedSha256.equalsIgnoreCase(SigV4RequestValidator.hexEncode(digest.digest())); - } catch (NoSuchFileException e) { + } catch (NoSuchFileException expected) { + // Routine, not diagnostically interesting: the metadata already confirmed this asset + // exists (the only way this method gets called), so a missing file here means something + // touched the asset store directly without going through Floci, e.g. a partial restore + // that skipped it. That is exactly the case this method's caller repairs, not a real + // failure worth a log line. return false; } catch (IOException e) { - // Any I/O error reading the existing file (not just a clean "not found") reads the - // same as "can't confirm it's intact": a repair write is the safe response, not a - // thrown exception that would turn an otherwise-successful idempotent retry into one. + // Unlike a missing file, any other I/O error reading an existing file is unexpected and + // worth keeping a record of, even though the safe response is still the same: a repair + // write, not a thrown exception that would turn an otherwise-successful idempotent + // retry into one. + LOG.warnv(e, "Could not verify existing CodeArtifact asset file {0}: {1}", filePath, e.getMessage()); return false; } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("JVM does not support SHA-256", e); diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java index 18149fdc64..92f1e8b141 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactAssetPersistenceTest.java @@ -11,23 +11,28 @@ import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.Publ import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactDomain; import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactPackageVersion; import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository; +import io.github.hectorvent.floci.testutil.LogCapture; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.AccessDeniedException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.Base64; import java.util.List; import java.util.Map; +import java.util.logging.LogRecord; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -213,6 +218,60 @@ class CodeArtifactAssetPersistenceTest { assertEquals("hello world", new String(fetched.asset().getContent(), StandardCharsets.UTF_8)); } + /** + * A backing file that exists but can't be read at all (a permission problem, not the missing- + * file case) must go through the same repair as a corrupted or missing one, not fail the whole + * publish, and must actually log the warning this behavior exists to add: a test that only + * checks the repair succeeded would still pass if {@code LOG.warnv} were deleted entirely, so + * this captures the real log record and asserts on it directly, the same way + * {@code PersistentPathValidatorTest} already does against the same {@code org.jboss.logging} + * backend. Skipped when the test runner can read regardless of permission bits (e.g. running as + * root), since removing read permission would not actually block the read there; the assumption + * keeps that a visibly skipped test, not a silently vacuous pass. + */ + @Test + void republishingWithMatchingContentRepairsAnUnreadableBackingFileAndLogsAWarning(@TempDir Path dir) + throws IOException { + CodeArtifactService service = newService(dir); + service.createDomain(REGION, "dom", null, Map.of()); + service.createRepository(REGION, "dom", null, "repo", null, null, Map.of()); + byte[] content = "hello world".getBytes(StandardCharsets.UTF_8); + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", "a.txt", + sha256Hex(content), "true", content); + + Path assetFile; + try (Stream paths = Files.walk(dir.resolve("codeartifact-assets"))) { + assetFile = paths.filter(Files::isRegularFile).findFirst().orElseThrow(); + } + Assumptions.assumeTrue(assetFile.toFile().setReadable(false), + "test filesystem must support removing read permission"); + Assumptions.assumeFalse(Files.isReadable(assetFile), + "removing read permission must actually block reads (not running as root)"); + + try { + List records = LogCapture.capture(CodeArtifactService.class, () -> + service.publishPackageVersion(REGION, "dom", null, "repo", "generic", null, "my-pkg", "1.0.0", + "a.txt", sha256Hex(content), "true", content)); + + assertTrue(records.stream().anyMatch(r -> r.getMessage() != null + && r.getMessage().contains("Could not verify") + && r.getThrown() instanceof AccessDeniedException + && r.getParameters() != null + && r.getParameters().length > 0 + && assetFile.toString().equals(String.valueOf(r.getParameters()[0]))), + "expected a warning logging the AccessDeniedException with the real file path " + + "(not, say, the internal composite key) as its first parameter, got: " + records); + + assertEquals("Unfinished", service.describePackageVersion(REGION, "dom", null, "repo", "generic", null, + "my-pkg", "1.0.0").getStatus()); + PackageVersionAssetResult fetched = service.getPackageVersionAsset(REGION, "dom", null, "repo", + "generic", null, "my-pkg", "1.0.0", "a.txt", null); + assertEquals("hello world", new String(fetched.asset().getContent(), StandardCharsets.UTF_8)); + } finally { + assetFile.toFile().setReadable(true); + } + } + /** * The overwhelmingly common case, an intact asset republished unchanged, must stay a true * no-op that needs no write capacity at all: Greptile caught that always rewriting (an earlier