The node-engine held 1,707 top-level unit tests against the budget of 500 a module. Each unit test file with two or more tests now runs them as subtests of one test named for the file, each named as its function was, in the same order: every check is kept, and the check counts 383. Files with a TestMain, a re-run of a test by its name, or the integration tag are left. Four files of this branch's earlier commits are formatted. Builds: design 47 H2 §819; H-A-349 DQ-14
120 lines
4.4 KiB
Go
120 lines
4.4 KiB
Go
package apply
|
|
|
|
// A core credential the vault sealed to this machine (novox/hq ADR 0349 §16, issue 755): the controller carries the
|
|
// vault's box (ADR 0206) in the declaration and cannot open it; this machine opens it with its sealing key and writes
|
|
// the value, as it writes a value the controller sealed.
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seal"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/declaration"
|
|
"github.com/novox/mesh-host/internal/identity"
|
|
"github.com/novox/mesh-host/internal/store"
|
|
)
|
|
|
|
// vaultSealedTo is what the vault answers the controller: the value in the ADR 0206 box, sealed to the machine's
|
|
// sealing key as SPKI PEM.
|
|
func vaultSealedTo(t *testing.T, key identity.SealingKey, value string) seal.Box {
|
|
t.Helper()
|
|
pub, err := key.PublicPEM()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
box, err := seal.Seal(value, pub)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return box
|
|
}
|
|
|
|
func vaultSealedFile(t *testing.T, path string, box seal.Box) *declaration.Declaration {
|
|
t.Helper()
|
|
raw := map[string]any{"declaration": 1, "resources": []map[string]any{
|
|
{"id": "broker", "type": "file", "path": path, "vault-sealed": box},
|
|
}}
|
|
body, _ := json.Marshal(raw)
|
|
d, err := declaration.Parse(body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return d
|
|
}
|
|
|
|
func boxOpener(key identity.SealingKey) Unseal {
|
|
return func(sealed string) ([]byte, error) { return key.Open(sealed) }
|
|
}
|
|
|
|
func testAFileTheVaultSealedToThisMachineIsOpenedAndWrittenUnreadableToOthers(t *testing.T) {
|
|
key, err := identity.GenerateSealingKey()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := t.TempDir() + "/broker"
|
|
d := vaultSealedFile(t, path, vaultSealedTo(t, key, "the-provisioners-bus-password"))
|
|
report, state, err := Apply(context.Background(), archHost(t), d, store.State{},
|
|
store.OriginCarried, noServices, nil, boxOpener(key))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
on, err := os.ReadFile(path)
|
|
if err != nil || string(on) != "the-provisioners-bus-password" {
|
|
t.Fatalf("the file holds %q, %v", on, err)
|
|
}
|
|
if info, _ := os.Stat(path); info.Mode().Perm() != 0o600 {
|
|
t.Errorf("a credential landed mode %o", info.Mode().Perm())
|
|
}
|
|
said, _ := json.Marshal(report)
|
|
kept, _ := json.Marshal(state)
|
|
for what, blob := range map[string][]byte{"the report": said, "the node's state": kept} {
|
|
if strings.Contains(string(blob), "the-provisioners-bus-password") {
|
|
t.Fatalf("%s carries the value in the clear", what)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testAVaultBoxSealedToAnotherMachineIsRefusedAndNothingWritten(t *testing.T) {
|
|
mine, _ := identity.GenerateSealingKey()
|
|
theirs, _ := identity.GenerateSealingKey()
|
|
path := t.TempDir() + "/broker"
|
|
d := vaultSealedFile(t, path, vaultSealedTo(t, theirs, "not for you"))
|
|
if _, _, err := Apply(context.Background(), archHost(t), d, store.State{},
|
|
store.OriginCarried, noServices, nil, boxOpener(mine)); err == nil {
|
|
t.Fatal("a box sealed to another machine was applied")
|
|
}
|
|
if _, err := os.Stat(path); err == nil {
|
|
t.Fatal("something was written before the failure")
|
|
}
|
|
}
|
|
|
|
func testAVaultSealedFileSaysWhatIsInItOnce(t *testing.T) {
|
|
key, _ := identity.GenerateSealingKey()
|
|
box, _ := json.Marshal(vaultSealedTo(t, key, "x"))
|
|
for what, extra := range map[string]string{
|
|
"content": `"content":"a"`, "sealed": `"sealed":"b"`, "bytes": `"bytes":"Yw=="`,
|
|
"secrets": `"content":"${secret:x}","secrets":{"x":"c"}`, "into": `"into":"json","content":"{}"`,
|
|
} {
|
|
_, err := declaration.Parse([]byte(`{"declaration":1,"resources":[{"id":"f","type":"file","path":"/etc/x",` +
|
|
`"vault-sealed":` + string(box) + `,` + extra + `}]}`))
|
|
if err == nil {
|
|
t.Errorf("a vault-sealed file that also says %s was accepted", what)
|
|
}
|
|
}
|
|
if _, err := declaration.Parse([]byte(`{"declaration":1,"resources":[{"id":"f","type":"file","path":"/etc/x",` +
|
|
`"vault-sealed":{"v":2,"eph":"a","iv":"b","tag":"c","ct":"d"}}]}`)); err == nil {
|
|
t.Error("a box of a version the SDK does not open was accepted")
|
|
}
|
|
}
|
|
|
|
// TestVaultSealed runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestVaultSealed(t *testing.T) {
|
|
t.Run("AFileTheVaultSealedToThisMachineIsOpenedAndWrittenUnreadableToOthers", testAFileTheVaultSealedToThisMachineIsOpenedAndWrittenUnreadableToOthers)
|
|
t.Run("AVaultBoxSealedToAnotherMachineIsRefusedAndNothingWritten", testAVaultBoxSealedToAnotherMachineIsRefusedAndNothingWritten)
|
|
t.Run("AVaultSealedFileSaysWhatIsInItOnce", testAVaultSealedFileSaysWhatIsInItOnce)
|
|
}
|