From c632bcf1e85dab2183b2d54c0e82e2fecc905cb0 Mon Sep 17 00:00:00 2001 From: Matthew Oliver Date: Fri, 10 Apr 2026 18:20:58 -0700 Subject: [PATCH] The wsus.Init function now returns an error if an empty slice is provided for the WSUS servers, instead of defaulting to Windows Update. Added a test case to cover this scenario. PiperOrigin-RevId: 897979585 --- README.md | 20 ++++++++++++++------ wsus.go | 26 +++++++++++++++++++++++++- wsus/wsus_test.go | 7 +++++++ wsus/wsus_windows.go | 3 +-- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0831f01..4f63909 100644 --- a/README.md +++ b/README.md @@ -145,18 +145,26 @@ Uninstall Cabbie service: ### Wsus -Initializes the wsus server configuration and restarts the windows update -service. If `--wsus_servers` flag is provided, Cabbie will check for +Initializes the WSUS server configuration and restarts the Windows Update +service. If the `--wsus_servers` flag is provided, Cabbie checks for connectivity to Windows Update endpoints. If it fails to connect, or if -`--force` is true, it will write the provided list of servers to the -`WsusServers` value in `HKLM:\SOFTWARE\Google\Cabbie` before initializing WSUS -configuration and restarting the Windows Update service. +`--force` is true, it writes the provided list of servers to the +`WSUSServers` value in `HKLM:\SOFTWARE\Google\Cabbie`. + +By default, Cabbie pings the WSUS servers to find the fastest one. If `--force` +is true, it skips this verification and unconditionally sets the first server. +If no WSUS servers are reachable and `--force` is not active, it reverts to +Windows Update and displays a warning. + +If no servers are provided and none are configured in the registry, a helpful +message is displayed and the command will fail. `cabbie wsus [--wsus_servers=,] [--force]` Example: -`cabbie wsus --wsus_servers=server1.google.com,server2.google.com` +`"C:\Program Files\Google\Cabbie\cabbie.exe" wsus +--wsus_servers="wsus1.example.com,wsus2.example.com"` `cabbie wsus` diff --git a/wsus.go b/wsus.go index 456532b..88fb2bf 100644 --- a/wsus.go +++ b/wsus.go @@ -99,13 +99,37 @@ func (c *wsusCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...any) sub } } - if _, err := wsus.Init(config.WSUSServers); err != nil { + if len(config.WSUSServers) == 0 { + fmt.Println("No WSUS servers configured. Please provide servers using the --wsus_servers flag.") + return subcommands.ExitFailure + } + + var w *wsus.WSUS + var err error + + if c.force { + deck.Info("Forcing WSUS configuration, skipping verification.") + w = &wsus.WSUS{Servers: config.WSUSServers} + err = w.Set(0) + if err == nil { + w.ServerSelection = wsus.ManagedServer + } + } else { + w, err = wsus.Init(config.WSUSServers) + } + + if err != nil { msg := fmt.Sprintf("Failed to initialize WSUS: %v\n", err) deck.ErrorfA("%s", msg).With(eventID(cablib.EvtErrMisc)).Go() fmt.Print(msg) return subcommands.ExitFailure } + if w.ServerSelection == wsus.WindowsUpdate && !c.force { + deck.Warning("No WSUS servers were reachable. Reverted to Windows Update.") + fmt.Println("Warning: No WSUS servers were reachable. Reverted to Windows Update.") + } + deck.InfoA("WSUS configuration refreshed, restarting wuauserv to apply settings.").With(eventID(cablib.EvtMisc)).Go() if err := helpers.RestartService("wuauserv"); err != nil { msg := fmt.Sprintf("Failed to restart wuauserv: %v\n", err) diff --git a/wsus/wsus_test.go b/wsus/wsus_test.go index 21128eb..75d7da3 100644 --- a/wsus/wsus_test.go +++ b/wsus/wsus_test.go @@ -59,3 +59,10 @@ func TestSet(t *testing.T) { } } } + +func TestInit_EmptyServers(t *testing.T) { + _, err := Init([]string{}) + if err == nil { + t.Error("Init([]) expected error for empty servers list, got nil") + } +} diff --git a/wsus/wsus_windows.go b/wsus/wsus_windows.go index 37222ea..fd8bb16 100644 --- a/wsus/wsus_windows.go +++ b/wsus/wsus_windows.go @@ -59,8 +59,7 @@ func Init(servers []string) (*WSUS, error) { var err error if len(servers) == 0 { - w.ServerSelection = WindowsUpdate - return &w, w.Clear() + return nil, fmt.Errorf("servers list cannot be empty") } wlog, err = eventlog.Open("Cabbie WSUS")