Every package publish fails since about 11:45 CET: the package-registry and forge bundles ask the vault for module/gitea/* from inside the tool runner, whose account the vault refuses (no module's account), and the manifest since C4 hands them no token file. Until gitea runs as its own account, a bundle the vault refuses reads the token gitea's TypeScript bundle keeps under its state directory (token, packages-token, each with its scopes line naming the account), says once at start which source each token came from, and reads the file again at each call. The manifest hands the two bundles MESH_GITEA_TOKEN_FILE and MESH_GITEA_PACKAGES_TOKEN_FILE again. The fallback ends with gitea's own account. Builds: design 47 H2 H-A-564 (hq 1b286366), H-A-543
275 lines
9.5 KiB
Go
275 lines
9.5 KiB
Go
package forgehttp
|
|
|
|
// The forge module's tokens as its Go bundles read them (design 47 H2 §808; H-A-422 (1), H-A-426): kept in the vault as
|
|
// the module's own, `module/gitea/<account>`, written by the TypeScript bundle's minter through own-put, and read here
|
|
// through the seat `mesh-vault`'s `get`, sealed to this process's own key (the Go SDK's seal opens it), at start and at
|
|
// each `secret-changed` that names it. The bundle holds the token in memory only, and says nothing of it.
|
|
//
|
|
// Until gitea runs as its own account (H-A-543), the vault refuses the tool runner's account these bundles are called
|
|
// under, so (H-A-564) when the vault refuses the caller's account, the token is read from the file gitea's TypeScript
|
|
// bundle keeps it in under its state directory (MESH_GITEA_TOKEN_FILE for the tools' account,
|
|
// MESH_GITEA_PACKAGES_TOKEN_FILE for the packages account, each with its scopes line beside it), and Follow says once
|
|
// at start which source each token came from. The fallback ends with H-A-543's delivery.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/seal"
|
|
)
|
|
|
|
// VaultGet is the vault seat's verb a token is read by, and SecretChanged the vault's event that a secret changed.
|
|
const (
|
|
VaultGet = "seat:mesh-vault.get"
|
|
SecretChanged = "mesh-vault.secret-changed"
|
|
)
|
|
|
|
// VaultName is the vault's name of an account's token (H-A-426).
|
|
func VaultName(account string) string { return "module/gitea/" + account }
|
|
|
|
// Ask asks one seat verb and answers its reply (stdio.Ask).
|
|
type Ask func(address string, args any) (json.RawMessage, error)
|
|
|
|
// Refusals are what the vault answers when it refuses the caller's account a module's own secret (mesh-vault's
|
|
// callerModule and its own-secret judgement): the only answers the token file is read after (H-A-564).
|
|
var Refusals = []string{"is no module's account", "handed only to an account of"}
|
|
|
|
// refused is whether err is the vault refusing the caller's account.
|
|
func refused(err error) bool {
|
|
for _, r := range Refusals {
|
|
if strings.Contains(err.Error(), r) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Sources of a token: the vault, or the file gitea's TypeScript bundle keeps (H-A-564).
|
|
const (
|
|
SourceVault = "the vault"
|
|
SourceFile = "the file gitea keeps"
|
|
)
|
|
|
|
// VaultToken is one account's token as this process holds it, read from the vault, or from File when the vault
|
|
// refuses the caller's account (H-A-564).
|
|
type VaultToken struct {
|
|
Account string
|
|
// File is the token file gitea's TypeScript bundle keeps for Account, "" when the manifest names none.
|
|
File string
|
|
ask Ask
|
|
keys seal.KeyPair
|
|
|
|
mu sync.Mutex
|
|
token string
|
|
generation int
|
|
source string
|
|
}
|
|
|
|
// NewVaultToken is an account's token read through ask, sealed to a key made for this process.
|
|
func NewVaultToken(account string, ask Ask) (*VaultToken, error) {
|
|
keys, err := seal.GenerateKeyPair()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("no key to read %s's token with: %w", account, err)
|
|
}
|
|
return &VaultToken{Account: account, ask: ask, keys: keys}, nil
|
|
}
|
|
|
|
// Load reads the account's token from the vault again; from File when the vault refuses the caller's account.
|
|
func (t *VaultToken) Load() error {
|
|
raw, err := t.ask(VaultGet, map[string]any{"name": VaultName(t.Account), "public_key": t.keys.PublicKey})
|
|
if err != nil {
|
|
if t.File != "" && refused(err) {
|
|
token, ferr := t.fromFile()
|
|
if ferr != nil {
|
|
return fmt.Errorf("the vault did not hand %s (%v), and %w", VaultName(t.Account), err, ferr)
|
|
}
|
|
t.mu.Lock()
|
|
t.token, t.generation, t.source = token, 0, SourceFile
|
|
t.mu.Unlock()
|
|
return nil
|
|
}
|
|
return fmt.Errorf("the vault did not hand %s: %w", VaultName(t.Account), err)
|
|
}
|
|
var got struct {
|
|
Generation int `json:"generation"`
|
|
Sealed *seal.Box `json:"sealed"`
|
|
}
|
|
if err := json.Unmarshal(raw, &got); err != nil || got.Sealed == nil {
|
|
return fmt.Errorf("the vault answered no sealed %s", VaultName(t.Account))
|
|
}
|
|
opened, err := seal.Open(*got.Sealed, t.keys.PrivateKey)
|
|
if err != nil {
|
|
return fmt.Errorf("%s does not open with this process's key: %w", VaultName(t.Account), err)
|
|
}
|
|
// The value holds the token and the scopes it was minted with (H-A-439); the minter judges the scopes, this reads the token.
|
|
var value struct {
|
|
Token string `json:"token"`
|
|
}
|
|
if err := json.Unmarshal([]byte(opened), &value); err != nil {
|
|
return fmt.Errorf("the vault's %s is not a token with its scopes: %w", VaultName(t.Account), err)
|
|
}
|
|
token := strings.TrimSpace(value.Token)
|
|
if token == "" {
|
|
return fmt.Errorf("the vault's %s is empty", VaultName(t.Account))
|
|
}
|
|
t.mu.Lock()
|
|
t.token, t.generation, t.source = token, got.Generation, SourceVault
|
|
t.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// fromFile is the token gitea's TypeScript bundle keeps in File, refused unless its scopes line names Account (as the
|
|
// bundle writes it: "<scopes> for <account>").
|
|
func (t *VaultToken) fromFile() (string, error) {
|
|
raw, err := os.ReadFile(t.File)
|
|
if err != nil {
|
|
return "", fmt.Errorf("the token file gitea keeps for %s cannot be read: %w", t.Account, err)
|
|
}
|
|
line, err := os.ReadFile(t.File + ".scopes")
|
|
if err != nil || !strings.HasSuffix(strings.TrimSpace(string(line)), " for "+t.Account) {
|
|
return "", fmt.Errorf("the token kept at %s is not %s's (its scopes line does not name it); nothing is sent", t.File, t.Account)
|
|
}
|
|
token := strings.TrimSpace(string(raw))
|
|
if token == "" {
|
|
return "", fmt.Errorf("the token kept at %s is empty; nothing is sent", t.File)
|
|
}
|
|
return token, nil
|
|
}
|
|
|
|
// Source is where the token held came from, SourceVault or SourceFile; "" when none is held.
|
|
func (t *VaultToken) Source() string {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.source
|
|
}
|
|
|
|
// Token is the token held; read from the vault when none is yet (the module mints it first).
|
|
func (t *VaultToken) Token() (string, error) {
|
|
t.mu.Lock()
|
|
token, source := t.token, t.source
|
|
t.mu.Unlock()
|
|
if source == SourceFile {
|
|
// gitea's TypeScript bundle mints into its file again on a refusal: the file is read at each call, as before C4.
|
|
return t.fromFile()
|
|
}
|
|
if token != "" {
|
|
return token, nil
|
|
}
|
|
if err := t.Load(); err != nil {
|
|
return "", fmt.Errorf("the token for %s is not kept yet (the module mints it): %w", t.Account, err)
|
|
}
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.token, nil
|
|
}
|
|
|
|
// Generation is the vault's generation of the token held, 0 when none is held.
|
|
func (t *VaultToken) Generation() int {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.generation
|
|
}
|
|
|
|
// Changed takes one `secret-changed`: the token is read again when it names this account's; answers whether it did.
|
|
func (t *VaultToken) Changed(body []byte) (bool, error) {
|
|
var e struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.Unmarshal(body, &e); err != nil {
|
|
return false, fmt.Errorf("%s is not a secret's change: %w", SecretChanged, err)
|
|
}
|
|
if e.Name != VaultName(t.Account) {
|
|
return false, nil
|
|
}
|
|
return true, t.Load()
|
|
}
|
|
|
|
var (
|
|
processTokens = map[string]*VaultToken{}
|
|
processTokensMu sync.Mutex
|
|
)
|
|
|
|
// TokenOf is this process's one token of account, read through the runtime's seat calls.
|
|
func TokenOf(account string) (*VaultToken, error) {
|
|
processTokensMu.Lock()
|
|
defer processTokensMu.Unlock()
|
|
if t, ok := processTokens[account]; ok {
|
|
return t, nil
|
|
}
|
|
t, err := NewVaultToken(account, stdio.Ask)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
t.File = TokenFile(account)
|
|
processTokens[account] = t
|
|
return t, nil
|
|
}
|
|
|
|
// TokenFile is the file the manifest names for account's token as gitea's TypeScript bundle keeps it (H-A-564):
|
|
// MESH_GITEA_PACKAGES_TOKEN_FILE for the packages account, MESH_GITEA_TOKEN_FILE for the tools' account.
|
|
func TokenFile(account string) string {
|
|
packages := os.Getenv("MESH_GITEA_PACKAGES_USER")
|
|
if packages == "" {
|
|
packages = PackagesAccount
|
|
}
|
|
tools := os.Getenv("MESH_GITEA_TOOLS_USER")
|
|
if tools == "" {
|
|
tools = ToolsAccount
|
|
}
|
|
switch account {
|
|
case packages:
|
|
return os.Getenv("MESH_GITEA_PACKAGES_TOKEN_FILE")
|
|
case tools:
|
|
return os.Getenv("MESH_GITEA_TOKEN_FILE")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Follow reads each account's token once the runtime serves, and again at each `secret-changed` naming it, saying
|
|
// through logf what it read and, once at start, from which source (never the token). It returns when the
|
|
// subscription ends.
|
|
func Follow(logf func(format string, args ...any), accounts ...string) error {
|
|
var held []*VaultToken
|
|
for _, a := range accounts {
|
|
t, err := TokenOf(a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
held = append(held, t)
|
|
}
|
|
<-stdio.Served()
|
|
Start(logf, held...)
|
|
return stdio.Subscribe(SecretChanged, func(e stdio.Envelope) error {
|
|
var errs []error
|
|
for _, t := range held {
|
|
if read, err := t.Changed(e.Body); err != nil {
|
|
errs = append(errs, err)
|
|
logf("%s changed and could not be read again: %v", VaultName(t.Account), err)
|
|
} else if read {
|
|
logf("read %s again at generation %d", VaultName(t.Account), t.Generation())
|
|
}
|
|
}
|
|
return errors.Join(errs...)
|
|
})
|
|
}
|
|
|
|
// Start reads each token once and says, through logf, from which source it came (H-A-564), never the token.
|
|
func Start(logf func(format string, args ...any), held ...*VaultToken) {
|
|
for _, t := range held {
|
|
switch err := t.Load(); {
|
|
case err != nil:
|
|
logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err)
|
|
case t.Source() == SourceFile:
|
|
logf("the vault refuses %s to this process's account, so %s's token is read from %s, %s (H-A-564)",
|
|
VaultName(t.Account), t.Account, SourceFile, t.File)
|
|
default:
|
|
logf("read %s at generation %d from %s", VaultName(t.Account), t.Generation(), SourceVault)
|
|
}
|
|
}
|
|
}
|