probod-bootstrap was writing empty strings and stub blocks such as
`esign: {}` into generated YAML. The post-marshal prune pass caused
part of that by stripping empty leaf strings while leaving empty
parent maps behind.
Drop the prune round-trip in WriteConfig and rely on struct-level
omitzero/omitempty tags plus custom IsZero() helpers on probodconfig.
Only include LLM providers when an API key is set, use a nil map for
extra API headers, and extend the dev-config Makefile recipe with the
local dev defaults already documented in .env.example.
Config loading is unchanged: omitted keys still decode to Go zero
values.
Signed-off-by: Ludovic Vielle <ludovic@probo.com>
433 lines
12 KiB
Go
433 lines
12 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package bootstrap
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.probo.inc/probo/pkg/probodconfig"
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
func TestWriteConfig(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Unit: probodconfig.UnitConfig{
|
|
Metrics: probodconfig.MetricsConfig{Addr: "localhost:9090"},
|
|
},
|
|
Probod: probodconfig.Config{
|
|
BaseURL: "http://localhost:8080",
|
|
EncryptionKey: "test-key",
|
|
},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
|
|
var loaded probodconfig.FullConfig
|
|
|
|
err = yaml.Unmarshal(data, &loaded)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, cfg.Unit.Metrics.Addr, loaded.Unit.Metrics.Addr)
|
|
assert.Equal(t, cfg.Probod.BaseURL, loaded.Probod.BaseURL)
|
|
assert.Equal(t, cfg.Probod.EncryptionKey, loaded.Probod.EncryptionKey)
|
|
}
|
|
|
|
func TestWriteConfig_CreatesDirectory(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "nested", "dir", "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Probod: probodconfig.Config{BaseURL: "http://localhost:8080"},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
_, err = os.Stat(configPath)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestWriteConfig_FilePermissions(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
info, err := os.Stat(configPath)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, os.FileMode(0600), info.Mode().Perm())
|
|
}
|
|
|
|
func TestWriteConfig_OmitsOptionalFields(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Unit: probodconfig.UnitConfig{
|
|
Metrics: probodconfig.MetricsConfig{Addr: "localhost:9090"},
|
|
Tracing: probodconfig.TracingConfig{Addr: ""},
|
|
},
|
|
Probod: probodconfig.Config{
|
|
BaseURL: "http://localhost:8080",
|
|
ChromeDPAddr: "",
|
|
Pg: probodconfig.PgConfig{
|
|
Addr: "localhost:5432",
|
|
Username: "postgres",
|
|
Password: "",
|
|
Database: "",
|
|
PoolSize: 100,
|
|
},
|
|
},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
|
|
var tree map[string]any
|
|
|
|
err = yaml.Unmarshal(data, &tree)
|
|
require.NoError(t, err)
|
|
|
|
probod, ok := tree["probod"].(map[string]any)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, "http://localhost:8080", probod["base-url"])
|
|
assert.NotContains(t, probod, "chrome-dp-addr")
|
|
assert.NotContains(t, probod, "esign")
|
|
|
|
pg, ok := probod["pg"].(map[string]any)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, "localhost:5432", pg["addr"])
|
|
assert.Equal(t, "postgres", pg["username"])
|
|
assert.NotContains(t, pg, "password")
|
|
assert.NotContains(t, pg, "database")
|
|
assert.Contains(t, pg, "pool-size")
|
|
|
|
unit, ok := tree["unit"].(map[string]any)
|
|
require.True(t, ok)
|
|
|
|
metrics, ok := unit["metrics"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "localhost:9090", metrics["addr"])
|
|
|
|
tracing, ok := unit["tracing"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, tracing, "addr")
|
|
}
|
|
|
|
func TestWriteConfig_OmitsEmptyOptionalBlocks(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Probod: probodconfig.Config{
|
|
BaseURL: "http://localhost:8080",
|
|
EncryptionKey: "test-key",
|
|
Api: probodconfig.APIConfig{
|
|
Addr: ":8080",
|
|
},
|
|
Auth: probodconfig.AuthConfig{
|
|
Cookie: probodconfig.CookieConfig{
|
|
Name: "SSID",
|
|
Secret: "secret",
|
|
},
|
|
Password: probodconfig.PasswordConfig{
|
|
Pepper: "pepper",
|
|
},
|
|
},
|
|
TrustCenter: probodconfig.TrustCenterConfig{
|
|
HTTPAddr: ":80",
|
|
},
|
|
CustomDomains: probodconfig.CustomDomainsConfig{
|
|
RenewalInterval: 3600,
|
|
},
|
|
Agents: probodconfig.AgentsConfig{
|
|
Default: probodconfig.LLMAgentConfig{
|
|
Provider: "openai",
|
|
ModelName: "gpt-4o",
|
|
},
|
|
ThirdPartyDisambiguation: probodconfig.LLMAgentConfig{
|
|
MaxTokens: new(4096),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
|
|
var tree map[string]any
|
|
|
|
err = yaml.Unmarshal(data, &tree)
|
|
require.NoError(t, err)
|
|
|
|
probod, ok := tree["probod"].(map[string]any)
|
|
require.True(t, ok)
|
|
|
|
assert.NotContains(t, probod, "esign")
|
|
|
|
api, ok := probod["api"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, api, "cors")
|
|
assert.NotContains(t, api, "proxy-protocol")
|
|
assert.NotContains(t, api, "extra-header-fields")
|
|
|
|
auth, ok := probod["auth"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, auth, "google")
|
|
assert.NotContains(t, auth, "microsoft")
|
|
|
|
customDomains, ok := probod["custom-domains"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, customDomains, "acme")
|
|
|
|
trustCenter, ok := probod["trust-center"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, trustCenter, "proxy-protocol")
|
|
|
|
llm, ok := probod["llm"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, llm, "probo")
|
|
assert.NotContains(t, llm, "third-party-disambiguation")
|
|
assert.NotContains(t, llm, "tools")
|
|
}
|
|
|
|
func TestWriteConfig_OmitsEmptyProxyProtocolAndCorsSlices(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Probod: probodconfig.Config{
|
|
BaseURL: "http://localhost:8080",
|
|
Api: probodconfig.APIConfig{
|
|
Addr: ":8080",
|
|
ProxyProtocol: probodconfig.ProxyProtocolConfig{
|
|
TrustedProxies: []string{},
|
|
},
|
|
Cors: probodconfig.CorsConfig{
|
|
AllowedOrigins: []string{},
|
|
},
|
|
},
|
|
TrustCenter: probodconfig.TrustCenterConfig{
|
|
HTTPAddr: ":10080",
|
|
ProxyProtocol: probodconfig.ProxyProtocolConfig{
|
|
TrustedProxies: make([]string, 0),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
|
|
var tree map[string]any
|
|
|
|
err = yaml.Unmarshal(data, &tree)
|
|
require.NoError(t, err)
|
|
|
|
probod, ok := tree["probod"].(map[string]any)
|
|
require.True(t, ok)
|
|
|
|
api, ok := probod["api"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, api, "proxy-protocol")
|
|
assert.NotContains(t, api, "cors")
|
|
|
|
trustCenter, ok := probod["trust-center"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, trustCenter, "proxy-protocol")
|
|
}
|
|
|
|
func TestWriteConfig_OmitsEmptyExtraHeaderFieldsMap(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Probod: probodconfig.Config{
|
|
BaseURL: "http://localhost:8080",
|
|
Api: probodconfig.APIConfig{
|
|
Addr: ":8080",
|
|
ExtraHeaderFields: map[string]string{},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
|
|
var tree map[string]any
|
|
|
|
err = yaml.Unmarshal(data, &tree)
|
|
require.NoError(t, err)
|
|
|
|
probod, ok := tree["probod"].(map[string]any)
|
|
require.True(t, ok)
|
|
|
|
api, ok := probod["api"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.NotContains(t, api, "extra-header-fields")
|
|
}
|
|
|
|
func TestWriteConfig_CompleteConfig(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.yml")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Unit: probodconfig.UnitConfig{
|
|
Metrics: probodconfig.MetricsConfig{Addr: "localhost:8081"},
|
|
Tracing: probodconfig.TracingConfig{
|
|
Addr: "localhost:4317",
|
|
MaxBatchSize: 512,
|
|
BatchTimeout: 5,
|
|
ExportTimeout: 30,
|
|
MaxQueueSize: 2048,
|
|
},
|
|
},
|
|
Probod: probodconfig.Config{
|
|
BaseURL: "http://localhost:8080",
|
|
EncryptionKey: "test-key",
|
|
ChromeDPAddr: "localhost:9222",
|
|
Api: probodconfig.APIConfig{
|
|
Addr: ":8080",
|
|
Cors: probodconfig.CorsConfig{
|
|
AllowedOrigins: []string{"http://localhost:8080"},
|
|
},
|
|
},
|
|
Pg: probodconfig.PgConfig{
|
|
Addr: "localhost:5432",
|
|
Username: "postgres",
|
|
Password: "postgres",
|
|
Database: "probod",
|
|
PoolSize: 100,
|
|
MinPoolSize: 10,
|
|
MaxConnIdleTimeSeconds: 1800,
|
|
MaxConnLifetimeSeconds: 3600,
|
|
},
|
|
Connectors: []probodconfig.ConnectorConfig{
|
|
{
|
|
Provider: "slack",
|
|
Protocol: "oauth2",
|
|
RawConfig: probodconfig.ConnectorConfigOAuth2{
|
|
ClientID: "client-id",
|
|
ClientSecret: "client-secret",
|
|
},
|
|
RawSettings: map[string]any{
|
|
"signing-secret": "secret",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatYAML)
|
|
require.NoError(t, err)
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
|
|
var loaded probodconfig.FullConfig
|
|
|
|
err = yaml.Unmarshal(data, &loaded)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, cfg.Unit.Metrics.Addr, loaded.Unit.Metrics.Addr)
|
|
assert.Equal(t, cfg.Unit.Tracing.MaxBatchSize, loaded.Unit.Tracing.MaxBatchSize)
|
|
assert.Equal(t, cfg.Probod.Api.Cors.AllowedOrigins, loaded.Probod.Api.Cors.AllowedOrigins)
|
|
assert.Equal(t, cfg.Probod.Pg.PoolSize, loaded.Probod.Pg.PoolSize)
|
|
assert.Equal(t, cfg.Probod.Pg.MinPoolSize, loaded.Probod.Pg.MinPoolSize)
|
|
assert.Equal(t, cfg.Probod.Pg.MaxConnIdleTimeSeconds, loaded.Probod.Pg.MaxConnIdleTimeSeconds)
|
|
assert.Equal(t, cfg.Probod.Pg.MaxConnLifetimeSeconds, loaded.Probod.Pg.MaxConnLifetimeSeconds)
|
|
require.Len(t, loaded.Probod.Connectors, 1)
|
|
assert.Equal(t, "SLACK", loaded.Probod.Connectors[0].Provider)
|
|
}
|
|
|
|
func TestWriteConfig_JSON(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.json")
|
|
|
|
cfg := &probodconfig.FullConfig{
|
|
Unit: probodconfig.UnitConfig{
|
|
Metrics: probodconfig.MetricsConfig{Addr: "localhost:9090"},
|
|
},
|
|
Probod: probodconfig.Config{
|
|
BaseURL: "http://localhost:8080",
|
|
EncryptionKey: "",
|
|
},
|
|
}
|
|
|
|
err := WriteConfig(cfg, configPath, FormatJSON)
|
|
require.NoError(t, err)
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
|
|
var tree map[string]any
|
|
|
|
err = json.Unmarshal(data, &tree)
|
|
require.NoError(t, err)
|
|
|
|
probod, ok := tree["probod"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "http://localhost:8080", probod["base-url"])
|
|
assert.Equal(t, "", probod["encryption-key"])
|
|
|
|
var loaded probodconfig.FullConfig
|
|
|
|
err = json.Unmarshal(data, &loaded)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, cfg.Unit.Metrics.Addr, loaded.Unit.Metrics.Addr)
|
|
assert.Equal(t, cfg.Probod.BaseURL, loaded.Probod.BaseURL)
|
|
}
|
|
|
|
func TestWriteConfig_UnsupportedFormat(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "probod.txt")
|
|
|
|
cfg := &probodconfig.FullConfig{}
|
|
|
|
err := WriteConfig(cfg, configPath, Format("toml"))
|
|
require.Error(t, err)
|
|
}
|