Skip to content

Commit 712c44c

Browse files
Allow absolute path for truststore (#299)
* Added support for aboslute paths in the filesytem * Updated documentation and added a test case for file system based loading * Cleaned up a couple of issues pointed out by style check and static code analysis tools * Apply suggestions from code review --------- Co-authored-by: Ryan Skraba <ryan.skraba@aiven.io>
1 parent 7247fdf commit 712c44c

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

docs/sink-connector-config-options.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ Connection
5252

5353
``http.ssl.truststore.location``
5454
Path to the SSL truststore file. Only JKS (Java KeyStore) format is supported.
55+
56+
The truststore file can be located in the following ways (searched in order):
57+
58+
1. **Class-based resource loading**: File bundled within the connector JAR
59+
2. **Context classloader resource loading**: File accessible via the broader classpath (including Kafka Connect plugin directory)
60+
3. **File system path**:
61+
62+
- **Absolute path**: Direct file system path (e.g., ``/path/to/truststore.jks``)
63+
- **Relative path**: Path relative to the connector JAR's parent directory
5564

5665
* Type: string
5766
* Default: null

src/main/java/io/aiven/kafka/connect/http/sender/TrustStoreLoader.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,26 @@ private static InputStream tryFileSystem(final String path) {
8181
return null;
8282
}
8383
try {
84-
final URL jarLocation = TrustStoreLoader.class.getProtectionDomain().getCodeSource().getLocation();
85-
LOG.debug("JAR location: {}", jarLocation);
86-
final Path jarPath = Paths.get(jarLocation.toURI());
87-
final Path parentPath = jarPath.getParent();
88-
if (parentPath == null) {
89-
LOG.info("JAR has no parent directory, skipping file system lookup");
90-
return null;
84+
final Path truststorePath;
85+
if (Paths.get(path).isAbsolute()) {
86+
// Use absolute path directly
87+
truststorePath = Paths.get(path);
88+
LOG.info("Using absolute file system path: {}", truststorePath);
89+
} else {
90+
// Resolve relative path against JAR location
91+
final URL jarLocation = TrustStoreLoader.class.getProtectionDomain().getCodeSource().getLocation();
92+
LOG.info("JAR location: {}", jarLocation);
93+
final Path jarPath = Paths.get(jarLocation.toURI());
94+
LOG.info("Using relative path against the JAR location: {}", jarPath);
95+
final Path parentPath = jarPath.getParent();
96+
LOG.info("JAR parentPath: {}", parentPath);
97+
if (parentPath == null) {
98+
LOG.info("JAR has no parent directory, skipping file system lookup");
99+
return null;
100+
}
101+
truststorePath = parentPath.resolve(path);
102+
LOG.info("Trying relative file system path: {}", truststorePath);
91103
}
92-
final Path truststorePath = parentPath.resolve(path.startsWith("/") ? path.substring(1) : path);
93-
LOG.debug("Trying file system path: {}", truststorePath);
94104
final File truststoreFile = truststorePath.toFile();
95105
if (truststoreFile.exists()) {
96106
LOG.info("Found via file system");

src/test/java/io/aiven/kafka/connect/http/sender/TrustStoreLoaderTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616

1717
package io.aiven.kafka.connect.http.sender;
1818

19+
import java.io.File;
20+
import java.io.FileOutputStream;
21+
import java.io.IOException;
1922
import java.io.InputStream;
23+
import java.nio.file.Path;
2024

2125
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
2227

2328
import static org.assertj.core.api.Assertions.assertThat;
2429

@@ -35,4 +40,19 @@ void findTrustStoreInputStreamReturnsNullForEmptyPath() {
3540
final InputStream result = TrustStoreLoader.findTrustStoreInputStream("");
3641
assertThat(result).isNull();
3742
}
43+
44+
@Test
45+
void findTrustStoreInputStreamFindsFileWithAbsolutePath(@TempDir final Path tempDir) throws IOException {
46+
// Create a temporary trust store file
47+
final File trustStoreFile = tempDir.resolve("test-truststore.jks").toFile();
48+
try (final FileOutputStream fos = new FileOutputStream(trustStoreFile)) {
49+
fos.write("dummy truststore content".getBytes(java.nio.charset.StandardCharsets.UTF_8));
50+
}
51+
52+
// Test with absolute path
53+
final InputStream result = TrustStoreLoader.findTrustStoreInputStream(trustStoreFile.getAbsolutePath());
54+
55+
assertThat(result).isNotNull();
56+
result.close();
57+
}
3858
}

0 commit comments

Comments
 (0)