Skip to main content
Video tutorial · 2:39

Java keystore module

Deploys the certificate into a Java keystore — a JKS, PKCS12 or JCEKS file that Tomcat, Jetty, WildFly, Kafka, Elasticsearch, Spring Boot and most other Java servers read. The module replaces one alias and leaves every other entry of the keystore exactly as it was: other keys (also those with their own key password), trusted certificates and secret keys.

It works with the host's own keytool, so the keystore stays a file the host's Java wrote, in the format and protection that Java expects. The work is done on a copy. The copy is checked, and only then is the keystore replaced.

Every behaviour on this page was measured with Eclipse Temurin 8u512, 11, 17, 21 and 25, on Ubuntu 24.04 with OpenJDK 17 over SSH, and on Windows Server 2022 with Temurin 21 over WinRM.

At a glance​

  • Type and credential: type java_keystore, with a platform:
    • Linux — reached over SSH with the target's SSH connection and host-key pin; credential ssh_private_key or ssh_password.
    • Windows — reached over WinRM with the target's WinRM connection; credential winrm_password.
  • Passwords come from project secret variables (Settings → Variables). The target stores only their names.
  • Per deploy:
    1. Read the keystore: copy it into the work directory and list it with keytool.
    2. Decide: the alias is new, ours, or refused (see Which alias).
    3. Import the certificate into the copy, list the copy again, and open the new key with its password.
    4. Check the copy: every other entry must be exactly as before.
    5. Replace the keystore with the copy, keeping its owner and permissions (Linux) or its ACL (Windows).
    6. Activate: restart a service or run a command, when configured.
  • Rollback: supported; the previous version is imported into the same alias the same way.

What it does not do​

  • Keep the existing key. CertAutoPilot issues every certificate with its own key, so the alias gets a new key on every renewal.
  • Hot reload. Most Java servers read their keystore only at start. Configure a service restart or a command; without one the application reads the new certificate the next time it starts (JAVA_KEYSTORE_NOT_ACTIVATED).
  • Rewrite certificates keytool would lose. A PKCS12 written by another tool (for example OpenSSL with -certfile) can hold certificates that belong to no key and are not marked trusted for Java. keytool does not list them and drops them when it rewrites the file (measured on Java 8, 11 and 25). Such a keystore is refused and nothing is written (JAVA_KEYSTORE_UNSUPPORTED).
  • Replace trusted certificates or secret keys. An alias that holds one is never replaced (JAVA_KEYSTORE_ENTRY_TYPE).
  • Live pickers. The target is typed by hand; a dry run shows what the deploy would do.

