// SPDX-License-Identifier: GPL-3.0-or-later // Package config resolves hub settings: defaults, then ~/.config/security-hub/hub.json, then environment. package config import ( "encoding/json" "errors" "fmt" "os" "path/filepath" "strings" ) // HubDirName is the directory of the Security Hub workspace inside the repository root. const HubDirName = "salt-security-hub" // Config holds the resolved settings. Token is never part of it: only the path to the token file is. type Config struct { FleetURL string `json:"fleet_url"` TokenFile string `json:"token_file"` RepoPath string `json:"repo_path"` // repository root (contains hosts, local/, salt-security-hub/) Inventory string `json:"inventory"` // Ansible INI inventory; default /hosts // ManagedPathsFile lists absolute paths Salt already installs, one per line (`hub suggest` does not offer them // as candidates). Empty: the built-in default list. ManagedPathsFile string `json:"managed_paths_file"` // UpdateURL and UpdateRepo choose the forge that `hub self-update` reads releases from. UpdateURL string `json:"update_url"` UpdateRepo string `json:"update_repo"` ConfigDir string `json:"-"` // directory of hub.json, for messages Sources []string `json:"-"` // which layers contributed, for `hub version -v` style debugging } // Env abstracts the environment so tests do not touch the process environment. type Env struct { Getenv func(string) string Home string Cwd string } // OSEnv returns the real process environment. func OSEnv() Env { home, _ := os.UserHomeDir() cwd, _ := os.Getwd() return Env{Getenv: os.Getenv, Home: home, Cwd: cwd} } // ConfigPath returns the config file location (HUB_CONFIG overrides the default). func ConfigPath(env Env) string { if p := env.Getenv("HUB_CONFIG"); p != "" { return p } return filepath.Join(env.Home, ".config", "security-hub", "hub.json") } // Load applies defaults, the config file (absent is fine), then environment overrides. func Load(env Env) (Config, error) { cfg := Config{ TokenFile: filepath.Join(env.Home, ".config", "security-hub", "fleet-token"), } path := ConfigPath(env) cfg.ConfigDir = filepath.Dir(path) raw, err := os.ReadFile(path) switch { case err == nil: var file Config dec := json.NewDecoder(strings.NewReader(string(raw))) dec.DisallowUnknownFields() if err := dec.Decode(&file); err != nil { return cfg, fmt.Errorf("config %s: %v", path, err) } if file.FleetURL != "" { cfg.FleetURL = file.FleetURL } if file.TokenFile != "" { cfg.TokenFile = expandHome(file.TokenFile, env.Home) } cfg.RepoPath = expandHome(file.RepoPath, env.Home) cfg.ManagedPathsFile = expandHome(file.ManagedPathsFile, env.Home) cfg.UpdateURL = file.UpdateURL cfg.UpdateRepo = file.UpdateRepo cfg.Inventory = expandHome(file.Inventory, env.Home) cfg.Sources = append(cfg.Sources, path) case errors.Is(err, os.ErrNotExist): default: return cfg, fmt.Errorf("config %s unreadable", path) } if v := env.Getenv("SECURITY_HUB_FLEET_URL"); v != "" { cfg.FleetURL = v } if v := env.Getenv("HUB_TOKEN_FILE"); v != "" { cfg.TokenFile = expandHome(v, env.Home) } if v := env.Getenv("HUB_REPO"); v != "" { cfg.RepoPath = expandHome(v, env.Home) } if v := env.Getenv("HUB_INVENTORY"); v != "" { cfg.Inventory = expandHome(v, env.Home) } if v := env.Getenv("HUB_MANAGED_PATHS"); v != "" { cfg.ManagedPathsFile = expandHome(v, env.Home) } if cfg.RepoPath == "" { cfg.RepoPath = detectRepo(env.Cwd) } if cfg.Inventory == "" && cfg.RepoPath != "" { cfg.Inventory = filepath.Join(cfg.RepoPath, "hosts") } return cfg, nil } // HubDir is /salt-security-hub. func (c Config) HubDir() string { return filepath.Join(c.RepoPath, HubDirName) } // ProfileDir is /local/profiles. func (c Config) ProfileDir() string { return filepath.Join(c.RepoPath, "local", "profiles") } // RequireFleetURL fails with a clear message when no fleet API address is configured. There is no default address. func (c Config) RequireFleetURL() error { if c.FleetURL == "" { return errors.New("set fleet_url (in hub.json, or SECURITY_HUB_FLEET_URL, or --url)") } return nil } // RequireRepo fails when no repository root is known. func (c Config) RequireRepo() error { if c.RepoPath == "" { return errors.New("repo_path not set (hub.json, HUB_REPO, or run inside the repository)") } return nil } func expandHome(p, home string) string { if p == "~" { return home } if strings.HasPrefix(p, "~/") { return filepath.Join(home, p[2:]) } return p } // detectRepo walks up from dir looking for a directory that holds both `hosts` and salt-security-hub/. func detectRepo(dir string) string { for dir != "" { if st, err := os.Stat(filepath.Join(dir, HubDirName)); err == nil && st.IsDir() { if st, err := os.Stat(filepath.Join(dir, "hosts")); err == nil && st.Mode().IsRegular() { return dir } } parent := filepath.Dir(dir) if parent == dir { return "" } dir = parent } return "" }