PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
REFACTOR (agent): Drive verification-agent config flags from a binding registry
Rostislav Dugin33ea9e73d872
diff --git a/agent/verification/internal/config/config.go b/agent/verification/internal/config/config.goindex 5ac17e6d..1be13ad3 100644--- a/agent/verification/internal/config/config.go+++ b/agent/verification/internal/config/config.go@@ -34,7 +34,7 @@ type Config struct {AllowInsecureHTTP bool `json:"allowInsecureHttp"`VerificationPgImageRepo string `json:"verificationPgImageRepo"`- flags parsedFlags+ flagSources map[string]string}func (c *Config) GetVerificationPgImageRepo() string {@@ -59,38 +59,25 @@ type Capacity struct {// and overrides JSON values with any explicitly provided CLI flags.func (c *Config) LoadFromJSONAndArgs(fs *flag.FlagSet, args []string) {c.loadFromJSON()- c.initSources()-- c.flags.databasusHost = fs.String(- "databasus-host",- "",- "Databasus server URL (e.g. https://your-server:4005)",- )- c.flags.agentID = fs.String("agent-id", "", "Verification agent ID")- c.flags.token = fs.String("token", "", "Verification agent token")- c.flags.maxCPU = fs.Int("max-cpu", 0, "Total CPU cores available to the agent")- c.flags.maxRAMMb = fs.Int("max-ram-mb", 0, "Total RAM in MB available to the agent")- c.flags.maxDiskGb = fs.Int("max-disk-gb", 0, "Total scratch disk in GB available to the agent")- c.flags.maxConcurrentJobs = fs.Int("max-concurrent-jobs", 0, "Number of verifications to run in parallel")- c.flags.allowInsecureHTTP = fs.Bool(- "allow-insecure-http",- false,- "Permit a plain http:// databasus-host (token and backup data sent unencrypted)",- )- c.flags.verificationPgImageRepo = fs.String(- "verification-pg-image-repo",- "",- "Container image repo bundling PostgreSQL extensions for verification "+- "(default "+DefaultVerificationPgImageRepo+"; set to 'postgres' for the stock image)",- )++ bindings := c.flagBindings()+ c.flagSources = sourcesFromConfig(bindings, c)++ appliers := make([]func(*Config, map[string]string), 0, len(bindings))+ for _, binding := range bindings {+ appliers = append(appliers, binding.register(fs))+ }if err := fs.Parse(args); err != nil {os.Exit(1)}- c.applyFlags()+ for _, apply := range appliers {+ apply(c, c.flagSources)+ }+log.Info("========= Loading config ============")- c.logConfigSources()+ c.logConfigSources(bindings)log.Info("========= Config has been loaded ====")}@@ -217,136 +204,6 @@ func (c *Config) loadFromJSON() {log.Info("Configuration loaded from " + configFileName)}-func (c *Config) initSources() {- c.flags.sources = map[string]string{- "databasus-host": "not configured",- "agent-id": "not configured",- "token": "not configured",- "max-cpu": "not configured",- "max-ram-mb": "not configured",- "max-disk-gb": "not configured",- "max-concurrent-jobs": "not configured",- "allow-insecure-http": "not configured",- "verification-pg-image-repo": "not configured",- }-- if c.DatabasusHost != "" {- c.flags.sources["databasus-host"] = configFileName- }-- if c.AgentID != "" {- c.flags.sources["agent-id"] = configFileName- }-- if c.Token != "" {- c.flags.sources["token"] = configFileName- }-- if c.MaxCPU != 0 {- c.flags.sources["max-cpu"] = configFileName- }-- if c.MaxRAMMb != 0 {- c.flags.sources["max-ram-mb"] = configFileName- }-- if c.MaxDiskGb != 0 {- c.flags.sources["max-disk-gb"] = configFileName- }-- if c.MaxConcurrentJobs != 0 {- c.flags.sources["max-concurrent-jobs"] = configFileName- }-- if c.AllowInsecureHTTP {- c.flags.sources["allow-insecure-http"] = configFileName- }-- if c.VerificationPgImageRepo != "" {- c.flags.sources["verification-pg-image-repo"] = configFileName- }-}--func (c *Config) applyFlags() {- if c.flags.databasusHost != nil && *c.flags.databasusHost != "" {- c.DatabasusHost = *c.flags.databasusHost- c.flags.sources["databasus-host"] = "command line args"- }-- if c.flags.agentID != nil && *c.flags.agentID != "" {- c.AgentID = *c.flags.agentID- c.flags.sources["agent-id"] = "command line args"- }-- if c.flags.token != nil && *c.flags.token != "" {- c.Token = *c.flags.token- c.flags.sources["token"] = "command line args"- }-- if c.flags.maxCPU != nil && *c.flags.maxCPU != 0 {- c.MaxCPU = *c.flags.maxCPU- c.flags.sources["max-cpu"] = "command line args"- }-- if c.flags.maxRAMMb != nil && *c.flags.maxRAMMb != 0 {- c.MaxRAMMb = *c.flags.maxRAMMb- c.flags.sources["max-ram-mb"] = "command line args"- }-- if c.flags.maxDiskGb != nil && *c.flags.maxDiskGb != 0 {- c.MaxDiskGb = *c.flags.maxDiskGb- c.flags.sources["max-disk-gb"] = "command line args"- }-- if c.flags.maxConcurrentJobs != nil && *c.flags.maxConcurrentJobs != 0 {- c.MaxConcurrentJobs = *c.flags.maxConcurrentJobs- c.flags.sources["max-concurrent-jobs"] = "command line args"- }-- // --allow-insecure-http is a presence flag: passing it (or a persisted- // true) latches on; it is never turned back off from the CLI so a- // restart under systemd does not re-prompt.- if c.flags.allowInsecureHTTP != nil && *c.flags.allowInsecureHTTP {- c.AllowInsecureHTTP = true- c.flags.sources["allow-insecure-http"] = "command line args"- }-- if c.flags.verificationPgImageRepo != nil && *c.flags.verificationPgImageRepo != "" {- c.VerificationPgImageRepo = *c.flags.verificationPgImageRepo- c.flags.sources["verification-pg-image-repo"] = "command line args"- }-}--func (c *Config) logConfigSources() {- log.Info("databasus-host", "value", c.DatabasusHost, "source", c.flags.sources["databasus-host"])- log.Info("agent-id", "value", c.AgentID, "source", c.flags.sources["agent-id"])- log.Info("token", "value", maskSensitive(c.Token), "source", c.flags.sources["token"])- log.Info("max-cpu", "value", c.MaxCPU, "source", c.flags.sources["max-cpu"])- log.Info("max-ram-mb", "value", c.MaxRAMMb, "source", c.flags.sources["max-ram-mb"])- log.Info("max-disk-gb", "value", c.MaxDiskGb, "source", c.flags.sources["max-disk-gb"])- log.Info(- "max-concurrent-jobs",- "value",- c.MaxConcurrentJobs,- "source",- c.flags.sources["max-concurrent-jobs"],- )- log.Info(- "allow-insecure-http",- "value",- fmt.Sprintf("%v", c.AllowInsecureHTTP),- "source",- c.flags.sources["allow-insecure-http"],- )- log.Info(- "verification-pg-image-repo",- "value",- c.GetVerificationPgImageRepo(),- "source",- c.flags.sources["verification-pg-image-repo"],- )-}-func (c *Config) consentToInsecureHTTP(isStdinTTY bool, in *bufio.Reader) error {if c.AllowInsecureHTTP {log.Warn("databasus-host is plain HTTP; transport is unencrypted")diff --git a/agent/verification/internal/config/config_test.go b/agent/verification/internal/config/config_test.goindex 4b82b20d..074dac8a 100644--- a/agent/verification/internal/config/config_test.go+++ b/agent/verification/internal/config/config_test.go@@ -2,6 +2,7 @@ package configimport ("bufio"+ "flag""strings""testing"@@ -9,6 +10,20 @@ import ("github.com/stretchr/testify/require")+func loadConfig(t *testing.T, args ...string) *Config {+ t.Helper()++ cfg := &Config{}+ cfg.LoadFromJSONAndArgs(flag.NewFlagSet("test", flag.ContinueOnError), args)++ return cfg+}++var allFlagNames = []string{+ "databasus-host", "agent-id", "token", "max-cpu", "max-ram-mb",+ "max-disk-gb", "max-concurrent-jobs", "allow-insecure-http", "verification-pg-image-repo",+}+func emptyReader() *bufio.Reader {return bufio.NewReader(strings.NewReader(""))}@@ -186,15 +201,90 @@ func Test_GetVerificationPgImageRepo_WhenSet_ReturnsConfiguredValue(t *testing.Tassert.Equal(t, "registry.internal/pg-verify", cfg.GetVerificationPgImageRepo())}-func Test_ApplyFlags_WhenImageRepoFlagSet_OverridesFileValue(t *testing.T) {- cfg := &Config{VerificationPgImageRepo: "from-file"}- cfg.initSources()+func Test_LoadFromJSONAndArgs_WhenFlagsProvided_OverrideEveryValue(t *testing.T) {+ t.Chdir(t.TempDir())++ cfg := loadConfig(t,+ "--databasus-host", "https://flag:4005",+ "--agent-id", "agent-x",+ "--token", "tok",+ "--max-cpu", "8",+ "--max-ram-mb", "4096",+ "--max-disk-gb", "100",+ "--max-concurrent-jobs", "4",+ "--allow-insecure-http",+ "--verification-pg-image-repo", "registry/pg",+ )++ assert.Equal(t, "https://flag:4005", cfg.DatabasusHost)+ assert.Equal(t, "agent-x", cfg.AgentID)+ assert.Equal(t, "tok", cfg.Token)+ assert.Equal(t, 8, cfg.MaxCPU)+ assert.Equal(t, 4096, cfg.MaxRAMMb)+ assert.Equal(t, 100, cfg.MaxDiskGb)+ assert.Equal(t, 4, cfg.MaxConcurrentJobs)+ assert.True(t, cfg.AllowInsecureHTTP)+ assert.Equal(t, "registry/pg", cfg.VerificationPgImageRepo)++ for _, name := range allFlagNames {+ assert.Equal(t, "command line args", cfg.flagSources[name], name)+ }+}++func Test_LoadFromJSONAndArgs_WhenOnlyFilePresent_LoadsFileAndMarksSource(t *testing.T) {+ t.Chdir(t.TempDir())+ source := &Config{DatabasusHost: "https://file:4005", AgentID: "agent-file", MaxCPU: 6}+ require.NoError(t, source.SaveToJSON())++ cfg := loadConfig(t)++ assert.Equal(t, "https://file:4005", cfg.DatabasusHost)+ assert.Equal(t, "agent-file", cfg.AgentID)+ assert.Equal(t, 6, cfg.MaxCPU)+ assert.Equal(t, configFileName, cfg.flagSources["databasus-host"])+}++func Test_LoadFromJSONAndArgs_WhenFlagAndFile_FlagOverridesFile(t *testing.T) {+ t.Chdir(t.TempDir())+ source := &Config{DatabasusHost: "https://file:4005", AgentID: "agent-file"}+ require.NoError(t, source.SaveToJSON())++ cfg := loadConfig(t, "--databasus-host", "https://flag:4005")++ assert.Equal(t, "https://flag:4005", cfg.DatabasusHost, "flag overrides file")+ assert.Equal(t, "agent-file", cfg.AgentID, "untouched field keeps the file value")+ assert.Equal(t, "command line args", cfg.flagSources["databasus-host"])+ assert.Equal(t, configFileName, cfg.flagSources["agent-id"])+}- flagValue := "from-flag"- cfg.flags.verificationPgImageRepo = &flagValue+func Test_LoadFromJSONAndArgs_WhenZeroValuedFlags_DoNotOverrideFile(t *testing.T) {+ t.Chdir(t.TempDir())+ source := &Config{MaxCPU: 8, VerificationPgImageRepo: "registry/pg"}+ require.NoError(t, source.SaveToJSON())++ cfg := loadConfig(t, "--max-cpu", "0", "--verification-pg-image-repo", "")++ assert.Equal(t, 8, cfg.MaxCPU, "an explicit zero flag cannot unset a file value")+ assert.Equal(t, "registry/pg", cfg.VerificationPgImageRepo)+}- cfg.applyFlags()+func Test_LoadFromJSONAndArgs_WhenAllowInsecureHTTPInFile_StaysTrueWithoutFlag(t *testing.T) {+ t.Chdir(t.TempDir())+ source := &Config{AllowInsecureHTTP: true}+ require.NoError(t, source.SaveToJSON())- assert.Equal(t, "from-flag", cfg.VerificationPgImageRepo)- assert.Equal(t, "command line args", cfg.flags.sources["verification-pg-image-repo"])+ cfg := loadConfig(t)++ assert.True(t, cfg.AllowInsecureHTTP)+ assert.Equal(t, configFileName, cfg.flagSources["allow-insecure-http"])+}++func Test_LoadFromJSONAndArgs_WhenNothingProvided_AllSourcesNotConfigured(t *testing.T) {+ t.Chdir(t.TempDir())++ cfg := loadConfig(t)++ for _, name := range allFlagNames {+ assert.Equal(t, "not configured", cfg.flagSources[name], name)+ }}diff --git a/agent/verification/internal/config/dto.go b/agent/verification/internal/config/dto.godeleted file mode 100644index 2b2c8148..00000000--- a/agent/verification/internal/config/dto.go+++ /dev/null@@ -1,15 +0,0 @@-package config--type parsedFlags struct {- databasusHost *string- agentID *string- token *string- maxCPU *int- maxRAMMb *int- maxDiskGb *int- maxConcurrentJobs *int- allowInsecureHTTP *bool- verificationPgImageRepo *string-- sources map[string]string-}diff --git a/agent/verification/internal/config/flags.go b/agent/verification/internal/config/flags.gonew file mode 100644index 00000000..0424ed3c--- /dev/null+++ b/agent/verification/internal/config/flags.go@@ -0,0 +1,166 @@+package config++import "flag"++const (+ sourceNotConfigured = "not configured"+ sourceCommandLine = "command line args"+)++// flagBinding ties one CLI flag to its Config field for the whole lifecycle —+// registration, applying an explicitly-set value, source attribution, and+// logging. Collecting these per flag keeps adding a flag a one-entry change+// instead of edits across four parallel blocks that silently drift apart.+type flagBinding struct {+ name string+ usage string++ // register declares the flag on fs and returns an applier that copies an+ // explicitly-set value onto c and records the command-line source. A+ // zero/empty flag is treated as "not provided" — it never overrides nor+ // unsets a value already loaded from JSON.+ register func(fs *flag.FlagSet) func(c *Config, sources map[string]string)++ // isSet reports whether c already carries a value (loaded from JSON).+ isSet func(c *Config) bool++ // value is the loggable value, masked for secrets.+ value func(c *Config) any+}++func stringBinding(+ name, usage string,+ get func(*Config) string,+ set func(*Config, string),+) flagBinding {+ return flagBinding{+ name: name,+ usage: usage,+ register: func(fs *flag.FlagSet) func(*Config, map[string]string) {+ value := fs.String(name, "", usage)++ return func(c *Config, sources map[string]string) {+ if *value != "" {+ set(c, *value)+ sources[name] = sourceCommandLine+ }+ }+ },+ isSet: func(c *Config) bool { return get(c) != "" },+ value: func(c *Config) any { return get(c) },+ }+}++func secretBinding(+ name, usage string,+ get func(*Config) string,+ set func(*Config, string),+) flagBinding {+ binding := stringBinding(name, usage, get, set)+ binding.value = func(c *Config) any { return maskSensitive(get(c)) }++ return binding+}++func intBinding(+ name, usage string,+ get func(*Config) int,+ set func(*Config, int),+) flagBinding {+ return flagBinding{+ name: name,+ usage: usage,+ register: func(fs *flag.FlagSet) func(*Config, map[string]string) {+ value := fs.Int(name, 0, usage)++ return func(c *Config, sources map[string]string) {+ if *value != 0 {+ set(c, *value)+ sources[name] = sourceCommandLine+ }+ }+ },+ isSet: func(c *Config) bool { return get(c) != 0 },+ value: func(c *Config) any { return get(c) },+ }+}++// boolBinding latches: the flag only ever turns the value on, so a persisted+// true is never flipped back off by its absence on the command line.+func boolBinding(+ name, usage string,+ get func(*Config) bool,+ set func(*Config, bool),+) flagBinding {+ return flagBinding{+ name: name,+ usage: usage,+ register: func(fs *flag.FlagSet) func(*Config, map[string]string) {+ value := fs.Bool(name, false, usage)++ return func(c *Config, sources map[string]string) {+ if *value {+ set(c, true)+ sources[name] = sourceCommandLine+ }+ }+ },+ isSet: func(c *Config) bool { return get(c) },+ value: func(c *Config) any { return get(c) },+ }+}++func (c *Config) flagBindings() []flagBinding {+ return []flagBinding{+ stringBinding("databasus-host", "Databasus server URL (e.g. https://your-server:4005)",+ func(c *Config) string { return c.DatabasusHost },+ func(c *Config, v string) { c.DatabasusHost = v }),+ stringBinding("agent-id", "Verification agent ID",+ func(c *Config) string { return c.AgentID },+ func(c *Config, v string) { c.AgentID = v }),+ secretBinding("token", "Verification agent token",+ func(c *Config) string { return c.Token },+ func(c *Config, v string) { c.Token = v }),+ intBinding("max-cpu", "Total CPU cores available to the agent",+ func(c *Config) int { return c.MaxCPU },+ func(c *Config, v int) { c.MaxCPU = v }),+ intBinding("max-ram-mb", "Total RAM in MB available to the agent",+ func(c *Config) int { return c.MaxRAMMb },+ func(c *Config, v int) { c.MaxRAMMb = v }),+ intBinding("max-disk-gb", "Total scratch disk in GB available to the agent",+ func(c *Config) int { return c.MaxDiskGb },+ func(c *Config, v int) { c.MaxDiskGb = v }),+ intBinding("max-concurrent-jobs", "Number of verifications to run in parallel",+ func(c *Config) int { return c.MaxConcurrentJobs },+ func(c *Config, v int) { c.MaxConcurrentJobs = v }),+ boolBinding("allow-insecure-http",+ "Permit a plain http:// databasus-host (token and backup data sent unencrypted)",+ func(c *Config) bool { return c.AllowInsecureHTTP },+ func(c *Config, v bool) { c.AllowInsecureHTTP = v }),+ stringBinding("verification-pg-image-repo",+ "Container image repo bundling PostgreSQL extensions for verification "++ "(default "+DefaultVerificationPgImageRepo+"; set to 'postgres' for the stock image)",+ func(c *Config) string { return c.VerificationPgImageRepo },+ func(c *Config, v string) { c.VerificationPgImageRepo = v }),+ }+}++func sourcesFromConfig(bindings []flagBinding, c *Config) map[string]string {+ sources := make(map[string]string, len(bindings))++ for _, binding := range bindings {+ if binding.isSet(c) {+ sources[binding.name] = configFileName+ } else {+ sources[binding.name] = sourceNotConfigured+ }+ }++ return sources+}++func (c *Config) logConfigSources(bindings []flagBinding) {+ for _, binding := range bindings {+ log.Info(binding.name, "value", binding.value(c), "source", c.flagSources[binding.name])+ }+}