Requirements on the host​

  • keytool of the Java the application runs on. When the target names none:
    • Linux: $JAVA_HOME/bin/keytool, keytool on PATH, the keytool beside the java on PATH, then /usr/lib/jvm, /usr/java, /opt.
    • Windows: JAVA_HOME, keytool.exe on PATH, the Java runtimes in the registry (JavaSoft), then C:\Program Files\ (Eclipse Adoptium, Java, Microsoft, Amazon Corretto, Zulu).
    • The health check reports which keytool and Java version were found.
    • Only a keytool the host's administrators own is run. keytool gets the keystore's passwords and the new private key, and Java loads libraries and settings from its home. On Linux the program must belong to root or to the account the steps run as, with no write permission for anyone else, in directories no other account can change, and the same goes for the lib directory of its Java home. On Windows the program, its folder, the Java home and its lib and conf folders must belong to Administrators, SYSTEM, TrustedInstaller or the WinRM account and let no other account add or change a file (a folder made directly under C:\ lets Users create files in it — measured; C:\Program Files does not), and no folder above may be movable by another account. A keytool that fails the rule is skipped by the search; when none passes, or the one the target names fails, nothing is run (JAVA_KEYSTORE_PERMISSION, naming the file or folder and the account).
    • The program must answer -J-version like a Java tool before it is handed anything (JAVA_KEYSTORE_KEYTOOL_NOT_FOUND otherwise): a wrong path on the target never receives the keystore or the passwords.
  • Linux: sh, od, stat, readlink, flock and sha256sum (or openssl); timeout bounds each keytool call where it is installed. The SSH account's login shell must be a Bourne-style shell (sh, bash, dash, ksh, zsh, busybox ash; fish works too): csh and tcsh cannot take the multi-line commands CertAutoPilot sends (measured), so an account with one is refused when it connects (SSH_VALIDATION, naming the shell). With the sudo privilege, the SSH user needs NOPASSWD sudo (sudo -n); this is root on the host. A sudo -n that is refused — a password is required, or the account may not sudo — fails the target with JAVA_KEYSTORE_PRIVILEGE (not retried). The keystore's folder, and every folder above it, must not be changeable by another account (owned by root, the account the deploy runs as or the keystore's owner; not writable by its group or by others — above the keystore's own folder, a folder with the sticky bit such as /tmp is fine); otherwise nothing is written (JAVA_KEYSTORE_PERMISSION). The work directory is made private (mode 0700, owned by the account the deploy runs as); one another account owns or can write is refused.
  • Windows: PowerShell 5.1 or later. The WinRM account must be able to read and replace the keystore and to create the work directory. Refused before anything is created (JAVA_KEYSTORE_PERMISSION): a folder on the work directory's path, up to the drive's root, owned by an account other than Administrators, SYSTEM, TrustedInstaller or the WinRM account; one whose ACL lets another account delete, rename, re-permission or take it (or delete what it holds) — such an account could move the work tree away between two steps; a link (junction or symbolic link) anywhere on the path; a java_keystore folder whose ACL lets another account change it. Letting users create folders is fine — C:\ and C:\ProgramData do (measured on Windows Server 2022); the work directory holds the keystore copies.

Passwords​

FieldHolds
Keystore password variablethe name of a secret project variable with the keystore password
Key password variableJKS / JCEKS: a secret project variable with the password of the entry's key. Empty = the keystore password. When the alias already holds a key, that key must open with this password before it is replaced: a key with a password of its own (Tomcat's keyPass) is refused (JAVA_KEYSTORE_PASSWORD, nothing written) until the variable names it — the application opens the new key with the same password.

Rules (checked before anything connects, JAVA_KEYSTORE_PASSWORD_UNSUPPORTED):

  • At least 6 characters. keytool refuses shorter store and key passwords, even for an existing keystore.
  • Printable ASCII only. Java versions and other tools encode other characters differently. Java 8 and 17 cannot open an OpenSSL keystore with the password pässwört1 that Java 25 opens, and the session's locale changes the result. A key protected with a differently decoded password would fail when the application next starts.
  • PKCS12 has one password. Java protects every key of a PKCS12 keystore with the keystore password, so a key password variable is refused for PKCS12. The target form empties the field when the keystore type is set to PKCS12 and does not send it.

A password variable that does not exist or is not marked secret, or a key password variable on a keystore that turns out to be PKCS12, fails the target with JAVA_KEYSTORE_VALIDATION before anything is written.

Passwords reach the host only on the standard input of the step and are handed to keytool through its environment (:env, measured on Java 8, 11, 17, 21 and 25 with ASCII passwords). They are never written to a file, never appear on a command line or in a script, and (on Windows) never in the PowerShell script-block log. The activation command does not get them. A step killed in the middle therefore leaves no password behind; its directory holds at most copies of the keystore (protected by the keystore password, like the keystore itself) and a one-entry PKCS12 whose random password existed only in that step, and the next step removes it once its process is gone and no process left behind by it (a keytool) still names the directory; a directory whose step is still running is kept however old.

Target fields​

