Skip to content

Commit 464540d

Browse files
SNOW-1958601: Fix NPE when cache folder is inaccessible (#2108)
1 parent ffadfb0 commit 464540d

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/main/java/net/snowflake/client/core/FileCacheManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ synchronized String getCacheFilePath() {
9191
* @param newCacheFile a file object to override the default one.
9292
*/
9393
synchronized void overrideCacheFile(File newCacheFile) {
94-
if (!newCacheFile.exists()) {
95-
logger.debug("Cache file doesn't exists. File: {}", newCacheFile);
94+
if (!FileUtil.exists(newCacheFile)) {
95+
logger.debug("Cache file doesn't exist. File: {}", newCacheFile);
9696
}
9797
if (onlyOwnerPermissions) {
9898
FileUtil.handleWhenFilePermissionsWiderThanUserOnly(newCacheFile, "Override cache file");
@@ -264,8 +264,8 @@ synchronized <T> T withLock(Supplier<T> supplier) {
264264
/** Reads the cache file. */
265265
synchronized JsonNode readCacheFile() {
266266
try {
267-
if (!cacheFile.exists()) {
268-
logger.debug("Cache file doesn't exists. File: {}", cacheFile);
267+
if (!FileUtil.exists(cacheFile)) {
268+
logger.debug("Cache file doesn't exist. File: {}", cacheFile);
269269
return null;
270270
}
271271

@@ -373,8 +373,8 @@ private synchronized void deleteCacheLockIfExpired() {
373373
* @return epoch time in ms
374374
*/
375375
private static synchronized long fileCreationTime(File targetFile) {
376-
if (!targetFile.exists()) {
377-
logger.debug("File not exists. File: {}", targetFile);
376+
if (!FileUtil.exists(targetFile)) {
377+
logger.debug("File does not exist. File: {}", targetFile);
378378
return -1;
379379
}
380380
try {

src/main/java/net/snowflake/client/core/FileUtil.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ private static void logWarnWhenAccessibleByOthers(
134134
boolean isReadableByOthers = isPermPresent(filePermissions, READ_BY_OTHERS);
135135
boolean isExecutable = isPermPresent(filePermissions, EXECUTABLE);
136136

137-
if (isWritableByOthers || (isReadableByOthers || isExecutable)) {
137+
if (isWritableByOthers || (isReadableByOthers && logReadAccess) || isExecutable) {
138138
logger.warn(
139139
"{}File {} is accessible by others to:{}{}",
140140
getContextStr(context),
141141
filePath,
142142
isReadableByOthers && logReadAccess ? " read" : "",
143143
isWritableByOthers ? " write" : "",
144-
isExecutable ? " executable" : "");
144+
isExecutable ? " execute" : "");
145145
}
146146
} catch (IOException e) {
147147
logger.warn(
@@ -192,4 +192,11 @@ static String getFileOwnerName(Path filePath) throws IOException {
192192
private static String getContextStr(String context) {
193193
return Strings.isNullOrEmpty(context) ? "" : context + ": ";
194194
}
195+
196+
public static boolean exists(File file) {
197+
if (file == null) {
198+
return false;
199+
}
200+
return file.exists();
201+
}
195202
}

src/test/java/net/snowflake/client/core/FileCacheManagerTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ public void throwWhenReadCacheFileWithPermissionDifferentThanReadWriteForUserTes
104104
}
105105
}
106106

107+
@Test
108+
@RunOnLinuxOrMac
109+
public void notThrowExceptionWhenCacheFolderIsNotAccessible() throws IOException {
110+
try {
111+
Files.setPosixFilePermissions(
112+
cacheFile.getParentFile().toPath(), PosixFilePermissions.fromString("---------"));
113+
FileCacheManager fcm =
114+
FileCacheManager.builder()
115+
.setCacheDirectorySystemProperty(CACHE_DIR_PROP)
116+
.setCacheDirectoryEnvironmentVariable(CACHE_DIR_ENV)
117+
.setBaseCacheFileName(CACHE_FILE_NAME)
118+
.setCacheFileLockExpirationInSeconds(CACHE_FILE_LOCK_EXPIRATION_IN_SECONDS)
119+
.build();
120+
assertDoesNotThrow(fcm::readCacheFile);
121+
} finally {
122+
Files.setPosixFilePermissions(
123+
cacheFile.getParentFile().toPath(), PosixFilePermissions.fromString("rwx------"));
124+
}
125+
}
126+
107127
@Test
108128
@RunOnLinuxOrMac
109129
public void throwWhenOverrideCacheFileHasDifferentOwnerThanCurrentUserTest() {

0 commit comments

Comments
 (0)