mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* refactor: remove guard mode execution paths and guard-only packages Guard (non-proxy) mode is removed; all package-manager commands now always run the proxy flow. Removes the guard engine, the common flow, the extractor package, the npm/pypi dependency resolvers and the PackageResolver plumbing that only guard mode consumed. The guard package retains only PackageManagerGuardInteraction, which the proxy flow and confirmation interceptors reuse for user prompts. Proxy behavior is unchanged. * refactor: remove proxy opt-out surfaces, guard references in config, action and docs Removes Config.ProxyMode, ProxyConfig.Enabled, IsProxyModeEnabled, the proxy_mode legacy fallback, PMG_PROXY_ENABLED handling and the --proxy-mode / --include-dev-dependencies flags. Proxy interception can no longer be disabled. Also removes the proxy-mode input from the GitHub Action, the proxy-mode doctor check and setup info row, updates the E2E workflow to stop passing --proxy-mode=false, and sweeps guard-mode wording from docs and the config template. The legacy proxy_install_only flat key and PMG_PROXY_INSTALL_ONLY env var remain supported. audit.FlowTypeGuard is kept so previously recorded audit events still translate for cloud sync. * feat: fail loudly when a removed proxy opt-out is still configured A leftover proxy.enabled: false / proxy_mode: false config key or PMG_PROXY_ENABLED=false / PMG_PROXY_MODE=false env var previously meant guard mode; silently ignoring it would switch those users to proxy interception without notice. PMG now exits with an actionable error naming the exact source. Precedence mirrors the old resolution order: env (ignored under lockdown) > proxy.enabled > legacy proxy_mode. The pmg config subtree is exempt so the config file can still be fixed with pmg config edit/set. The GitHub Action's proxy-mode input is kept as a tombstone that fails the action when set to false and warns otherwise. * refactor: extract flows.RunProxy and address review findings Collapses the identical parse-then-run body duplicated across the 12 package manager commands into flows.RunProxy. Documents the cache-hit / offline analysis trade-off versus the removed guard manifest path, fixes a stale non-proxy label in the E2E workflow and a stale guard reference in the uvx parser comment. * fix(config): mirror old proxy opt-out precedence exactly PMG_PROXY_MODE only ever took effect through the legacy fallback, which was gated on the presence of a proxy: key in the config file (even a null one). Promoting it to the top env tier caused two inversions: a stale PMG_PROXY_MODE=false hard-failed configs that resolved to proxy mode, and PMG_PROXY_MODE=true silently overrode an explicit proxy.enabled: false file opt-out. The check now resolves in the old order: PMG_PROXY_ENABLED > proxy: section (presence gates the legacy tier) > PMG_PROXY_MODE > flat proxy_mode. parseOptOutBool also accepts numeric values (0 = false) to match viper's WeaklyTypedInput/cast.ToBool coercion, so proxy.enabled: 0 and proxy_mode: 0 are detected as opt-outs. * refactor: move package manager interaction out of guard * refactor: trim package manager interaction * fix(config): normalize config keys viper-style in proxy opt-out check Viper resolved config file keys case-insensitively and expanded dotted keys, so spellings like Proxy:, Enabled:, a literal proxy.enabled key or Proxy_Mode selected guard mode before the removal. The opt-out check now lowercases keys recursively and nests dotted keys before matching, so those existing opt-outs fail loudly instead of being silently ignored. * refactor: remove inert transitive controls, dead parser state and guard audit variant transitive / transitive_depth lost their only consumers with the dependency resolvers; remove the config fields, flags, template and doc entries, and the report/audit plumbing that misreported transitive analysis as enabled. Remove write-only parser state (PackageInstallTarget.Extras, ParsedCommand.ManifestFiles, ShouldExtractFromManifest); IsManifestInstall stays as it feeds sandbox gating via IsInstallationCommand. Remove audit.FlowTypeGuard and its cloud mapping; guard events recorded by pre-removal versions in an unsynced WAL translate to UNSPECIFIED. * fix: address review findings on the opt-out wiring and cleanups Move the removed-opt-out rejection from the CLI PersistentPreRun into proxyFlow.Run: the check now fires exactly for package-manager runs, so non-install commands (pmg setup remove, doctor, config, version) stay usable to fix or remove an opted-out installation, and future commands inherit or avoid the check by construction instead of by exemption list. Also: make the e2e malicious-package assertion actually fail the job when an install is not blocked, route pmg go through flows.RunProxy, and drop the dead extras return from pypiParsePackageInfo (extras are still stripped from package names). * fix(config): make the removed opt-out check faithful to the old resolution The gate that silenced the legacy proxy_mode surfaces matched the raw proxy key case-sensitively in the old code, while values resolved viper-style (case-insensitive, dotted keys); applying each semantic where the old code did fixes both divergences: a case-variant Proxy: section no longer hides a flat proxy_mode: false opt-out, and a dotted proxy.enabled: false overridden by proxy_mode: true no longer errors. Replace the generic key-tree normalization with two targeted lookups (the check only ever resolves proxy.enabled and proxy_mode), which also makes colliding spellings resolve deterministically. Coerce legacy-tier values cast.ToBool-style so PMG_PROXY_MODE=off style opt-outs are detected, log the config read error instead of swallowing it, and shorten the error to a one-line statement with the specific remedy in the help text. Add lockdown coverage (env inert both directions) and a repeated-run determinism test. * fix(config): fall back to defaults for unrecognized proxy opt-out values The old loader swallowed viper errors and ran on defaults, so values like proxy.enabled: yes or PMG_PROXY_ENABLED=banana silently discarded the whole config and defaulted to proxy. Treat them the same way now: unrecognized values mean the default (proxy on) instead of a hard error, and the doc comment no longer claims the old loader failed loudly. Only values that actually meant guard mode fail. Also check the removed opt-out before the CA trust check in pmg go, restoring the old error precedence: a config problem must not steer the user into an unnecessary OS trust store change. * fix(e2e): PMG_PROXY_MODE assertion must match the legacy gate semantics The runner's setup step writes the template config, which has a proxy: section — and with one present the legacy PMG_PROXY_MODE was always inert, so expecting a loud failure there asserts pre-fidelity-fix behavior. Assert both sides instead: inert (command succeeds) with the standard config, loud failure against an empty config dir where the legacy fallback actually applied. * refactor(config): collapse parseOptOutBool to ParseBool over the string form YAML hands us typed values (bool, int), so route them through fmt.Sprintf %v and strconv.ParseBool instead of a per-type switch. Identical behavior for every recognized value; numbers other than 0/1 now read as no opinion instead of cast.ToBool's nonzero-true, which no real config relies on.
630 lines
18 KiB
Go
630 lines
18 KiB
Go
package setup
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/safedep/dry/log"
|
|
"github.com/safedep/pmg/config"
|
|
"github.com/safedep/pmg/internal/alias"
|
|
"github.com/safedep/pmg/internal/doctor"
|
|
"github.com/safedep/pmg/internal/fsutil"
|
|
"github.com/safedep/pmg/internal/shim"
|
|
"github.com/safedep/pmg/internal/ui"
|
|
"github.com/safedep/pmg/internal/version"
|
|
"github.com/safedep/pmg/proxy/certmanager"
|
|
"github.com/safedep/pmg/sandbox/platform"
|
|
"github.com/safedep/pmg/truststore"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const (
|
|
checkConfigFile = "config-file"
|
|
checkEventLogDir = "event-log-dir"
|
|
checkShellAliases = "shell-aliases"
|
|
checkShimDirectory = "shim-directory"
|
|
checkShimInPath = "shim-in-path"
|
|
checkDependencyCooldown = "dependency-cooldown"
|
|
checkEventLogging = "event-logging"
|
|
checkSandbox = "sandbox"
|
|
checkProtectionNpm = "protection-npm"
|
|
checkProtectionPip = "protection-pip"
|
|
checkCA = "ca-cert"
|
|
checkSystemBinary = "system-binary"
|
|
|
|
aliasesInstalledMessage = "Shell aliases installed"
|
|
)
|
|
|
|
func NewDoctorCommand() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "doctor",
|
|
Short: "Validate PMG installation and protection",
|
|
SilenceUsage: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Print(ui.GeneratePMGBanner(version.Version, version.Commit))
|
|
err := executeDoctorChecks()
|
|
if _, ok := err.(*doctorFailError); ok {
|
|
cmd.SilenceErrors = true
|
|
}
|
|
return err
|
|
},
|
|
}
|
|
}
|
|
|
|
type doctorFailError struct{}
|
|
|
|
func (e *doctorFailError) Error() string { return "" }
|
|
func (e *doctorFailError) ExitCode() int { return 1 }
|
|
|
|
func executeDoctorChecks() error {
|
|
cfg := config.Get()
|
|
|
|
coreResults := runCoreChecks(cfg)
|
|
protectionResults := runProtectionChecks(coreResults)
|
|
allResults := append(coreResults, protectionResults...)
|
|
|
|
printResults(allResults)
|
|
|
|
if doctor.HasFailures(allResults) {
|
|
return &doctorFailError{}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runCoreChecks(cfg *config.RuntimeConfig) []doctor.CheckResult {
|
|
checks := []doctor.Check{
|
|
{
|
|
Name: checkConfigFile,
|
|
Category: "Configuration",
|
|
Run: func() doctor.CheckResult {
|
|
if _, err := os.Stat(cfg.ConfigFilePath()); err != nil {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: "Config file not found",
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: "Config file found",
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: checkEventLogDir,
|
|
Category: "Configuration",
|
|
Run: func() doctor.CheckResult {
|
|
return checkEventLogDirResult(cfg.Config.SkipEventLogging, cfg.EventLogDir(), cfg.ConfigDir())
|
|
},
|
|
},
|
|
{
|
|
Name: checkShellAliases,
|
|
Category: "Shell Integration",
|
|
Run: func() doctor.CheckResult {
|
|
aliasCfg := alias.DefaultConfig()
|
|
rcFileManager, err := alias.NewDefaultRcFileManager(aliasCfg.RcFileName)
|
|
if err != nil {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: fmt.Sprintf("Could not check aliases: %v", err),
|
|
}
|
|
}
|
|
aliasManager := alias.New(aliasCfg, rcFileManager)
|
|
installed, err := aliasManager.IsInstalled()
|
|
if err != nil {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: fmt.Sprintf("Could not determine alias status: %v", err),
|
|
}
|
|
}
|
|
if installed {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: aliasesInstalledMessage,
|
|
ImpliesInterception: true,
|
|
}
|
|
}
|
|
if shim.SystemShimsInstalled() {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: "No aliases (system install)",
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: "Aliases not installed",
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: checkShimDirectory,
|
|
Category: "Shell Integration",
|
|
Run: func() doctor.CheckResult {
|
|
if shim.SystemShimsInstalled() {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: fmt.Sprintf("System shim directory found (%s)", shim.SystemBinDir()),
|
|
}
|
|
}
|
|
sm, err := shim.NewDefaultShimManager()
|
|
if err != nil {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: fmt.Sprintf("Could not check shims: %v", err),
|
|
}
|
|
}
|
|
shimDir := sm.GetBinDir()
|
|
info, err := os.Stat(shimDir)
|
|
if err != nil || !info.IsDir() {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: "Shim directory not found",
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: "Shim directory found",
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: checkShimInPath,
|
|
Category: "Shell Integration",
|
|
Run: checkShimInPathResult,
|
|
},
|
|
{
|
|
Name: checkDependencyCooldown,
|
|
Category: "Security",
|
|
Run: func() doctor.CheckResult {
|
|
if cfg.Config.DependencyCooldown.Enabled {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: "Dependency cooldown is enabled",
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: "Dependency cooldown is disabled",
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: checkEventLogging,
|
|
Category: "Security",
|
|
Run: func() doctor.CheckResult {
|
|
if !cfg.Config.SkipEventLogging {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: "Event logging is enabled",
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: "Event logging is disabled",
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: checkSandbox,
|
|
Category: "Security",
|
|
Run: func() doctor.CheckResult {
|
|
sb, err := platform.NewSandbox()
|
|
available := err == nil && sb != nil && sb.IsAvailable()
|
|
if !cfg.Config.Sandbox.Enabled {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: "Sandbox is disabled",
|
|
}
|
|
}
|
|
if !available {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: "Sandbox enabled but no driver available on this platform",
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: fmt.Sprintf("Sandbox enabled (%s)", sb.Name()),
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: checkCA,
|
|
Category: "Security",
|
|
Run: func() doctor.CheckResult {
|
|
user, system, _ := truststore.Status(certmanager.CACommonName)
|
|
return evaluateCACheck(cfg.ConfigDir(), user, system, truststore.UserScopeSupported())
|
|
},
|
|
},
|
|
}
|
|
|
|
// System-only: the binary every user's shim execs must stay root-owned and
|
|
// non-writable. Validation runs at install; re-check it here to catch later
|
|
// permission/ownership drift (redeploy, chmod, image rebuild).
|
|
if shim.SystemShimsInstalled() {
|
|
checks = append(checks, doctor.Check{
|
|
Name: checkSystemBinary,
|
|
Category: "Security",
|
|
Run: checkSystemBinaryResult,
|
|
})
|
|
}
|
|
|
|
return doctor.RunChecks(checks)
|
|
}
|
|
|
|
func checkSystemBinaryResult() doctor.CheckResult {
|
|
path, ok := shim.SystemShimBinary()
|
|
if !ok {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: "Could not determine system shim binary",
|
|
}
|
|
}
|
|
if err := shim.ValidateSystemBinary(path); err != nil {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: fmt.Sprintf("System binary unsafe: %v", err),
|
|
Fix: "Reinstall with pmg setup install --system, or restore root ownership/permissions",
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: fmt.Sprintf("System binary is root-owned and safe (%s)", path),
|
|
}
|
|
}
|
|
|
|
// checkEventLogDirResult is the testable core of the event-log dir check.
|
|
// Event logging is mandatory (init failure is fatal), so an unwritable dir
|
|
// fail-closes every pmg command for this user. The remedy is triaged: chown
|
|
// when another account created files in this user's home, an environment fix
|
|
// when a leaked HOME/XDG_CONFIG_HOME points at another user's home.
|
|
func checkEventLogDirResult(skipEventLogging bool, logDir, configDir string) doctor.CheckResult {
|
|
if skipEventLogging {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: "Event logging is disabled",
|
|
}
|
|
}
|
|
info, err := os.Stat(logDir)
|
|
if err != nil {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: "Event log directory not found",
|
|
}
|
|
}
|
|
if !info.IsDir() {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: "Event log path is not a directory",
|
|
}
|
|
}
|
|
|
|
probe, err := os.CreateTemp(logDir, ".pmg-doctor-*")
|
|
if err != nil {
|
|
_, fix := config.UnwritableConfigDirRemedy(configDir)
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: "Event log directory not writable",
|
|
Fix: fix,
|
|
}
|
|
}
|
|
if err := probe.Close(); err != nil {
|
|
log.Warnf("failed to close doctor probe file: %v", err)
|
|
}
|
|
if err := os.Remove(probe.Name()); err != nil {
|
|
log.Warnf("failed to remove doctor probe file: %v", err)
|
|
}
|
|
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: "Event log directory found",
|
|
}
|
|
}
|
|
|
|
func pathContainsDir(pathEntries []string, dir string) bool {
|
|
if dir == "" {
|
|
return false
|
|
}
|
|
cleanDir := filepath.Clean(dir)
|
|
for _, entry := range pathEntries {
|
|
if filepath.Clean(entry) == cleanDir {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// classifyPackageManagerResolutions splits package managers by where they
|
|
// resolve on PATH: underShim means the command runs through a pmg shim (so it
|
|
// is intercepted), shadowed means a real npm/pip sits ahead of the shims (so
|
|
// interception is bypassed and the user should be warned).
|
|
func classifyPackageManagerResolutions(packageManagers []string, shimDirs []string, lookPath func(string) (string, error)) (underShim, shadowed []string) {
|
|
for _, pm := range packageManagers {
|
|
resolved, err := lookPath(pm)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if resolvesUnderAny(resolved, shimDirs) {
|
|
underShim = append(underShim, pm)
|
|
continue
|
|
}
|
|
shadowed = append(shadowed, pm)
|
|
}
|
|
return underShim, shadowed
|
|
}
|
|
|
|
func resolvesUnderAny(path string, dirs []string) bool {
|
|
for _, dir := range dirs {
|
|
if fsutil.PathWithinDir(path, dir) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// shimDirs lists every directory a package manager may legitimately resolve
|
|
// into. System and per-user installs are supported side by side, so resolution
|
|
// into either shim directory counts as intercepted rather than shadowed.
|
|
func shimDirs() []string {
|
|
dirs := []string{shim.SystemBinDir()}
|
|
if userDir, err := shim.UserBinDir(); err == nil {
|
|
dirs = append(dirs, userDir)
|
|
}
|
|
return dirs
|
|
}
|
|
|
|
// checkShimDirResolution classifies interception against all shim dirs via
|
|
// shimDirs(); shimDir/pathLabel only select the PATH-membership fallback and
|
|
// the display label, not which directories count as intercepting.
|
|
func checkShimDirResolution(shimDir, pathLabel string, pathEntries []string) doctor.CheckResult {
|
|
underShim, shadowed := classifyPackageManagerResolutions(
|
|
alias.DefaultConfig().PackageManagers,
|
|
shimDirs(),
|
|
exec.LookPath,
|
|
)
|
|
|
|
if len(shadowed) > 0 {
|
|
if pathContainsDir(pathEntries, shimDir) || len(underShim) > 0 {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: fmt.Sprintf("%s resolved outside %s", strings.Join(shadowed, ", "), pathLabel),
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: fmt.Sprintf("%s not in PATH", pathLabel),
|
|
}
|
|
}
|
|
if len(underShim) > 0 {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: fmt.Sprintf("Package managers resolve to %s", pathLabel),
|
|
ImpliesInterception: true,
|
|
}
|
|
}
|
|
if pathContainsDir(pathEntries, shimDir) {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusPass,
|
|
Message: fmt.Sprintf("%s is in PATH", pathLabel),
|
|
ImpliesInterception: true,
|
|
}
|
|
}
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusFail,
|
|
Message: fmt.Sprintf("%s not in PATH", pathLabel),
|
|
}
|
|
}
|
|
|
|
func checkShimInPathResult() doctor.CheckResult {
|
|
pathEntries := filepath.SplitList(os.Getenv("PATH"))
|
|
|
|
// The primary directory only picks the label and PATH-membership target;
|
|
// classification accepts resolution into either shim dir regardless.
|
|
shimDir, pathLabel := shim.SystemBinDir(), "System shim directory"
|
|
if !shim.SystemShimsInstalled() {
|
|
userDir, err := shim.UserBinDir()
|
|
if err != nil {
|
|
return doctor.CheckResult{
|
|
Status: doctor.StatusWarn,
|
|
Message: fmt.Sprintf("Could not resolve shim directory: %v", err),
|
|
}
|
|
}
|
|
shimDir, pathLabel = userDir, "Shim directory"
|
|
}
|
|
|
|
return checkShimDirResolution(shimDir, pathLabel, pathEntries)
|
|
}
|
|
|
|
func runProtectionChecks(coreResults []doctor.CheckResult) []doctor.CheckResult {
|
|
if !isInterceptionActive(coreResults) {
|
|
var results []doctor.CheckResult
|
|
for _, tc := range doctor.ProtectionTestCases() {
|
|
results = append(results, doctor.CheckResult{
|
|
Name: fmt.Sprintf("protection-%s", tc.PackageManager),
|
|
Category: "Protection",
|
|
Status: doctor.StatusFail,
|
|
Message: "Aliases and shims not active",
|
|
})
|
|
}
|
|
return results
|
|
}
|
|
|
|
pmgBinary, err := os.Executable()
|
|
if err != nil {
|
|
pmgBinary = "pmg"
|
|
}
|
|
|
|
var results []doctor.CheckResult
|
|
for _, tc := range doctor.ProtectionTestCases() {
|
|
result := doctor.RunProtectionCheck(tc, pmgBinary)
|
|
result.Category = "Protection"
|
|
result.Name = fmt.Sprintf("protection-%s", tc.PackageManager)
|
|
results = append(results, result)
|
|
}
|
|
return results
|
|
}
|
|
|
|
func isInterceptionActive(coreResults []doctor.CheckResult) bool {
|
|
for _, r := range coreResults {
|
|
if r.ImpliesInterception {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
var checkDisplayNames = map[string]string{
|
|
checkConfigFile: "Config file",
|
|
checkEventLogDir: "Event log directory",
|
|
checkShellAliases: "Shell aliases",
|
|
checkShimDirectory: "Shim directory",
|
|
checkShimInPath: "Shim in PATH",
|
|
checkDependencyCooldown: "Dependency cooldown",
|
|
checkEventLogging: "Event logging",
|
|
checkSandbox: "Sandbox",
|
|
checkProtectionNpm: "npm protection",
|
|
checkProtectionPip: "pip protection",
|
|
checkCA: "MITM CA",
|
|
checkSystemBinary: "System binary",
|
|
}
|
|
|
|
var checkFixes = map[string]string{
|
|
checkConfigFile: "pmg setup install",
|
|
checkEventLogDir: "pmg setup install",
|
|
checkShellAliases: "pmg setup install",
|
|
checkShimDirectory: "pmg setup install",
|
|
checkShimInPath: "Restart shell or source profile",
|
|
checkSandbox: "Set sandbox.enabled: true in config",
|
|
checkDependencyCooldown: "Set dependency_cooldown.enabled: true in config",
|
|
checkEventLogging: "Set skip_event_logging: false in config",
|
|
checkProtectionNpm: "pmg setup install",
|
|
checkProtectionPip: "pmg setup install",
|
|
checkCA: "pmg setup cert install",
|
|
}
|
|
|
|
// evaluateCACheck is the testable core of the CA doctor check. Trust booleans
|
|
// and userScopeSupported are injected so the check is hermetic; the live check
|
|
// fills them from truststore.
|
|
func evaluateCACheck(dir string, userTrusted, systemTrusted, userScopeSupported bool) doctor.CheckResult {
|
|
st, err := certmanager.InspectCA(dir)
|
|
if err != nil {
|
|
return doctor.CheckResult{Status: doctor.StatusFail, Message: "CA files present but unreadable"}
|
|
}
|
|
|
|
if !st.KeyPresent && !st.CertPresent {
|
|
return doctor.CheckResult{Status: doctor.StatusWarn, Message: "MITM CA not installed (optional; run pmg setup cert install)"}
|
|
}
|
|
|
|
st.UserTrusted, st.SystemTrusted = userTrusted, systemTrusted
|
|
|
|
if drift, reason := st.Drift(); drift {
|
|
return doctor.CheckResult{Status: doctor.StatusFail, Message: reason}
|
|
}
|
|
|
|
if st.Trusted() {
|
|
if st.ExpiringSoon {
|
|
return doctor.CheckResult{Status: doctor.StatusWarn, Message: "CA trusted but expiring within 30 days"}
|
|
}
|
|
return doctor.CheckResult{Status: doctor.StatusPass, Message: "MITM CA installed and trusted"}
|
|
}
|
|
|
|
// On disk but not trusted in any store.
|
|
if !userScopeSupported {
|
|
// Linux: env-var injection (SSL_CERT_FILE) already covers Go; store trust optional.
|
|
return doctor.CheckResult{Status: doctor.StatusWarn, Message: "CA on disk; not in OS store (Linux uses SSL_CERT_FILE; --system for store trust)"}
|
|
}
|
|
return doctor.CheckResult{Status: doctor.StatusFail, Message: "CA on disk but not trusted; run pmg setup cert install"}
|
|
}
|
|
|
|
func printResults(results []doctor.CheckResult) {
|
|
fmt.Println()
|
|
fmt.Println(ui.Colors.Cyan("Setup Diagnostics"))
|
|
fmt.Println(ui.Colors.Normal("--------------------"))
|
|
|
|
rows := [][]string{{
|
|
ui.Colors.Bold("STATUS"),
|
|
ui.Colors.Bold("CHECK"),
|
|
ui.Colors.Bold("SUMMARY"),
|
|
ui.Colors.Bold("FIX"),
|
|
}}
|
|
for _, r := range results {
|
|
fix := ui.Colors.Dim("—")
|
|
if r.Status != doctor.StatusPass {
|
|
fix = fixHint(r.Name)
|
|
if r.Fix != "" {
|
|
fix = r.Fix
|
|
}
|
|
}
|
|
rows = append(rows, []string{
|
|
statusBadge(r.Status),
|
|
displayName(r.Name),
|
|
ui.Truncate(r.Message, 60),
|
|
fix,
|
|
})
|
|
}
|
|
|
|
if err := ui.RenderTable(os.Stdout, rows, nil); err != nil {
|
|
fmt.Fprintf(os.Stderr, "render error: %v\n", err)
|
|
}
|
|
|
|
fmt.Println()
|
|
printSummaryLine(results)
|
|
}
|
|
|
|
func statusBadge(s doctor.CheckStatus) string {
|
|
switch s {
|
|
case doctor.StatusPass:
|
|
return ui.Colors.Green("OK")
|
|
case doctor.StatusWarn:
|
|
return ui.Colors.Yellow("WARN")
|
|
case doctor.StatusFail:
|
|
return ui.Colors.Red("FAIL")
|
|
default:
|
|
return "?"
|
|
}
|
|
}
|
|
|
|
func displayName(name string) string {
|
|
if dn, ok := checkDisplayNames[name]; ok {
|
|
return dn
|
|
}
|
|
return name
|
|
}
|
|
|
|
func fixHint(name string) string {
|
|
if fix, ok := checkFixes[name]; ok {
|
|
return fix
|
|
}
|
|
return ui.Colors.Dim("—")
|
|
}
|
|
|
|
func printSummaryLine(results []doctor.CheckResult) {
|
|
passCount, warnCount, failCount := 0, 0, 0
|
|
for _, r := range results {
|
|
switch r.Status {
|
|
case doctor.StatusPass:
|
|
passCount++
|
|
case doctor.StatusWarn:
|
|
warnCount++
|
|
case doctor.StatusFail:
|
|
failCount++
|
|
}
|
|
}
|
|
|
|
summary := fmt.Sprintf("%d passed", passCount)
|
|
if warnCount > 0 {
|
|
summary += fmt.Sprintf(", %d warnings", warnCount)
|
|
}
|
|
if failCount > 0 {
|
|
summary += fmt.Sprintf(", %d failed", failCount)
|
|
fmt.Printf("%s %s\n", ui.Colors.Red("FAIL"), ui.Colors.Red(summary))
|
|
} else if warnCount > 0 {
|
|
fmt.Printf("%s %s\n", ui.Colors.Yellow("WARN"), ui.Colors.Yellow(summary))
|
|
} else {
|
|
fmt.Printf("%s %s\n", ui.Colors.Green("OK"), ui.Colors.Green(summary))
|
|
}
|
|
}
|