Documentation
¶
Overview ¶
Package json implements functions to load the Public key data from an EJSON file, and to walk that data file, encrypting or decrypting any keys which, according to the specification, are marked as encryptable (see README.md for details).
It may be non-obvious why this is implemented using a scanner and not by loading the structure, manipulating it, then dumping it. Since Go's maps are explicitly randomized, that would cause the entire structure to be randomized each time the file was written, rendering diffs over time essentially useless.
Index ¶
- Constants
- Variables
- func CheckRotationFields(data []byte, migratingToV2 bool) error
- func CollapseMultilineStringLiterals(data []byte) ([]byte, error)
- func DeleteSessions(data []byte) ([]byte, error)
- func DeleteTopLevelField(data []byte, key string) ([]byte, error)
- func DeleteTopLevelFieldsByPrefix(data []byte, prefix string, keep func(name string) bool) ([]byte, error)
- func ExtractCryptoPublicKey(data []byte) (ejsoncrypto.PublicKey, error)
- func ExtractPublicKey(data []byte) (key [32]byte, err error)
- func ExtractSessions(data []byte) ([]ejsoncrypto.Session, error)
- func InsertSession(data []byte, session ejsoncrypto.Session) ([]byte, error)
- func ReplacePublicKey(data []byte, value string) ([]byte, error)
- type SkippedSessionRef
- type Walker
Constants ¶
const ( // PublicKeyField is the key name at which the public key should be // stored in an EJSON document. PublicKeyField = "_public_key" )
Variables ¶
var ErrPublicKeyDuplicate = errors.New("public key appears more than once in EJSON file")
ErrPublicKeyDuplicate means the PublicKeyField key appears more than once at the top level, so the document's recipient is ambiguous.
var ErrPublicKeyInvalid = errors.New("public key has invalid format")
ErrPublicKeyInvalid means that the PublicKeyField key was found, but the value could not be parsed into a valid key.
var ErrPublicKeyMissing = errors.New("public key not present in EJSON file")
ErrPublicKeyMissing indicates that the PublicKeyField key was not found at the top level of the JSON document provided.
var ErrSessionDuplicate = errors.New("v2 session field appears more than once in EJSON file")
ErrSessionDuplicate means a top-level _ejson_v2_session_<sid> field appears more than once at the top level.
var ErrSessionFieldCollision = errors.New("field name is reserved for v2 session metadata")
ErrSessionFieldCollision means a v1 document uses the top-level _ejson_v2_session_ prefix, which v1 never reserved, for its own data.
var ErrSessionInvalid = errors.New("v2 session field has invalid format")
ErrSessionInvalid means a top-level _ejson_v2_session_<sid> field was malformed.
Functions ¶
func CheckRotationFields ¶ added in v1.7.0
CheckRotationFields rejects ambiguous recipients and v1 fields reserved by v2.
func CollapseMultilineStringLiterals ¶ added in v1.4.0
It's common to want to paste multiline secrets into an EJSON file, and JSON doesn't handle multiline literals, so we cheat here. Our first pass over the file is to replace embedded newlines in string literals with escaped newlines.
func DeleteSessions ¶ added in v1.7.0
DeleteSessions removes only verified v2 session records.
func DeleteTopLevelField ¶ added in v1.7.0
DeleteTopLevelField removes a top-level key and its value from a JSON object without reformatting unrelated keys. No-op if key is absent.
func DeleteTopLevelFieldsByPrefix ¶ added in v1.7.0
func DeleteTopLevelFieldsByPrefix(data []byte, prefix string, keep func(name string) bool) ([]byte, error)
DeleteTopLevelFieldsByPrefix removes every top-level field whose name starts with prefix and does not satisfy keep(name).
func ExtractCryptoPublicKey ¶ added in v1.7.0
func ExtractCryptoPublicKey(data []byte) (ejsoncrypto.PublicKey, error)
ExtractCryptoPublicKey finds the _public_key value in an EJSON document and parses it into a schema-aware public key (v1 legacy or v2 hybrid).
Pure v1 documents retain legacy last-wins behavior for duplicate _public_key fields; documents involving a v2 public key fail closed with ErrPublicKeyDuplicate when _public_key appears more than once (comparing decoded JSON key names).
func ExtractPublicKey ¶
ExtractPublicKey finds the _public_key value in an EJSON document and parses it into a legacy v1 key usable with the crypto library.
func ExtractSessions ¶ added in v1.7.0
func ExtractSessions(data []byte) ([]ejsoncrypto.Session, error)
ExtractSessions parses every top-level v2 session field in document order. Both readers and writers require every _ejson_v2_session_<sid> field to be valid and unique (comparing decoded JSON key names), so a later valid record cannot mask an earlier malformed or duplicate one.
func InsertSession ¶ added in v1.7.0
func InsertSession(data []byte, session ejsoncrypto.Session) ([]byte, error)
InsertSession adds a new session after the last session field, or after _public_key.
Types ¶
type SkippedSessionRef ¶ added in v1.7.0
SkippedSessionRef records a string value skipped by Walker (under an underscore-prefixed key) that holds a structurally valid v2 ciphertext.
func SkippedHybridSessionRefs ¶ added in v1.7.0
func SkippedHybridSessionRefs(data []byte) ([]SkippedSessionRef, error)
SkippedHybridSessionRefs returns every string value skipped by Walker that holds a structurally valid v2 EJ[2:<sid>:<n>:<ct>] ciphertext, in document order. It mirrors Walker's raw-underscore scoping rules so every v2 value in a document is accounted for by either Walker or this scan.
type Walker ¶
Walker takes an Action, which will run on fields selected by EJSON for encryption, and provides a Walk method, which iterates on all the fields in a JSON text, running the Action on all selected fields. Fields are selected if they are a Value (not a Key) of type string, and their referencing Key did *not* begin with an Underscore. Note that this underscore-to-disable-encryption syntax does not propagate down the hierarchy to children. That is:
- In {"_a": "b"}, Action will not be run at all.
- In {"a": "b"}, Action will be run with "b", and the return value will replace "b".
- In {"k": {"a": ["b"]}, Action will run on "b".
- In {"_k": {"a": ["b"]}, Action run on "b".
- In {"k": {"_a": ["b"]}, Action will not run.
func (*Walker) Walk ¶
Walk walks an entire JSON structure, running the ejsonWalker.Action on each actionable node. A node is actionable if it's a string *value*, and its referencing key doesn't begin with an underscore. For each actionable node, the contents are replaced with the result of Action. Everything else is unchanged.