Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cscli machines: lint + write output to stdout instead of log #2657

Merged
merged 13 commits into from
Dec 13, 2023
52 changes: 26 additions & 26 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"io"
"math/big"
"os"
"slices"
"strings"
"time"

Expand All @@ -18,16 +17,16 @@
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
"slices"

"github.com/crowdsecurity/machineid"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/database"
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
"github.com/crowdsecurity/crowdsec/pkg/types"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
)

const passwordLength = 64
Expand Down Expand Up @@ -63,9 +62,9 @@
}
log.Debugf("failed to get machine-id with usual files: %s", err)

bId, err := uuid.NewRandom()
bID, err := uuid.NewRandom()

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L65

Added line #L65 was not covered by tests
if err == nil {
return bId.String(), nil
return bID.String(), nil

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L67

Added line #L67 was not covered by tests
}
return "", fmt.Errorf("generating machine id: %w", err)
}
Expand Down Expand Up @@ -107,27 +106,27 @@
if err != nil {
return fmt.Errorf("unable to list machines: %s", err)
}
if csConfig.Cscli.Output == "human" {

switch csConfig.Cscli.Output {
case "human":
getAgentsTable(out, machines)
} else if csConfig.Cscli.Output == "json" {
case "json":
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
if err := enc.Encode(machines); err != nil {
return fmt.Errorf("failed to marshal")
}
return nil
} else if csConfig.Cscli.Output == "raw" {
case "raw":

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L120

Added line #L120 was not covered by tests
csvwriter := csv.NewWriter(out)
err := csvwriter.Write([]string{"machine_id", "ip_address", "updated_at", "validated", "version", "auth_type", "last_heartbeat"})
if err != nil {
return fmt.Errorf("failed to write header: %s", err)
}
for _, m := range machines {
var validated string
validated := "false"

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L127

Added line #L127 was not covered by tests
if m.IsValidated {
validated = "true"
} else {
validated = "false"
}
hb, _ := getLastHeartbeat(m)
err := csvwriter.Write([]string{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, m.AuthType, hb})
Expand All @@ -136,13 +135,13 @@
}
}
csvwriter.Flush()
} else {
log.Errorf("unknown output '%s'", csConfig.Cscli.Output)
default:
return fmt.Errorf("unknown output '%s'", csConfig.Cscli.Output)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L138-L139

Added lines #L138 - L139 were not covered by tests
}
return nil
}

type cliMachines struct {}
type cliMachines struct{}

