Skip to content

Commit

Permalink
cscli machines/bouncers: dry helper code and move to cscli (#3123)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jul 11, 2024
1 parent 6f5e970 commit 1b01041
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 77 deletions.
90 changes: 62 additions & 28 deletions cmd/crowdsec-cli/bouncers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)

type featureflagProvider interface {
GetFeatureflags() string
}

type osProvider interface {
GetOsname() string
GetOsversion() string
}

func getOSNameAndVersion(o osProvider) string {
ret := o.GetOsname()
if o.GetOsversion() != "" {
if ret != "" {
ret += "/"
}

ret += o.GetOsversion()
}

if ret == "" {
return "?"
}

return ret
}

func getFeatureFlagList(o featureflagProvider) []string {
if o.GetFeatureflags() == "" {
return nil
}

return strings.Split(o.GetFeatureflags(), ",")
}

func askYesNo(message string, defaultAnswer bool) (bool, error) {
var answer bool

Expand Down Expand Up @@ -113,32 +147,32 @@ func (cli *cliBouncers) listHuman(out io.Writer, bouncers ent.Bouncers) {

// bouncerInfo contains only the data we want for inspect/list
type bouncerInfo struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Revoked bool `json:"revoked"`
IPAddress string `json:"ip_address"`
Type string `json:"type"`
Version string `json:"version"`
LastPull *time.Time `json:"last_pull"`
AuthType string `json:"auth_type"`
OS string `json:"os,omitempty"`
Featureflags []string `json:"featureflags,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Revoked bool `json:"revoked"`
IPAddress string `json:"ip_address"`
Type string `json:"type"`
Version string `json:"version"`
LastPull *time.Time `json:"last_pull"`
AuthType string `json:"auth_type"`
OS string `json:"os,omitempty"`
Featureflags []string `json:"featureflags,omitempty"`
}

func newBouncerInfo(b *ent.Bouncer) bouncerInfo {
return bouncerInfo{
CreatedAt: b.CreatedAt,
UpdatedAt: b.UpdatedAt,
Name: b.Name,
Revoked: b.Revoked,
IPAddress: b.IPAddress,
Type: b.Type,
Version: b.Version,
LastPull: b.LastPull,
AuthType: b.AuthType,
OS: b.GetOSNameAndVersion(),
Featureflags: b.GetFeatureFlagList(),
CreatedAt: b.CreatedAt,
UpdatedAt: b.UpdatedAt,
Name: b.Name,
Revoked: b.Revoked,
IPAddress: b.IPAddress,
Type: b.Type,
Version: b.Version,
LastPull: b.LastPull,
AuthType: b.AuthType,
OS: getOSNameAndVersion(b),
Featureflags: getFeatureFlagList(b),
}
}

Expand Down Expand Up @@ -166,10 +200,10 @@ func (cli *cliBouncers) listCSV(out io.Writer, bouncers ent.Bouncers) error {
}

csvwriter.Flush()

return nil
}


func (cli *cliBouncers) list(out io.Writer) error {
bouncers, err := cli.db.ListBouncers()
if err != nil {
Expand Down Expand Up @@ -342,7 +376,7 @@ func (cli *cliBouncers) newDeleteCmd() *cobra.Command {
func (cli *cliBouncers) prune(duration time.Duration, force bool) error {
if duration < 2*time.Minute {
if yes, err := askYesNo(
"The duration you provided is less than 2 minutes. " +
"The duration you provided is less than 2 minutes. "+
"This may remove active bouncers. Continue?", false); err != nil {
return err
} else if !yes {
Expand All @@ -365,7 +399,7 @@ func (cli *cliBouncers) prune(duration time.Duration, force bool) error {

if !force {
if yes, err := askYesNo(
"You are about to PERMANENTLY remove the above bouncers from the database. " +
"You are about to PERMANENTLY remove the above bouncers from the database. "+
"These will NOT be recoverable. Continue?", false); err != nil {
return err
} else if !yes {
Expand Down Expand Up @@ -434,10 +468,10 @@ func (cli *cliBouncers) inspectHuman(out io.Writer, bouncer *ent.Bouncer) {
{"Version", bouncer.Version},
{"Last Pull", lastPull},
{"Auth type", bouncer.AuthType},
{"OS", bouncer.GetOSNameAndVersion()},
{"OS", getOSNameAndVersion(bouncer)},
})

for _, ff := range bouncer.GetFeatureFlagList() {
for _, ff := range getFeatureFlagList(bouncer) {
t.AppendRow(table.Row{"Feature Flags", ff})
}

Expand All @@ -463,10 +497,10 @@ func (cli *cliBouncers) inspect(bouncer *ent.Bouncer) error {
default:
return fmt.Errorf("output format '%s' not supported for this command", outputFormat)
}

return nil
}


func (cli *cliBouncers) newInspectCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "inspect [bouncer_name]",
Expand Down
10 changes: 5 additions & 5 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (cli *cliMachines) listHuman(out io.Writer, machines ent.Machines) {
hb = emoji.Warning + " " + hb
}

t.AppendRow(table.Row{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, m.GetOSNameAndVersion(), m.AuthType, hb})
t.AppendRow(table.Row{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, getOSNameAndVersion(m), m.AuthType, hb})
}

fmt.Fprintln(out, t.Render())
Expand Down Expand Up @@ -236,8 +236,8 @@ func newMachineInfo(m *ent.Machine) machineInfo {
Version: m.Version,
IsValidated: m.IsValidated,
AuthType: m.AuthType,
OS: m.GetOSNameAndVersion(),
Featureflags: m.GetFeatureFlagList(),
OS: getOSNameAndVersion(m),
Featureflags: getFeatureFlagList(m),
Datasources: m.Datasources,
}
}
Expand Down Expand Up @@ -642,15 +642,15 @@ func (cli *cliMachines) inspectHuman(out io.Writer, machine *ent.Machine) {
{"Last Heartbeat", machine.LastHeartbeat},
{"Validated?", machine.IsValidated},
{"CrowdSec version", machine.Version},
{"OS", machine.GetOSNameAndVersion()},
{"OS", getOSNameAndVersion(machine)},
{"Auth type", machine.AuthType},
})

for dsName, dsCount := range machine.Datasources {
t.AppendRow(table.Row{"Datasources", fmt.Sprintf("%s: %d", dsName, dsCount)})
}

for _, ff := range machine.GetFeatureFlagList() {
for _, ff := range getFeatureFlagList(machine) {
t.AppendRow(table.Row{"Feature Flags", ff})
}

Expand Down
58 changes: 14 additions & 44 deletions pkg/database/ent/helpers.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,25 @@
package ent

import (
"strings"
)

func (m *Machine) GetOSNameAndVersion() string {
ret := m.Osname
if m.Osversion != "" {
if ret != "" {
ret += "/"
}

ret += m.Osversion
}

if ret == "" {
return "?"
}

return ret
func (m *Machine) GetOsname() string {
return m.Osname
}

func (b *Bouncer) GetOSNameAndVersion() string {
ret := b.Osname
if b.Osversion != "" {
if ret != "" {
ret += "/"
}

ret += b.Osversion
}

if ret == "" {
return "?"
}

return ret
func (b *Bouncer) GetOsname() string {
return b.Osname
}

func (m *Machine) GetFeatureFlagList() []string {
if m.Featureflags == "" {
return nil
}
func (m *Machine) GetOsversion() string {
return m.Osversion
}

return strings.Split(m.Featureflags, ",")
func (b *Bouncer) GetOsversion() string {
return b.Osversion
}

func (b *Bouncer) GetFeatureFlagList() []string {
if b.Featureflags == "" {
return nil
}
func (m *Machine) GetFeatureflags() string {
return m.Featureflags
}

return strings.Split(b.Featureflags, ",")
func (b *Bouncer) GetFeatureflags() string {
return b.Featureflags
}

0 comments on commit 1b01041

Please sign in to comment.