mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-10-26 21:26:39 +00:00
* fix(security): add DEER_FLOW_CREDENTIALS_KEY for at-rest credential encryption
There was no at-rest encryption key. ChannelCredentialCipher had no
production caller: the channel service, the scheduled-task notification
outbox and the channel-connections router all built
ChannelConnectionRepository without a cipher, so store_credentials refused
(RuntimeError) and get_credentials always returned None.
- New harness module deerflow.config.credentials_key: env-only
DEER_FLOW_CREDENTIALS_KEY (comma-separated Fernet keys; first encrypts,
all decrypt via MultiFernet), CredentialsCipher with encrypt_text /
decrypt_text / rotate_text, a fernet:v2: prefix with legacy fernet:v1:
values still readable, and get_credentials_cipher() as the process-wide
entry point. Malformed keys are refused without echoing key material.
Without the variable a single instance generates
{base_dir}/.credentials_key. Never derived from AUTH_JWT_SECRET.
- New deerflow.config.secret_file.read_or_create_secret_file: O_EXCL
create (0600) plus read-back with a bounded settle window, shared by
.credentials_key, .jwt_secret (fixes two replicas cold-starting on a
shared volume keeping different JWT secrets) and the managed-model key.
- ChannelCredentialCipher now builds on CredentialsCipher (same
constructors); the Gateway loads the cipher off the event loop at
startup when channel_connections is enabled and passes it to every
ChannelConnectionRepository. Undecryptable values stay "missing + warn".
- Startup gate: a malformed key stops startup in any topology; a declared
multi-instance deployment with channel_connections enabled and no key
is refused. Plain multi-worker shares the generated file and is allowed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* feat(deploy): generate and inject DEER_FLOW_CREDENTIALS_KEY in Helm, compose and deploy.sh
- Helm: the app Secret generates a Fernet key (randBytes 32 translated to
the urlsafe alphabet) and preserves it across upgrades via lookup like
AUTH_JWT_SECRET; the gateway reads it through an optional secretKeyRef
so a user-managed existingAppSecret without it still boots (the Gateway
gate refuses only multi-instance deployments that store credentials).
README, NOTES and values document backup, rotation and existingAppSecret.
- scripts/deploy.sh resolves the key shell -> .env -> persisted file ->
generated, persisting it as $DEER_FLOW_HOME/.credentials_key (0600,
noclobber/O_EXCL with read-back): the same file the Gateway generates in
its runtime home, so either side's key is the one both keep.
- Both compose files pass DEER_FLOW_CREDENTIALS_KEY=${...:-} to the gateway.
- .env.example entry with a generation hint; README and CHANGELOG notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(security): make abandoned secret-file replacement single-winner
A rename is last-writer-wins, so replicas that restart together and all
find the same abandoned empty secret file could each replace it and keep
a different value. The process that exclusively creates
<name>.replacing now owns the replacement, re-checks the file is still
empty, and publishes by renaming the claim over it; peers wait for that
value, bounded so a crashed replacer cannot block startup forever.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs(changelog): link DEER_FLOW_CREDENTIALS_KEY entries to #6611
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(security): name the setting that turns off a credentials-key consumer
The multi-instance refusal ended with "or disable
channel_connections.enabled=true"; it now says "or set
channel_connections.enabled=false".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(security): never expose a secret file before its value is written
O_EXCL published the name before its content, so a creator that stalled
mid-write left an empty file a peer replaced after the settle window; the
resumed creator then returned a value nobody persisted, or deleted the
peer's valid file when its own write failed. Creators now write and sync
a private temporary file and hard-link it to the name, which fails if
the name exists; a failed creator only removes its own temporary file.
Filesystems without hard links publish through the same single-winner
<name>.replacing claim that replaces abandoned empty files, so creation
and replacement are serialized there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(models): recover an empty managed-model key when no catalog exists
An interrupted first save could leave an empty key file; every later save
then passed empty bytes to Fernet and the endpoint returned 503 until the
file was removed by hand. An empty key with no catalog encrypted nothing,
so it now goes through the secret-file recovery path; next to an existing
catalog it is still refused.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(docker): pass the dotenv DEER_FLOW_CREDENTIALS_KEY through make docker-start
scripts/docker.sh runs Compose from docker/ without --env-file, so the dev
gateway's DEER_FLOW_CREDENTIALS_KEY=${DEER_FLOW_CREDENTIALS_KEY:-} entry
interpolated to empty and overrode the key the checkout .env supplied via
env_file. Export it from the dotenv like AUTH_TRUSTED_PROXIES.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
255 lines
10 KiB
Python
255 lines
10 KiB
Python
"""At-rest credentials key ``DEER_FLOW_CREDENTIALS_KEY`` and the cipher built from it.
|
|
|
|
The key is env-only: one or more comma-separated urlsafe-base64 Fernet keys,
|
|
the first encrypting and every one decrypting, so an operator rotates by
|
|
prepending a new key. Without it a single instance generates
|
|
``{base_dir}/.credentials_key`` once. It is never derived from
|
|
``AUTH_JWT_SECRET``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from cryptography.fernet import Fernet, InvalidToken
|
|
|
|
from deerflow.config import credentials_key as ck
|
|
from deerflow.config.credentials_key import (
|
|
CIPHERTEXT_PREFIX,
|
|
CREDENTIALS_KEY_ENV_VAR,
|
|
CREDENTIALS_KEY_FILENAME,
|
|
GENERATE_KEY_COMMAND,
|
|
CredentialsCipher,
|
|
CredentialsDecryptError,
|
|
CredentialsKeyError,
|
|
get_credentials_cipher,
|
|
load_credentials_cipher,
|
|
parse_credentials_keys,
|
|
reset_credentials_cipher,
|
|
)
|
|
from deerflow.persistence.channel_connections import ChannelCredentialCipher
|
|
|
|
KEY_OLD = Fernet.generate_key().decode("ascii")
|
|
KEY_NEW = Fernet.generate_key().decode("ascii")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _isolated_key_env(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.delenv(CREDENTIALS_KEY_ENV_VAR, raising=False)
|
|
reset_credentials_cipher()
|
|
yield
|
|
reset_credentials_cipher()
|
|
|
|
|
|
def _std_base64_key() -> str:
|
|
"""A 32-byte key spelled with the standard alphabet (``+``/``/``), as Helm's randBytes emits it."""
|
|
raw = bytes([0xFB, 0xFF] * 16)
|
|
value = base64.b64encode(raw).decode("ascii")
|
|
assert "+" in value or "/" in value
|
|
return value
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Parsing
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_parse_accepts_a_single_key() -> None:
|
|
assert parse_credentials_keys(KEY_NEW) == [KEY_NEW]
|
|
|
|
|
|
def test_parse_keeps_rotation_order_and_ignores_blank_entries() -> None:
|
|
assert parse_credentials_keys(f" {KEY_NEW} , {KEY_OLD} ,") == [KEY_NEW, KEY_OLD]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"raw",
|
|
[
|
|
"",
|
|
" , ",
|
|
"not-a-fernet-key",
|
|
KEY_NEW[:-1],
|
|
KEY_NEW + "A",
|
|
_std_base64_key(),
|
|
"é" * 44,
|
|
f"{KEY_NEW},short-second-key",
|
|
],
|
|
ids=["empty", "only-separators", "garbage", "truncated", "too-long", "standard-alphabet", "non-ascii", "second-entry-bad"],
|
|
)
|
|
def test_parse_rejects_malformed_keys_without_echoing_them(raw: str) -> None:
|
|
with pytest.raises(CredentialsKeyError) as exc_info:
|
|
parse_credentials_keys(raw)
|
|
|
|
message = str(exc_info.value)
|
|
assert CREDENTIALS_KEY_ENV_VAR in message
|
|
assert GENERATE_KEY_COMMAND in message, "the refusal must say how to generate a valid key"
|
|
for entry in filter(None, (part.strip() for part in raw.split(","))):
|
|
assert entry not in message, "key material must never reach logs or refusal text"
|
|
assert exc_info.value.__cause__ is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Cipher
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_cipher_writes_a_versioned_prefix_and_round_trips() -> None:
|
|
cipher = CredentialsCipher.from_keys([KEY_NEW])
|
|
|
|
token = cipher.encrypt_text("xoxb-secret")
|
|
|
|
assert token.startswith(CIPHERTEXT_PREFIX)
|
|
assert CIPHERTEXT_PREFIX == "fernet:v2:"
|
|
assert "xoxb-secret" not in token
|
|
assert cipher.decrypt_text(token) == "xoxb-secret"
|
|
|
|
|
|
def test_the_first_key_encrypts_and_every_key_decrypts() -> None:
|
|
old_token = CredentialsCipher.from_keys([KEY_OLD]).encrypt_text("written-before-rotation")
|
|
rotated = CredentialsCipher.from_keys([KEY_NEW, KEY_OLD])
|
|
|
|
assert rotated.decrypt_text(old_token) == "written-before-rotation"
|
|
|
|
new_token = rotated.encrypt_text("written-after-rotation")
|
|
assert CredentialsCipher.from_keys([KEY_NEW]).decrypt_text(new_token) == "written-after-rotation"
|
|
with pytest.raises(CredentialsDecryptError):
|
|
CredentialsCipher.from_keys([KEY_OLD]).decrypt_text(new_token)
|
|
|
|
|
|
def test_rotate_re_encrypts_under_the_primary_key() -> None:
|
|
old_token = CredentialsCipher.from_keys([KEY_OLD]).encrypt_text("long-lived-token")
|
|
rotated = CredentialsCipher.from_keys([KEY_NEW, KEY_OLD])
|
|
|
|
re_encrypted = rotated.rotate_text(old_token)
|
|
|
|
assert re_encrypted.startswith(CIPHERTEXT_PREFIX)
|
|
assert CredentialsCipher.from_keys([KEY_NEW]).decrypt_text(re_encrypted) == "long-lived-token"
|
|
|
|
|
|
def test_an_undecryptable_value_raises_an_invalid_token_subclass() -> None:
|
|
token = CredentialsCipher.from_keys([KEY_OLD]).encrypt_text("secret")
|
|
cipher = CredentialsCipher.from_keys([KEY_NEW])
|
|
|
|
with pytest.raises(CredentialsDecryptError) as exc_info:
|
|
cipher.decrypt_text(token)
|
|
assert isinstance(exc_info.value, InvalidToken), "existing `except InvalidToken` call sites keep working"
|
|
|
|
for garbage in ("fernet:v2:not-a-token", "fernet:v2:é", "plain text"):
|
|
with pytest.raises(CredentialsDecryptError):
|
|
cipher.decrypt_text(garbage)
|
|
|
|
|
|
def test_legacy_v1_values_written_by_the_passphrase_cipher_stay_readable() -> None:
|
|
"""Exactly what ``ChannelCredentialCipher.from_key`` wrote before the v2 format."""
|
|
derived = Fernet(base64.urlsafe_b64encode(hashlib.sha256(b"legacy-passphrase").digest()))
|
|
legacy_value = "fernet:v1:" + derived.encrypt(b"xoxb-legacy").decode("ascii")
|
|
|
|
cipher = ChannelCredentialCipher.from_key("legacy-passphrase")
|
|
|
|
assert cipher.decrypt_text(legacy_value) == "xoxb-legacy"
|
|
assert cipher.decrypt_text(legacy_value.removeprefix("fernet:v1:")) == "xoxb-legacy", "the old reader accepted a bare token"
|
|
assert cipher.encrypt_text("new-value").startswith(CIPHERTEXT_PREFIX)
|
|
assert isinstance(cipher, CredentialsCipher)
|
|
|
|
|
|
def test_v1_values_under_a_raw_key_are_readable_by_the_deployment_cipher() -> None:
|
|
"""``ChannelCredentialCipher(Fernet(key))`` wrote ``fernet:v1:`` under a raw key; the same key in the env reads it."""
|
|
legacy_value = "fernet:v1:" + Fernet(KEY_OLD).encrypt(b"value").decode("ascii")
|
|
|
|
assert ChannelCredentialCipher(Fernet(KEY_OLD)).decrypt_text(legacy_value) == "value"
|
|
assert CredentialsCipher.from_keys([KEY_NEW, KEY_OLD]).decrypt_text(legacy_value) == "value"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Loading
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_the_environment_key_list_wins_and_no_file_is_written(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
monkeypatch.setenv(CREDENTIALS_KEY_ENV_VAR, f"{KEY_NEW},{KEY_OLD}")
|
|
old_token = CredentialsCipher.from_keys([KEY_OLD]).encrypt_text("old")
|
|
|
|
cipher = load_credentials_cipher(base_dir=tmp_path)
|
|
|
|
assert cipher.decrypt_text(old_token) == "old"
|
|
assert CredentialsCipher.from_keys([KEY_NEW]).decrypt_text(cipher.encrypt_text("new")) == "new"
|
|
assert not (tmp_path / CREDENTIALS_KEY_FILENAME).exists()
|
|
|
|
|
|
def test_a_single_instance_generates_the_key_file_once(tmp_path: Path) -> None:
|
|
first = load_credentials_cipher(base_dir=tmp_path)
|
|
key_file = tmp_path / CREDENTIALS_KEY_FILENAME
|
|
|
|
assert key_file.exists()
|
|
persisted = key_file.read_text(encoding="utf-8").strip()
|
|
assert parse_credentials_keys(persisted) == [persisted]
|
|
if os.name == "posix":
|
|
assert key_file.stat().st_mode & 0o777 == 0o600
|
|
|
|
token = first.encrypt_text("survives a restart")
|
|
assert load_credentials_cipher(base_dir=tmp_path).decrypt_text(token) == "survives a restart"
|
|
assert key_file.read_text(encoding="utf-8").strip() == persisted
|
|
|
|
|
|
def test_a_blank_environment_value_means_unset(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
"""Compose renders ``${DEER_FLOW_CREDENTIALS_KEY:-}`` as an empty string."""
|
|
monkeypatch.setenv(CREDENTIALS_KEY_ENV_VAR, " ")
|
|
|
|
load_credentials_cipher(base_dir=tmp_path)
|
|
|
|
assert (tmp_path / CREDENTIALS_KEY_FILENAME).exists()
|
|
|
|
|
|
def test_a_malformed_environment_key_is_refused_before_touching_disk(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
monkeypatch.setenv(CREDENTIALS_KEY_ENV_VAR, "definitely-not-a-key")
|
|
|
|
with pytest.raises(CredentialsKeyError):
|
|
load_credentials_cipher(base_dir=tmp_path)
|
|
|
|
assert not (tmp_path / CREDENTIALS_KEY_FILENAME).exists()
|
|
|
|
|
|
def test_a_corrupt_key_file_is_refused_and_left_alone(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
"""The file may be the only copy of the key that encrypted stored credentials."""
|
|
monkeypatch.setattr(ck, "_KEY_FILE_SETTLE_SECONDS", 0.05)
|
|
key_file = tmp_path / CREDENTIALS_KEY_FILENAME
|
|
key_file.write_text("corrupted-key-material", encoding="utf-8")
|
|
|
|
with pytest.raises(CredentialsKeyError) as exc_info:
|
|
load_credentials_cipher(base_dir=tmp_path)
|
|
|
|
assert str(key_file) in str(exc_info.value)
|
|
assert "corrupted-key-material" not in str(exc_info.value)
|
|
assert key_file.read_text(encoding="utf-8") == "corrupted-key-material"
|
|
|
|
|
|
def test_the_key_is_never_derived_from_the_jwt_secret(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
monkeypatch.setenv("AUTH_JWT_SECRET", KEY_OLD)
|
|
jwt_derived_token = CredentialsCipher.from_keys([KEY_OLD]).encrypt_text("secret")
|
|
|
|
cipher = load_credentials_cipher(base_dir=tmp_path)
|
|
|
|
with pytest.raises(CredentialsDecryptError):
|
|
cipher.decrypt_text(jwt_derived_token)
|
|
assert (tmp_path / CREDENTIALS_KEY_FILENAME).read_text(encoding="utf-8").strip() != KEY_OLD
|
|
|
|
|
|
def test_the_process_cipher_is_loaded_once_until_reset(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
from deerflow.config.paths import Paths
|
|
|
|
monkeypatch.setattr("deerflow.config.paths.get_paths", lambda: Paths(base_dir=tmp_path))
|
|
monkeypatch.setenv(CREDENTIALS_KEY_ENV_VAR, KEY_OLD)
|
|
|
|
first = get_credentials_cipher()
|
|
monkeypatch.setenv(CREDENTIALS_KEY_ENV_VAR, KEY_NEW)
|
|
assert get_credentials_cipher() is first
|
|
|
|
reset_credentials_cipher()
|
|
reloaded = get_credentials_cipher()
|
|
assert reloaded is not first
|
|
assert CredentialsCipher.from_keys([KEY_NEW]).decrypt_text(reloaded.encrypt_text("x")) == "x"
|