clockworklabs/SpacetimeDB · critical · anyhow::Error

Unable to read public key for JWT token verification

Error message

Unable to read public key for JWT token verification

What it means

On startup the node loads the EC key pair (PEM files at the configured public/private key paths) used to sign and verify JWTs. The two files are read independently; if the private key loads but the public key file cannot be read (missing, unreadable permissions, or unreadable content), startup aborts. Keys are auto-generated only when BOTH files are absent -- a half-present pair is treated as an error rather than silently replaced.

Solutions

  1. Restore the matching public key from backup (it must correspond to the existing private key)
  2. Otherwise regenerate the pair: remove BOTH key files and restart the node -- it generates and persists a fresh pair (note: previously issued tokens become invalid)
  3. Fix filesystem permissions so the node process can read both files
  4. Verify pair coherence: `openssl pkey -in private.pem -pubout` should equal the public key file

Example fix

# before: only the private key is present
$ ls /keys
jwt_private.pem

# after: remove the orphan and restart -- the node regenerates the pair
$ rm -f /keys/jwt_private.pem
$ systemctl restart spacetimedb
Defensive patterns

Strategy: validation

Validate before calling

#!/usr/bin/env bash
# run before starting the node
for f in "$JWT_PUBLIC_KEY" "$JWT_PRIVATE_KEY"; do
  [ -r "$f" ] || { echo "unreadable key: $f" >&2; exit 1; }
done
# optional: verify the pair matches
openssl pkey -in "$JWT_PRIVATE_KEY" -pubout 2>/dev/null | diff -q - "$JWT_PUBLIC_KEY" \
  || echo 'WARNING: public key does not match private key' >&2

Prevention

When it happens

Trigger: The private key file exists but the public key file was deleted, moved, truncated, or has restrictive permissions; a partial copy or restore of the key directory; public and private key paths configured to different directories where only one file landed.

Common situations: Manual cleanup removing one file; a Docker volume mounting only one key; key rotation writing the new private key but failing before the public key; running as a service user without read permission on the public key file.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20). Data as JSON: /api/errors/701955dd1c213a5f. Report an issue: GitHub.

Appendix: source

Thrown at crates/core/src/auth/mod.rs:65

// Get the key pair if the given files exist. If they don't, create them.
// If only one of the files exists, return an error.
pub fn get_or_create_keys(certs: &CertificateAuthority) -> anyhow::Result<JwtKeys> {
    let public_key_path = &certs.jwt_pub_key_path;
    let private_key_path = &certs.jwt_priv_key_path;

    let public_key_bytes = public_key_path.read().ok();
    let private_key_bytes = private_key_path.read().ok();

    // If both keys are unspecified, create them
    let key_pair = match (public_key_bytes, private_key_bytes) {
        (Some(pub_), Some(priv_)) => EcKeyPair::new(pub_, priv_),
        (None, None) => {
            let keys = EcKeyPair::generate()?;
            keys.write_to_files(public_key_path, private_key_path)?;
            keys
        }
        (None, Some(_)) => anyhow::bail!("Unable to read public key for JWT token verification"),
        (Some(_), None) => anyhow::bail!("Unable to read private key for JWT token signing"),
    };

    key_pair.try_into()
}

// An Ec key pair in pem format.
pub struct EcKeyPair {
    pub public_key_bytes: Vec<u8>,
    pub private_key_bytes: Vec<u8>,
}

impl TryFrom<EcKeyPair> for JwtKeys {
    type Error = anyhow::Error;
    fn try_from(pair: EcKeyPair) -> anyhow::Result<Self> {
        JwtKeys::new(pair.public_key_bytes, pair.private_key_bytes)
    }
}

View on GitHub (pinned to 6dee26c6ef)