SPB Git

Repository Public

auditexport.go 3.6 KB 110 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/cmd/mccctl/auditexport.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package main4344import (45	"crypto/ed25519"46	"encoding/base64"47	"errors"48	"flag"49	"fmt"50	"io"51	"os"52	"path/filepath"5354	"clustr.spboucher.ai/mcc/internal/audit"55	"clustr.spboucher.ai/mcc/internal/signkey"56)5758// verifyExport checks an audit export file offline: no database, no network.59//60//	mccctl audit-verify-export [--pubkey <base64>] <file>61//62// The public key is the one published by GET /api/v1/audit/verify63// (export_public_key). Without --pubkey, and only on the host itself, the key64// is derived from the local signing key in the secrets directory.65func verifyExport(args []string, out io.Writer) error {66	fs := flag.NewFlagSet("audit-verify-export", flag.ContinueOnError)67	pubB64 := fs.String("pubkey", "", "base64 Ed25519 public key (export_public_key of GET /audit/verify)")68	if err := fs.Parse(args); err != nil {69		return err70	}71	if fs.NArg() != 1 {72		return errors.New("usage: mccctl audit-verify-export [--pubkey <base64>] <file>")73	}74	var pub ed25519.PublicKey75	if *pubB64 != "" {76		raw, err := base64.StdEncoding.DecodeString(*pubB64)77		if err != nil || len(raw) != ed25519.PublicKeySize {78			return errors.New("--pubkey is not a base64 Ed25519 public key")79		}80		pub = raw81	} else {82		dir := os.Getenv("MCC_SECRETS_DIR")83		if dir == "" {84			home, _ := os.UserHomeDir()85			dir = filepath.Join(home, ".config", "mcc")86		}87		path := filepath.Join(dir, "audit-signing.key")88		if _, err := os.Stat(path); err != nil {89			return fmt.Errorf("no --pubkey given and no local signing key at %s", path)90		}91		key, err := signkey.LoadOrCreate(path)92		if err != nil {93			return err94		}95		pub = key.Public().(ed25519.PublicKey)96	}97	f, err := os.Open(fs.Arg(0))98	if err != nil {99		return err100	}101	defer f.Close()102	m, err := audit.VerifyExport(f, pub)103	if err != nil {104		return fmt.Errorf("export NOT verified: %w", err)105	}106	fmt.Fprintf(out, "export verified: %d entries, seq %d..%d, range %s .. %s, head %s, key %s\n", m.Count, m.FirstSeq, m.LastSeq,107		m.From.Format("2006-01-02T15:04:05Z"), m.To.Format("2006-01-02T15:04:05Z"), m.HeadHash, m.KeyID)108	return nil109}110
Files main
Menu

Theme