Skip to content

Commit

Permalink
cscli refact: package 'clicapi', 'clilapi' (#3185)
Browse files Browse the repository at this point in the history
* extract functions to own files

* package clilapi

* package clicapi

* package crowdsec-cli/reload
  • Loading branch information
mmetc authored Aug 26, 2024
1 parent da495e8 commit 9c0422f
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 139 deletions.
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 @@ 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/alertcontext"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
Expand All @@ -28,11 +30,13 @@ import (

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 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string) e
cfg := cli.cfg()

if lapiUser == "" {
lapiUser, err = generateID("")
lapiUser, err = idgen.GenerateMachineID("")
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 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string) e
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

valueFound := false

// get the index
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]
}

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 @@ import (
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 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
if metabasePassword == "" {
isValid := passwordIsValid(metabasePassword)
for !isValid {
metabasePassword = generatePassword(16)
metabasePassword = idgen.GeneratePassword(16)
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
}

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

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

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

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

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

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

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"

charset := upper + lower + digits
charsetLength := len(charset)

buf := make([]byte, length)

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

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

0 comments on commit 9c0422f

Please sign in to comment.