diff --git a/elide-contrib/elide-dynamic-config-helpers/src/main/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifier.java b/elide-contrib/elide-dynamic-config-helpers/src/main/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifier.java index 53f94fb69b..941b154fe1 100644 --- a/elide-contrib/elide-dynamic-config-helpers/src/main/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifier.java +++ b/elide-contrib/elide-dynamic-config-helpers/src/main/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifier.java @@ -15,15 +15,15 @@ import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; -import org.apache.commons.io.FileUtils; import lombok.extern.slf4j.Slf4j; import java.io.BufferedInputStream; -import java.io.File; +import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.KeyStore; @@ -54,7 +54,7 @@ public class DynamicConfigVerifier { * @throws MissingOptionException */ public static void main(String[] args) throws ParseException, InvalidKeyException, NoSuchAlgorithmException, - SignatureException, FileNotFoundException, KeyStoreException, IOException { + SignatureException, FileNotFoundException, KeyStoreException, IOException { Options options = prepareOptions(); CommandLine cli = new DefaultParser().parse(options, args); @@ -77,6 +77,7 @@ public static void main(String[] args) throws ParseException, InvalidKeyExceptio } else { log.error("Could not verify " + modelTarFile + " with details provided"); + System.exit(-1); } } @@ -111,12 +112,24 @@ public static boolean verify(String fileContent, String signature, PublicKey pub */ public static String readTarContents(String archiveFile) throws FileNotFoundException, IOException { StringBuffer sb = new StringBuffer(); - TarArchiveInputStream archive = new TarArchiveInputStream( - new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile)))); - TarArchiveEntry entry; - while ((entry = archive.getNextTarEntry()) != null) { - sb.append(FileUtils.readFileToString(new File(entry.getName()), StandardCharsets.UTF_8)); + BufferedReader br = null; + TarArchiveInputStream archiveInputStream = null; + try { + archiveInputStream = new TarArchiveInputStream( + new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile)))); + TarArchiveEntry entry; + while ((entry = archiveInputStream.getNextTarEntry()) != null) { + br = new BufferedReader(new InputStreamReader(archiveInputStream)); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + } + } finally { + archiveInputStream.close(); + br.close(); } + return sb.toString(); } diff --git a/elide-contrib/elide-dynamic-config-helpers/src/test/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifiesTest.java b/elide-contrib/elide-dynamic-config-helpers/src/test/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifiesTest.java index a46645629c..7db5bf12c0 100644 --- a/elide-contrib/elide-dynamic-config-helpers/src/test/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifiesTest.java +++ b/elide-contrib/elide-dynamic-config-helpers/src/test/java/com/yahoo/elide/contrib/dynamicconfighelpers/verify/DynamicConfigVerifiesTest.java @@ -12,9 +12,21 @@ import org.apache.commons.cli.MissingArgumentException; import org.apache.commons.cli.MissingOptionException; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; +import org.apache.commons.compress.utils.IOUtils; +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.security.KeyPairGenerator; @@ -27,16 +39,25 @@ public class DynamicConfigVerifiesTest { private static KeyPair kp; private static String signature; + private static String tarContent = null; + private static final String TAR_FILE_PATH = "src/test/resources/test.tar.gz"; @BeforeAll public static void setUp() throws Exception { + createTarGZ(); kp = generateKeyPair(); - signature = sign("testing-signature", kp.getPrivate()); + tarContent = DynamicConfigVerifier.readTarContents(TAR_FILE_PATH); + signature = sign(tarContent, kp.getPrivate()); + } + + @AfterAll + public static void after() { + FileUtils.deleteQuietly(FileUtils.getFile(TAR_FILE_PATH)); } @Test public void testValidSignature() throws Exception { - assertTrue(DynamicConfigVerifier.verify("testing-signature", signature, kp.getPublic())); + assertTrue(DynamicConfigVerifier.verify(tarContent, signature, kp.getPublic())); } @Test @@ -86,4 +107,37 @@ private static String sign(String data, PrivateKey privateKey) throws Exception byte[] signature = privateSignature.sign(); return Base64.getEncoder().encodeToString(signature); } + + private static void createTarGZ() throws FileNotFoundException, IOException { + TarArchiveOutputStream tarOutputStream = null; + try { + String modelPath = "src/test/resources/models_missing/"; + tarOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream( + new BufferedOutputStream(new FileOutputStream(new File(TAR_FILE_PATH))))); + addFileToTarGz(tarOutputStream, modelPath, ""); + } finally { + tarOutputStream.finish(); + tarOutputStream.close(); + } + } + + private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException { + File f = new File(path); + String entryName = base + f.getName(); + TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName); + tOut.putArchiveEntry(tarEntry); + + if (f.isFile()) { + IOUtils.copy(new FileInputStream(f), tOut); + tOut.closeArchiveEntry(); + } else { + tOut.closeArchiveEntry(); + File[] children = f.listFiles(); + if (children != null) { + for (File child : children) { + addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/"); + } + } + } + } }