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 refact: package 'clicapi', 'clilapi' #3185

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ issues:

- linters:
- revive
path: "cmd/crowdsec-cli/machines.go"
path: "cmd/crowdsec-cli/idgen/password.go"
text: "deep-exit: .*"

- linters:
Expand Down
14 changes: 9 additions & 5 deletions cmd/crowdsec-cli/capi.go → cmd/crowdsec-cli/clicapi/capi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clicapi

import (
"context"
Expand All @@ -12,6 +12,8 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/idgen"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/reload"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
Expand All @@ -21,11 +23,13 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)

type configGetter func() *csconfig.Config

type cliCapi struct {
cfg configGetter
}

func NewCLICapi(cfg configGetter) *cliCapi {
func New(cfg configGetter) *cliCapi {
return &cliCapi{
cfg: cfg,
}
Expand Down Expand Up @@ -56,12 +60,12 @@ func (cli *cliCapi) NewCommand() *cobra.Command {
func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error {
cfg := cli.cfg()

capiUser, err := generateID(capiUserPrefix)
capiUser, err := idgen.GenerateMachineID(capiUserPrefix)
if err != nil {
return fmt.Errorf("unable to generate machine id: %w", err)
}

password := strfmt.Password(generatePassword(passwordLength))
password := strfmt.Password(idgen.GeneratePassword(idgen.PasswordLength))

apiurl, err := url.Parse(types.CAPIBaseURL)
if err != nil {
Expand Down Expand Up @@ -114,7 +118,7 @@ func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error {
fmt.Println(string(apiConfigDump))
}

log.Warning(reloadMessage)
log.Warning(reload.Message)

return nil
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/crowdsec-cli/cliconsole/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/crowdsecurity/go-cs-lib/ptr"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/reload"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
Expand All @@ -30,14 +31,12 @@ import (
type configGetter func() *csconfig.Config

type cliConsole struct {
cfg func() *csconfig.Config
reloadMessage string
cfg configGetter
}

func New(cfg configGetter, reloadMessage string) *cliConsole {
func New(cfg configGetter) *cliConsole {
return &cliConsole{
cfg: cfg,
reloadMessage: reloadMessage,
}
}

Expand Down Expand Up @@ -215,7 +214,7 @@ Enable given information push to the central API. Allows to empower the console`
log.Infof("%v have been enabled", args)
}

log.Info(cli.reloadMessage)
log.Info(reload.Message)

return nil
},
Expand Down Expand Up @@ -249,7 +248,7 @@ Disable given information push to the central API.`,
log.Infof("%v have been disabled", args)
}

log.Info(cli.reloadMessage)
log.Info(reload.Message)

return nil
},
Expand Down
16 changes: 10 additions & 6 deletions cmd/crowdsec-cli/lapi.go → cmd/crowdsec-cli/clilapi/lapi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clilapi

import (
"context"
Expand All @@ -15,6 +15,8 @@
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/idgen"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/reload"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/alertcontext"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
Expand All @@ -28,11 +30,13 @@

const LAPIURLPrefix = "v1"

type configGetter func() *csconfig.Config

type cliLapi struct {
cfg configGetter
cfg configGetter
}

func NewCLILapi(cfg configGetter) *cliLapi {
func New(cfg configGetter) *cliLapi {
return &cliLapi{
cfg: cfg,
}
Expand Down Expand Up @@ -100,13 +104,13 @@
cfg := cli.cfg()

if lapiUser == "" {
lapiUser, err = generateID("")
lapiUser, err = idgen.GenerateMachineID("")

Check warning on line 107 in cmd/crowdsec-cli/clilapi/lapi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clilapi/lapi.go#L107

Added line #L107 was not covered by tests
if err != nil {
return fmt.Errorf("unable to generate machine id: %w", err)
}
}

password := strfmt.Password(generatePassword(passwordLength))
password := strfmt.Password(idgen.GeneratePassword(idgen.PasswordLength))

apiurl, err := prepareAPIURL(cfg.API.Client, apiURL)
if err != nil {
Expand Down Expand Up @@ -158,7 +162,7 @@
fmt.Printf("%s\n", string(apiConfigDump))
}

log.Warning(reloadMessage)
log.Warning(reload.Message)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clilapi

import (
"testing"
Expand Down
24 changes: 24 additions & 0 deletions cmd/crowdsec-cli/clilapi/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package clilapi

func removeFromSlice(val string, slice []string) []string {
var i int
var value string

Check warning on line 6 in cmd/crowdsec-cli/clilapi/utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clilapi/utils.go#L6

Added line #L6 was not covered by tests
valueFound := false

// get the index

Check warning on line 9 in cmd/crowdsec-cli/clilapi/utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clilapi/utils.go#L8-L9

Added lines #L8 - L9 were not covered by tests
for i, value = range slice {
if value == val {
valueFound = true
break
}
}

if valueFound {
slice[i] = slice[len(slice)-1]
slice[len(slice)-1] = ""
slice = slice[:len(slice)-1]
}

Check warning on line 21 in cmd/crowdsec-cli/clilapi/utils.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clilapi/utils.go#L21

Added line #L21 was not covered by tests

return slice
}
3 changes: 2 additions & 1 deletion cmd/crowdsec-cli/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/idgen"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/metabase"
)
Expand Down Expand Up @@ -137,7 +138,7 @@
if metabasePassword == "" {
isValid := passwordIsValid(metabasePassword)
for !isValid {
metabasePassword = generatePassword(16)
metabasePassword = idgen.GeneratePassword(16)

Check warning on line 141 in cmd/crowdsec-cli/dashboard.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L141

Added line #L141 was not covered by tests
isValid = passwordIsValid(metabasePassword)
}
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/crowdsec-cli/idgen/machineid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package idgen

import (
"fmt"
"strings"

"github.com/google/uuid"
log "github.com/sirupsen/logrus"

"github.com/crowdsecurity/machineid"
)

// Returns a unique identifier for each crowdsec installation, using an
// identifier of the OS installation where available, otherwise a random
// string.
func generateMachineIDPrefix() (string, error) {
prefix, err := machineid.ID()
if err == nil {
return prefix, nil
}

Check warning on line 20 in cmd/crowdsec-cli/idgen/machineid.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/machineid.go#L20

Added line #L20 was not covered by tests

log.Debugf("failed to get machine-id with usual files: %s", err)

bID, err := uuid.NewRandom()
if err == nil {
return bID.String(), nil
}

Check warning on line 27 in cmd/crowdsec-cli/idgen/machineid.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/machineid.go#L22-L27

Added lines #L22 - L27 were not covered by tests

return "", fmt.Errorf("generating machine id: %w", err)

Check warning on line 29 in cmd/crowdsec-cli/idgen/machineid.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/machineid.go#L29

Added line #L29 was not covered by tests
}

// Generate a unique identifier, composed by a prefix and a random suffix.
// The prefix can be provided by a parameter to use in test environments.
func GenerateMachineID(prefix string) (string, error) {
var err error
if prefix == "" {
prefix, err = generateMachineIDPrefix()
}

Check warning on line 38 in cmd/crowdsec-cli/idgen/machineid.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/machineid.go#L38

Added line #L38 was not covered by tests

if err != nil {
return "", err
}

Check warning on line 42 in cmd/crowdsec-cli/idgen/machineid.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/machineid.go#L41-L42

Added lines #L41 - L42 were not covered by tests

prefix = strings.ReplaceAll(prefix, "-", "")[:32]
suffix := GeneratePassword(16)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/machineid.go#L46

Added line #L46 was not covered by tests
return prefix + suffix, nil
}
32 changes: 32 additions & 0 deletions cmd/crowdsec-cli/idgen/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package idgen

import (
"math/big"
saferand "crypto/rand"

log "github.com/sirupsen/logrus"
)

const PasswordLength = 64

func GeneratePassword(length int) string {
upper := "ABCDEFGHIJKLMNOPQRSTUVWXY"
lower := "abcdefghijklmnopqrstuvwxyz"
digits := "0123456789"

Check warning on line 16 in cmd/crowdsec-cli/idgen/password.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/password.go#L16

Added line #L16 was not covered by tests
charset := upper + lower + digits
charsetLength := len(charset)

Check warning on line 19 in cmd/crowdsec-cli/idgen/password.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/password.go#L19

Added line #L19 was not covered by tests
buf := make([]byte, length)

Check warning on line 21 in cmd/crowdsec-cli/idgen/password.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/password.go#L21

Added line #L21 was not covered by tests
for i := range length {
rInt, err := saferand.Int(saferand.Reader, big.NewInt(int64(charsetLength)))
if err != nil {
log.Fatalf("failed getting data from prng for password generation : %s", err)
}

Check warning on line 26 in cmd/crowdsec-cli/idgen/password.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/idgen/password.go#L25-L26

Added lines #L25 - L26 were not covered by tests

buf[i] = charset[rInt.Int64()]
}

return string(buf)
}
11 changes: 6 additions & 5 deletions cmd/crowdsec-cli/itemcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/reload"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)
Expand Down Expand Up @@ -92,7 +93,7 @@ func (cli cliItem) install(ctx context.Context, args []string, downloadOnly bool
}
}

log.Info(reloadMessage)
log.Info(reload.Message)

return nil
}
Expand Down Expand Up @@ -170,7 +171,7 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
log.Infof("Removed %d %s", removed, cli.name)

if removed > 0 {
log.Info(reloadMessage)
log.Info(reload.Message)
}

return nil
Expand Down Expand Up @@ -212,7 +213,7 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
log.Infof("Removed %d %s", removed, cli.name)

if removed > 0 {
log.Info(reloadMessage)
log.Info(reload.Message)
}

return nil
Expand Down Expand Up @@ -273,7 +274,7 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, force bool, all b
log.Infof("Updated %d %s", updated, cli.name)

if updated > 0 {
log.Info(reloadMessage)
log.Info(reload.Message)
}

return nil
Expand Down Expand Up @@ -304,7 +305,7 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, force bool, all b
}

if updated > 0 {
log.Info(reloadMessage)
log.Info(reload.Message)
}

return nil
Expand Down
Loading