store

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package store holds Emberwire's persistence: the context stores, the flow file, and credentials.

Index

Constants

View Source
const DefaultBackupGenerations = 3

DefaultBackupGenerations is how many previous flow files are retained.

Variables

View Source
var ErrBadSecret = errors.New("could not decrypt credentials: wrong secret or the file has been tampered with")

ErrBadSecret is returned when decryption fails authentication, which means either the wrong secret or a tampered file. The two are deliberately not distinguished — telling an attacker which one they got is a free oracle.

View Source
var ErrNoSecret = errors.New("credentials are encrypted but no credential secret was provided")

ErrNoSecret is returned when an encrypted credential file exists but no secret was supplied to decrypt it.

View Source
var ErrRevisionConflict = errors.New("flow revision conflict: the flows were modified by someone else")

ErrRevisionConflict is returned when a save is attempted against a stale revision, meaning somebody else deployed in between.

Node-RED calls this a deploy conflict and carries the same "rev" token on /flows. Without it, two editor tabs open on the same runtime silently overwrite each other's work.

Functions

func BackupNames

func BackupNames(path string, keep int) []string

BackupNames returns the backup paths for a file, newest first.

func RotateBackups

func RotateBackups(path string, keep int) error

RotateBackups shifts path.bak.N up by one and copies path to path.bak.1, keeping at most keep generations.

Backups exist because atomic writes protect against a torn file, not against a bad one: a deploy that saves a valid but wrong flow set is just as much a loss, and the operator needs somewhere to roll back to.

func SecretsEqual

func SecretsEqual(a, b string) bool

SecretsEqual compares two secrets in constant time. Used when rotating.

func WriteFileAtomic

func WriteFileAtomic(path string, data []byte, perm os.FileMode) error

WriteFileAtomic replaces a file's contents such that a reader — or a crash — never observes a partial write.

The sequence is: write a sibling temp file, fsync it, rename it over the target, then fsync the containing directory. Every step matters:

  • Writing to a temp file means the target keeps its previous contents until the new ones are complete on disk.
  • fsync on the temp file forces the data out before the rename is durable, otherwise a crash can leave a renamed file full of zeroes. Ordering the two is the whole point; POSIX does not do it for you.
  • Rename is atomic within a filesystem, so the target is always either the old file or the new one.
  • fsync on the directory makes the rename itself durable. Without it, a power loss can lose the directory entry and take the file with it.

This is the property "survivable storage in its pod" actually requires. A pod evicted mid-write, a node rebooted, a PVC detached — none of them may leave a truncated flows.json behind, because a truncated flow file is a plant that does not come back up.

Types

type CredentialStore

type CredentialStore struct {
	// contains filtered or unexported fields
}

CredentialStore holds per-node secrets, encrypted at rest.

func NewCredentialStore

func NewCredentialStore(path, secret string) *CredentialStore

NewCredentialStore returns a store backed by path. An empty secret means credentials are held in memory only and never written — appropriate for tests, and refused at startup in production by the config layer.

func (*CredentialStore) Get

func (c *CredentialStore) Get(nodeID string) map[string]string

Get returns the decrypted credentials for a node.

func (*CredentialStore) HasSecret

func (c *CredentialStore) HasSecret() bool

HasSecret reports whether a secret is configured.

func (*CredentialStore) Load

func (c *CredentialStore) Load() error

Load reads and decrypts the credential file. A missing file is not an error.

func (*CredentialStore) Merge

func (c *CredentialStore) Merge(nodeID string, incoming map[string]any)

func (*CredentialStore) MigratedFromLegacy

func (c *CredentialStore) MigratedFromLegacy() bool

MigratedFromLegacy reports whether the file was read in Node-RED's format.

func (*CredentialStore) NodeIDs

func (c *CredentialStore) NodeIDs() []string

NodeIDs returns the ids that have credentials, for diagnostics. It never returns the values.

func (*CredentialStore) Prune

func (c *CredentialStore) Prune(liveNodes map[string]bool) int

Prune removes credentials belonging to nodes that no longer exist. Without it, a credential outlives the node it belonged to and sits encrypted on the PVC indefinitely.

func (*CredentialStore) Save

func (c *CredentialStore) Save() error

Save encrypts and writes the credential file.

func (*CredentialStore) Set

func (c *CredentialStore) Set(nodeID string, creds map[string]string)

Set replaces the credentials for a node. An empty map removes the entry.

type FlowStore

type FlowStore struct {
	// contains filtered or unexported fields
}

FlowStore persists the flow file.

func NewFlowStore

func NewFlowStore(path string) *FlowStore

NewFlowStore returns a store for the flow file at path.

func (*FlowStore) Load

func (s *FlowStore) Load() (*engine.Flows, error)

Load reads and parses the flow file.

If the primary file is missing, an empty flow set is returned — a fresh pod with an empty PVC is a normal state, not an error. If it is present but unparseable, each backup generation is tried in turn, newest first. Starting empty because the file was corrupt would look like a successful start and silently discard every flow the customer had.

