From 3852fbd7b1f9e3e9505f10c7d184b9f6c723a6af Mon Sep 17 00:00:00 2001 From: Piotr Bulawa Date: Tue, 20 May 2025 14:48:52 +0200 Subject: [PATCH 1/4] Add null check for write cache file --- .../client/core/FileCacheManager.java | 5 ++-- .../client/core/FileCacheManagerTest.java | 28 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/FileCacheManager.java b/src/main/java/net/snowflake/client/core/FileCacheManager.java index f69259a505..b4b7f469c7 100644 --- a/src/main/java/net/snowflake/client/core/FileCacheManager.java +++ b/src/main/java/net/snowflake/client/core/FileCacheManager.java @@ -265,7 +265,7 @@ synchronized T withLock(Supplier supplier) { synchronized JsonNode readCacheFile() { try { if (!FileUtil.exists(cacheFile)) { - logger.debug("Cache file doesn't exist. File: {}", cacheFile); + logger.debug("Cache file doesn't exist. Ignoring read."); return null; } @@ -290,7 +290,8 @@ synchronized JsonNode readCacheFile() { synchronized void writeCacheFile(JsonNode input) { logger.debug("Writing cache file. File: {}", cacheFile); try { - if (input == null) { + if (input == null || !FileUtil.exists(cacheFile)) { + logger.debug("Cache file doesn't exist. Ignoring write."); return; } try (Writer writer = diff --git a/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java b/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java index 2f91399266..ba86f10749 100644 --- a/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java +++ b/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java @@ -14,6 +14,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.stream.Collectors; @@ -106,7 +107,7 @@ public void throwWhenReadCacheFileWithPermissionDifferentThanReadWriteForUserTes @Test @RunOnLinuxOrMac - public void notThrowExceptionWhenCacheFolderIsNotAccessible() throws IOException { + public void notThrowExceptionWhenCacheFolderIsNotAccessibleWhenReadFromCache() throws IOException { try { Files.setPosixFilePermissions( cacheFile.getParentFile().toPath(), PosixFilePermissions.fromString("---------")); @@ -124,6 +125,31 @@ public void notThrowExceptionWhenCacheFolderIsNotAccessible() throws IOException } } + @Test + @RunOnLinuxOrMac + public void notThrowExceptionWhenCacheFolderIsNotAccessibleWhenWriteToCache() throws IOException { + String tmpDirPath = System.getProperty("java.io.tmpdir"); + String cacheDirPath = tmpDirPath + File.separator + "snowflake-cache-dir-noaccess"; + System.setProperty("FILE_CACHE_MANAGER_CACHE_PATH", cacheDirPath); + try { + Files.createDirectory( + Paths.get(cacheDirPath), + PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("---------"))); + + FileCacheManager fcm = + FileCacheManager.builder() + .setOnlyOwnerPermissions(false) + .setCacheDirectorySystemProperty("FILE_CACHE_MANAGER_CACHE_PATH") + .setCacheDirectoryEnvironmentVariable("NONEXISTENT") + .setBaseCacheFileName("cache-file") + .build(); + assertDoesNotThrow(() -> fcm.writeCacheFile(mapper.createObjectNode())); + } finally { + Files.deleteIfExists(Paths.get(cacheDirPath)); + System.clearProperty("FILE_CACHE_MANAGER_CACHE_PATH"); + } + } + @Test @RunOnLinuxOrMac public void throwWhenOverrideCacheFileHasDifferentOwnerThanCurrentUserTest() { From a426d642a55db00ec283991ec5e6bf90972e0574 Mon Sep 17 00:00:00 2001 From: Piotr Bulawa Date: Tue, 20 May 2025 14:51:07 +0200 Subject: [PATCH 2/4] Format --- .../client/core/FileCacheManagerTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java b/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java index ba86f10749..903cfd9237 100644 --- a/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java +++ b/src/test/java/net/snowflake/client/core/FileCacheManagerTest.java @@ -14,7 +14,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.stream.Collectors; @@ -107,7 +106,8 @@ public void throwWhenReadCacheFileWithPermissionDifferentThanReadWriteForUserTes @Test @RunOnLinuxOrMac - public void notThrowExceptionWhenCacheFolderIsNotAccessibleWhenReadFromCache() throws IOException { + public void notThrowExceptionWhenCacheFolderIsNotAccessibleWhenReadFromCache() + throws IOException { try { Files.setPosixFilePermissions( cacheFile.getParentFile().toPath(), PosixFilePermissions.fromString("---------")); @@ -133,16 +133,16 @@ public void notThrowExceptionWhenCacheFolderIsNotAccessibleWhenWriteToCache() th System.setProperty("FILE_CACHE_MANAGER_CACHE_PATH", cacheDirPath); try { Files.createDirectory( - Paths.get(cacheDirPath), - PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("---------"))); + Paths.get(cacheDirPath), + PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("---------"))); FileCacheManager fcm = - FileCacheManager.builder() - .setOnlyOwnerPermissions(false) - .setCacheDirectorySystemProperty("FILE_CACHE_MANAGER_CACHE_PATH") - .setCacheDirectoryEnvironmentVariable("NONEXISTENT") - .setBaseCacheFileName("cache-file") - .build(); + FileCacheManager.builder() + .setOnlyOwnerPermissions(false) + .setCacheDirectorySystemProperty("FILE_CACHE_MANAGER_CACHE_PATH") + .setCacheDirectoryEnvironmentVariable("NONEXISTENT") + .setBaseCacheFileName("cache-file") + .build(); assertDoesNotThrow(() -> fcm.writeCacheFile(mapper.createObjectNode())); } finally { Files.deleteIfExists(Paths.get(cacheDirPath)); From 486a91a736062b608bb1c58646d6c333496d7985 Mon Sep 17 00:00:00 2001 From: Piotr Bulawa Date: Wed, 21 May 2025 07:56:14 +0200 Subject: [PATCH 3/4] Add message --- src/main/java/net/snowflake/client/core/FileCacheManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/FileCacheManager.java b/src/main/java/net/snowflake/client/core/FileCacheManager.java index b4b7f469c7..63e00ae223 100644 --- a/src/main/java/net/snowflake/client/core/FileCacheManager.java +++ b/src/main/java/net/snowflake/client/core/FileCacheManager.java @@ -265,7 +265,7 @@ synchronized T withLock(Supplier supplier) { synchronized JsonNode readCacheFile() { try { if (!FileUtil.exists(cacheFile)) { - logger.debug("Cache file doesn't exist. Ignoring read."); + logger.debug("Cache file doesn't exist. Ignoring read. File: {}", cacheFile); return null; } @@ -291,7 +291,7 @@ synchronized void writeCacheFile(JsonNode input) { logger.debug("Writing cache file. File: {}", cacheFile); try { if (input == null || !FileUtil.exists(cacheFile)) { - logger.debug("Cache file doesn't exist. Ignoring write."); + logger.debug("Cache file doesn't exist or input is null. Ignoring write. File: {}", cacheFile); return; } try (Writer writer = From e1b6cade0d022232a8a36eb20950e48ba233de8a Mon Sep 17 00:00:00 2001 From: Piotr Bulawa Date: Wed, 21 May 2025 08:25:43 +0200 Subject: [PATCH 4/4] Format --- src/main/java/net/snowflake/client/core/FileCacheManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/snowflake/client/core/FileCacheManager.java b/src/main/java/net/snowflake/client/core/FileCacheManager.java index 63e00ae223..08e40aa862 100644 --- a/src/main/java/net/snowflake/client/core/FileCacheManager.java +++ b/src/main/java/net/snowflake/client/core/FileCacheManager.java @@ -291,7 +291,8 @@ synchronized void writeCacheFile(JsonNode input) { logger.debug("Writing cache file. File: {}", cacheFile); try { if (input == null || !FileUtil.exists(cacheFile)) { - logger.debug("Cache file doesn't exist or input is null. Ignoring write. File: {}", cacheFile); + logger.debug( + "Cache file doesn't exist or input is null. Ignoring write. File: {}", cacheFile); return; } try (Writer writer =