Replacing a dead library with Sluice's own keyring code

Sluice can send your photos to a paid AI model to judge, and for that it needs your API key for that provider. A key like that shouldn’t sit in a plain settings file. So Sluice keeps it in the credential store your operating system already has: Credential Manager on Windows, the Keychain on macOS, and on Linux the desktop’s keyring, reached through a standard called the Secret Service. Where none of those works, it falls back to a file only your account can read. An environment variable, if you set one, overrides both.

This entry is about calling those three stores directly, without a library, and the surprises that only turned up on real machines. The AI agent that writes Sluice’s code did the binding, and I approved each commit.

The library whose CI never ran its own tests

The plan had a library picked out, one that covers all three systems. It had since been archived, so nobody maintains it any more.

Its own CI had never run the tests that store a secret and read it back. Those are integration tests, which Maven runs in a later phase. The CI ran mvn -B package on a single Linux machine, and that stops before integration tests start.

The second candidate wasn’t archived. But it had no commit in years and had never been tested past Java 17. Using an unmaintained library for the one part that holds a user’s API key means fixing its bugs inside Sluice the first time a new Java version breaks it.

Solution in 15 minutes

The third route was calling each system’s own API directly from Java, through java.lang.foreign. That is Java’s standard foreign-function API, final since Java 22, and it needs no JNI glue code in C. The plan had priced that as “three small bindings, and the Windows CREDENTIAL struct layout is the fiddly one.”

Instead of guessing how hard that would be, the agent wrote a throwaway spike. It was one Java file binding four Windows functions: write a credential, read one, delete one, and free the memory Windows hands back. It stored a secret, read it back, compared the two, deleted it, then checked that a second read found nothing.

It worked at the first attempt. The struct came out at its documented 80 bytes, and a secret with non-ASCII characters came back byte for byte. The part the plan had called fiddly took about fifteen minutes.

So Sluice now calls all three stores directly. The new estimate was about 1,800 lines, up from the 750 in the plan. Each system’s binding was tested on a machine running that system.

When Sluice looks for a key, it checks three places in order. First an environment variable, if you set one. Then the operating system’s credential store. Then the file only your account can read, which is where a key goes when the credential store doesn’t work.

Windows: the test that skipped itself

The file fallback refuses to write a key into a file it can’t restrict to you. An earlier review agent had flagged files owned by a group rather than a person, and offered two fixes: refuse them, or grant access to the account the app runs as. The agent took the refusal, and wrote down that it “only fires for elevated/Administrators-token processes. Acceptable.” I approved that commit.

CI went red on GitHub’s Windows machine, while the Linux machine and my own Windows desktop passed. GitHub’s Windows runners create files owned by BUILTIN\Administrators, which is a group. So the refusal fired on every write, and the fallback stored nothing at all.

The note called the refusal “Acceptable” because only elevated processes would hit it. Neither the agent nor I checked which machines actually run that way. Every Windows machine GitHub hands out does, and so does any admin account set up that way by default. So the fix took the other option. Sluice now grants access to the account running the app, and refuses only when no such account can be found.

The test that should have caught it skipped itself on GitHub’s machine, the only place the bug showed up. Its guard read:

assumeFalse(ownerOfAFileIn(secrets) instanceof GroupPrincipal,
        "this process creates files owned by a group, which no single rule can restrict");

In JUnit, a failed assumption aborts the test instead of failing it, and Maven counts an aborted test as skipped. The guard was there so the permission check wouldn’t fail on an elevated process. So the test skipped itself on exactly the setup that broke the code.

The same test class on two Windows machines. On my machine, files are owned by my account, writes work and the test runs. On GitHub's machine, files are owned by the Administrators group, every write is refused and the test skips itself. The skip count was 1 on mine and 2 on GitHub's. One test class, two Windows machines My machine Files owned by my account Writes work. The test runs. GitHub's machine Files owned by the Administrators group Every write refused. The test skips. Skipped: 1 on mine, 2 on GitHub's. The extra skip was the test meant to catch the bug.
The guard was written to spare elevated processes, and GitHub's Windows machine runs its jobs as one.

The skip count found it before any log did. That test class reported Skipped: 2 on the runner and 1 on my machine. The one skip on mine was a test of Unix-style permissions, which every Windows machine skips. The extra skip on the runner was this guard firing. The agent had nearly blamed a change made an hour earlier, which turned out to be innocent. The guard is gone now, and the test runs on both machines.

Windows had one smaller surprise of the same kind. Reading a missing credential and deleting one both fail with error 1168, “not found”. A delete that reports failure looks like an error. But Sluice clears every place a key could be on every removal, so deleting something that isn’t there is the normal case. Treating 1168 as an error would have made every removal throw.

