crowdsec/cmd/crowdsec-cli/idgen/machineid.go
mmetc 9c0422f75b
cscli refact: package 'clicapi', 'clilapi' (#3185)
* extract functions to own files

* package clilapi

* package clicapi

* package crowdsec-cli/reload
2024-08-26 09:39:36 +02:00

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
}