Skip to content
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
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Java caller
→ returns C.Result (out/err/stdOut/stdErr) → JNA → Result → parsed into Release
```

Native library loading: `Helm` class holds `HelmLibHolder.INSTANCE`, initialized lazily by `NativeLibrary.getInstance().load()`. `getInstance()` uses Java `ServiceLoader` to discover the platform module on the classpath (each platform JAR has `META-INF/services/com.marcnuri.helm.jni.NativeLibrary`) and `NativeLibrarySelector` picks the first one whose `getOs()`/`getArch()` match the running JVM, else the first that declares neither (detection in `Platform`; providers declaring another platform are never picked; the `com.marcnuri.jkube-helm.nativeLibrary` system property forces one by class name, `com.marcnuri.jkube-helm.tempDir` sets the extraction dir); if none are registered, falls back to `RemoteJarLoader` (downloads the correct platform JAR at runtime — works for Gradle, fails when air-gapped). `helm-java/pom.xml` has OS-activated profiles that pull in the right platform module automatically; both `amd64`/`x86_64` and `arm64`/`aarch64` aliases are handled.
Native library loading: `Helm` class holds `HelmLibHolder.INSTANCE`, initialized lazily by `NativeLibrary.getInstance().load()`. `getInstance()` uses Java `ServiceLoader` to discover the platform module on the classpath (each platform JAR has `META-INF/services/com.marcnuri.helm.jni.NativeLibrary`) and `NativeLibrarySelector` picks the first one whose `getOs()`/`getArch()` match the running JVM, else the first that declares neither (detection in `Platform`; providers declaring another platform are never picked; the `com.marcnuri.jkube-helm.nativeLibrary` system property forces one by class name, `com.marcnuri.jkube-helm.tempDir` sets the extraction dir); if none are registered, falls back to `RemoteJarLoader` (downloads the correct platform JAR at runtime — works for Gradle, fails when air-gapped). The download is verified against the repository's `.sha1` and cached in the per-user `CacheDirectory` (`~/.cache/helm-java`, `$XDG_CACHE_HOME/helm-java`, `%LOCALAPPDATA%\helm-java\cache`, or the `com.marcnuri.jkube-helm.cacheDir` system property; on POSIX the directory and files are owner-only, and the directory is refused if a symlink, another user's, or group/world-writable; an unusable default falls back to a per-JVM temp directory); a cache hit isn't re-hashed: the JAR and its `<jar>.verified` record must be private files and the JAR must have the recorded size, so a same-size replacement by the same user isn't detected. Failures surface as the cause of `getInstance()`'s `IllegalStateException`. `helm-java/pom.xml` has OS-activated profiles that pull in the right platform module automatically; both `amd64`/`x86_64` and `arm64`/`aarch64` aliases are handled.

## Public API shape (Helm.java)

Expand Down Expand Up @@ -151,6 +151,7 @@ Verbs with both forms: `install`, `template`, `show`, `upgrade`. Most others are
- The registry configs come from the test resources (`helm-java/src/test/resources/helm-home/`, copied by Maven and IDEs alike) and hold a placeholder entry, so registry logins go to that file and not to an OS credential store (Keychain, Credential Manager, `pass`, Secret Service).
- Trade-off: Testcontainers reads `DOCKER_CONFIG` too, so the KinD and Ryuk image pulls in `HelmKubernetesTest` are anonymous (Docker Hub rate limits apply; an authenticated mirror won't be used).
- Don't make a test depend on the working directory (e.g. packaging `Paths.get("")`): `target/helm-home` lives in it and holds large repository indexes.
- Tests that reach the remote native JAR download (`NativeLibraryTest`) set the `com.marcnuri.jkube-helm.cacheDir` system property to their own `@TempDir` (and restore it), so they never write the user's `~/.cache/helm-java`.

```java
class HelmFeatureTest {
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Helm runs in-process from a native library built for each platform, shipped in i
An application built on Linux x86_64 gets `linux-amd64`, whatever host it runs on later.
- **Without a platform artifact** (e.g. Gradle, which doesn't activate Maven profiles): the matching artifact is downloaded from Maven Central the first time it's needed.
This doesn't work in air-gapped environments.
The download is verified against the repository's SHA-1 checksum (`.sha1`) and cached in a per-user directory: `~/.cache/helm-java` (or `$XDG_CACHE_HOME/helm-java`) on Linux and macOS, `%LOCALAPPDATA%\helm-java\cache` on Windows.
On Linux and macOS the directory and its files are created owner-only, and the directory is refused if it's a symbolic link, belongs to another user, or is group- or world-writable.
If the default directory can't be created or written (no home directory, a read-only file system), a new private temporary directory is used, so each JVM downloads the JAR again (a `java.util.logging` warning says why).
A cached JAR is used if it's still a file of the size recorded when it was verified that other users can't write; otherwise it's downloaded and verified again.
It isn't hashed again on each use, so a replacement of the same size by the same user isn't detected.

**Building for another platform.** When the build host and the target host differ (e.g. a container image for `linux/arm64` built on an x86_64 machine), exclude the platform artifacts and add the one for the target explicitly:

Expand Down Expand Up @@ -79,6 +84,7 @@ Libraries declaring another platform are never used. If libraries are on the cla
|-----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `com.marcnuri.jkube-helm.tempDir` | Base directory where the native library is extracted before loading it (defaults to `java.io.tmpdir`). It's created if missing, and each JVM extracts into its own `helm-java*` subdirectory inside it. Set it to an executable location if `/tmp` is mounted with `noexec`. |
| `com.marcnuri.jkube-helm.nativeLibrary` | Fully qualified class name of the `com.marcnuri.helm.jni.NativeLibrary` implementation to use, regardless of the detected platform. The class must be registered in a `META-INF/services/com.marcnuri.helm.jni.NativeLibrary` file on the classpath, like the ones in the platform artifacts. |
| `com.marcnuri.jkube-helm.cacheDir` | Directory where the native library JAR downloaded from Maven Central is cached (see above for the default). It must not be writable by other users: on Linux and macOS it's created owner-only if missing, and refused if it's a symbolic link, belongs to another user, or is group- or world-writable. Unlike the default directory, a directory set here that can't be created or written is an error, not a reason to fall back to a temporary one. |

## Features

Expand Down
69 changes: 63 additions & 6 deletions helm-java/src/test/java/com/marcnuri/helm/NativeLibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@

import com.marcnuri.helm.jni.NativeLibrary;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -29,6 +36,8 @@
*/
class NativeLibraryTest {

private static final String CACHE_DIR = "com.marcnuri.jkube-helm.cacheDir";

@Test
void getInstanceFromClassPath() {
System.setProperty("com.marcnuri.jkube-helm.skipRemoteJar", "true");
Expand All @@ -41,11 +50,15 @@ void getInstanceFromClassPath() {
}

@Test
void getSnapshotInstanceFromRemoteJar() {
void getSnapshotInstanceFromRemoteJar() throws IOException {
final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
// An empty cache directory of its own: always downloads, and leaves nothing in the user's cache. Not a @TempDir:
// the downloaded JAR stays open (its class loader is never closed), so on Windows it can't be deleted, and a
// @TempDir that can't be deleted fails the test
final Path cacheDir = Files.createTempDirectory("helm-java-test-cache");
final String previousCacheDir = System.setProperty(CACHE_DIR, cacheDir.toString());
// Use a PUBLISHED snapshot version
System.setProperty("com.marcnuri.jkube-helm.version", "0.0-SNAPSHOT");
System.setProperty("com.marcnuri.jkube-helm.forceUpdate", "true");
try {
Thread.currentThread().setContextClassLoader(new URLClassLoader(new java.net.URL[0], null));
assertThat(NativeLibrary.serviceProviderLibrary(null)).isNull();
Expand All @@ -54,16 +67,21 @@ void getSnapshotInstanceFromRemoteJar() {
} finally {
Thread.currentThread().setContextClassLoader(currentClassLoader);
System.clearProperty("com.marcnuri.jkube-helm.version");
System.clearProperty("com.marcnuri.jkube-helm.forceUpdate");
restoreCacheDir(previousCacheDir);
deleteQuietly(cacheDir);
}
}

@Test
void getReleaseInstanceFromRemoteJar() {
void getReleaseInstanceFromRemoteJar() throws IOException {
final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
// An empty cache directory of its own: always downloads, and leaves nothing in the user's cache. Not a @TempDir:
// the downloaded JAR stays open (its class loader is never closed), so on Windows it can't be deleted, and a
// @TempDir that can't be deleted fails the test
final Path cacheDir = Files.createTempDirectory("helm-java-test-cache");
final String previousCacheDir = System.setProperty(CACHE_DIR, cacheDir.toString());
// Use a PUBLISHED release version
System.setProperty("com.marcnuri.jkube-helm.version", "0.0.1");
System.setProperty("com.marcnuri.jkube-helm.forceUpdate", "true");
try {
Thread.currentThread().setContextClassLoader(new URLClassLoader(new java.net.URL[0], null));
assertThat(NativeLibrary.serviceProviderLibrary(null)).isNull();
Expand All @@ -72,7 +90,27 @@ void getReleaseInstanceFromRemoteJar() {
} finally {
Thread.currentThread().setContextClassLoader(currentClassLoader);
System.clearProperty("com.marcnuri.jkube-helm.version");
System.clearProperty("com.marcnuri.jkube-helm.forceUpdate");
restoreCacheDir(previousCacheDir);
deleteQuietly(cacheDir);
}
}

@Test
void getInstanceNotAvailableReportsRemoteJarFailureAsCause(@TempDir Path tempDir) {
final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
final String previousCacheDir = System.setProperty(CACHE_DIR, tempDir.toString());
// An UNPUBLISHED version: a 404 from Maven Central, or a connection error when offline
System.setProperty("com.marcnuri.jkube-helm.version", "0.0.0-unpublished");
try {
Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0], null));
assertThatThrownBy(NativeLibrary::getInstance)
.isInstanceOf(IllegalStateException.class)
.hasMessage("No NativeLibrary implementation found, please add one of the supported dependencies to your project")
.cause().isInstanceOf(IOException.class);
} finally {
Thread.currentThread().setContextClassLoader(currentClassLoader);
System.clearProperty("com.marcnuri.jkube-helm.version");
restoreCacheDir(previousCacheDir);
}
}

Expand All @@ -91,4 +129,23 @@ void getInstanceNotAvailable() {
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
}

private static void restoreCacheDir(String previousCacheDir) {
if (previousCacheDir == null) {
System.clearProperty(CACHE_DIR);
} else {
System.setProperty(CACHE_DIR, previousCacheDir);
}
}

/**
* Deletes what can be deleted (on Windows, not the JAR a class loader still holds open).
*/
private static void deleteQuietly(Path directory) {
try (Stream<Path> files = Files.walk(directory)) {
files.sorted(Comparator.reverseOrder()).forEach(file -> file.toFile().delete());
} catch (IOException exception) {
// Best effort
}
}
}
Loading
Loading