Skip to main content
The palyra-vault crate provides a secure, encrypted storage system for sensitive credentials (API keys, tokens, etc.) used by the Palyra daemon and CLI. It utilizes Envelope Encryption where individual secrets are encrypted with unique data keys, which are in turn protected by a Device Key Encryption Key (KEK) derived from the system’s identity.

Architecture Overview

The Vault is designed to be “zero-trust” regarding the underlying storage medium. Whether secrets are stored in a flat file or a system keychain, the payload is always encrypted before it leaves the Vault boundary.

Data Flow & Encryption Logic

  1. Key Derivation: Upon initialization, the Vault locates the palyra-identity store. It extracts the CA private key material crates/palyra-vault/src/crypto.rs#50-78 and uses HKDF-SHA256 with a static salt (palyra.vault.kek.v1) to derive a 32-byte Device KEK crates/palyra-vault/src/crypto.rs#80-87.
  2. Secret Insertion: When put_secret is called, a random nonce is generated. The secret is encrypted using AES-GCM (via ring) with Additional Authenticated Data (AAD) that binds the secret to its specific VaultScope and key crates/palyra-vault/src/crypto.rs#89-91.
  3. Storage: The encrypted blob is passed to a BlobBackend.

Entity Mapping: Natural Language to Code

System ConceptCode EntityFile Path
Vault InstanceVault structcrates/palyra-vault/src/api.rs
Encryption Logicderive_device_kek / sealcrates/palyra-vault/src/crypto.rs
Storage Interfacetrait BlobBackendcrates/palyra-vault/src/backend.rs#88-93
Scope Enforcementenum VaultScopecrates/palyra-vault/src/scope.rs
Metadata TrackingMetadataFilecrates/palyra-vault/src/metadata.rs
Sources: crates/palyra-vault/src/lib.rs#1-20, crates/palyra-vault/src/crypto.rs#13-33

Vault Scopes

Secrets are partitioned into scopes to prevent naming collisions and enforce logical separation between global configuration and workspace-specific credentials.
ScopeStorage StringDescription
GlobalglobalSystem-wide secrets (e.g., primary OpenAI key).
Workspacews:{id}Secrets tied to a specific project or workspace.
Principalpr:{id}User-specific secrets for multi-tenant environments.
Sources: crates/palyra-vault/src/scope.rs#1-16, crates/palyra-vault/src/crypto.rs#115-122

Storage Backends

The Vault supports multiple backends via the BlobBackend trait. The selection is determined by BackendPreference (Auto vs. EncryptedFile) and the presence of a backend.kind marker file in the vault root crates/palyra-vault/src/backend.rs#95-133.

Backend Implementation Flow

Supported Backends

Sources: crates/palyra-vault/src/backend.rs#39-80, crates/palyra-vault/src/backend.rs#135-158

CLI Secret Commands

The palyra CLI provides a management interface for the vault, allowing users to set, get, and audit secrets.

Command Execution Logic

Key CLI Operations

Sources: crates/palyra-cli/src/commands/secrets.rs#61-146, crates/palyra-cli/tests/secrets_cli.rs#57-94

Security Hardening

Memory Safety

The SensitiveBytes wrapper is used for secret material in memory. It implements Drop to zero-fill the underlying buffer when the object goes out of scope, minimizing the window for memory forensics crates/palyra-vault/src/crypto.rs#156-174.

Filesystem Permissions

The Vault enforces strict ownership checks. Before opening a vault root or writing objects, it ensures that the directory is restricted to the current user (mode 0700 on Unix) crates/palyra-vault/src/backend.rs#99-100.

Concurrency & Locking

To prevent corruption during simultaneous CLI and Daemon access, the Vault uses a file-based locking mechanism for metadata updates (.metadata.lock). It includes logic to reclaim “stale” locks if a process crashed while holding them crates/palyra-vault/src/metadata.rs#7-9. Sources: crates/palyra-vault/src/crypto.rs#156-174, crates/palyra-vault/src/filesystem.rs#1-20, crates/palyra-vault/src/metadata.rs#34-37