FieldMeaning
Platformlinux (SSH) or windows (WinRM). Changing it on a saved target clears a credential of the other kind in the form (an SSH credential does not fit Windows, a winrm_password does not fit Linux); pick one that fits before saving. The credential is checked again at every run, because a target without its own credential uses its group's default, and a group can hold Linux and Windows keystores: a credential of the other kind is refused before anything is sent to the host (JAVA_KEYSTORE_VALIDATION). Give such a target its own credential, or keep the two platforms in separate groups.
Keystore fileabsolute path; Windows: on a drive (C:\…), not a UNC path. A Linux symlink is followed and the file it points to is replaced; a Windows link is refused.
Aliasthe entry the target writes. Empty = cap-<id>-<domain> per certificate. Stored in lower case (Java stores aliases so).
Keystore password variablesee Passwords
Key password variablesee Passwords
Keystore typeempty = read from the file's first bytes; set to make sure it is that type. Required when the keystore is created.
After the updatenothing, restart a service, or run a command (see Activation)
Privilege (Linux)run as the SSH user, or every step under sudo -n sh
keytoolthe keytool to use (see Requirements)
Work directorywhere the working copy is made; an ASCII path. Empty = /var/lib/certautopilot (sudo), ~/.certautopilot (no sudo), C:\ProgramData\CertAutoPilot (Windows).
Create the keystore if missingoff (default): a missing file is refused (JAVA_KEYSTORE_NOT_FOUND). On: a new keystore of the chosen type is created and the deploy warns JAVA_KEYSTORE_CREATED — on Linux owned by the connecting account with mode 0600, on Windows with its folder's permissions. Give the Java application's account read access; later deploys keep the owner and mode you set.
Take over an existing aliasoff (default): an alias holding a key no CertAutoPilot certificate wrote is refused. On: it is replaced.

On Linux, the keytool and the work directory are plain absolute paths: ASCII letters, digits and . _ - + = @ % only. The keystore path itself may hold spaces and other letters.

A per-distribution override can give a certificate another alias and another keystore file on the same host (target, group and Default rows). Setting or changing the keystore file in an override needs the Admin role; for an operator whose change does not go through approval, the drawer says so on that input and the save is refused (403).

Which alias​

The alias…Result
does not existit is added
holds this certificate's leafnothing is written (also after an answer that was lost)
was written by this certificate on this targetit is replaced
holds an earlier version of this certificate, without a record (a deploy whose answer was lost)it is replaced; the job log says so
was written by another CertAutoPilot certificaterefused (JAVA_KEYSTORE_CONFLICT)
holds a key no CertAutoPilot certificate wroterefused (JAVA_KEYSTORE_CONFLICT), or replaced with Take over an existing alias (JAVA_KEYSTORE_ADOPTED)
is a trusted certificate or a secret keyalways refused (JAVA_KEYSTORE_ENTRY_TYPE)

Aliases are compared the way Java stores them: in lower case (JKS, PKCS12 and JCEKS alike), so Tomcat and tomcat are the same entry. keytool prints aliases in UTF-8, so an alias such as ş1 reads as itself — under a plain C locale two such aliases both printed as ?1 (measured). The other entries are compared as a whole set (alias as printed, type, certificates), so two aliases printed alike cannot be mixed up. -list -v shows no secret-key bytes: a secret key is compared by its alias and type.

When the target is pointed at another machine (another host name or IP, or port), this certificate's own record no longer counts there: the alias is judged as one CertAutoPilot did not write, and the application is activated again after a deploy. Another certificate's record still claims its alias, whatever the address — it deploys through the same target.

