Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1958601: Fix NPE when cache folder is inaccessible #2108

Merged
merged 5 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/net/snowflake/client/core/FileCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ synchronized String getCacheFilePath() {
* @param newCacheFile a file object to override the default one.
*/
synchronized void overrideCacheFile(File newCacheFile) {
if (!newCacheFile.exists()) {
logger.debug("Cache file doesn't exists. File: {}", newCacheFile);
if (!FileUtil.exists(newCacheFile)) {
logger.debug("Cache file doesn't exist. File: {}", newCacheFile);
}
if (onlyOwnerPermissions) {
FileUtil.handleWhenFilePermissionsWiderThanUserOnly(newCacheFile, "Override cache file");
Expand Down Expand Up @@ -264,8 +264,8 @@ synchronized <T> T withLock(Supplier<T> supplier) {
/** Reads the cache file. */
synchronized JsonNode readCacheFile() {
try {
if (!cacheFile.exists()) {
logger.debug("Cache file doesn't exists. File: {}", cacheFile);
if (!FileUtil.exists(cacheFile)) {
logger.debug("Cache file doesn't exist. File: {}", cacheFile);
return null;
}

Expand Down Expand Up @@ -373,8 +373,8 @@ private synchronized void deleteCacheLockIfExpired() {
* @return epoch time in ms
*/
private static synchronized long fileCreationTime(File targetFile) {
if (!targetFile.exists()) {
logger.debug("File not exists. File: {}", targetFile);
if (!FileUtil.exists(targetFile)) {
logger.debug("File does not exist. File: {}", targetFile);
return -1;
}
try {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/snowflake/client/core/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ private static void logWarnWhenAccessibleByOthers(
boolean isReadableByOthers = isPermPresent(filePermissions, READ_BY_OTHERS);
boolean isExecutable = isPermPresent(filePermissions, EXECUTABLE);

if (isWritableByOthers || (isReadableByOthers || isExecutable)) {
if (isWritableByOthers || (isReadableByOthers && logReadAccess) || isExecutable) {
logger.warn(
"{}File {} is accessible by others to:{}{}",
getContextStr(context),
filePath,
isReadableByOthers && logReadAccess ? " read" : "",
isWritableByOthers ? " write" : "",
isExecutable ? " executable" : "");
isExecutable ? " execute" : "");
}
} catch (IOException e) {
logger.warn(
Expand Down Expand Up @@ -192,4 +192,11 @@ static String getFileOwnerName(Path filePath) throws IOException {
private static String getContextStr(String context) {
return Strings.isNullOrEmpty(context) ? "" : context + ": ";
}

public static boolean exists(File file) {
if (file == null) {
return false;
}
return file.exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public void throwWhenReadCacheFileWithPermissionDifferentThanReadWriteForUserTes
}
}

@Test
@RunOnLinuxOrMac
public void notThrowExceptionWhenCacheFolderIsNotAccessible() throws IOException {
try {
Files.setPosixFilePermissions(
cacheFile.getParentFile().toPath(), PosixFilePermissions.fromString("---------"));
FileCacheManager fcm =
FileCacheManager.builder()
.setCacheDirectorySystemProperty(CACHE_DIR_PROP)
.setCacheDirectoryEnvironmentVariable(CACHE_DIR_ENV)
.setBaseCacheFileName(CACHE_FILE_NAME)
.setCacheFileLockExpirationInSeconds(CACHE_FILE_LOCK_EXPIRATION_IN_SECONDS)
.build();
assertDoesNotThrow(fcm::readCacheFile);
} finally {
Files.setPosixFilePermissions(
cacheFile.getParentFile().toPath(), PosixFilePermissions.fromString("rwx------"));
}
}

@Test
@RunOnLinuxOrMac
public void throwWhenOverrideCacheFileHasDifferentOwnerThanCurrentUserTest() {
Expand Down
Loading