Skip to content

refactor(server): introduce CryptoService, replace MasterKey DI#119

Merged
martsokha merged 1 commit into
mainfrom
refactor/crypto-service
Jun 13, 2026
Merged

refactor(server): introduce CryptoService, replace MasterKey DI#119
martsokha merged 1 commit into
mainfrom
refactor/crypto-service

Conversation

@martsokha

Copy link
Copy Markdown
Member

Replaces the MasterKey DI service + free crypto functions with a single CryptoService that handlers use directly.

Before

State(master_key): State<MasterKey>,
// ...
let workspace_key = master_key.derive_workspace_key(workspace_id);
let encrypted = encrypt_json(&workspace_key, &data)
    .map_err(|e: CryptoError| ErrorKind::InternalServerError.with_message(...).with_context(...))?;

After

State(crypto): State<CryptoService>,
// ...
let encrypted = crypto.encrypt_json(workspace_id, &data)?;

Changes

  • CryptoService owns the master key (loaded from the key file, absorbing MasterKey's loading logic) and exposes workspace-scoped encrypt/decrypt/encrypt_json/decrypt_json (deriving the per-workspace key internally) plus master-scoped variants. Clone for DI.
  • The cipher functions and EncryptionKey become crate-internal; the public crypto surface is CryptoService + CryptoConfig (renamed from MasterKeyConfig) + CryptoError.
  • From<CryptoError> for the handler HTTP error so call sites use ? — removing the repeated map_err boilerplate.
  • DI rewired (crypto replaces master_key in ServiceState), cli CryptoArgs, server test helper, and the four connection/context call sites updated. service/security/master_key.rs deleted.

Verification

Full workspace green: cargo check, clippy -D warnings, cargo +nightly fmt --check, cargo doc -D warnings, cargo deny check all, tests (incl. 4 new CryptoService tests). Server boots end-to-end and loads the key through CryptoService.

🤖 Generated with Claude Code

Replace the MasterKey DI service + free crypto functions with a single
CryptoService that owns the master key and exposes workspace-scoped (and
master-scoped) encrypt/decrypt directly.

- CryptoService loads the key file (absorbing MasterKey's loading) and
  derives the per-workspace key internally; handlers call one method
  instead of derive-then-encrypt.
- The cipher functions and EncryptionKey become crate-internal; the public
  crypto surface is CryptoService + CryptoConfig (renamed from
  MasterKeyConfig) + CryptoError.
- Add From<CryptoError> for the handler HTTP error so call sites use `?`,
  removing the repeated map_err boilerplate.
- Rewire ServiceState DI (crypto replaces master_key), cli CryptoArgs, and
  the four connection/context call sites. Delete service/security/master_key.rs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@martsokha martsokha self-assigned this Jun 13, 2026
@martsokha martsokha added server API handlers, middleware, auth refactor code restructuring without behavior change labels Jun 13, 2026
@martsokha martsokha merged commit b138a65 into main Jun 13, 2026
5 checks passed
@martsokha martsokha deleted the refactor/crypto-service branch June 13, 2026 00:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor code restructuring without behavior change server API handlers, middleware, auth

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant