Migrate config to mcdsl: Load[T], env overrides, embedded types
Replace local TOML loading with mcdsl/config.Load[Config] which adds METACRYPT_ environment variable overrides. ServerConfig and MCIASConfig now embed their mcdsl counterparts, extending with ExternalURL and ServiceToken respectively. DatabaseConfig and LogConfig replaced with mcdsl types directly. TOML structure is preserved — no config file changes needed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2
go.mod
2
go.mod
@@ -6,7 +6,6 @@ require (
|
|||||||
git.wntrmute.dev/kyle/goutils v1.21.0
|
git.wntrmute.dev/kyle/goutils v1.21.0
|
||||||
git.wntrmute.dev/kyle/mcdsl v1.0.0
|
git.wntrmute.dev/kyle/mcdsl v1.0.0
|
||||||
github.com/go-chi/chi/v5 v5.2.5
|
github.com/go-chi/chi/v5 v5.2.5
|
||||||
github.com/pelletier/go-toml/v2 v2.3.0
|
|
||||||
github.com/spf13/cobra v1.10.2
|
github.com/spf13/cobra v1.10.2
|
||||||
github.com/spf13/viper v1.21.0
|
github.com/spf13/viper v1.21.0
|
||||||
golang.org/x/crypto v0.49.0
|
golang.org/x/crypto v0.49.0
|
||||||
@@ -23,6 +22,7 @@ require (
|
|||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||||
|
github.com/pelletier/go-toml/v2 v2.3.0 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/pelletier/go-toml/v2"
|
mcdslauth "git.wntrmute.dev/kyle/mcdsl/auth"
|
||||||
|
mcdslconfig "git.wntrmute.dev/kyle/mcdsl/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config is the top-level configuration for Metacrypt.
|
// Config is the top-level configuration for Metacrypt.
|
||||||
@@ -13,29 +13,24 @@ type Config struct {
|
|||||||
Server ServerConfig `toml:"server"`
|
Server ServerConfig `toml:"server"`
|
||||||
Web WebConfig `toml:"web"`
|
Web WebConfig `toml:"web"`
|
||||||
MCIAS MCIASConfig `toml:"mcias"`
|
MCIAS MCIASConfig `toml:"mcias"`
|
||||||
Database DatabaseConfig `toml:"database"`
|
Database mcdslconfig.DatabaseConfig `toml:"database"`
|
||||||
Log LogConfig `toml:"log"`
|
Log mcdslconfig.LogConfig `toml:"log"`
|
||||||
Seal SealConfig `toml:"seal"`
|
Seal SealConfig `toml:"seal"`
|
||||||
Audit AuditConfig `toml:"audit"`
|
Audit AuditConfig `toml:"audit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuditConfig holds audit logging settings.
|
// ServerConfig holds HTTP/gRPC server settings. It embeds the standard
|
||||||
type AuditConfig struct {
|
// mcdsl server config and adds the ACME external URL.
|
||||||
// Mode controls audit log output: "file", "stdout", or "" (disabled).
|
type ServerConfig struct {
|
||||||
Mode string `toml:"mode"`
|
mcdslconfig.ServerConfig
|
||||||
// Path is the audit log file path (required when mode is "file").
|
ExternalURL string `toml:"external_url"` // public base URL for ACME directory
|
||||||
Path string `toml:"path"`
|
|
||||||
// IncludeReads enables audit logging for read-only operations.
|
|
||||||
IncludeReads bool `toml:"include_reads"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerConfig holds HTTP/gRPC server settings.
|
// MCIASConfig holds MCIAS integration settings. It embeds the standard
|
||||||
type ServerConfig struct {
|
// mcdsl auth config and adds the service token for web UI login.
|
||||||
ListenAddr string `toml:"listen_addr"`
|
type MCIASConfig struct {
|
||||||
GRPCAddr string `toml:"grpc_addr"`
|
mcdslauth.Config
|
||||||
TLSCert string `toml:"tls_cert"`
|
ServiceToken string `toml:"service_token"`
|
||||||
TLSKey string `toml:"tls_key"`
|
|
||||||
ExternalURL string `toml:"external_url"` // public base URL for ACME directory, e.g. "https://metacrypt.example.com"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebConfig holds settings for the standalone web UI server (metacrypt-web).
|
// WebConfig holds settings for the standalone web UI server (metacrypt-web).
|
||||||
@@ -56,16 +51,14 @@ type WebConfig struct {
|
|||||||
TLSKey string `toml:"tls_key"`
|
TLSKey string `toml:"tls_key"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseConfig holds SQLite database settings.
|
// AuditConfig holds audit logging settings.
|
||||||
type DatabaseConfig struct {
|
type AuditConfig struct {
|
||||||
|
// Mode controls audit log output: "file", "stdout", or "" (disabled).
|
||||||
|
Mode string `toml:"mode"`
|
||||||
|
// Path is the audit log file path (required when mode is "file").
|
||||||
Path string `toml:"path"`
|
Path string `toml:"path"`
|
||||||
}
|
// IncludeReads enables audit logging for read-only operations.
|
||||||
|
IncludeReads bool `toml:"include_reads"`
|
||||||
// MCIASConfig holds MCIAS integration settings.
|
|
||||||
type MCIASConfig struct {
|
|
||||||
ServerURL string `toml:"server_url"`
|
|
||||||
CACert string `toml:"ca_cert"`
|
|
||||||
ServiceToken string `toml:"service_token"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SealConfig holds Argon2id parameters for the seal process.
|
// SealConfig holds Argon2id parameters for the seal process.
|
||||||
@@ -75,46 +68,31 @@ type SealConfig struct {
|
|||||||
Argon2Threads uint8 `toml:"argon2_threads"`
|
Argon2Threads uint8 `toml:"argon2_threads"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogConfig holds logging settings.
|
// Load reads and parses a TOML config file with METACRYPT_ env var overrides.
|
||||||
type LogConfig struct {
|
|
||||||
Level string `toml:"level"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load reads and parses a TOML config file.
|
|
||||||
func Load(path string) (*Config, error) {
|
func Load(path string) (*Config, error) {
|
||||||
data, err := os.ReadFile(path) //nolint:gosec
|
return mcdslconfig.Load[Config](path, "METACRYPT")
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("config: read file: %w", err)
|
|
||||||
}
|
|
||||||
var cfg Config
|
|
||||||
if err := toml.Unmarshal(data, &cfg); err != nil {
|
|
||||||
return nil, fmt.Errorf("config: parse: %w", err)
|
|
||||||
}
|
|
||||||
if err := cfg.Validate(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &cfg, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks required fields and applies defaults.
|
// Validate implements mcdslconfig.Validator for metacrypt-specific rules.
|
||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
|
// Required fields.
|
||||||
if c.Server.ListenAddr == "" {
|
if c.Server.ListenAddr == "" {
|
||||||
return fmt.Errorf("config: server.listen_addr is required")
|
return fmt.Errorf("server.listen_addr is required")
|
||||||
}
|
}
|
||||||
if c.Server.TLSCert == "" {
|
if c.Server.TLSCert == "" {
|
||||||
return fmt.Errorf("config: server.tls_cert is required")
|
return fmt.Errorf("server.tls_cert is required")
|
||||||
}
|
}
|
||||||
if c.Server.TLSKey == "" {
|
if c.Server.TLSKey == "" {
|
||||||
return fmt.Errorf("config: server.tls_key is required")
|
return fmt.Errorf("server.tls_key is required")
|
||||||
}
|
}
|
||||||
if c.Database.Path == "" {
|
if c.Database.Path == "" {
|
||||||
return fmt.Errorf("config: database.path is required")
|
return fmt.Errorf("database.path is required")
|
||||||
}
|
}
|
||||||
if c.MCIAS.ServerURL == "" {
|
if c.MCIAS.ServerURL == "" {
|
||||||
return fmt.Errorf("config: mcias.server_url is required")
|
return fmt.Errorf("mcias.server_url is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply defaults for seal parameters.
|
// Seal defaults.
|
||||||
if c.Seal.Argon2Time == 0 {
|
if c.Seal.Argon2Time == 0 {
|
||||||
c.Seal.Argon2Time = 3
|
c.Seal.Argon2Time = 3
|
||||||
}
|
}
|
||||||
@@ -125,25 +103,26 @@ func (c *Config) Validate() error {
|
|||||||
c.Seal.Argon2Threads = 4
|
c.Seal.Argon2Threads = 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log default.
|
||||||
if c.Log.Level == "" {
|
if c.Log.Level == "" {
|
||||||
c.Log.Level = "info"
|
c.Log.Level = "info"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply defaults for web server.
|
// Web default.
|
||||||
if c.Web.ListenAddr == "" {
|
if c.Web.ListenAddr == "" {
|
||||||
c.Web.ListenAddr = "127.0.0.1:8080"
|
c.Web.ListenAddr = "127.0.0.1:8080"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate audit config.
|
// Audit validation.
|
||||||
switch c.Audit.Mode {
|
switch c.Audit.Mode {
|
||||||
case "", "stdout":
|
case "", "stdout":
|
||||||
// ok
|
// ok
|
||||||
case "file":
|
case "file":
|
||||||
if c.Audit.Path == "" {
|
if c.Audit.Path == "" {
|
||||||
return fmt.Errorf("config: audit.path is required when audit.mode is \"file\"")
|
return fmt.Errorf("audit.path is required when audit.mode is \"file\"")
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("config: audit.mode must be \"file\", \"stdout\", or empty")
|
return fmt.Errorf("audit.mode must be \"file\", \"stdout\", or empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -41,6 +41,38 @@ server_url = "https://mcias.example.com"
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadEnvOverride(t *testing.T) {
|
||||||
|
content := `
|
||||||
|
[server]
|
||||||
|
listen_addr = ":8443"
|
||||||
|
tls_cert = "cert.pem"
|
||||||
|
tls_key = "key.pem"
|
||||||
|
|
||||||
|
[database]
|
||||||
|
path = "test.db"
|
||||||
|
|
||||||
|
[mcias]
|
||||||
|
server_url = "https://mcias.example.com"
|
||||||
|
`
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "test.toml")
|
||||||
|
_ = os.WriteFile(path, []byte(content), 0600)
|
||||||
|
|
||||||
|
t.Setenv("METACRYPT_SERVER_LISTEN_ADDR", ":9999")
|
||||||
|
t.Setenv("METACRYPT_MCIAS_SERVER_URL", "https://override.example.com")
|
||||||
|
|
||||||
|
cfg, err := Load(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Load: %v", err)
|
||||||
|
}
|
||||||
|
if cfg.Server.ListenAddr != ":9999" {
|
||||||
|
t.Errorf("ListenAddr env override: got %q, want %q", cfg.Server.ListenAddr, ":9999")
|
||||||
|
}
|
||||||
|
if cfg.MCIAS.ServerURL != "https://override.example.com" {
|
||||||
|
t.Errorf("ServerURL env override: got %q", cfg.MCIAS.ServerURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadMissingRequired(t *testing.T) {
|
func TestLoadMissingRequired(t *testing.T) {
|
||||||
content := `
|
content := `
|
||||||
[server]
|
[server]
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ import (
|
|||||||
"git.wntrmute.dev/kyle/metacrypt/internal/barrier"
|
"git.wntrmute.dev/kyle/metacrypt/internal/barrier"
|
||||||
"git.wntrmute.dev/kyle/metacrypt/internal/config"
|
"git.wntrmute.dev/kyle/metacrypt/internal/config"
|
||||||
"git.wntrmute.dev/kyle/metacrypt/internal/crypto"
|
"git.wntrmute.dev/kyle/metacrypt/internal/crypto"
|
||||||
|
|
||||||
|
mcdslauth "git.wntrmute.dev/kyle/mcdsl/auth"
|
||||||
|
mcdslconfig "git.wntrmute.dev/kyle/mcdsl/config"
|
||||||
"git.wntrmute.dev/kyle/metacrypt/internal/db"
|
"git.wntrmute.dev/kyle/metacrypt/internal/db"
|
||||||
"git.wntrmute.dev/kyle/metacrypt/internal/engine"
|
"git.wntrmute.dev/kyle/metacrypt/internal/engine"
|
||||||
"git.wntrmute.dev/kyle/metacrypt/internal/policy"
|
"git.wntrmute.dev/kyle/metacrypt/internal/policy"
|
||||||
@@ -46,12 +49,14 @@ func setupTestServer(t *testing.T) (*Server, *seal.Manager, chi.Router) {
|
|||||||
|
|
||||||
cfg := &config.Config{
|
cfg := &config.Config{
|
||||||
Server: config.ServerConfig{
|
Server: config.ServerConfig{
|
||||||
ListenAddr: ":0",
|
ServerConfig: mcdslconfig.ServerConfig{
|
||||||
TLSCert: "cert.pem",
|
ListenAddr: ":0",
|
||||||
TLSKey: "key.pem",
|
TLSCert: "cert.pem",
|
||||||
|
TLSKey: "key.pem",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Database: config.DatabaseConfig{Path: filepath.Join(dir, "test.db")},
|
Database: mcdslconfig.DatabaseConfig{Path: filepath.Join(dir, "test.db")},
|
||||||
MCIAS: config.MCIASConfig{ServerURL: "https://mcias.test"},
|
MCIAS: config.MCIASConfig{Config: mcdslauth.Config{ServerURL: "https://mcias.test"}},
|
||||||
Seal: config.SealConfig{
|
Seal: config.SealConfig{
|
||||||
Argon2Time: 1,
|
Argon2Time: 1,
|
||||||
Argon2Memory: 64 * 1024,
|
Argon2Memory: 64 * 1024,
|
||||||
|
|||||||
Reference in New Issue
Block a user