|
| 1 | +package net.snowflake.client.core.crl; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | +import java.net.URLEncoder; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.StandardOpenOption; |
| 11 | +import java.nio.file.attribute.BasicFileAttributes; |
| 12 | +import java.nio.file.attribute.FileTime; |
| 13 | +import java.nio.file.attribute.PosixFilePermissions; |
| 14 | +import java.security.cert.CRLException; |
| 15 | +import java.security.cert.CertificateException; |
| 16 | +import java.security.cert.CertificateFactory; |
| 17 | +import java.security.cert.X509CRL; |
| 18 | +import java.time.Duration; |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.concurrent.locks.Lock; |
| 21 | +import java.util.concurrent.locks.ReentrantLock; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.Stream; |
| 24 | +import net.snowflake.client.core.Constants; |
| 25 | +import net.snowflake.client.jdbc.SnowflakeSQLLoggedException; |
| 26 | +import net.snowflake.client.log.SFLogger; |
| 27 | +import net.snowflake.client.log.SFLoggerFactory; |
| 28 | +import net.snowflake.common.core.SqlState; |
| 29 | + |
| 30 | +class CRLFileCache implements CRLCache { |
| 31 | + |
| 32 | + private static final SFLogger logger = SFLoggerFactory.getLogger(CRLFileCache.class); |
| 33 | + |
| 34 | + private final Path cacheDir; |
| 35 | + private final Duration removalDelay; |
| 36 | + private final Lock cacheLock = new ReentrantLock(); |
| 37 | + |
| 38 | + CRLFileCache(Path cacheDir, Duration removalDelay) throws SnowflakeSQLLoggedException { |
| 39 | + this.cacheDir = cacheDir; |
| 40 | + this.removalDelay = removalDelay; |
| 41 | + |
| 42 | + ensureCacheDirectoryExists(cacheDir); |
| 43 | + } |
| 44 | + |
| 45 | + public CRLCacheEntry get(String crlUrl) { |
| 46 | + try { |
| 47 | + cacheLock.lock(); |
| 48 | + Path crlFilePath = getCrlFilePath(crlUrl); |
| 49 | + if (Files.exists(crlFilePath)) { |
| 50 | + logger.debug("Found CRL on disk for {}", crlFilePath); |
| 51 | + |
| 52 | + BasicFileAttributes attrs = Files.readAttributes(crlFilePath, BasicFileAttributes.class); |
| 53 | + Instant downloadTime = attrs.lastModifiedTime().toInstant(); |
| 54 | + |
| 55 | + CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 56 | + try (InputStream crlBytes = Files.newInputStream(crlFilePath)) { |
| 57 | + X509CRL crl = (X509CRL) certFactory.generateCRL(crlBytes); |
| 58 | + return new CRLCacheEntry(crl, downloadTime); |
| 59 | + } |
| 60 | + } |
| 61 | + } catch (Exception e) { |
| 62 | + logger.warn("Failed to read CRL from disk cache for {}: {}", crlUrl, e.getMessage()); |
| 63 | + } finally { |
| 64 | + cacheLock.unlock(); |
| 65 | + } |
| 66 | + |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + public void put(String crlUrl, CRLCacheEntry entry) { |
| 71 | + try { |
| 72 | + cacheLock.lock(); |
| 73 | + Path crlFilePath = getCrlFilePath(crlUrl); |
| 74 | + |
| 75 | + Files.write( |
| 76 | + crlFilePath, |
| 77 | + entry.getCrl().getEncoded(), |
| 78 | + StandardOpenOption.CREATE, |
| 79 | + StandardOpenOption.WRITE, |
| 80 | + StandardOpenOption.TRUNCATE_EXISTING); |
| 81 | + |
| 82 | + Files.setLastModifiedTime(crlFilePath, FileTime.from(entry.getDownloadTime())); |
| 83 | + |
| 84 | + if (Constants.getOS() != Constants.OS.WINDOWS) { |
| 85 | + Files.setPosixFilePermissions(crlFilePath, PosixFilePermissions.fromString("rw-------")); |
| 86 | + } |
| 87 | + |
| 88 | + logger.debug("Updated disk cache for {}", crlUrl); |
| 89 | + } catch (Exception e) { |
| 90 | + logger.warn("Failed to write CRL to disk cache for {}: {}", crlUrl, e.getMessage()); |
| 91 | + } finally { |
| 92 | + cacheLock.unlock(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void cleanup() { |
| 97 | + Instant now = Instant.now(); |
| 98 | + logger.debug("Cleaning up on-disk CRL cache at {}", now); |
| 99 | + |
| 100 | + try { |
| 101 | + if (!Files.exists(cacheDir)) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + int removedCount = 0; |
| 106 | + try (Stream<Path> files = Files.list(cacheDir)) { |
| 107 | + cacheLock.lock(); |
| 108 | + for (Path filePath : files.filter(Files::isRegularFile).collect(Collectors.toList())) { |
| 109 | + try { |
| 110 | + try (InputStream crlBytes = Files.newInputStream(filePath)) { |
| 111 | + CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 112 | + X509CRL crl = (X509CRL) certFactory.generateCRL(crlBytes); |
| 113 | + CRLCacheEntry entry = |
| 114 | + new CRLCacheEntry(crl, Files.getLastModifiedTime(filePath).toInstant()); |
| 115 | + |
| 116 | + boolean expired = entry.isCrlExpired(now); |
| 117 | + boolean evicted = entry.isEvicted(now, removalDelay); |
| 118 | + if (expired || evicted) { |
| 119 | + Files.delete(filePath); |
| 120 | + removedCount++; |
| 121 | + logger.debug( |
| 122 | + "Removing file based CRL cache entry for {}: expired={}, evicted={}", |
| 123 | + filePath, |
| 124 | + expired, |
| 125 | + evicted); |
| 126 | + } |
| 127 | + } |
| 128 | + } catch (IOException | CRLException | CertificateException e) { |
| 129 | + // If we can't parse the file, it's probably corrupted - remove it |
| 130 | + try { |
| 131 | + Files.delete(filePath); |
| 132 | + removedCount++; |
| 133 | + } catch (IOException deleteError) { |
| 134 | + logger.warn( |
| 135 | + "Failed to delete corrupted CRL file {}: {}", filePath, deleteError.getMessage()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } finally { |
| 140 | + cacheLock.unlock(); |
| 141 | + } |
| 142 | + |
| 143 | + if (removedCount > 0) { |
| 144 | + logger.debug("Removed {} expired/corrupted files from disk CRL cache", removedCount); |
| 145 | + } |
| 146 | + } catch (Exception e) { |
| 147 | + logger.warn("Failed to cleanup disk CRL cache: {}", e.getMessage()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private Path getCrlFilePath(String crlUrl) throws UnsupportedEncodingException { |
| 152 | + String encodedUrl = URLEncoder.encode(crlUrl, StandardCharsets.UTF_8.toString()); |
| 153 | + return cacheDir.resolve(encodedUrl); |
| 154 | + } |
| 155 | + |
| 156 | + private static boolean ownerOnlyPermissions(Path cacheDir) throws IOException { |
| 157 | + return Files.getPosixFilePermissions(cacheDir) |
| 158 | + .equals(PosixFilePermissions.fromString("rwx------")); |
| 159 | + } |
| 160 | + |
| 161 | + private static void ensureCacheDirectoryExists(Path cacheDir) throws SnowflakeSQLLoggedException { |
| 162 | + try { |
| 163 | + boolean exists = Files.exists(cacheDir); |
| 164 | + if (!exists) { |
| 165 | + Files.createDirectories(cacheDir); |
| 166 | + logger.debug("Initialized CRL cache directory: {}", cacheDir); |
| 167 | + } |
| 168 | + |
| 169 | + if (Constants.getOS() != Constants.OS.WINDOWS && !ownerOnlyPermissions(cacheDir)) { |
| 170 | + Files.setPosixFilePermissions(cacheDir, PosixFilePermissions.fromString("rwx------")); |
| 171 | + logger.debug("Set CRL cache directory permissions to 'rwx------"); |
| 172 | + } |
| 173 | + } catch (Exception e) { |
| 174 | + throw new SnowflakeSQLLoggedException( |
| 175 | + null, null, SqlState.INTERNAL_ERROR, "Failed to create CRL cache directory", e); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments