SPB Git

Repository Public

auditexport_test.go 2.8 KB 91 lines go

1package main23import (4	"bytes"5	"context"6	"crypto/ed25519"7	"encoding/base64"8	"os"9	"path/filepath"10	"strings"11	"testing"12	"time"1314	"clustr.spboucher.ai/mcc/internal/audit"15	"clustr.spboucher.ai/mcc/internal/signkey"16	"clustr.spboucher.ai/mcc/internal/testdb"17)1819func TestAuditVerifyExportCommand(t *testing.T) {20	pool, _ := testdb.New(t)21	ctx := context.Background()22	for i := 0; i < 25; i++ {23		if _, err := audit.Append(ctx, pool, audit.Entry{ActorKind: "cli", ActorLabel: "test", Action: "test.event", Outcome: audit.Success,24			Params: map[string]any{"n": i, "nested": map[string]any{"b": 1, "a": "é"}}, Detail: "entry <" + strings.Repeat("x", i) + ">"}); err != nil {25			t.Fatal(err)26		}27	}28	secrets := t.TempDir()29	key, err := signkey.LoadOrCreate(filepath.Join(secrets, "audit-signing.key"))30	if err != nil {31		t.Fatal(err)32	}33	pub := base64.StdEncoding.EncodeToString(key.Public().(ed25519.PublicKey))34	var buf bytes.Buffer35	from, to := time.Now().Add(-time.Hour), time.Now().Add(time.Hour)36	m, err := audit.WriteExport(ctx, pool, &buf, from, to, key, signkey.ID(key.Public().(ed25519.PublicKey)), time.Now())37	if err != nil || m.Count != 25 {38		t.Fatalf("export: %v %+v", err, m)39	}40	file := filepath.Join(t.TempDir(), "export.jsonl")41	write := func(b []byte) {42		if err := os.WriteFile(file, b, 0o600); err != nil {43			t.Fatal(err)44		}45	}46	write(buf.Bytes())4748	var out bytes.Buffer49	if err := verifyExport([]string{"--pubkey", pub, file}, &out); err != nil {50		t.Fatalf("valid export refused: %v", err)51	}52	if !strings.Contains(out.String(), "export verified: 25 entries, seq 1..25") {53		t.Fatalf("output: %s", out.String())54	}55	// Without --pubkey, on the host: the local key is used.56	t.Setenv("MCC_SECRETS_DIR", secrets)57	if err := verifyExport([]string{file}, &out); err != nil {58		t.Fatalf("local key: %v", err)59	}60	t.Setenv("MCC_SECRETS_DIR", t.TempDir())61	if err := verifyExport([]string{file}, &out); err == nil {62		t.Fatal("verified without any key")63	}64	// Every single byte matters.65	orig := buf.Bytes()66	for i := 0; i < len(orig); i += 1 + len(orig)/300 {67		mod := bytes.Clone(orig)68		mod[i] ^= 0x2069		if mod[i] == orig[i] {70			mod[i] ^= 0x0171		}72		write(mod)73		if err := verifyExport([]string{"--pubkey", pub, file}, &out); err == nil {74			t.Fatalf("change of byte %d (%q -> %q) went undetected", i, orig[i], mod[i])75		}76	}77	// Wrong key, malformed key, missing file, wrong usage.78	write(orig)79	otherPub, _, _ := ed25519.GenerateKey(nil)80	for name, args := range map[string][]string{81		"wrong key":     {"--pubkey", base64.StdEncoding.EncodeToString(otherPub), file},82		"malformed key": {"--pubkey", "not-base64", file},83		"missing file":  {"--pubkey", pub, file + ".nope"},84		"no file":       {"--pubkey", pub},85	} {86		if err := verifyExport(args, &out); err == nil {87			t.Errorf("%s: accepted", name)88		}89	}90}91
Files main
Menu

Theme