2026-01-01 12:33:52 +05:30
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-05-21 16:37:26 +05:30
|
|
|
"github.com/safedep/dry/log"
|
2026-01-01 12:33:52 +05:30
|
|
|
"github.com/spf13/viper"
|
2026-05-06 18:27:23 +05:30
|
|
|
"gopkg.in/yaml.v3"
|
2026-01-01 12:33:52 +05:30
|
|
|
)
|
|
|
|
|
|
2026-04-17 01:13:30 +05:30
|
|
|
// loadViperConfig loads the configuration using Viper.
|
|
|
|
|
// Precedence (highest to lowest): cobra flags > env vars > config file > defaults.
|
2026-05-21 16:37:26 +05:30
|
|
|
// When a globally managed config is active, env overrides are disabled and
|
|
|
|
|
// managed flags are rejected, so the managed file is authoritative.
|
2026-04-17 01:13:30 +05:30
|
|
|
// Cobra flags write directly to the config struct after this function runs.
|
2026-03-31 19:21:01 +05:30
|
|
|
func loadViperConfig() error {
|
2026-05-21 14:32:30 +05:30
|
|
|
// Use the active config path resolved by initConfig (globally managed file
|
|
|
|
|
// when present, otherwise the per-user file).
|
|
|
|
|
configPath := globalConfig.configFilePath
|
2026-01-01 12:33:52 +05:30
|
|
|
|
|
|
|
|
v := viper.New()
|
|
|
|
|
v.SetConfigType("yaml")
|
2026-05-21 16:37:26 +05:30
|
|
|
|
|
|
|
|
// A locked global config must not be bypassable via PMG_* env vars, so
|
|
|
|
|
// AutomaticEnv is enabled unless lockdown is in force. An unlocked managed
|
|
|
|
|
// config stays an overridable baseline.
|
|
|
|
|
if !globalConfig.IsLocked() {
|
|
|
|
|
v.SetEnvPrefix("PMG")
|
|
|
|
|
v.AutomaticEnv()
|
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
|
|
|
|
|
}
|
2026-01-01 12:33:52 +05:30
|
|
|
|
2026-04-17 01:13:30 +05:30
|
|
|
// Load the embedded template as the base so Viper knows all keys and their
|
2026-05-21 16:37:26 +05:30
|
|
|
// defaults, and (when env overrides are enabled) can resolve PMG_* vars for
|
2026-04-17 01:13:30 +05:30
|
|
|
// keys that are absent from or newer than the user's config file.
|
|
|
|
|
if err := v.ReadConfig(strings.NewReader(templateConfig)); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to load default config: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Merge user config on top if it exists.
|
|
|
|
|
if _, statErr := os.Stat(configPath); statErr == nil {
|
|
|
|
|
v.SetConfigFile(configPath)
|
|
|
|
|
if err := v.MergeInConfig(); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to read config file %s: %w", configPath, err)
|
|
|
|
|
}
|
2026-01-01 12:33:52 +05:30
|
|
|
}
|
|
|
|
|
|
2026-04-08 21:04:17 +05:30
|
|
|
merged := globalConfig.Config
|
|
|
|
|
if err := v.Unmarshal(&merged); err != nil {
|
2026-03-31 19:21:01 +05:30
|
|
|
return fmt.Errorf("failed to unmarshal config: %w", err)
|
2026-01-01 12:33:52 +05:30
|
|
|
}
|
|
|
|
|
|
2026-04-08 21:04:17 +05:30
|
|
|
globalConfig.Config = merged
|
2026-05-16 09:55:54 +05:30
|
|
|
globalConfig.viper = v
|
2026-05-06 18:27:23 +05:30
|
|
|
|
|
|
|
|
// Resolve proxy config: new proxy section > legacy flat keys.
|
|
|
|
|
// Viper can't distinguish "value from template" vs "value from user config"
|
|
|
|
|
// (v.IsSet is always true for template keys), so we check the raw user file.
|
|
|
|
|
if !hasProxySectionInFile(configPath) {
|
|
|
|
|
applyProxyLegacyFallback(v)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 19:21:01 +05:30
|
|
|
return nil
|
2026-01-01 12:33:52 +05:30
|
|
|
}
|
2026-05-06 18:27:23 +05:30
|
|
|
|
2026-05-21 16:37:26 +05:30
|
|
|
// readConfigFileKeys reads path and returns its top-level YAML mapping.
|
|
|
|
|
func readConfigFileKeys(path string) (map[string]any, error) {
|
2026-05-06 18:27:23 +05:30
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
2026-05-21 16:37:26 +05:30
|
|
|
return nil, err
|
2026-05-06 18:27:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var raw map[string]any
|
|
|
|
|
if err := yaml.Unmarshal(data, &raw); err != nil {
|
2026-05-21 16:37:26 +05:30
|
|
|
return nil, fmt.Errorf("failed to parse %s: %w", path, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return raw, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hasProxySectionInFile checks whether the config file at path contains a
|
|
|
|
|
// top-level "proxy" key. A missing or unparseable file reports false.
|
|
|
|
|
func hasProxySectionInFile(path string) bool {
|
|
|
|
|
raw, err := readConfigFileKeys(path)
|
|
|
|
|
if err != nil {
|
2026-05-06 18:27:23 +05:30
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, ok := raw["proxy"]
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 16:37:26 +05:30
|
|
|
// globalConfigEnablesLockdown reports whether the global config file at path
|
|
|
|
|
// enables lockdown. It is only called when a global config is present, so a read
|
|
|
|
|
// or parse failure means a managed file we cannot interpret: fail closed
|
|
|
|
|
// (locked) rather than silently dropping policy. global_lockdown is read directly
|
|
|
|
|
// from the file, so it cannot be flipped via env or CLI.
|
|
|
|
|
func globalConfigEnablesLockdown(path string) bool {
|
|
|
|
|
raw, err := readConfigFileKeys(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warnf("could not read global config %q to determine lockdown (%v); defaulting to locked", path, err)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value, ok := raw["global_lockdown"]
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enabled, isBool := value.(bool)
|
|
|
|
|
if !isBool {
|
|
|
|
|
log.Warnf("config %q sets global_lockdown to a non-boolean value (%v); treating as disabled", path, value)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enabled
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 18:27:23 +05:30
|
|
|
// applyProxyLegacyFallback populates the new Proxy struct from deprecated
|
|
|
|
|
// flat keys when the user's config file does not have a proxy: section.
|
|
|
|
|
// New env vars (PMG_PROXY_ENABLED, PMG_PROXY_INSTALL_ONLY) take precedence
|
|
|
|
|
// over legacy config file keys to respect the documented precedence order.
|
|
|
|
|
func applyProxyLegacyFallback(v *viper.Viper) {
|
2026-05-21 16:37:26 +05:30
|
|
|
// A locked config ignores env, so env must not suppress the legacy migration.
|
|
|
|
|
envIgnored := globalConfig.IsLocked()
|
|
|
|
|
|
|
|
|
|
if (envIgnored || os.Getenv("PMG_PROXY_ENABLED") == "") && v.IsSet("proxy_mode") {
|
2026-05-16 09:55:54 +05:30
|
|
|
val := v.GetBool("proxy_mode")
|
|
|
|
|
globalConfig.Config.Proxy.Enabled = val
|
|
|
|
|
v.Set("proxy.enabled", val)
|
2026-05-06 18:27:23 +05:30
|
|
|
}
|
|
|
|
|
|
2026-05-21 16:37:26 +05:30
|
|
|
if (envIgnored || os.Getenv("PMG_PROXY_INSTALL_ONLY") == "") && v.IsSet("proxy_install_only") {
|
2026-05-16 09:55:54 +05:30
|
|
|
val := v.GetBool("proxy_install_only")
|
|
|
|
|
globalConfig.Config.Proxy.InstallOnly = val
|
|
|
|
|
v.Set("proxy.install_only", val)
|
2026-05-06 18:27:23 +05:30
|
|
|
}
|
|
|
|
|
}
|