How a deploy works​

  1. Read. One script copies the keystore into a fresh directory under the work directory, checks that the copy has the same SHA-256, reads the file's type from its first bytes and runs keytool -list -v on the copy. A missing file (JAVA_KEYSTORE_NOT_FOUND, unless the target creates it), another type than the target says (JAVA_KEYSTORE_TYPE_MISMATCH), a wrong password (JAVA_KEYSTORE_PASSWORD) or a file that is not a keystore keytool can read (JAVA_KEYSTORE_CORRUPT) stops the deploy with nothing written.
  2. Plan. The alias decision above, the certificate check (key type, expiry, key matches), and the PKCS12 check for certificates keytool would drop: keytool reports how many certificate bags the file holds, and that must equal what the entries use. A PKCS12 for which keytool reports no count (another PKCS12 provider configured in java.security) is refused (JAVA_KEYSTORE_UNSUPPORTED): whether a rewrite would drop certificates cannot be told.
  3. Import into a copy. A second script copies the keystore again (it must still have the hash the first one read), imports a one-entry PKCS12 with the new certificate into the copy (keytool -importkeystore, the target's alias, the keystore's own type, -destkeypass always given), lists the copy, and opens the new key with its password. The copy comes back to CertAutoPilot.
  4. Check. Every entry other than the alias must have the same type and the same certificate fingerprints as before, nothing may be added or dropped, and the alias must hold the new certificate. Any difference stops the deploy; the keystore was not touched (JAVA_KEYSTORE_VERIFY_FAILED).
  5. Replace.
    • Linux: the work directory must have room first (512 KB plus twice the keystore's size; JAVA_KEYSTORE_NO_SPACE otherwise, nothing staged). The checked copy is staged beside the keystore as <keystore>.captmp.<run id> with its owner, mode, SELinux label and ACL, the keystore must still have the hash read in step 1, it is backed up, and the copy is renamed over it. One deploy per host runs at a time (JAVA_KEYSTORE_IN_PROGRESS).
    • Windows: the copy is written beside the keystore as <keystore>.captmp.<run> (a new file only — never one already there; with the keystore's access rules; its owner is the WinRM account) and swapped in with [IO.File]::Replace, which keeps the keystore's own ACL (measured: Move-Item loses explicit entries) and leaves the previous keystore as <keystore>.capbak.<run>, removed as soon as the swap is done — before the activation, so while the service restarts nothing lies beside the keystore: a dry run or a health check reads it as usual, and a second deploy answers JAVA_KEYSTORE_IN_PROGRESS. A process holding the keystore open without allowing it to be replaced blocks the swap; after three tries two seconds apart the deploy fails with nothing changed (JAVA_KEYSTORE_LOCKED, retried). While copies an earlier run left beside the keystore are there (.capbak…, .captmp… — after a failed recovery one of them may be the only good copy), every run refuses (JAVA_KEYSTORE_RECOVERY_FAILED, naming them) until you have kept the right one and removed the others: nothing is overwritten or deleted.
    • Only this step takes the host lock: reading the keystore and preparing the copy do not, so a health check or dry run does not collide with a deploy; the hash check of step 5 catches a keystore that changed in between.
  6. Activate, when configured.

When keytool itself fails. A keytool call that does not finish within 300 seconds, or whose Java runtime cannot get memory, fails the target with JAVA_KEYSTORE_KEYTOOL_UNAVAILABLE (retried). A keytool failure CertAutoPilot does not recognise is JAVA_KEYSTORE_KEYTOOL_FAILED; the message quotes keytool's line (not retried). Both happen on the working copy: the keystore is not written.

A dry run does steps 1 and 2 — it has no private key, so it does not try the import. It lists the activation exactly when a deploy would run it: for a keystore already holding the certificate, only when no activation is recorded for this version with the current activation setting.

Older PKCS12 protection. A newer keytool writes a new key with AES even into a keystore an older Java wrote with older algorithms, and a Java 8 older than 8u301 cannot read such a key. When the keystore's integrity check or certificate encryption is SHA-1 / RC2 / 3DES, the new entry is written with the older algorithms too (JAVA_KEYSTORE_LEGACY_PROTECTION).

Windows paths. keytool cannot open a path holding characters outside the system's code page (measured: Bad pathname for ş on an English-language server). The work is always done on a copy in the work directory, so a keystore in such a folder works; the work directory itself must be an ASCII path.

Activation​

ChoiceWhat happens
Nothing (default)The keystore is updated; the deploy warns JAVA_KEYSTORE_NOT_ACTIVATED.
Restart a serviceLinux: systemctl restart <unit>, then waits until the unit is active. Windows: stops and starts the service and waits until it runs; services depending on it that were running are started again after it.
Run a commandLinux: sh -c as the SSH user (under sudo with the sudo privilege). Windows: PowerShell as the WinRM account. The command gets no standard input and no password, and a process it leaves running (a daemon it starts) does not keep the deploy waiting. Linux: the command's output goes to <work dir>/java_keystore/activation-<id>.log (one per keystore, appended to, emptied at a run's start once over 1 MiB), and the deploy reports what the run added — a process the command leaves running keeps writing there unless the command sends its output elsewhere. The log's name is checked before the keystore is replaced: when something other than a regular file is there, the deploy fails with nothing changed (JAVA_KEYSTORE_PERMISSION). On Windows, processes the command starts end with the WinRM session: start a long-running application as a service. A distribution whose targets run a command needs the Admin role to start by hand, like an Action Set.

The wait is bounded (default 60 s, 5–600). A failed restart or command keeps the new keystore — the file is valid, and a service that does not start may have another reason — and fails the target (JAVA_KEYSTORE_ACTIVATION_FAILED, retried). The next run — the automatic re-run of a failed or partial run, or a run by hand — finds the alias current and runs only the activation. Auto-rollback does not answer this failure: it would write the previous version and run the same activation that just failed.

A recorded activation covers the certificate version and the activation setting: after changing the mode, the service or the command, the next run activates again (without writing). When a run stops after the swap and before the activation, the next run finds the alias current and runs only the activation — after checking that the keystore is still the one it read; one changed since is not activated (JAVA_KEYSTORE_CHANGED_DURING_DEPLOY, retried).

Runs over many servers​

  • Hosts at once. One run works on up to five hosts at a time (the module configuration's concurrency, 1–50); the targets of one host run one after another. The SSH Max Concurrency setting (Settings → General) is a ceiling for hosts reached over SSH and Parallel Windows Hosts for Windows hosts — a run over both kinds takes the lower one; neither raises the module's own value. With Parallel Execution off a run takes one host at a time.
  • A time budget per target. A target that stops answering is given up after 4 minutes, plus three activation waits when it restarts a service or runs a command (7 minutes with the default 60 s). It fails with JAVA_KEYSTORE_SCRIPT (Linux) or the WinRM timeout code (Windows), and the other hosts go on. The step on the host finishes on its own; the next run reads the keystore.
  • A partial run is run again. When some targets succeeded and others did not, the distribution sweep runs the distribution again for the targets that failed, were not reached, or failed their post-deploy check — up to three times per certificate version, one scheduler cycle apart (hourly by default). The targets that hold the version are not contacted, so their services are not restarted again; the distribution's result always describes every target. A run started by hand covers every target. After the third attempt the distribution stays partial until it is run by hand. This is the rule of a distribution that runs in one job; one large enough to fan out retries inside its batches instead (transient failures and targets not reached, three waves), and its final partial is not run again.
  • Auto-rollback. A Linux host the run could not connect to was sent nothing: with auto-rollback on it does not roll the other hosts back. Refusals made before the keystore is replaced do not either — keytool's own failures included, which are read on the working copy. A failed activation is finished by the next run, not rolled back (Activation). A lost answer or a failed restore still rolls back (Rollback).
  • Fan-out. Batches are sized from the targets' time budgets, and a target a batch did not reach before its time ended is retried together with the transient failures (Fan-out).

Rollback​

The previous retained version is imported into the same alias with the same checks and activation.

Identity and state​

Per certificate the target records the keystore, its real path, the alias, the keystore type, the leaf it holds and what the activation last ran for (the leaf and the activation setting). A keystore that already holds the leaf is not written again. The record is saved as each target finishes, so a run that is interrupted keeps the records of the targets it finished (their services are not restarted a second time).

Health check​

Connects (and runs sudo -n when chosen), finds keytool and its Java version, and reads the keystore's type from the file. A health check has no project variables, so it does not open the keystore. Reports a missing file (unless it is created at the first deploy) and a type that differs from the target's.

Limits​

  • Keystores up to 16 MiB.
  • One alias per certificate per keystore; a second keystore on the same host is a second target or a per-distribution override.
  • Not measured: IBM Java (IBMJCE keystores), BCFKS and other provider keystores, a keystore on a network share, SELinux-enforcing hosts. On Windows, finding keytool through JAVA_HOME, PATH or the registry and the service restart were not tried on a real host (the lab named keytool and activated with a command).

Error codes​

See Error codes → Java keystore. Transport failures use the SSH codes (Linux) or the WinRM codes (Windows).

Video tutorial

Short, hands-on videos showing this feature working for real. English audio, English and Turkish subtitles. All video tutorials →

Update One Alias of a Java Keystore (JKS / PKCS12)YouTube ↗