The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Avoid temp files when reading JAR (#73638)
Chris Truterb423ddfeee37
diff --git a/src/metabase/sql_parsing/pool.clj b/src/metabase/sql_parsing/pool.cljindex 2b18971f69a..26cc98063d2 100644--- a/src/metabase/sql_parsing/pool.clj+++ b/src/metabase/sql_parsing/pool.clj@@ -17,8 +17,11 @@PoolPools)(java.io Closeable File)- (java.nio.file FileSystems)+ (java.net URI)+ (java.nio.channels SeekableByteChannel)+ (java.nio.file AccessMode DirectoryStream DirectoryStream$Filter Files FileSystems Path)(java.time Duration)+ (java.util Map Set)(java.util.concurrent TimeUnit TimeoutException)(org.graalvm.polyglot Context HostAccess)(org.graalvm.polyglot.io FileSystem IOAccess)))@@ -121,10 +124,44 @@;;; -------------------------------------------------- Python path delay --------------------------------------------------+(defn- nio-polyglot-fs+ "Wrap a NIO `java.nio.file.FileSystem` as a polyglot `FileSystem`, routing reads to+ `Files/newByteChannel` instead of the provider's `newFileChannel`.++ The stock wrapper triggers ZipFileSystem to extract deflated entries to a temp file beside the jar,+ which fails when the jar's parent directory isn't writable (e.g. non-root in a Kubernetes pod).+ See https://github.com/metabase/metabase/issues/73541."+ ^FileSystem [^java.nio.file.FileSystem nio-fs]+ (let [provider (.provider nio-fs)]+ (reify FileSystem+ (^Path parsePath [_ ^URI uri]+ (.getPath provider uri))+ (^Path parsePath [_ ^String s]+ (.getPath nio-fs s (into-array String [])))+ (^void checkAccess [_ ^Path p ^Set modes ^"[Ljava.nio.file.LinkOption;" _opts]+ ;; LinkOption opts are dropped: the only value is NOFOLLOW_LINKS, and neither our jar zip FS+ ;; nor resources/python-sources contains symlinks.+ (.checkAccess provider p ^"[Ljava.nio.file.AccessMode;" (into-array AccessMode modes)))+ (^void createDirectory [_ ^Path p ^"[Ljava.nio.file.attribute.FileAttribute;" attrs]+ (Files/createDirectory p attrs))+ (^void delete [_ ^Path p]+ (Files/delete p))+ (^SeekableByteChannel newByteChannel+ [_ ^Path p ^Set opts ^"[Ljava.nio.file.attribute.FileAttribute;" attrs]+ (Files/newByteChannel p opts attrs))+ (^DirectoryStream newDirectoryStream [_ ^Path dir ^DirectoryStream$Filter filter]+ (Files/newDirectoryStream dir filter))+ (^Path toAbsolutePath [_ ^Path p]+ (.toAbsolutePath p))+ (^Path toRealPath [_ ^Path p ^"[Ljava.nio.file.LinkOption;" opts]+ (.toRealPath p opts))+ (^Map readAttributes [_ ^Path p ^String attrs ^"[Ljava.nio.file.LinkOption;" opts]+ (Files/readAttributes p attrs opts)))))+(defn- read-only-polyglot-fs"Wrap an NIO `java.nio.file.FileSystem` as a read-only polyglot FileSystem suitable for GraalPy."^FileSystem [^java.nio.file.FileSystem nio-fs]- (-> nio-fs FileSystem/newFileSystem FileSystem/newReadOnlyFileSystem))+ (-> nio-fs nio-polyglot-fs FileSystem/newReadOnlyFileSystem))(defonce ^:private^{:doc "A read-only polyglot FileSystem and the PythonPath within it.diff --git a/test/metabase/sql_parsing/jar_python_fs_repro_test.clj b/test/metabase/sql_parsing/jar_python_fs_repro_test.cljnew file mode 100644index 00000000000..5d2ab44d47e--- /dev/null+++ b/test/metabase/sql_parsing/jar_python_fs_repro_test.clj@@ -0,0 +1,48 @@+(ns metabase.sql-parsing.jar-python-fs-repro-test+ "Regression for https://github.com/metabase/metabase/issues/73541.++ GraalVM's bundled polyglot `FileSystem` wrapper routes `newByteChannel` through+ `provider.newFileChannel`, which forces JDK ZipFileSystem to extract deflated entries to a temp file+ beside the jar — fails when that directory isn't writable (non-root in a Kubernetes pod).+ The fix in `metabase.sql-parsing.pool/nio-polyglot-fs` overrides `newByteChannel` to call+ `Files/newByteChannel`, which returns an in-memory `ByteArrayChannel` for compressed entries."+ (:require+ [clojure.test :refer :all]+ [metabase.sql-parsing.pool :as pool]+ [metabase.util.files :as u.files])+ (:import+ (java.io FileOutputStream)+ (java.nio.channels FileChannel)+ (java.nio.file Files StandardOpenOption)+ (java.nio.file.attribute FileAttribute)+ (java.util EnumSet)+ (java.util.zip ZipEntry ZipOutputStream)+ (org.graalvm.polyglot.io FileSystem)))++(set! *warn-on-reflection* true)++(deftest ^:parallel new-byte-channel-returns-in-memory-channel-test+ (testing "newByteChannel on a compressed zip entry returns an in-memory channel rather than extracting+ to a temp file beside the jar. With the stock GraalVM wrapper, the channel is a `FileChannel`+ backed by temp-file extraction — that path fails when the jar's parent dir isn't writable."+ (let [zip-file (Files/createTempFile "metabase-fs-repro" ".zip" (make-array FileAttribute 0))]+ (try+ ;; Write a DEFLATE-compressed zip entry — the case that triggered the bug; STORED entries don't+ ;; need temp extraction.+ (with-open [zos (ZipOutputStream. (FileOutputStream. (str zip-file)))]+ (.setLevel zos java.util.zip.Deflater/DEFAULT_COMPRESSION)+ (let [entry (ZipEntry. "/python-sources/sql_tools.py")]+ (.setMethod entry ZipEntry/DEFLATED)+ (.putNextEntry zos entry)+ (.write zos (.getBytes "print('hello')\n" "UTF-8"))+ (.closeEntry zos)))+ (with-open [^java.nio.file.FileSystem nio-fs (u.files/nio-fs (str zip-file))]+ (let [^FileSystem polyfs (#'pool/read-only-polyglot-fs nio-fs)+ path (.parsePath polyfs "/python-sources/sql_tools.py")]+ (with-open [ch (.newByteChannel polyfs path+ (EnumSet/of StandardOpenOption/READ)+ (make-array FileAttribute 0))]+ (is (not (instance? FileChannel ch))+ "Channel must not be a FileChannel — that implies temp-file extraction beside the jar."))))+ (finally+ (Files/deleteIfExists zip-file))))))