mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-18 15:24:25 +02:00
* extract functions to own files * package clilapi * package clicapi * package crowdsec-cli/reload
48 lines
1 KiB
Go
48 lines
1 KiB
Go
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
|
|
}
|