Skip to content

[Server] Encrypt stored credentials with an operator key - #2083

Closed
shba24 wants to merge 5 commits into
unitycatalog:mainfrom
shba24:stack/ep31-credential-encryption
Closed

shba24 wants to merge 5 commits into
unitycatalog:mainfrom
shba24:stack/ep31-credential-encryption

Conversation

@shba24

@shba24 shba24 commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

🥞 Stacked PR

Use this link to review incremental changes.

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

This is part 5 of 5 in a stack; each PR handles one kind of secret end to end (server, Helm chart, docs, tests). Review only this PR's changes with its incremental link above.

Description of changes

Storage credentials are now encrypted in the database when the operator sets a key. Before, they were stored as plaintext JSON (// TODO: encrypt the credential).

  • Set SERVER_CREDENTIAL_ENCRYPTION_KEY (or server.credential-encryption-key), or point server.credential-encryption-key-file at a file holding the key. The key is 32 random bytes in base64, for example openssl rand -base64 32.
  • The credential column is encrypted with AES-256-GCM. Setting both settings, or a value that is not a 32-byte base64 key, stops startup.
  • Credentials stored before the key was set stay readable, and are encrypted the next time they are updated.
  • Without a key, nothing changes: credentials are stored as before, and the server logs a warning at startup.
  • The Helm chart can pass the key from a Secret: server.credentialEncryptionKeySecret.name (and .key).
📐 Design notes & details

Format. The encrypted value is stored in the same column as enc:v1: followed by the base64 of a 12-byte random IV and the ciphertext with its 128-bit tag. No schema change or migration is needed, and values without the prefix are read as plaintext. The v1 leaves room for key rotation later.

Decision log.

  • Encrypt with an operator key, warn and store plaintext without one: user decision.
  • Accept both an inline key and a key file: user decision.
  • Both set is an error: my call (delegated). A silent pick could write rows under the wrong key, which mixes keys in the table or makes rows unreadable.
  • Wiring (proposed, please confirm). The converter is a JPA AttributeConverter on CredentialDAO.credential. Hibernate creates @Convert converters itself, so HibernateConfigurator hands it the configured instance through a Hibernate BeanContainer; every other bean is created as before. The BeanContainer is a Hibernate SPI, so a Hibernate upgrade should re-check it (the version is pinned to 6.5.0).

Known limits.

  • Wrong or missing key. If a key is lost or changed, encrypted credentials cannot be read. Reads return a 500 whose message includes Hibernate's "Error attempting to apply AttributeConverter"; the server log names the cause ("A stored credential is encrypted, but no credential encryption key is set" or "the credential encryption key may be wrong").
  • Ciphertext isn't tied to its row. No associated data binds a value to its row id, which a converter cannot see. Someone with write access to the database could copy one row's ciphertext into another.
  • No bulk re-encryption. Old plaintext rows are encrypted only when updated.
  • What's in the column today. The only credential type today, an AWS IAM role, stores a role ARN and an external ID. This PR encrypts the column for that and for the credential types that will hold real secrets.

Tests.

  • HibernateConfiguratorTest.encryptsStoredCredentialsWithTheConfiguredKey:
    • the raw column starts with enc:v1: and does not contain the role ARN;
    • entities read back;
    • a row with plaintext JSON in the column stays readable.
  • New CredentialCipherTest:
    • the key can be inline or in a file;
    • both settings set, a short key, and invalid base64 are rejected;
    • another key, or no key, fails to read an encrypted value.
  • Checked by hand:
    • Local server (H2): the stored value is enc:v1:…. A restart without the key fails reads with the message above; with the key again they work. Both settings set stops startup.
    • kind, PostgreSQL 18, chart value set: the large-object column holds enc:v1:…, and credential get returns the role.

This pull request and its description were written by Isaac.

shba24 and others added 5 commits October 29, 2026 07:26
Operators can now set the OAuth client secret, the AWS secret key and
session token, and the per-bucket S3 and ADLS secrets in environment
variables a shell can export, such as SERVER_CLIENT_SECRET or
S3_SECRETKEY_0. The name is the key in upper case with `_` for each
other character. Only secret keys get the upper-case name, because
cloud runtimes already set variables such as AWS_REGION.

Secrets in server.properties keep working, but the server now logs a
deprecation warning for each one it reads from the file, and the docs
mark that way as deprecated.

The Helm chart passes these secrets to the server container as
environment variables instead of rendering them into server.properties.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
The database password could only come from hibernate.properties. The
server now also takes it from the HIBERNATE_CONNECTION_PASSWORD
environment variable (or the hibernate.connection.password system
property), which wins over the file, the same order as server.properties.

A password kept in hibernate.properties still works, but the server logs
a deprecation warning, and the docs mark that way as deprecated.

The Helm chart passes the password to the server container as an
environment variable instead of rendering it into hibernate.properties.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
The server signed its tokens with a key it generated into etc/conf with
default file permissions, and the Helm chart copied its key Secret into
etc/conf (converting PEM to DER) because the location was fixed.

The new server.token-signing-key-dir setting names a directory holding
private_key.der or private_key.pem (PKCS unitycatalog#8, or PKCS unitycatalog#1 as the chart
and OpenSSL 1.1 `openssl genrsa` write it) and key_id.txt. The server
only reads it, and derives the public key from the private key, so a
read-only Secret mount works as is.

Without the setting the server keeps its current behavior, but now
creates a newly generated private key readable by its owner only and
logs a warning at each start.

The Helm chart points the server at its key Secret mount and no longer
copies the key into etc/conf.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
The server wrote an admin token that never expires to etc/conf/token.txt
on every start. The server never reads that file; it only handed the
token to people, and anyone who can read it can already read the
signing key next to it.

The server now keeps the token in memory and deletes a token.txt found
in the configuration folder, so no plaintext copy stays on disk.
Deleting the file does not revoke copies of that token; changing the
signing key does.

The new bin/uc-admin-token prints an admin token signed with the
server's key. It only reads the key (from server.token-signing-key-dir
or etc/conf), fails if there is none, and writes no files. The docs use
`bin/uc --auth_token $(bin/uc-admin-token) ...` in place of
`$(cat etc/conf/token.txt)`.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Storage credentials were saved to the database as plaintext JSON (with
a "TODO: encrypt the credential"). The server now encrypts the
credential column with AES-256-GCM when an operator key is set, in
server.credential-encryption-key (SERVER_CREDENTIAL_ENCRYPTION_KEY) or
in a file named by server.credential-encryption-key-file. Setting both,
or a key that is not 32 bytes in base64, stops startup.

