Expand description
ledvar-core — the reference implementation of the Ledvar protocol core:
the data model, the canonical content-addressed hashing, and well-formedness.
The crate is deliberately domain-blind (it knows nothing about security,
cloud, or any domain) and dependency-light (serde for the wire model,
sha2 for hashing — nothing else). Hashing streams the canonical bytes
straight into SHA-256 with no intermediate allocation; see the design note in
docs/canonical-hashing.md.
use ledvar_core::Node;
use std::collections::{BTreeMap, BTreeSet};
let mut content = BTreeMap::new();
content.insert("grants".to_string(), BTreeSet::from(["SELECT".into(), "INSERT".into()]));
content.insert("risk".to_string(), BTreeSet::from(["Medium".into()]));
let node = Node {
path: vec!["mydb".into(), "user:app".into()],
content,
labels: BTreeMap::new(),
refs: Vec::new(),
};
assert_eq!(node.identity_key().unwrap(), "8e303e0e8139ff7d377e0eaf9097e2790071f9914bc7cf8b28020bb98a3cca95");
assert_eq!(node.content_hash().unwrap(), "296190489d577d410f0f7fa09dfa24660722227fe405daaf621f4e2666bf370f");Structs§
- Node
- A node of the tree: identity (
path) + content (attributes). The collector fills it; this crate computes the hashes (SPEC §4.2, §5). - Ref
- A directed annotation edge from one node to another (SPEC §4.3).
- Snapshot
- One observation of some state at a point in time (SPEC §4.1).
Enums§
- Error
- Why a snapshot is not well-formed, or a version cannot be parsed.
Constants§
- SUPPORTED_
PROTOCOL_ MAJOR - Protocol MAJOR this implementation understands. Snapshots with a different
MAJOR are rejected by
crate::validate. - SUPPORTED_
PROTOCOL_ MINOR - Protocol MINOR this implementation targets. While MAJOR is 0 the exact MINOR is
contract-significant — a MINOR bump within 0.x may move the canonical form (SPEC §10) — so
crate::validaterejects a snapshot whose MINOR differs while MAJOR is 0.
Functions§
- canonical_
json_ string - Canonical JSON encoding of ONE string value (SPEC §6.1), quotes included: minimal
RFC 8259 escaping, lowercase
\uXXXX, no Unicode normalization — the exact same routine the protocol hashes run through. Exposed so higher layers (e.g. an ecosystem convention that canonicalises the non-hashedlabels/refsfields) can produce byte-identical encodings without re-implementing the escaping rules. - parse
- Parse a
MAJOR.MINOR.PATCHstring into its three numeric components. - parse_
major - Parse just the MAJOR (only MAJOR is contract-significant from MAJOR ≥ 1). Thin wrapper over
parse; the full grammar (including MINOR/PATCH and no-leading-zeros) is still enforced. - validate
- Validate that a snapshot is well-formed (SPEC §9): supported version, every node well-formed
(see
validate_node), and all paths unique within the snapshot. - validate_
node - Well-formedness of a single node (SPEC §9), independent of any snapshot: a non-empty
pathwith no empty segment, no empty attribute name, and no empty value set.indexis used only for error reporting (pass0for a bare node). Called byvalidateper node, and byNode::identity_key/Node::content_hashthemselves, so an ill-formed node is never hashed no matter how the caller reaches the hash (SPEC §9: an implementation MUST NOT hash an ill-formed Snapshot).