-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cscli refact: package 'clicapi', 'clilapi' (#3185)
* extract functions to own files * package clilapi * package clicapi * package crowdsec-cli/reload
- Loading branch information
Showing
23 changed files
with
169 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
cmd/crowdsec-cli/lapi_test.go → cmd/crowdsec-cli/clilapi/lapi_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package clilapi | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.