An encrypted value is "enc:v1:" plus the base64 IV and ciphertext, in
the same column, so there is no schema change. Rows written before the
key was set stay readable and are encrypted when next updated. Without
a key the server keeps storing plaintext and logs a warning at startup.

The converter gets its key through a Hibernate bean container, since
Hibernate creates @convert converters itself. The Helm chart can pass
the key from a Secret (server.credentialEncryptionKeySecret).

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
@shba24

shba24 commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

Local AI review run

The CI /review workflow is skipped for this account, so the repository's reviewer roles were run locally once, on the stack before its final fixes (Claude models; the Codex role was not run):

  • uc-domain-reviewer and maintainer-claude-reviewer (one pass)
  • architecture-reviewer, docs-reviewer and test-coverage-reviewer (one pass)
  • a silent-failure pass

Each finding was checked against the code before acting on it.

Finding Outcome
A line break at the end of a Secret value broke env-sourced secrets (the old envsubst path dropped it) Fixed in #2079: dropped for the upper-case names
bin/uc-admin-token could generate a new key when run from another directory, and rewrote certs.json, which the server re-reads for every internal token check Fixed in #2082: reads the key only, fails if missing, writes no files, runs from the install directory
A stale public_key.der in the key directory could mismatch the signing key Fixed in #2081: the public key is always derived from the private key
token.txt deletion described as making the old token invalid Fixed in #2082: the comment, log and docs say copies stay valid until the key changes
Misleading warning for hibernate.connection.password in server.properties Fixed in #2080
Encryption key: untrimmed inline value, invalid base64 reported as wrong length, key file path missing from errors, name-based bean lookup Fixed in #2083
Weak test assertion on the in-memory token Fixed in #2082: the token is verified against the key
No AAD binding a ciphertext to its row; no bulk re-encryption; generic 500 when a key is missing Kept, documented in #2083
Public HibernateConfigurator(Properties) stores plaintext Kept: callers are tests only
PEM key path has no unit test Kept: the secret scanner blocks PEM headers in test sources; covered end to end with an openssl genrsa key and the chart's generated key on kind

Reviewed heads: 2f4bb5a, 690745f, 8b8356b, c0bc199, d7bf525. Current heads: 81e48b4, eba9f92, 99ae5a0, b91905f, b19e7a5.

@shba24 shba24 closed this Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant