Skip to content

Commit

Permalink
🐛 properly handle windows powershell errors (#4749)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Oct 15, 2024
1 parent 6418cdc commit a6298a9
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 1 deletion.
9 changes: 9 additions & 0 deletions providers/os/resources/packages/windows_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ func (w *WinPkgManager) getInstalledApps() ([]Package, error) {
if err != nil {
return nil, fmt.Errorf("could not read app package list")
}

if cmd.ExitStatus != 0 {
stderr, err := io.ReadAll(cmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve installed apps: " + string(stderr))
}

return ParseWindowsAppPackages(cmd.Stdout)
}

Expand Down
8 changes: 8 additions & 0 deletions providers/os/resources/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,14 @@ func (p *mqlPorts) listWindows() ([]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve network connections: " + string(stderr))
}

list, err := p.parseWindowsPorts(executedCmd.Stdout, processes)
if err != nil {
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions providers/os/resources/smbios/win.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package smbios

import (
"encoding/json"
"errors"
"io"
"strconv"

Expand Down Expand Up @@ -139,6 +140,14 @@ func (s *WindowsSmbiosManager) Info() (*SmBiosInfo, error) {
return nil, err
}

if c.ExitStatus != 0 {
stderr, err := io.ReadAll(c.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve smbios info: " + string(stderr))
}

winBios, err := ParseWindowsSmbiosInfo(c.Stdout)
if err != nil {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions providers/os/resources/updates/win_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package updates

import (
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -66,6 +67,13 @@ func (um *WindowsUpdateManager) List() ([]OperatingSystemUpdate, error) {
if err != nil {
return nil, fmt.Errorf("could not read package list")
}
if c.ExitStatus != 0 {
stderr, err := io.ReadAll(c.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve updates: " + string(stderr))
}
return ParseWindowsUpdates(c.Stdout)
}

Expand Down
32 changes: 32 additions & 0 deletions providers/os/resources/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ func (w *mqlWindows) hotfixes() ([]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve hotfixes: " + string(stderr))
}

hotfixes, err := packages.ParseWindowsHotfixes(executedCmd.Stdout)
if err != nil {
return nil, err
Expand Down Expand Up @@ -185,6 +193,14 @@ func (w *mqlWindows) features() ([]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve features: " + string(stderr))
}

features, err := windows.ParseWindowsFeatures(executedCmd.Stdout)
if err != nil {
return nil, err
Expand Down Expand Up @@ -263,6 +279,14 @@ func (w *mqlWindows) serverFeatures() ([]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve features: " + string(stderr))
}

features, err := windows.ParseWindowsFeatures(executedCmd.Stdout)
if err != nil {
return nil, err
Expand Down Expand Up @@ -341,6 +365,14 @@ func (w *mqlWindows) optionalFeatures() ([]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve optional features: " + string(stderr))
}

features, err := windows.ParseWindowsOptionalFeatures(executedCmd.Stdout)
if err != nil {
return nil, err
Expand Down
9 changes: 8 additions & 1 deletion providers/os/resources/windows/bitlocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package windows

import (
"encoding/json"
"errors"
"io"

"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
Expand Down Expand Up @@ -146,7 +147,13 @@ func GetBitLockerVolumes(p shared.Connection) ([]bitlockerVolumeStatus, error) {
if err != nil {
return nil, err
}

if c.ExitStatus != 0 {
stderr, err := io.ReadAll(c.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve bitlocker info: " + string(stderr))
}
return ParseWindowsBitlockerStatus(c.Stdout)
}

Expand Down
9 changes: 9 additions & 0 deletions providers/os/resources/windows/security_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package windows

import (
"encoding/json"
"errors"
"io"

"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
Expand Down Expand Up @@ -89,6 +90,14 @@ func GetSecurityProviderHealth(p shared.Connection) (*windowsSecurityHealth, err
return nil, err
}

if c.ExitStatus != 0 {
stderr, err := io.ReadAll(c.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve security health: " + string(stderr))
}

return ParseSecurityProviderHealth(c.Stdout)
}

Expand Down
9 changes: 9 additions & 0 deletions providers/os/resources/windows/security_products.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package windows

import (
"encoding/json"
"errors"
"io"
"time"

Expand Down Expand Up @@ -157,6 +158,14 @@ func GetSecurityProducts(p shared.Connection) ([]securityProduct, error) {
return nil, err
}

if c.ExitStatus != 0 {
stderr, err := io.ReadAll(c.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve security products: " + string(stderr))
}

return ParseWindowsSecurityProducts(c.Stdout)
}

Expand Down
27 changes: 27 additions & 0 deletions providers/os/resources/windows_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package resources

import (
"errors"
"io"

"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
Expand All @@ -29,6 +32,14 @@ func (w *mqlWindowsFirewall) settings() (map[string]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve computer info: " + string(stderr))
}

fwSettings, err := windows.ParseWindowsFirewallSettings(executedCmd.Stdout)
if err != nil {
return nil, err
Expand All @@ -46,6 +57,14 @@ func (w *mqlWindowsFirewall) profiles() ([]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve firewall profiles: " + string(stderr))
}

fwProfiles, err := windows.ParseWindowsFirewallProfiles(executedCmd.Stdout)
if err != nil {
return nil, err
Expand Down Expand Up @@ -95,6 +114,14 @@ func (w *mqlWindowsFirewall) rules() ([]interface{}, error) {
return nil, err
}

if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve firewall rules: " + string(stderr))
}

fwRules, err := windows.ParseWindowsFirewallRules(executedCmd.Stdout)
if err != nil {
return nil, err
Expand Down

0 comments on commit a6298a9

Please sign in to comment.