Skip to content

Commit

Permalink
cscli machines/bouncers: dry helper code and move to cscli
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jul 11, 2024
1 parent 6f5e970 commit 10b7683
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 += "/"
}

Check warning on line 44 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L42-L44

Added lines #L42 - L44 were not covered by tests

ret += o.GetOsversion()

Check warning on line 46 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L46

Added line #L46 was not covered by tests
}

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

Check warning on line 51 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L51

Added line #L51 was not covered by tests

return ret

Check warning on line 53 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L53

Added line #L53 was not covered by tests
}

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

Check warning on line 59 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L59

Added line #L59 was not covered by tests

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

Check warning on line 61 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L61

Added line #L61 was not covered by tests
}

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()

Check warning on line 203 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L203

Added line #L203 was not covered by tests
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. "+

Check warning on line 379 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L379

Added line #L379 was not covered by tests
"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. "+

Check warning on line 402 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L402

Added line #L402 was not covered by tests
"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)},

Check warning on line 471 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L471

Added line #L471 was not covered by tests
})

for _, ff := range bouncer.GetFeatureFlagList() {
for _, ff := range getFeatureFlagList(bouncer) {

Check warning on line 474 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L474

Added line #L474 was not covered by tests
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)},

Check warning on line 645 in cmd/crowdsec-cli/machines.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L645

Added line #L645 was not covered by tests
{"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) {

Check warning on line 653 in cmd/crowdsec-cli/machines.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L653

Added line #L653 was not covered by tests
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 10b7683

Please sign in to comment.