configstore

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 18 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 configstore persists the UI configuration (spec 3): objects and settings with a revision (config_rev), history snapshots, audit entries with a diff, and encrypted secrets. Validation of the whole configuration is done by the caller (internal/app).

Index

Constants

View Source
const (
	KindConnection = "connection"
	KindDevice     = "device"
	KindMeter      = "meter"
)

Kinds of UI objects.

View Source
const ActionChange = "config_change"

ActionChange is the audit action of a saved UI change.

View Source
const Masked = "***"

Masked is shown instead of a stored secret; sent back unchanged it keeps the secret.

Variables

View Source
var (
	// ErrNoKey is returned when a secret is read without a usable secret.key.
	ErrNoKey = errors.New("configstore: secret.key is missing")
	// ErrBadSecret is returned for a stored secret that cannot be decrypted.
	ErrBadSecret = errors.New("configstore: secret cannot be decrypted")
)
View Source
var (
	// ErrNotFound is returned for an unknown object.
	ErrNotFound = errors.New("configstore: not found")
	// ErrUnknownKind is returned for a kind other than connection, device or meter.
	ErrUnknownKind = errors.New("configstore: unknown kind")
	// ErrConflict is returned when another change was saved since the edited revision.
	ErrConflict = errors.New("configstore: configuration changed meanwhile")
	// ErrLocked is returned for objects and settings owned by config.yaml or env.
	ErrLocked = errors.New("configstore: defined in config.yaml")
)

Functions

func Section

func Section(kind string) (string, error)

Section returns the config section of a kind ("device" → "devices").

Types

type Change

type Change struct {
	Kind string         `json:"kind"`
	ID   string         `json:"id"`
	Body map[string]any `json:"body"`
}

Change is one object write of a batch (PUT /api/v1/config, the setup probe); a nil Body deletes the object.

type Cipher

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

Cipher encrypts the secrets of the UI configuration with XChaCha20-Poly1305 and the 256-bit key in secret.key. A Cipher without key (CLI jobs that must not create one) fails every Seal and Open with ErrNoKey.

func LoadKey

func LoadKey(path string, create bool, rnd io.Reader) (*Cipher, error)

LoadKey reads the key file; a missing file is created with a new key when create is set (server start), else the returned Cipher has no key.

func NewCipher

func NewCipher(key []byte, rnd io.Reader) (*Cipher, error)

NewCipher builds a Cipher from a 32-byte key.

func (*Cipher) Open

func (c *Cipher) Open(sealed string) (string, error)

Open decrypts a value written by Seal.

func (*Cipher) Seal

func (c *Cipher) Seal(plain string) (string, error)

Seal encrypts plain into enc:v1:<base64(nonce+ciphertext)>.

type Deps

type Deps struct {
	Store  Store
	Sealer Sealer
	Clock  util.Clock
}

Deps are the dependencies of Store.

type Key

type Key struct{ Kind, ID string }

Key names one object.

type Object

type Object struct {
	Body       map[string]any // file form: {"connection": "lan", "auth": {"password": …}}
	UpdatedAt  time.Time
	UpdatedBy  string
	Unreadable []string // secret paths that could not be decrypted (lost secret.key)
	// contains filtered or unexported fields
}

Object is one UI object with plain secrets.

type ObjectView

type ObjectView struct {
	Kind       string         `json:"kind"`
	ID         string         `json:"id"`
	Locked     bool           `json:"locked"` // defined in config.yaml or env: read-only
	Body       map[string]any `json:"body"`
	UpdatedAt  *time.Time     `json:"updated_at"`
	UpdatedBy  string         `json:"updated_by,omitempty"`
	Unreadable []string       `json:"unreadable,omitempty"` // secrets to enter again
}

ObjectView is one object as the settings API shows it (secrets masked).

type Overview

type Overview struct {
	Rev      int64                  `json:"config_rev"`
	Objects  []ObjectView           `json:"objects"`
	Settings map[string]SettingView `json:"settings"`
}

Overview is the whole configuration as the settings pages need it.

type Problem

type Problem struct {
	Path    string `json:"path"`
	Message string `json:"message"`
}

Problem is one rejected field with its path.

type Sealer

type Sealer interface {
	Seal(plain string) (string, error)
	Open(sealed string) (string, error)
}

Sealer encrypts and decrypts secrets (*Cipher).

type Service

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

Service loads, edits and commits the UI configuration.

func New

func New(s Settings, d Deps) (*Service, error)

New builds the service.

func (*Service) Commit

func (sv *Service) Commit(ctx context.Context, base, next *Snapshot, by string,
) (int64, error)

Commit stores next as the change from base and returns the new revision; without a difference nothing is written. ErrConflict when another change was saved since base.

func (*Service) Load

func (sv *Service) Load(ctx context.Context) (*Snapshot, error)

Load reads the stored UI configuration and decrypts its secrets; a secret that cannot be decrypted is empty and listed in Object.Unreadable.

func (*Service) Masked

func (sv *Service) Masked(kind string, o Object) map[string]any

Masked returns the body of o with every set secret replaced by Masked.

func (*Service) View

func (sv *Service) View(k Key, o Object) ObjectView

View returns the view of a stored object.

func (*Service) WithObject

func (sv *Service) WithObject(base *Snapshot, k Key, body map[string]any, by string,
) (*Snapshot, error)

WithObject returns base with the object k set to body. A secret sent as Masked keeps the stored value; secret references (${env:…}, ${file:…}) are refused, because they would let the UI read server files and variables.

func (*Service) WithSettings

func (sv *Service) WithSettings(base *Snapshot, set map[string]any) (*Snapshot, error)

WithSettings returns base with the given settings; a nil value removes the setting (back to the default).

func (*Service) WithoutObject

func (sv *Service) WithoutObject(base *Snapshot, k Key) (*Snapshot, error)

WithoutObject returns base without the object k.

type SettingView

type SettingView struct {
	Value  any  `json:"value"`
	Locked bool `json:"locked"` // set in config.yaml or env
	Stored bool `json:"stored"` // set in the UI configuration (else default or file)
}

SettingView is one UI setting with its effective value.

type Settings

type Settings struct {
	// Secrets lists the secret paths inside an object body per kind ("auth.password").
	Secrets map[string][]string
	// SettingKeys are the settings the UI may change.
	SettingKeys []string
}

Settings describe the schema parts the store needs to know.

type Snapshot

type Snapshot struct {
	Rev      int64
	Objects  map[Key]Object
	Settings map[string]any // flat keys: "site.name"
}

Snapshot is the UI configuration at one revision. It is never changed in place: the With… methods of Store return a new one.

func (*Snapshot) Keys

func (s *Snapshot) Keys(kind string) []Key

Keys returns the object keys of a kind, sorted by ID.

func (*Snapshot) Layer

func (s *Snapshot) Layer() map[string]any

Layer returns the snapshot as the DB layer of config.Load: objects nested under their section, settings as flat keys.

type Store

type Store interface {
	ConfigState(ctx context.Context) (storage.ConfigState, error)
	ApplyConfig(ctx context.Context, c storage.ConfigChange) (int64, error)
}

Store is the interface of storage the config service needs.

type ValidationError

type ValidationError struct{ Problems []Problem }

ValidationError lists every problem of a change (HTTP 422).

func (*ValidationError) Error

func (e *ValidationError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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