Files
mesh-catalog/modules/mesh-vault/cmd/mesh-vault/licences.go
T
jschoubben 45b0779b68
mesh/delivery merged: source.main moved to novox/mesh-catalog@792a79720b30
mesh/check checking 45b0779b6801 on 792a79720b30
mesh-vault: a store file that is not a record is listed by name with its error (issue 1409, V10)
Store.ListReadable lists every record and, beside them, each file it cannot read
by name with its error; RecordsHeld lists those as rows with an error and goes
on. recordsError stays for a store that cannot be listed at all. Store.List and
ListPrefix keep their behaviour for their other callers.

Builds: design 39 bus-and-switch §20; design 39 parts §12. Lead answers (issue 1409): V9 (keep recordsCount and recordsError), V10.
2026-11-12 17:41:39 +01:00

132 lines
4.8 KiB
Go

package main
// A licence's two secrets (design 39 bus-and-switch §15, B3 to B6): `licence.<licence key>.grant` and
// `licence.<licence key>.access`, handed to the vault by the licence manager through `licence-put` in two steps — name
// the licence, the part and the generation and get a call and a key made for it, then send the call and the value sealed
// to that key. The caller's generation is the record's; the vault keeps the newest and refuses an older one, and says
// `secret-changed` with the secret's name and generation, never the value.
import (
"errors"
"fmt"
"regexp"
"sort"
"strings"
"time"
)
// LicencePutVerb is the verb that hands a licence's secret to the vault.
const LicencePutVerb = "licence-put"
// The parts of a licence the vault holds (§15).
const (
LicenceGrant = "grant"
LicenceAccess = "access"
)
// licenceKey is a licence's key form (§10, B6): lower case, letters, digits, `-`, `.` and `_`.
var licenceKey = regexp.MustCompile(`^[a-z0-9_.-]{1,120}$`)
// LicenceSecret is the name of one part of a licence in the vault.
func LicenceSecret(key, part string) (string, error) {
if !licenceKey.MatchString(key) {
return "", fmt.Errorf("%q is not a licence's key: lower-case letters, digits, -, . and _", key)
}
if part != LicenceGrant && part != LicenceAccess {
return "", fmt.Errorf("the part %q is not a part of a licence: grant or access", part)
}
return "licence." + key + "." + part, nil
}
// LicencePutOpen opens a call to take one part of a licence at a generation.
func (v *Vault) LicencePutOpen(caller, licence, part string, generation int) (Opened, error) {
if err := fromAGrantedCaller(caller, LicencePutVerb); err != nil {
return Opened{}, err
}
secret, err := LicenceSecret(licence, part)
if err != nil {
return Opened{}, err
}
if generation < 1 {
return Opened{}, errors.New("licence-put takes the licence's generation, one or more; nothing is taken")
}
id, key, err := v.openCall(LicencePutVerb, secret, caller, nil)
if err != nil {
return Opened{}, err
}
v.callsMu.Lock()
if c := v.pending[id]; c != nil {
c.generation = generation
}
v.callsMu.Unlock()
return Opened{Call: id, SealTo: key, Secret: secret}, nil
}
// LicencePutTake opens the value sealed to the call's key and keeps it at the call's generation.
func (v *Vault) LicencePutTake(caller, call, sealed string) (Changed, error) {
c, err := v.takeCall(LicencePutVerb, call)
if err != nil {
return Changed{}, err
}
if c.by != caller {
return Changed{}, fmt.Errorf("the licence-put call %q was opened by another caller; nothing was taken", call)
}
value, err := c.open(sealed)
if err != nil {
return Changed{}, err
}
defer zero(value)
if len(value) == 0 {
return Changed{}, errors.New("the value handed in is empty; nothing was taken")
}
return v.keepWith(c.secret, string(value), caller, "", OriginAccepted, keepOptions{generation: c.generation})
}
// RecordHeld is one record of the vault's store as secret_holders lists it (issue 1409, design 39 parts §12): by name
// and fingerprint, never the value. For a licence's secret it also says the licence's key and the part.
type RecordHeld struct {
Name string `json:"name"`
Key string `json:"key,omitempty"`
Part string `json:"part,omitempty"`
Generation int `json:"generation"`
Since time.Time `json:"since"`
Changed time.Time `json:"changed"`
ChangedBy string `json:"changedBy"`
ChangedVia string `json:"changedVia,omitempty"`
Origin string `json:"origin"`
Length int `json:"length"`
Fingerprint string `json:"fingerprint"`
// Error says why the vault could not open its copy; the record stays in the list.
Error string `json:"error,omitempty"`
}
// RecordsHeld lists every record the store keeps, by name, ordered as the store lists them. The fingerprint is the
// vault's own of the whole value, as the ledger gives it.
func (v *Vault) RecordsHeld() ([]RecordHeld, error) {
records, bad, err := v.Store.ListReadable()
if err != nil {
return nil, err
}
out := make([]RecordHeld, 0, len(records))
for _, r := range records {
row := RecordHeld{Name: r.Name, Generation: r.Generation, Since: r.MadeAt, Changed: r.ChangedAt,
ChangedBy: r.ChangedBy, ChangedVia: r.ChangedVia, Origin: r.Origin}
if rest, ok := strings.CutPrefix(r.Name, "licence."); ok {
if i := strings.LastIndex(rest, "."); i > 0 {
row.Key, row.Part = rest[:i], rest[i+1:]
}
}
if value, err := openAtRest(r, v); err != nil {
row.Error = "the vault could not open its copy: " + err.Error()
} else {
row.Length, row.Fingerprint = len(value), Fingerprint(value)
}
out = append(out, row)
}
for _, b := range bad {
out = append(out, RecordHeld{Name: b.Name, Error: "the vault could not read the record's file: " + b.Err.Error()})
}
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out, nil
}