func (*FlowStore) Path

func (s *FlowStore) Path() string

Path returns the flow file path.

func (*FlowStore) Recovered

func (s *FlowStore) Recovered() (bool, string)

Recovered reports whether the last Load fell back to a backup, and which file it came from. The API surfaces this so an operator finds out that their flow file was corrupt, rather than discovering it when a flow they wrote last week turns out to be missing.

func (*FlowStore) Rev

func (s *FlowStore) Rev() string

Rev returns the current revision token.

func (*FlowStore) Save

func (s *FlowStore) Save(flows *engine.Flows, expectedRev string) (string, error)

Save writes the flow set, rotating backups first.

expectedRev implements optimistic concurrency: pass the revision the caller last read, and the save fails with ErrRevisionConflict if the file has moved on. Pass "" to force.

func (*FlowStore) SetBackupGenerations

func (s *FlowStore) SetBackupGenerations(n int)

SetBackupGenerations changes how many previous versions are kept.

type MemoryContext

type MemoryContext struct {
	// contains filtered or unexported fields
}

MemoryContext is a volatile context store.

It is the default for node-scoped context and the fallback when no persistent store is configured, matching Node-RED's "memory" store. Values do not survive a restart; for anything that must, see the bbolt-backed store.

Every operation is atomic with respect to every other, which is what makes CompareAndSwap and Increment meaningful. Node-RED's context API offers only get and set, and FlowFuse identifies that gap as the specific reason a Node-RED flow cannot safely be run in more than one instance: two copies doing get-modify-set on a shared counter race with no primitive available to fix it.

func NewMemoryContext

func NewMemoryContext() *MemoryContext

NewMemoryContext returns an empty in-memory store.

func (*MemoryContext) CompareAndSwap

func (c *MemoryContext) CompareAndSwap(key string, prev, next any) (bool, error)

CompareAndSwap sets key to next only if its current value deep-equals prev, reporting whether the swap happened. An absent key compares equal to a nil prev, so a caller can use CAS to claim a key exactly once.

func (*MemoryContext) Delete

func (c *MemoryContext) Delete(key string) error

Delete removes a key.

func (*MemoryContext) Get

func (c *MemoryContext) Get(key string) (any, bool, error)

Get returns the value at key. Values are cloned on the way out so that a caller mutating what it reads cannot corrupt the store — the failure mode where two nodes share a context object and one edits it in place.

func (*MemoryContext) Increment

func (c *MemoryContext) Increment(key string, delta float64) (float64, error)

Increment adds delta to a numeric key, creating it at zero if absent, and returns the new value. A non-numeric existing value is an error rather than a silent reset, because silently zeroing a counter hides the bug that set it to a string.

func (*MemoryContext) Keys

func (c *MemoryContext) Keys() ([]string, error)

Keys returns every key, sorted so that iteration order is stable.

func (*MemoryContext) Set

func (c *MemoryContext) Set(key string, value any) error

Set stores a value, cloning it on the way in for the same reason Get clones on the way out.

func (*MemoryContext) Update

func (c *MemoryContext) Update(key string, fn func(cur any) (any, error)) (any, error)

Update applies fn to the current value and stores the result atomically. It is the general form of CompareAndSwap: no other operation can interleave between the read and the write.

fn runs while the store lock is held, so it must not call back into the same context store.

type ScopedContexts

type ScopedContexts struct {
	// contains filtered or unexported fields
}

ScopedContexts holds the three context scopes a node can address, creating flow scopes on demand.

func NewScopedContexts

func NewScopedContexts() *ScopedContexts

NewScopedContexts returns a set of context scopes backed by memory stores.

func NewScopedContextsWith

func NewScopedContextsWith(newStore func(scope string) node.Context) *ScopedContexts

NewScopedContextsWith returns a set of context scopes whose stores are built by newStore, keyed by a scope string such as "global", "flow:<tabID>" or "node:<nodeID>".

func (*ScopedContexts) Clean

func (s *ScopedContexts) Clean(liveNodes, liveFlows map[string]bool)

Clean removes the context of every node that is not in the supplied set of live node ids, and every flow scope not in the live flow set.

func (*ScopedContexts) DropNode

func (s *ScopedContexts) DropNode(nodeID string)

DropNode removes a node's private context. Called when a node is deleted from the flow, so that redeploying does not accumulate context for nodes that no longer exist — the leak Node-RED's context "clean" pass exists to sweep up.

func (*ScopedContexts) Flow

func (s *ScopedContexts) Flow(flowID string) node.Context

Flow returns the context for a tab or subflow instance, creating it on first use.

func (*ScopedContexts) Global

func (s *ScopedContexts) Global() node.Context

Global returns the runtime-wide context.

func (*ScopedContexts) Node

func (s *ScopedContexts) Node(nodeID string) node.Context

Node returns the context private to one node instance.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL