From 6ec1fed2a482d597c9b45256f120a0a14527b897 Mon Sep 17 00:00:00 2001 From: Michal Petrov Date: Mon, 15 Jan 2024 15:56:24 +0100 Subject: [PATCH] JBEAP-26402: handle badly formatted artifact cache --- .../java/org/wildfly/prospero/ProsperoLogger.java | 4 ++++ .../wildfly/prospero/galleon/ArtifactCache.java | 15 ++++++++++----- .../prospero/galleon/ArtifactCacheTest.java | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java b/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java index 119ddcf61..b0750babe 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/ProsperoLogger.java @@ -34,6 +34,7 @@ import org.wildfly.prospero.api.exceptions.NoChannelException; import org.wildfly.prospero.api.exceptions.ProvisioningRuntimeException; +import java.io.IOException; import java.net.URI; import java.net.URL; import java.nio.file.Path; @@ -350,4 +351,7 @@ public interface ProsperoLogger extends BasicLogger { @Message(id = 260, value = "The selected folder %s cannot be created.") IllegalArgumentException dirMustBeWritable(Path directory); + + @Message(id = 264, value = "Bad artifact record format in the cache descriptor, line %d: '%s'") + IOException unableToReadArtifactCache(int row, String line, @Cause Exception e); } diff --git a/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java b/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java index fdfbdb819..329aac45e 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/galleon/ArtifactCache.java @@ -22,6 +22,7 @@ import org.jboss.galleon.util.IoUtils; import org.jboss.logging.Logger; import org.wildfly.channel.MavenArtifact; +import org.wildfly.prospero.ProsperoLogger; import org.wildfly.prospero.metadata.ProsperoMetadataUtils; import java.io.BufferedWriter; @@ -195,10 +196,14 @@ private void init() throws IOException { Path artifactLog = cacheDir.resolve(ArtifactCache.CACHE_FILENAME); if (Files.exists(artifactLog)) { + int row = 0; + final List lines = Files.readAllLines(artifactLog); try { - final List lines = Files.readAllLines(artifactLog); - for (String line : lines) { - final String[] splitLine = line.split(ArtifactCache.CACHE_LINE_SEPARATOR); + for ( ; row < lines.size(); row++) { + final String[] splitLine = lines.get(row).split(ArtifactCache.CACHE_LINE_SEPARATOR); + if (splitLine.length < 3) { + throw new IOException("Not enough segments, expected format is ::::"); + } String gav = splitLine[0]; String hash = splitLine[1]; Path path = Paths.get(splitLine[2]); @@ -207,8 +212,8 @@ private void init() throws IOException { paths.put(key, installationDir.resolve(path)); hashes.put(key, hash); } - } catch (MavenUniverseException e) { - throw new IOException("Unable to read cached items.", e); + } catch (MavenUniverseException | IOException e) { + throw ProsperoLogger.ROOT_LOGGER.unableToReadArtifactCache(row + 1, lines.get(row), e); } } } diff --git a/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java b/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java index 50aef9569..0aaf7384e 100644 --- a/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java +++ b/prospero-common/src/test/java/org/wildfly/prospero/galleon/ArtifactCacheTest.java @@ -24,12 +24,14 @@ import org.wildfly.channel.MavenArtifact; import java.io.File; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -63,6 +65,18 @@ public void testNoCacheFolderReturnsNoArtifacts() throws Exception { assertEquals(Optional.empty(),cache.getArtifact(GROUP_ID, ARTIFACT_ID, EXTENSION, CLASSIFIER, VERSION)); } + @Test + public void testReadBadlyFormattedFile() throws Exception { + Path newFolder = temp.newFolder().toPath(); + Files.createDirectories(newFolder.resolve(ArtifactCache.CACHE_FOLDER)); + Files.writeString(newFolder.resolve(ArtifactCache.CACHE_FOLDER).resolve(ArtifactCache.CACHE_FILENAME),"badformat"); + + assertThatThrownBy(() -> ArtifactCache.getInstance(newFolder)) + .isInstanceOf(IOException.class) + .hasMessageContaining("PRSP000264") + .hasStackTraceContaining("Not enough segments"); + } + @Test public void emptyCacheListReturnsNoArtifacts() throws Exception { assertEquals(Optional.empty(),cache.getArtifact(GROUP_ID, ARTIFACT_ID, EXTENSION, CLASSIFIER, VERSION));