macOS: GitHub had a Mac all along

Two planning documents said the macOS version couldn’t be tested, and would ship untested unless I found a Mac. While brainstorming that with the agent, I asked whether a Mac virtual machine would do. The real answer was that no plan had checked whether GitHub hands out macOS machines for CI. It does, on Apple Silicon.

“Untested” traced back to one line in the very first plan, “build + test on ubuntu + windows matrix”, with no reason written beside it. Every later document inherited it as a constraint. A fair reason was available, since on a private repository GitHub bills every macOS minute as ten. It was never written down, or decided.

The agent projected five to six minutes a run. The real job took 95 seconds. It runs on demand and once a week, rather than on every push.

When the Keychain binding landed, its run showed 8 tests and 0 skipped on the Mac. That number said more than the green tick did. If all eight had skipped, the tick would have looked exactly the same.

macOS also turned out to do one thing better than Linux, which comes next.

Linux: an empty answer, and a six-minute hang

On Linux, keys live in collections that the desktop may keep locked. Opening a locked one can pop up a password dialog asking you to unlock it. When unlocking fails, libsecret, the library Sluice calls, returns no value and no error. That is exactly what it returns when there is no key at all.

Without a fix, two parts of Sluice would have disagreed. The settings screen only checks that an entry exists, without unlocking anything, so it would have said a key was saved. But reading the key would get the empty answer, take it as “no key”, and look in the file instead, where there is nothing. You’d see a saved key that Sluice could never use, and no error anywhere. That’s why, when the keyring answers with nothing, Sluice now asks a second question: is there an entry under that name at all? If there is, and it won’t open, Sluice fails loudly and names the keyring.

macOS needs no second question. A Keychain that won’t open says so in its status code, so “locked” and “absent” arrive as different answers.

Asking two systems for a key. On Linux, a missing key and a locked keyring both answer with nothing, the same answer. On macOS, a missing key answers not found and a locked keychain answers with an error, two different answers. Linux needs a second question: is there an entry at all? Linux macOS No key: nothing Locked: nothing Same answer No key: not found Locked: an error Different answers Linux needs a second question: is there an entry at all?
libsecret gives up on a failed unlock without an error, so Sluice checks whether the entry exists before treating an empty answer as "no key".

Linux had a second surprise, in a test that needed the keyring to say no. To make it say no, the test named a collection by an alias no keyring defines:

sluice-fixture-no-such-collection

The assumption was that an unknown alias gives a clean error. Instead the Linux CI job hung for six minutes and was cancelled by hand. Every local run had been green, because the test only runs on Linux.

A fix that named the collection by an object path, with underscores, worked. The explanation for why it worked did not hold up. The first assumption was that libsecret turns an unknown alias into nothing, which trips a check. A review agent traced libsecret’s source and showed that an alias becomes a path, not nothing. The assumption was dropped, and the cause written down as unknown.

But the cause wasn’t unknown. Re-reading the log before sign-off answered it. The line had been there from the start:

assertion 'collection_path != NULL && g_variant_is_object_path (collection_path)' failed

Both assumptions had only looked at the first half. The second half checks that the path is valid. Each part of a D-Bus object path, between the slashes, may hold only letters, digits and underscores. The alias had hyphens. The path built from it wasn’t NULL, but it wasn’t valid either, so the check failed on its second half. libsecret then gives up on the call without completing it, and the code waiting for the answer waits forever.

The failing check had two halves. The first, collection_path is not NULL, passed. The second, g_variant_is_object_path, failed. The alias had hyphens, and each part of a path may hold only letters, digits and underscores. Both assumptions had read only the first half. One check, two halves collection_path != NULL passed g_variant_is_object_path(...) failed The alias had hyphens. Each part of a path holds only letters, digits and underscores. Both assumptions had read only the first half.
The log named the failing half from the first run.

So the fix had worked partly by accident. One edit had changed the alias to a path and the hyphens to underscores together, and only the underscores mattered.

Three changes now guard against it happening again. Sluice refuses a collection name holding anything but letters, digits and underscores, before it loads libsecret at all. The Linux keyring tests run under a 30-second timeout on a separate thread. A native call can’t be interrupted, and a timeout on the same thread only fires after the stuck call has returned. And the build jobs stop after 20 minutes, rather than GitHub’s default of six hours.

Read what the machine said

In each case, the machine had already shown the answer before anyone explained it. GitHub’s Windows runner said who owned its files. The skip count said which guard had fired. The Mac’s run said 0 skipped. The Linux log named the half of the check that failed.

Every explanation that turned out wrong was written before anyone read those.