From a8543f076266b5d503bc374911e0c6c26e448226 Mon Sep 17 00:00:00 2001 From: Berne Campbell Date: Sat, 27 Jan 2024 17:10:10 +1100 Subject: [PATCH 1/7] Command optional exec style --- resource/command.go | 34 ++++++++++++++++++++-------------- system/command.go | 15 ++++++++++----- system/system.go | 2 +- util/command.go | 26 ++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 20 deletions(-) diff --git a/resource/command.go b/resource/command.go index 79a1671e7..b4a78d092 100644 --- a/resource/command.go +++ b/resource/command.go @@ -13,15 +13,15 @@ import ( ) type Command struct { - Title string `json:"title,omitempty" yaml:"title,omitempty"` - Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"` - id string `json:"-" yaml:"-"` - Exec string `json:"exec,omitempty" yaml:"exec,omitempty"` - ExitStatus matcher `json:"exit-status" yaml:"exit-status"` - Stdout matcher `json:"stdout" yaml:"stdout"` - Stderr matcher `json:"stderr" yaml:"stderr"` - Timeout int `json:"timeout" yaml:"timeout"` - Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"` + Title string `json:"title,omitempty" yaml:"title,omitempty"` + Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"` + id string `json:"-" yaml:"-"` + Exec util.ExecCommand `json:"exec,omitempty" yaml:"exec,omitempty"` + ExitStatus matcher `json:"exit-status" yaml:"exit-status"` + Stdout matcher `json:"stdout" yaml:"stdout"` + Stderr matcher `json:"stderr" yaml:"stderr"` + Timeout int `json:"timeout" yaml:"timeout"` + Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"` } const ( @@ -41,11 +41,11 @@ func (c *Command) TypeName() string { return CommandResourceName } func (c *Command) GetTitle() string { return c.Title } func (c *Command) GetMeta() meta { return c.Meta } -func (c *Command) GetExec() string { - if c.Exec != "" { +func (c *Command) GetExec() util.ExecCommand { + if c.Exec.CmdStr != "" || len(c.Exec.CmdSlice) > 0 { return c.Exec } - return c.id + return util.ExecCommand{CmdStr: c.id} } func (c *Command) Validate(sys *system.System) []TestResult { @@ -71,10 +71,16 @@ func (c *Command) Validate(sys *system.System) []TestResult { } func NewCommand(sysCommand system.Command, config util.Config) (*Command, error) { - command := sysCommand.Command() + var id string + if sysCommand.Command().CmdStr != "" { + id = sysCommand.Command().CmdStr + } else { + id = sysCommand.Command().CmdSlice[0] + } exitStatus, err := sysCommand.ExitStatus() c := &Command{ - id: command, + id: id, + Command: sysCommand.Command() ExitStatus: exitStatus, Stdout: "", Stderr: "", diff --git a/system/command.go b/system/command.go index aea7ade9f..f5e432bec 100644 --- a/system/command.go +++ b/system/command.go @@ -12,7 +12,7 @@ import ( ) type Command interface { - Command() string + Command() util.ExecCommand Exists() (bool, error) ExitStatus() (int, error) Stdout() (io.Reader, error) @@ -21,7 +21,7 @@ type Command interface { type DefCommand struct { Ctx context.Context - command string + command util.ExecCommand exitStatus int stdout io.Reader stderr io.Reader @@ -30,7 +30,7 @@ type DefCommand struct { err error } -func NewDefCommand(ctx context.Context, command string, system *System, config util.Config) Command { +func NewDefCommand(ctx context.Context, command util.ExecCommand, system *System, config util.Config) Command { return &DefCommand{ Ctx: ctx, command: command, @@ -44,7 +44,12 @@ func (c *DefCommand) setup() error { } c.loaded = true - cmd := commandWrapper(c.command) + var cmd *util.Command + if c.command.CmdStr != "" { + cmd = commandWrapper(c.command.CmdStr) + } else { + cmd = util.NewCommand(c.command.CmdSlice[0], c.command.CmdSlice[1:]...) + } err := runCommand(cmd, c.Timeout) // We don't care about ExitError since it's covered by status @@ -63,7 +68,7 @@ func (c *DefCommand) setup() error { return c.err } -func (c *DefCommand) Command() string { +func (c *DefCommand) Command() util.ExecCommand { return c.command } diff --git a/system/system.go b/system/system.go index 6c6083558..b0e876e8b 100644 --- a/system/system.go +++ b/system/system.go @@ -27,7 +27,7 @@ type System struct { NewService func(context.Context, string, *System, util2.Config) Service NewUser func(context.Context, string, *System, util2.Config) User NewGroup func(context.Context, string, *System, util2.Config) Group - NewCommand func(context.Context, string, *System, util2.Config) Command + NewCommand func(context.Context, util2.ExecCommand, *System, util2.Config) Command NewDNS func(context.Context, string, *System, util2.Config) DNS NewProcess func(context.Context, string, *System, util2.Config) Process NewGossfile func(context.Context, string, *System, util2.Config) Gossfile diff --git a/util/command.go b/util/command.go index 460020356..17ff27062 100644 --- a/util/command.go +++ b/util/command.go @@ -2,12 +2,38 @@ package util import ( "bytes" + "encoding/json" //"fmt" "os/exec" "syscall" ) +// Allows passing a shell style command string +// or an exec style slice of strings. +type ExecCommand struct { + CmdStr string + CmdSlice []string +} + +func (e *ExecCommand) UnmarshalJSON(data []byte) error { + // Try to unmarshal as a string + if err := json.Unmarshal(data, &e.CmdStr); err != nil { + // If string unmarshalling fails, try as a slice + return json.Unmarshal(data, &e.CmdSlice) + } + return nil +} + +func (e *ExecCommand) UnmarshalYAML(unmarshal func(interface{}) error) error { + // Try to unmarshal as a string + if err := unmarshal(&e.CmdStr); err != nil { + // If string unmarshalling fails, try as a slice + return unmarshal(&e.CmdSlice) + } + return nil +} + type Command struct { name string Cmd *exec.Cmd From 09a547db7fe66c19834294c3199d9c0631ba44d9 Mon Sep 17 00:00:00 2001 From: Berne Campbell Date: Sat, 27 Jan 2024 17:10:46 +1100 Subject: [PATCH 2/7] Manual edit resource_list.go - This is wrong way to do it - This is meant to be generated with `genny` --- resource/resource_list.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/resource/resource_list.go b/resource/resource_list.go index e3aedb633..46f2fdc36 100644 --- a/resource/resource_list.go +++ b/resource/resource_list.go @@ -120,7 +120,8 @@ type CommandMap map[string]*Command func (r CommandMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Command, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewCommand(ctx, sr, sys, config) + cmd := util.ExecCommand{CmdStr: sr} + sysres := sys.NewCommand(ctx, cmd, sys, config) res, err := NewCommand(sysres, config) if err != nil { return nil, err @@ -135,7 +136,8 @@ func (r CommandMap) AppendSysResource(sr string, sys *system.System, config util func (r CommandMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Command, system.Command, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewCommand(ctx, sr, sys, util.Config{}) + cmd := util.ExecCommand{CmdStr: sr} + sysres := sys.NewCommand(ctx, cmd, sys, util.Config{}) res, err := NewCommand(sysres, util.Config{}) if err != nil { return nil, nil, false, err From d7a36a09d331604456ab1667eb1ce6f7d0f07a36 Mon Sep 17 00:00:00 2001 From: Berne Campbell Date: Sat, 27 Jan 2024 18:42:12 +1100 Subject: [PATCH 3/7] Fix field name --- resource/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource/command.go b/resource/command.go index b4a78d092..d3bf0bb92 100644 --- a/resource/command.go +++ b/resource/command.go @@ -80,7 +80,7 @@ func NewCommand(sysCommand system.Command, config util.Config) (*Command, error) exitStatus, err := sysCommand.ExitStatus() c := &Command{ id: id, - Command: sysCommand.Command() + Exec: sysCommand.Command(), ExitStatus: exitStatus, Stdout: "", Stderr: "", From fda61c93415f40b0e47d86fa215d819977613809 Mon Sep 17 00:00:00 2001 From: Berne Campbell Date: Sat, 27 Jan 2024 19:49:05 +1100 Subject: [PATCH 4/7] marshal as either string or list --- util/command.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/util/command.go b/util/command.go index 17ff27062..d95516b85 100644 --- a/util/command.go +++ b/util/command.go @@ -34,6 +34,20 @@ func (e *ExecCommand) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +func (e ExecCommand) MarshalJSON() ([]byte, error) { + if e.CmdStr != "" { + return json.Marshal(e.CmdStr) + } + return json.Marshal(e.CmdSlice) +} + +func (e ExecCommand) MarshalYAML() (interface{}, error) { + if e.CmdStr != "" { + return e.CmdStr, nil + } + return e.CmdSlice, nil +} + type Command struct { name string Cmd *exec.Cmd From fc02effbd04afb932004dd772d0a9db65bca247b Mon Sep 17 00:00:00 2001 From: Berne Campbell Date: Sat, 27 Jan 2024 19:56:23 +1100 Subject: [PATCH 5/7] Don't set Exec --- resource/command.go | 1 - 1 file changed, 1 deletion(-) diff --git a/resource/command.go b/resource/command.go index d3bf0bb92..328bd3a47 100644 --- a/resource/command.go +++ b/resource/command.go @@ -80,7 +80,6 @@ func NewCommand(sysCommand system.Command, config util.Config) (*Command, error) exitStatus, err := sysCommand.ExitStatus() c := &Command{ id: id, - Exec: sysCommand.Command(), ExitStatus: exitStatus, Stdout: "", Stderr: "", From 9313937112233ffa7d7be9ed618cee5bd0cd5867 Mon Sep 17 00:00:00 2001 From: Berne Campbell Date: Mon, 19 Feb 2024 01:09:01 +1100 Subject: [PATCH 6/7] Use `interface{}` --- resource/addr.go | 2 +- resource/command.go | 49 +++++++++++++++++++------- resource/dns.go | 2 +- resource/file.go | 2 +- resource/group.go | 2 +- resource/http.go | 2 +- resource/interface.go | 2 +- resource/kernel_param.go | 2 +- resource/mount.go | 2 +- resource/package.go | 2 +- resource/port.go | 2 +- resource/process.go | 2 +- resource/resource_list.go | 62 ++++++++++++++++----------------- resource/resource_list_genny.go | 4 +-- resource/service.go | 2 +- resource/user.go | 2 +- system/addr.go | 11 +++++- system/command.go | 23 ++++++++++-- system/dns.go | 10 +++++- system/file.go | 10 +++++- system/gossfile.go | 11 +++++- system/group.go | 11 +++++- system/http.go | 10 +++++- system/interface.go | 11 +++++- system/kernel_param.go | 11 +++++- system/mount.go | 10 +++++- system/package.go | 11 +++++- system/package_alpine.go | 11 +++++- system/package_deb.go | 11 +++++- system/package_pacman.go | 11 +++++- system/package_rpm.go | 11 +++++- system/port.go | 11 +++++- system/process.go | 11 +++++- system/service_init.go | 20 +++++++++-- system/service_systemd.go | 20 +++++++++-- system/service_upstart.go | 10 +++++- system/system.go | 30 ++++++++-------- system/user.go | 11 +++++- 38 files changed, 328 insertions(+), 99 deletions(-) diff --git a/resource/addr.go b/resource/addr.go index 2347583e9..18cf0060c 100644 --- a/resource/addr.go +++ b/resource/addr.go @@ -58,7 +58,7 @@ func (a *Addr) Validate(sys *system.System) []TestResult { a.Timeout = 500 } - sysAddr := sys.NewAddr(ctx, a.GetAddress(), sys, util.Config{Timeout: time.Duration(a.Timeout) * time.Millisecond, LocalAddress: a.LocalAddress}) + sysAddr, _ := sys.NewAddr(ctx, a.GetAddress(), sys, util.Config{Timeout: time.Duration(a.Timeout) * time.Millisecond, LocalAddress: a.LocalAddress}) var results []TestResult results = append(results, ValidateValue(a, "reachable", a.Reachable, sysAddr.Reachable, skip)) diff --git a/resource/command.go b/resource/command.go index 328bd3a47..eb12c67e4 100644 --- a/resource/command.go +++ b/resource/command.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "log" "strings" "time" @@ -41,11 +42,14 @@ func (c *Command) TypeName() string { return CommandResourceName } func (c *Command) GetTitle() string { return c.Title } func (c *Command) GetMeta() meta { return c.Meta } -func (c *Command) GetExec() util.ExecCommand { - if c.Exec.CmdStr != "" || len(c.Exec.CmdSlice) > 0 { - return c.Exec +func (c *Command) GetExec() interface{} { + if c.Exec.CmdStr != "" { + return c.Exec.CmdStr + } else if len(c.Exec.CmdSlice) > 0 { + return c.Exec.CmdSlice + } else { + return util.ExecCommand{CmdStr: c.id} } - return util.ExecCommand{CmdStr: c.id} } func (c *Command) Validate(sys *system.System) []TestResult { @@ -57,15 +61,34 @@ func (c *Command) Validate(sys *system.System) []TestResult { } var results []TestResult - sysCommand := sys.NewCommand(ctx, c.GetExec(), sys, util.Config{Timeout: time.Duration(c.Timeout) * time.Millisecond}) - - cExitStatus := deprecateAtoI(c.ExitStatus, fmt.Sprintf("%s: command.exit-status", c.ID())) - results = append(results, ValidateValue(c, "exit-status", cExitStatus, sysCommand.ExitStatus, skip)) - if isSet(c.Stdout) { - results = append(results, ValidateValue(c, "stdout", c.Stdout, sysCommand.Stdout, skip)) - } - if isSet(c.Stderr) { - results = append(results, ValidateValue(c, "stderr", c.Stderr, sysCommand.Stderr, skip)) + sysCommand, err := sys.NewCommand(ctx, c.GetExec(), sys, util.Config{Timeout: time.Duration(c.Timeout) * time.Millisecond}) + if err != nil { + log.Printf("[ERROR] Could not create new command: %v", err) + startTime := time.Now() + results = append( + results, + TestResult{ + Result: FAIL, + ResourceType: "Command", + ResourceId: c.id, + Title: c.Title, + Meta: c.Meta, + Property: "type", + Err: toValidateError(err), + StartTime: startTime, + EndTime: startTime, + Duration: startTime.Sub(startTime), + }, + ) + } else { + cExitStatus := deprecateAtoI(c.ExitStatus, fmt.Sprintf("%s: command.exit-status", c.ID())) + results = append(results, ValidateValue(c, "exit-status", cExitStatus, sysCommand.ExitStatus, skip)) + if isSet(c.Stdout) { + results = append(results, ValidateValue(c, "stdout", c.Stdout, sysCommand.Stdout, skip)) + } + if isSet(c.Stderr) { + results = append(results, ValidateValue(c, "stderr", c.Stderr, sysCommand.Stderr, skip)) + } } return results } diff --git a/resource/dns.go b/resource/dns.go index bd3274d99..31fbd9b06 100644 --- a/resource/dns.go +++ b/resource/dns.go @@ -58,7 +58,7 @@ func (d *DNS) Validate(sys *system.System) []TestResult { d.Timeout = 500 } - sysDNS := sys.NewDNS(ctx, d.GetResolve(), sys, util.Config{Timeout: time.Duration(d.Timeout) * time.Millisecond, Server: d.Server}) + sysDNS, _ := sys.NewDNS(ctx, d.GetResolve(), sys, util.Config{Timeout: time.Duration(d.Timeout) * time.Millisecond, Server: d.Server}) var results []TestResult // Backwards compatibility hack for now diff --git a/resource/file.go b/resource/file.go index dcb7f5a0e..85a8202f2 100644 --- a/resource/file.go +++ b/resource/file.go @@ -61,7 +61,7 @@ func (f *File) GetPath() string { func (f *File) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", f.ID()) skip := f.Skip - sysFile := sys.NewFile(ctx, f.GetPath(), sys, util.Config{}) + sysFile, _ := sys.NewFile(ctx, f.GetPath(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(f, "exists", f.Exists, sysFile.Exists, skip)) diff --git a/resource/group.go b/resource/group.go index 7ca928108..b391e6a30 100644 --- a/resource/group.go +++ b/resource/group.go @@ -49,7 +49,7 @@ func (g *Group) GetGroupname() string { func (g *Group) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", g.ID()) skip := g.Skip - sysgroup := sys.NewGroup(ctx, g.GetGroupname(), sys, util.Config{}) + sysgroup, _ := sys.NewGroup(ctx, g.GetGroupname(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(g, "exists", g.Exists, sysgroup.Exists, skip)) diff --git a/resource/http.go b/resource/http.go index b9a1006de..00b9a7073 100644 --- a/resource/http.go +++ b/resource/http.go @@ -68,7 +68,7 @@ func (u *HTTP) Validate(sys *system.System) []TestResult { if u.Timeout == 0 { u.Timeout = 5000 } - sysHTTP := sys.NewHTTP(ctx, u.getURL(), sys, util.Config{ + sysHTTP, _ := sys.NewHTTP(ctx, u.getURL(), sys, util.Config{ AllowInsecure: u.AllowInsecure, CAFile: u.CAFile, CertFile: u.CertFile, diff --git a/resource/interface.go b/resource/interface.go index 14921d133..c09ad65c3 100644 --- a/resource/interface.go +++ b/resource/interface.go @@ -52,7 +52,7 @@ func (i *Interface) GetName() string { func (i *Interface) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", i.ID()) skip := i.Skip - sysInterface := sys.NewInterface(ctx, i.GetName(), sys, util.Config{}) + sysInterface, _ := sys.NewInterface(ctx, i.GetName(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(i, "exists", i.Exists, sysInterface.Exists, skip)) diff --git a/resource/kernel_param.go b/resource/kernel_param.go index 494597a96..f18c12372 100644 --- a/resource/kernel_param.go +++ b/resource/kernel_param.go @@ -52,7 +52,7 @@ func (k *KernelParam) GetName() string { func (k *KernelParam) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", k.ID()) skip := k.Skip - sysKernelParam := sys.NewKernelParam(ctx, k.GetName(), sys, util.Config{}) + sysKernelParam, _ := sys.NewKernelParam(ctx, k.GetName(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(k, "value", k.Value, sysKernelParam.Value, skip)) diff --git a/resource/mount.go b/resource/mount.go index 895ebdff0..3de8d5f30 100644 --- a/resource/mount.go +++ b/resource/mount.go @@ -55,7 +55,7 @@ func (m *Mount) GetMountPoint() string { func (m *Mount) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", m.ID()) skip := m.Skip - sysMount := sys.NewMount(ctx, m.GetMountPoint(), sys, util.Config{}) + sysMount, _ := sys.NewMount(ctx, m.GetMountPoint(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(m, "exists", m.Exists, sysMount.Exists, skip)) diff --git a/resource/package.go b/resource/package.go index f8afffcb5..3ae43a5d4 100644 --- a/resource/package.go +++ b/resource/package.go @@ -49,7 +49,7 @@ func (p *Package) GetName() string { func (p *Package) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", p.ID()) skip := p.Skip - sysPkg := sys.NewPackage(ctx, p.GetName(), sys, util.Config{}) + sysPkg, _ := sys.NewPackage(ctx, p.GetName(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(p, "installed", p.Installed, sysPkg.Installed, skip)) diff --git a/resource/port.go b/resource/port.go index 70f72ae93..678688628 100644 --- a/resource/port.go +++ b/resource/port.go @@ -49,7 +49,7 @@ func (p *Port) GetPort() string { func (p *Port) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", p.ID()) skip := p.Skip - sysPort := sys.NewPort(ctx, p.GetPort(), sys, util.Config{}) + sysPort, _ := sys.NewPort(ctx, p.GetPort(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(p, "listening", p.Listening, sysPort.Listening, skip)) diff --git a/resource/process.go b/resource/process.go index 9012760e9..6ab19e565 100644 --- a/resource/process.go +++ b/resource/process.go @@ -48,7 +48,7 @@ func (p *Process) GetComm() string { func (p *Process) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", p.ID()) skip := p.Skip - sysProcess := sys.NewProcess(ctx, p.GetComm(), sys, util.Config{}) + sysProcess, _ := sys.NewProcess(ctx, p.GetComm(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(p, "running", p.Running, sysProcess.Running, skip)) diff --git a/resource/resource_list.go b/resource/resource_list.go index 46f2fdc36..af66cbd4d 100644 --- a/resource/resource_list.go +++ b/resource/resource_list.go @@ -19,7 +19,7 @@ type AddrMap map[string]*Addr func (r AddrMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Addr, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewAddr(ctx, sr, sys, config) + sysres, _ := sys.NewAddr(ctx, sr, sys, config) res, err := NewAddr(sysres, config) if err != nil { return nil, err @@ -34,7 +34,7 @@ func (r AddrMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r AddrMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Addr, system.Addr, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewAddr(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewAddr(ctx, sr, sys, util.Config{}) res, err := NewAddr(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -120,8 +120,7 @@ type CommandMap map[string]*Command func (r CommandMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Command, error) { ctx := context.WithValue(context.Background(), "id", sr) - cmd := util.ExecCommand{CmdStr: sr} - sysres := sys.NewCommand(ctx, cmd, sys, config) + sysres, _ := sys.NewCommand(ctx, sr, sys, config) res, err := NewCommand(sysres, config) if err != nil { return nil, err @@ -136,8 +135,7 @@ func (r CommandMap) AppendSysResource(sr string, sys *system.System, config util func (r CommandMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Command, system.Command, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - cmd := util.ExecCommand{CmdStr: sr} - sysres := sys.NewCommand(ctx, cmd, sys, util.Config{}) + sysres, _ := sys.NewCommand(ctx, sr, sys, util.Config{}) res, err := NewCommand(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -223,7 +221,7 @@ type DNSMap map[string]*DNS func (r DNSMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*DNS, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewDNS(ctx, sr, sys, config) + sysres, _ := sys.NewDNS(ctx, sr, sys, config) res, err := NewDNS(sysres, config) if err != nil { return nil, err @@ -238,7 +236,7 @@ func (r DNSMap) AppendSysResource(sr string, sys *system.System, config util.Con func (r DNSMap) AppendSysResourceIfExists(sr string, sys *system.System) (*DNS, system.DNS, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewDNS(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewDNS(ctx, sr, sys, util.Config{}) res, err := NewDNS(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -324,7 +322,7 @@ type FileMap map[string]*File func (r FileMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*File, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewFile(ctx, sr, sys, config) + sysres, _ := sys.NewFile(ctx, sr, sys, config) res, err := NewFile(sysres, config) if err != nil { return nil, err @@ -339,7 +337,7 @@ func (r FileMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r FileMap) AppendSysResourceIfExists(sr string, sys *system.System) (*File, system.File, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewFile(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewFile(ctx, sr, sys, util.Config{}) res, err := NewFile(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -425,7 +423,7 @@ type GossfileMap map[string]*Gossfile func (r GossfileMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Gossfile, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGossfile(ctx, sr, sys, config) + sysres, _ := sys.NewGossfile(ctx, sr, sys, config) res, err := NewGossfile(sysres, config) if err != nil { return nil, err @@ -440,7 +438,7 @@ func (r GossfileMap) AppendSysResource(sr string, sys *system.System, config uti func (r GossfileMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Gossfile, system.Gossfile, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGossfile(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewGossfile(ctx, sr, sys, util.Config{}) res, err := NewGossfile(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -526,7 +524,7 @@ type GroupMap map[string]*Group func (r GroupMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Group, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGroup(ctx, sr, sys, config) + sysres, _ := sys.NewGroup(ctx, sr, sys, config) res, err := NewGroup(sysres, config) if err != nil { return nil, err @@ -541,7 +539,7 @@ func (r GroupMap) AppendSysResource(sr string, sys *system.System, config util.C func (r GroupMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Group, system.Group, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewGroup(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewGroup(ctx, sr, sys, util.Config{}) res, err := NewGroup(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -627,7 +625,7 @@ type PackageMap map[string]*Package func (r PackageMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Package, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPackage(ctx, sr, sys, config) + sysres, _ := sys.NewPackage(ctx, sr, sys, config) res, err := NewPackage(sysres, config) if err != nil { return nil, err @@ -642,7 +640,7 @@ func (r PackageMap) AppendSysResource(sr string, sys *system.System, config util func (r PackageMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Package, system.Package, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPackage(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewPackage(ctx, sr, sys, util.Config{}) res, err := NewPackage(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -728,7 +726,7 @@ type PortMap map[string]*Port func (r PortMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Port, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPort(ctx, sr, sys, config) + sysres, _ := sys.NewPort(ctx, sr, sys, config) res, err := NewPort(sysres, config) if err != nil { return nil, err @@ -743,7 +741,7 @@ func (r PortMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r PortMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Port, system.Port, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewPort(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewPort(ctx, sr, sys, util.Config{}) res, err := NewPort(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -829,7 +827,7 @@ type ProcessMap map[string]*Process func (r ProcessMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Process, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewProcess(ctx, sr, sys, config) + sysres, _ := sys.NewProcess(ctx, sr, sys, config) res, err := NewProcess(sysres, config) if err != nil { return nil, err @@ -844,7 +842,7 @@ func (r ProcessMap) AppendSysResource(sr string, sys *system.System, config util func (r ProcessMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Process, system.Process, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewProcess(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewProcess(ctx, sr, sys, util.Config{}) res, err := NewProcess(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -930,7 +928,7 @@ type ServiceMap map[string]*Service func (r ServiceMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Service, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewService(ctx, sr, sys, config) + sysres, _ := sys.NewService(ctx, sr, sys, config) res, err := NewService(sysres, config) if err != nil { return nil, err @@ -945,7 +943,7 @@ func (r ServiceMap) AppendSysResource(sr string, sys *system.System, config util func (r ServiceMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Service, system.Service, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewService(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewService(ctx, sr, sys, util.Config{}) res, err := NewService(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1031,7 +1029,7 @@ type UserMap map[string]*User func (r UserMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*User, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewUser(ctx, sr, sys, config) + sysres, _ := sys.NewUser(ctx, sr, sys, config) res, err := NewUser(sysres, config) if err != nil { return nil, err @@ -1046,7 +1044,7 @@ func (r UserMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r UserMap) AppendSysResourceIfExists(sr string, sys *system.System) (*User, system.User, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewUser(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewUser(ctx, sr, sys, util.Config{}) res, err := NewUser(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1132,7 +1130,7 @@ type KernelParamMap map[string]*KernelParam func (r KernelParamMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*KernelParam, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewKernelParam(ctx, sr, sys, config) + sysres, _ := sys.NewKernelParam(ctx, sr, sys, config) res, err := NewKernelParam(sysres, config) if err != nil { return nil, err @@ -1147,7 +1145,7 @@ func (r KernelParamMap) AppendSysResource(sr string, sys *system.System, config func (r KernelParamMap) AppendSysResourceIfExists(sr string, sys *system.System) (*KernelParam, system.KernelParam, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewKernelParam(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewKernelParam(ctx, sr, sys, util.Config{}) res, err := NewKernelParam(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1233,7 +1231,7 @@ type MountMap map[string]*Mount func (r MountMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Mount, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewMount(ctx, sr, sys, config) + sysres, _ := sys.NewMount(ctx, sr, sys, config) res, err := NewMount(sysres, config) if err != nil { return nil, err @@ -1248,7 +1246,7 @@ func (r MountMap) AppendSysResource(sr string, sys *system.System, config util.C func (r MountMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Mount, system.Mount, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewMount(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewMount(ctx, sr, sys, util.Config{}) res, err := NewMount(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1334,7 +1332,7 @@ type InterfaceMap map[string]*Interface func (r InterfaceMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Interface, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewInterface(ctx, sr, sys, config) + sysres, _ := sys.NewInterface(ctx, sr, sys, config) res, err := NewInterface(sysres, config) if err != nil { return nil, err @@ -1349,7 +1347,7 @@ func (r InterfaceMap) AppendSysResource(sr string, sys *system.System, config ut func (r InterfaceMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Interface, system.Interface, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewInterface(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewInterface(ctx, sr, sys, util.Config{}) res, err := NewInterface(sysres, util.Config{}) if err != nil { return nil, nil, false, err @@ -1435,7 +1433,7 @@ type HTTPMap map[string]*HTTP func (r HTTPMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*HTTP, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewHTTP(ctx, sr, sys, config) + sysres, _ := sys.NewHTTP(ctx, sr, sys, config) res, err := NewHTTP(sysres, config) if err != nil { return nil, err @@ -1450,7 +1448,7 @@ func (r HTTPMap) AppendSysResource(sr string, sys *system.System, config util.Co func (r HTTPMap) AppendSysResourceIfExists(sr string, sys *system.System) (*HTTP, system.HTTP, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewHTTP(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewHTTP(ctx, sr, sys, util.Config{}) res, err := NewHTTP(sysres, util.Config{}) if err != nil { return nil, nil, false, err diff --git a/resource/resource_list_genny.go b/resource/resource_list_genny.go index a483bc4ea..3a5178c07 100644 --- a/resource/resource_list_genny.go +++ b/resource/resource_list_genny.go @@ -27,7 +27,7 @@ type ResourceTypeMap map[string]*ResourceType func (r ResourceTypeMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*ResourceType, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewResourceType(ctx, sr, sys, config) + sysres, _ := sys.NewResourceType(ctx, sr, sys, config) res, err := NewResourceType(sysres, config) if err != nil { return nil, err @@ -42,7 +42,7 @@ func (r ResourceTypeMap) AppendSysResource(sr string, sys *system.System, config func (r ResourceTypeMap) AppendSysResourceIfExists(sr string, sys *system.System) (*ResourceType, system.ResourceType, bool, error) { ctx := context.WithValue(context.Background(), "id", sr) - sysres := sys.NewResourceType(ctx, sr, sys, util.Config{}) + sysres, _ := sys.NewResourceType(ctx, sr, sys, util.Config{}) res, err := NewResourceType(sysres, util.Config{}) if err != nil { return nil, nil, false, err diff --git a/resource/service.go b/resource/service.go index 2285c3e1f..392fc94f8 100644 --- a/resource/service.go +++ b/resource/service.go @@ -50,7 +50,7 @@ func (s *Service) GetName() string { func (s *Service) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", s.ID()) skip := s.Skip - sysservice := sys.NewService(ctx, s.GetName(), sys, util.Config{}) + sysservice, _ := sys.NewService(ctx, s.GetName(), sys, util.Config{}) var results []TestResult if s.Enabled != nil { diff --git a/resource/user.go b/resource/user.go index 3895f8067..7211cd45e 100644 --- a/resource/user.go +++ b/resource/user.go @@ -53,7 +53,7 @@ func (u *User) GetUsername() string { func (u *User) Validate(sys *system.System) []TestResult { ctx := context.WithValue(context.Background(), "id", u.ID()) skip := u.Skip - sysuser := sys.NewUser(ctx, u.GetUsername(), sys, util.Config{}) + sysuser, _ := sys.NewUser(ctx, u.GetUsername(), sys, util.Config{}) var results []TestResult results = append(results, ValidateValue(u, "exists", u.Exists, sysuser.Exists, skip)) diff --git a/system/addr.go b/system/addr.go index 35ed063df..8f1f32d82 100644 --- a/system/addr.go +++ b/system/addr.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "net" "strings" "time" @@ -21,7 +22,15 @@ type DefAddr struct { Timeout int } -func NewDefAddr(_ context.Context, address string, system *System, config util.Config) Addr { +func NewDefAddr(_ context.Context, address interface{}, system *System, config util.Config) (Addr, error) { + strAddress, ok := address.(string) + if !ok { + return nil, fmt.Errorf("address must be of type string") + } + return newDefAddr(nil, strAddress, system, config), nil +} + +func newDefAddr(_ context.Context, address string, system *System, config util.Config) Addr { addr := normalizeAddress(address) return &DefAddr{ address: addr, diff --git a/system/command.go b/system/command.go index f5e432bec..2020a379a 100644 --- a/system/command.go +++ b/system/command.go @@ -30,10 +30,29 @@ type DefCommand struct { err error } -func NewDefCommand(ctx context.Context, command util.ExecCommand, system *System, config util.Config) Command { +func NewDefCommand(ctx context.Context, command interface{}, system *System, config util.Config) (Command, error) { + switch cmd := command.(type) { + case string: + return newDefCommand(ctx, cmd, system, config), nil + case []string: + return newDefExecCommand(ctx, cmd, system, config), nil + default: + return nil, fmt.Errorf("command type must be either string or []string") + } +} + +func newDefCommand(ctx context.Context, command string, system *System, config util.Config) Command { + return &DefCommand{ + Ctx: ctx, + command: util.ExecCommand{CmdStr: command}, + Timeout: config.TimeOutMilliSeconds(), + } +} + +func newDefExecCommand(ctx context.Context, command []string, system *System, config util.Config) Command { return &DefCommand{ Ctx: ctx, - command: command, + command: util.ExecCommand{CmdSlice: command}, Timeout: config.TimeOutMilliSeconds(), } } diff --git a/system/dns.go b/system/dns.go index a73b4229e..27b49f92c 100644 --- a/system/dns.go +++ b/system/dns.go @@ -34,7 +34,15 @@ type DefDNS struct { qtype string } -func NewDefDNS(_ context.Context, host string, system *System, config util.Config) DNS { +func NewDefDNS(_ context.Context, host interface{}, system *System, config util.Config) (DNS, error) { + strHost, ok := host.(string) + if !ok { + return nil, fmt.Errorf("host must be of type string") + } + return newDefDNS(nil, strHost, system, config), nil +} + +func newDefDNS(_ context.Context, host string, system *System, config util.Config) DNS { var h string var t string diff --git a/system/file.go b/system/file.go index 736c25ee8..3869d3e0b 100644 --- a/system/file.go +++ b/system/file.go @@ -48,7 +48,15 @@ type DefFile struct { err error } -func NewDefFile(_ context.Context, path string, system *System, config util.Config) File { +func NewDefFile(_ context.Context, path interface{}, system *System, config util.Config) (File, error) { + strPath, ok := path.(string) + if !ok { + return nil, fmt.Errorf("path must be of type string") + } + return newDefFile(nil, strPath, system, config), nil +} + +func newDefFile(_ context.Context, path string, system *System, config util.Config) File { var err error if !strings.HasPrefix(path, "~") { path, err = filepath.Abs(path) diff --git a/system/gossfile.go b/system/gossfile.go index 1af67852b..abfa05d59 100644 --- a/system/gossfile.go +++ b/system/gossfile.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "github.com/goss-org/goss/util" ) @@ -24,6 +25,14 @@ func (g *DefGossfile) Exists() (bool, error) { return false, nil } -func NewDefGossfile(_ context.Context, path string, system *System, config util.Config) Gossfile { +func NewDefGossfile(_ context.Context, path interface{}, system *System, config util.Config) (Gossfile, error) { + strPath, ok := path.(string) + if !ok { + return nil, fmt.Errorf("path must be of type string") + } + return newDefGossfile(nil, strPath, system, config), nil +} + +func newDefGossfile(_ context.Context, path string, system *System, config util.Config) Gossfile { return &DefGossfile{path: path} } diff --git a/system/group.go b/system/group.go index 9687969f9..cfc1c828a 100644 --- a/system/group.go +++ b/system/group.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "os/user" "strconv" @@ -18,7 +19,15 @@ type DefGroup struct { groupname string } -func NewDefGroup(_ context.Context, groupname string, system *System, config util.Config) Group { +func NewDefGroup(_ context.Context, groupname interface{}, system *System, config util.Config) (Group, error) { + strGroupname, ok := groupname.(string) + if !ok { + return nil, fmt.Errorf("groupname must be of type string") + } + return newDefGroup(nil, strGroupname, system, config), nil +} + +func newDefGroup(_ context.Context, groupname string, system *System, config util.Config) Group { return &DefGroup{groupname: groupname} } diff --git a/system/http.go b/system/http.go index cdbf589cf..96b8f1c21 100644 --- a/system/http.go +++ b/system/http.go @@ -45,7 +45,15 @@ type DefHTTP struct { Proxy string } -func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP { +func NewDefHTTP(_ context.Context, httpStr interface{}, system *System, config util.Config) (HTTP, error) { + strHttpStr, ok := httpStr.(string) + if !ok { + return nil, fmt.Errorf("httpStr must be of type string") + } + return newDefHTTP(nil, strHttpStr, system, config), nil +} + +func newDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP { headers := http.Header{} for _, r := range config.RequestHeader { str := strings.SplitN(r, ": ", 2) diff --git a/system/interface.go b/system/interface.go index 83585f398..036abc59d 100644 --- a/system/interface.go +++ b/system/interface.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "net" "github.com/goss-org/goss/util" @@ -22,7 +23,15 @@ type DefInterface struct { err error } -func NewDefInterface(_ context.Context, name string, systei *System, config util.Config) Interface { +func NewDefInterface(_ context.Context, name interface{}, system *System, config util.Config) (Interface, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newDefInterface(nil, strName, system, config), nil +} + +func newDefInterface(_ context.Context, name string, systei *System, config util.Config) Interface { return &DefInterface{ name: name, } diff --git a/system/kernel_param.go b/system/kernel_param.go index 867e08f65..c83ffa8a5 100644 --- a/system/kernel_param.go +++ b/system/kernel_param.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "github.com/achanda/go-sysctl" "github.com/goss-org/goss/util" @@ -18,7 +19,15 @@ type DefKernelParam struct { value string } -func NewDefKernelParam(_ context.Context, key string, system *System, config util.Config) KernelParam { +func NewDefKernelParam(_ context.Context, key interface{}, system *System, config util.Config) (KernelParam, error) { + strKey, ok := key.(string) + if !ok { + return nil, fmt.Errorf("key must be of type string") + } + return newDefKernelParam(nil, strKey, system, config), nil +} + +func newDefKernelParam(_ context.Context, key string, system *System, config util.Config) KernelParam { return &DefKernelParam{ key: key, } diff --git a/system/mount.go b/system/mount.go index fb8902bb6..ba0d9fe37 100644 --- a/system/mount.go +++ b/system/mount.go @@ -29,7 +29,15 @@ type DefMount struct { err error } -func NewDefMount(_ context.Context, mountPoint string, system *System, config util.Config) Mount { +func NewDefMount(_ context.Context, mountPoint interface{}, system *System, config util.Config) (Mount, error) { + strMountPoint, ok := mountPoint.(string) + if !ok { + return nil, fmt.Errorf("mountPoint must be of type string") + } + return newDefMount(nil, strMountPoint, system, config), nil +} + +func newDefMount(_ context.Context, mountPoint string, system *System, config util.Config) Mount { return &DefMount{ mountPoint: mountPoint, } diff --git a/system/package.go b/system/package.go index 2944a5a25..813c56f21 100644 --- a/system/package.go +++ b/system/package.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "github.com/goss-org/goss/util" ) @@ -20,7 +21,15 @@ type NullPackage struct { name string } -func NewNullPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewNullPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newNullPackage(nil, strName, system, config), nil +} + +func newNullPackage(_ context.Context, name string, system *System, config util.Config) Package { return &NullPackage{name: name} } diff --git a/system/package_alpine.go b/system/package_alpine.go index 9aa883a1f..96119170c 100644 --- a/system/package_alpine.go +++ b/system/package_alpine.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type AlpinePackage struct { installed bool } -func NewAlpinePackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewAlpinePackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newAlpinePackage(nil, strName, system, config), nil +} + +func newAlpinePackage(_ context.Context, name string, system *System, config util.Config) Package { return &AlpinePackage{name: name} } diff --git a/system/package_deb.go b/system/package_deb.go index 9c04fb761..032918c66 100644 --- a/system/package_deb.go +++ b/system/package_deb.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type DebPackage struct { installed bool } -func NewDebPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewDebPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newDebPackage(nil, strName, system, config), nil +} + +func newDebPackage(_ context.Context, name string, system *System, config util.Config) Package { return &DebPackage{name: name} } diff --git a/system/package_pacman.go b/system/package_pacman.go index 459fab82d..b2bc05169 100644 --- a/system/package_pacman.go +++ b/system/package_pacman.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type PacmanPackage struct { installed bool } -func NewPacmanPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewPacmanPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newPacmanPackage(nil, strName, system, config), nil +} + +func newPacmanPackage(_ context.Context, name string, system *System, config util.Config) Package { return &PacmanPackage{name: name} } diff --git a/system/package_rpm.go b/system/package_rpm.go index 6da5a58cf..780f8b98b 100644 --- a/system/package_rpm.go +++ b/system/package_rpm.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "strings" "github.com/goss-org/goss/util" @@ -15,7 +16,15 @@ type RpmPackage struct { installed bool } -func NewRpmPackage(_ context.Context, name string, system *System, config util.Config) Package { +func NewRpmPackage(_ context.Context, name interface{}, system *System, config util.Config) (Package, error) { + strName, ok := name.(string) + if !ok { + return nil, fmt.Errorf("name must be of type string") + } + return newRpmPackage(nil, strName, system, config), nil +} + +func newRpmPackage(_ context.Context, name string, system *System, config util.Config) Package { return &RpmPackage{name: name} } diff --git a/system/port.go b/system/port.go index b5cda8fbc..61b9e9da4 100644 --- a/system/port.go +++ b/system/port.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "strconv" "strings" @@ -21,7 +22,15 @@ type DefPort struct { sysPorts map[string][]GOnetstat.Process } -func NewDefPort(_ context.Context, port string, system *System, config util.Config) Port { +func NewDefPort(_ context.Context, port interface{}, system *System, config util.Config) (Port, error) { + strPort, ok := port.(string) + if !ok { + return nil, fmt.Errorf("port must be of type string") + } + return newDefPort(nil, strPort, system, config), nil +} + +func newDefPort(_ context.Context, port string, system *System, config util.Config) Port { p := normalizePort(port) return &DefPort{ port: p, diff --git a/system/process.go b/system/process.go index 271fac7ad..0279de94f 100644 --- a/system/process.go +++ b/system/process.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "github.com/goss-org/go-ps" "github.com/goss-org/goss/util" @@ -20,7 +21,15 @@ type DefProcess struct { err error } -func NewDefProcess(_ context.Context, executable string, system *System, config util.Config) Process { +func NewDefProcess(_ context.Context, executable interface{}, system *System, config util.Config) (Process, error) { + strExecutable, ok := executable.(string) + if !ok { + return nil, fmt.Errorf("executable must be of type string") + } + return newDefProcess(nil, strExecutable, system, config), nil +} + +func newDefProcess(_ context.Context, executable string, system *System, config util.Config) Process { pmap, err := system.ProcMap() return &DefProcess{ executable: executable, diff --git a/system/service_init.go b/system/service_init.go index cf1536b7d..7a3051436 100644 --- a/system/service_init.go +++ b/system/service_init.go @@ -16,11 +16,27 @@ type ServiceInit struct { runlevel string } -func NewServiceInit(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceInit(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceInit(nil, strService, system, config), nil +} + +func newServiceInit(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceInit{service: service} } -func NewAlpineServiceInit(_ context.Context, service string, system *System, config util.Config) Service { +func NewAlpineServiceInit(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newAlpineServiceInit(nil, strService, system, config), nil +} + +func newAlpineServiceInit(_ context.Context, service string, system *System, config util.Config) Service { runlevel := config.RunLevel if runlevel == "" { runlevel = "sysinit" diff --git a/system/service_systemd.go b/system/service_systemd.go index 01d4a244a..e59f599a9 100644 --- a/system/service_systemd.go +++ b/system/service_systemd.go @@ -13,13 +13,29 @@ type ServiceSystemd struct { legacy bool } -func NewServiceSystemd(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceSystemd(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceSystemd(nil, strService, system, config), nil +} + +func newServiceSystemd(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceSystemd{ service: service, } } -func NewServiceSystemdLegacy(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceSystemdLegacy(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceSystemdLegacy(nil, strService, system, config), nil +} + +func newServiceSystemdLegacy(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceSystemd{ service: service, legacy: true, diff --git a/system/service_upstart.go b/system/service_upstart.go index 9c24e62fe..dd1ae3c58 100644 --- a/system/service_upstart.go +++ b/system/service_upstart.go @@ -18,7 +18,15 @@ type ServiceUpstart struct { var upstartEnabled = regexp.MustCompile(`^\s*start on`) var upstartDisabled = regexp.MustCompile(`^manual`) -func NewServiceUpstart(_ context.Context, service string, system *System, config util.Config) Service { +func NewServiceUpstart(_ context.Context, service interface{}, system *System, config util.Config) (Service, error) { + strService, ok := service.(string) + if !ok { + return nil, fmt.Errorf("service must be of type string") + } + return newServiceUpstart(nil, strService, system, config), nil +} + +func newServiceUpstart(_ context.Context, service string, system *System, config util.Config) Service { return &ServiceUpstart{service: service} } diff --git a/system/system.go b/system/system.go index b0e876e8b..7cc625cde 100644 --- a/system/system.go +++ b/system/system.go @@ -20,21 +20,21 @@ type Resource interface { } type System struct { - NewPackage func(context.Context, string, *System, util2.Config) Package - NewFile func(context.Context, string, *System, util2.Config) File - NewAddr func(context.Context, string, *System, util2.Config) Addr - NewPort func(context.Context, string, *System, util2.Config) Port - NewService func(context.Context, string, *System, util2.Config) Service - NewUser func(context.Context, string, *System, util2.Config) User - NewGroup func(context.Context, string, *System, util2.Config) Group - NewCommand func(context.Context, util2.ExecCommand, *System, util2.Config) Command - NewDNS func(context.Context, string, *System, util2.Config) DNS - NewProcess func(context.Context, string, *System, util2.Config) Process - NewGossfile func(context.Context, string, *System, util2.Config) Gossfile - NewKernelParam func(context.Context, string, *System, util2.Config) KernelParam - NewMount func(context.Context, string, *System, util2.Config) Mount - NewInterface func(context.Context, string, *System, util2.Config) Interface - NewHTTP func(context.Context, string, *System, util2.Config) HTTP + NewPackage func(context.Context, interface{}, *System, util2.Config) (Package, error) + NewFile func(context.Context, interface{}, *System, util2.Config) (File, error) + NewAddr func(context.Context, interface{}, *System, util2.Config) (Addr, error) + NewPort func(context.Context, interface{}, *System, util2.Config) (Port, error) + NewService func(context.Context, interface{}, *System, util2.Config) (Service, error) + NewUser func(context.Context, interface{}, *System, util2.Config) (User, error) + NewGroup func(context.Context, interface{}, *System, util2.Config) (Group, error) + NewCommand func(context.Context, interface{}, *System, util2.Config) (Command, error) + NewDNS func(context.Context, interface{}, *System, util2.Config) (DNS, error) + NewProcess func(context.Context, interface{}, *System, util2.Config) (Process, error) + NewGossfile func(context.Context, interface{}, *System, util2.Config) (Gossfile, error) + NewKernelParam func(context.Context, interface{}, *System, util2.Config) (KernelParam, error) + NewMount func(context.Context, interface{}, *System, util2.Config) (Mount, error) + NewInterface func(context.Context, interface{}, *System, util2.Config) (Interface, error) + NewHTTP func(context.Context, interface{}, *System, util2.Config) (HTTP, error) ports map[string][]GOnetstat.Process portsOnce sync.Once procMap map[string][]ps.Process diff --git a/system/user.go b/system/user.go index 832715e7a..f0c29a75d 100644 --- a/system/user.go +++ b/system/user.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "os/user" "strconv" @@ -22,7 +23,15 @@ type DefUser struct { username string } -func NewDefUser(_ context.Context, username string, system *System, config util.Config) User { +func NewDefUser(_ context.Context, username interface{}, system *System, config util.Config) (User, error) { + strUsername, ok := username.(string) + if !ok { + return nil, fmt.Errorf("username must be of type string") + } + return newDefUser(nil, strUsername, system, config), nil +} + +func newDefUser(_ context.Context, username string, system *System, config util.Config) User { return &DefUser{username: username} } From c129fd41c0f759e8e4a557c878c623ceaabf4195 Mon Sep 17 00:00:00 2001 From: Berne Campbell Date: Mon, 19 Feb 2024 01:26:58 +1100 Subject: [PATCH 7/7] Fix GetExec to return c.id as plain string --- resource/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource/command.go b/resource/command.go index eb12c67e4..0c4db8b15 100644 --- a/resource/command.go +++ b/resource/command.go @@ -48,7 +48,7 @@ func (c *Command) GetExec() interface{} { } else if len(c.Exec.CmdSlice) > 0 { return c.Exec.CmdSlice } else { - return util.ExecCommand{CmdStr: c.id} + return c.id } }