func NewCLIMachines() *cliMachines {
return &cliMachines{}
Expand All @@ -158,9 +157,9 @@
Example: `cscli machines [action]`,
DisableAutoGenTag: true,
Aliases: []string{"machine"},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
var err error
if err := require.LAPI(csConfig); err != nil {
if err = require.LAPI(csConfig); err != nil {
return err
}
dbClient, err = database.NewClient(csConfig.DbConfig)
Expand Down Expand Up @@ -188,7 +187,7 @@
Example: `cscli machines list`,
Args: cobra.NoArgs,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
err := getAgents(color.Output, dbClient)
if err != nil {
return fmt.Errorf("unable to list machines: %s", err)
Expand Down Expand Up @@ -279,7 +278,8 @@
if dumpFile == "" && csConfig.API.Client != nil && csConfig.API.Client.CredentialsFilePath != "" {
credFile := csConfig.API.Client.CredentialsFilePath
// use the default only if the file does not exist
_, err := os.Stat(credFile)
_, err = os.Stat(credFile)

switch {
case os.IsNotExist(err) || force:
dumpFile = csConfig.API.Client.CredentialsFilePath
Expand Down Expand Up @@ -311,7 +311,7 @@
if err != nil {
return fmt.Errorf("unable to create machine: %s", err)
}
log.Infof("Machine '%s' successfully added to the local API", machineID)
fmt.Printf("Machine '%s' successfully added to the local API.\n", machineID)

if apiURL == "" {
if csConfig.API.Client != nil && csConfig.API.Client.Credentials != nil && csConfig.API.Client.Credentials.URL != "" {
Expand All @@ -336,7 +336,7 @@
if err != nil {
return fmt.Errorf("write api credentials in '%s' failed: %s", dumpFile, err)
}
log.Printf("API credentials written to '%s'", dumpFile)
fmt.Printf("API credentials written to '%s'.\n", dumpFile)
} else {
fmt.Printf("%s\n", string(apiConfigDump))
}
Expand All @@ -352,7 +352,7 @@
Args: cobra.MinimumNArgs(1),
Aliases: []string{"remove"},
DisableAutoGenTag: true,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
machines, err := dbClient.ListMachines()
if err != nil {
cobra.CompError("unable to list machines " + err.Error())
Expand All @@ -371,7 +371,7 @@
return cmd
}

func (cli cliMachines) delete(cmd *cobra.Command, args []string) error {
func (cli cliMachines) delete(_ *cobra.Command, args []string) error {
for _, machineID := range args {
err := dbClient.DeleteWatcher(machineID)
if err != nil {
Expand All @@ -395,7 +395,7 @@
cscli machines prune --not-validated-only --force`,
Args: cobra.NoArgs,
DisableAutoGenTag: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, _ []string) error {
dur, _ := cmd.Flags().GetString("duration")
var err error
parsedDuration, err = time.ParseDuration(fmt.Sprintf("-%s", dur))
Expand All @@ -404,7 +404,7 @@
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L407

Added line #L407 was not covered by tests
notValidOnly, _ := cmd.Flags().GetBool("not-validated-only")
force, _ := cmd.Flags().GetBool("force")
if parsedDuration >= 0-60*time.Second && !notValidOnly {
Expand Down Expand Up @@ -472,7 +472,7 @@
Example: `cscli machines validate "machine_name"`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
machineID := args[0]
if err := dbClient.ValidateMachine(machineID); err != nil {
return fmt.Errorf("unable to validate machine '%s': %s", machineID, err)
Expand Down
4 changes: 2 additions & 2 deletions debian/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if [ "$1" = configure ]; then
db_get crowdsec/capi
CAPI=$RET

[ -s /etc/crowdsec/local_api_credentials.yaml ] || cscli machines add -a --force
[ -s /etc/crowdsec/local_api_credentials.yaml ] || cscli machines add -a --force --error

if [ "$CAPI" = true ]; then
cscli capi register
Expand Down Expand Up @@ -104,4 +104,4 @@ if [ "$1" = configure ]; then
fi
fi

echo "You can always run the configuration again interactively by using '/usr/share/crowdsec/wizard.sh -c"
echo "You can always run the configuration again interactively by using '/usr/share/crowdsec/wizard.sh -c'"
2 changes: 1 addition & 1 deletion debian/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ if [ "$1" = upgrade ]; then
fi
fi

echo "You can always run the configuration again interactively by using '/usr/share/crowdsec/wizard.sh -c"
echo "You can always run the configuration again interactively by using '/usr/share/crowdsec/wizard.sh -c'"
2 changes: 1 addition & 1 deletion debian/prerm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if [ "$1" = "remove" ]; then
cscli dashboard remove -f -y || true
cscli dashboard remove -f -y --error || echo "Ignore the above error if you never installed the local dashboard."
systemctl stop crowdsec
systemctl disable crowdsec
fi
Expand Down
2 changes: 1 addition & 1 deletion rpm/SPECS/crowdsec.spec
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ if [ $1 == 1 ]; then
fi
if [ ! -f "%{_sysconfdir}/crowdsec/local_api_credentials.yaml" ] ; then
install -m 600 /dev/null /etc/crowdsec/local_api_credentials.yaml
cscli machines add -a --force
cscli machines add -a --force --error
fi

cscli hub update
Expand Down
6 changes: 3 additions & 3 deletions test/bats/30_machines.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ teardown() {
rune -0 jq -r '.msg' <(stderr)
assert_output --partial 'already exists: please remove it, use "--force" or specify a different file with "-f"'
rune -0 cscli machines add local -a --force
assert_stderr --partial "Machine 'local' successfully added to the local API"
assert_output --partial "Machine 'local' successfully added to the local API."
}

@test "add a new machine and delete it" {
rune -0 cscli machines add -a -f /dev/null CiTestMachine -o human
assert_stderr --partial "Machine 'CiTestMachine' successfully added to the local API"
assert_stderr --partial "API credentials written to '/dev/null'"
assert_output --partial "Machine 'CiTestMachine' successfully added to the local API"
assert_output --partial "API credentials written to '/dev/null'"

# we now have two machines
rune -0 cscli machines list -o json
Expand Down