Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Interfaces

Summary: the traits where the parts of secrets meet: the server’s ports, the hooks into auth-chad, the client’s transport, and the CLI’s adapters.
Key terms: port, Ports, Store, ProjectStore, Random, AuditBuffer, Rules, Hooks, Http, KeyDirectory, Gh, Opener.
Related: Map · Data · AGENTS.md

Every side effect in secrets is a trait that the caller injects, used through generic parameters. On the server, the use cases in App<P: Ports> name four ports: a store, a clock, a random source and an audit buffer. The stores and the test fakes implement them. auth-chad’s account API calls back into this application through its Hooks trait, which Rules implements. Clients reach the server through auth-chad’s Http trait, so the same client code runs in the CLI and in the browser.

The picture as text
flowchart LR
    subgraph http [secrets-server-http]
        R[routes]
        H[Rules: auth-chad Hooks]
    end
    subgraph app [secrets-server-app]
        A["App, generic over Ports"]
    end
    S[(Store: SQLite, PostgreSQL, MemStore)]
    C[Clock + Random]
    B[AuditBuffer]
    R --> A
    H --> A
    A --> S
    A --> C
    A --> B

Server ports

TraitMethodsImplemented by
PortsNames the four types: Store, Clock, Random, Auditsecrets-server’s wiring; MemPorts in tests
StoreHealth + KeyStore + ProjectStore + ProfileStore + AuditStore + auth-chad’s ReaderBlanket: any type with all six
HealthpingBoth stores, MemStore
KeyStorekeys by fingerprint, put_keysBoth stores, MemStore
ProjectStoreLog entries, wraps, records, commit(Batch), projects_for, rotation_requiredBoth stores, MemStore
ProfileStoreprofile, device_envelope, prune_expiredBoth stores, MemStore
AuditStoreappend_audit, audit (one page), prune_auditBoth stores, MemStore
Randomfill(buf)The OS random source; SeqRandom in tests
AuditBufferpush, take, restore: read events wait in memory and are stored once per secondMemoryAuditBuffer

A new storage backend implements these traits and runs secrets_store_tests::conformance! in its tests.

use secrets_core::id::{Id, Project};
use secrets_server_app::{ProjectStore, StoreError};

async fn log_length<S: ProjectStore>(store: &S, project: &Id<Project>) -> Result<usize, StoreError> {
    Ok(store.log_entries(project).await?.len())
}

A test assembles the ports from fakes:

use std::sync::atomic::{AtomicU8, Ordering};

use secrets_core::ports::FixedClock;
use secrets_server_app::{MemoryAuditBuffer, Ports, Random};
use secrets_store_tests::MemStore;

struct Counting(AtomicU8);

impl Random for Counting {
    fn fill(&self, buf: &mut [u8]) {
        buf.fill(self.0.fetch_add(1, Ordering::Relaxed)); // tests only
    }
}

struct TestPorts;

impl Ports for TestPorts {
    type Store = MemStore;
    type Clock = FixedClock;
    type Random = Counting;
    type Audit = MemoryAuditBuffer;
}

secrets_store_tests::fixtures::mem_app() builds a whole App this way.

Hooks into auth-chad

Rules<P> in crates/server-http/src/auth.rs implements auth-chad’s Hooks. auth-chad decides the account step; Rules adds this application’s checks and rows to the same transaction.

HookWhat secrets does there
commit_signupReads the identity public keys from the signup’s app field and stores the profile with auth-chad’s writes
valid_namesHandle ^[a-z0-9_-]{3,32}$ and a display name
invite_data, may_inviteOnly users with the admin flag invite; an invite from the API never makes an admin
may_request_device, device_kindChecks the scope grammar and the device keys; the scope decides user or machine
may_approveA machine request for a project is visible only to its owners
commit_approveA user device: the sealed identity key. A machine: the signed device_grant entries and wraps. One transaction
commit_revoke, commit_undoDeletes envelopes and marks the user’s environments rotation_required

The full contract of each hook is on auth-chad’s interfaces page.

Other server traits

TraitPurpose
FailTurns an application failure into the {"error": code} answer of a route
LogSinkReceives the optional request log, one line per request; NoLog discards it

Shared traits in secrets-core

TraitPurpose
ClockThe current time, injected so tests are exact; FixedClock for tests
KeyDirectoryPublic keys by fingerprint, so log validation can verify entries of past members; implemented for [PublicKeys]
IdKindThe type prefix of an ID (usr_, prj_, env_, dev_, sec_, inv_)

Client traits

secrets-client re-exports auth-chad’s client traits, so one transport serves the account API and this application’s routes.

TraitYou giveIn the browser
Httpsend(request) -> responseauth-chad’s browser::Fetch
Clock, DrawsTime and random bytesThe system

The CLI’s adapters.rs implements them with ureq and the OS. Tests run the real router in memory through the same traits.

CLI adapters

TraitPurposeReal implementation
GhRuns the GitHub CLI with input on stdin, for machine create --githubSystemGh
OpenerOpens an address in the person’s browserSystemBrowser

Where the details are

  • The ports and the Batch type: crates/server-app/src/ports.rs.
  • The hooks: crates/server-http/src/auth.rs.
  • The client API: crates/client/src/lib.rs, and auth-chad’s client recipe.