Created
March 25, 2021 13:32
-
-
Save sbz/3c23c52d6817246b3682922944a9c3ba to your computer and use it in GitHub Desktop.
crowdsec patch uuid for BSD systems
This file contains hidden or 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
diff --git c/cmd/crowdsec-cli/machines.go w/cmd/crowdsec-cli/machines.go | |
index bbdf11c..33e87e9 100644 | |
--- c/cmd/crowdsec-cli/machines.go | |
+++ w/cmd/crowdsec-cli/machines.go | |
@@ -7,6 +7,7 @@ import ( | |
"io/ioutil" | |
"math/big" | |
"os" | |
+ "runtime" | |
"strings" | |
"time" | |
@@ -59,17 +60,37 @@ func generatePassword(length int) string { | |
return string(buf) | |
} | |
+func readUUID() (string, error) { | |
+ var uuidfile string | |
+ | |
+ switch runtime.GOOS { | |
+ case "freebsd": | |
+ uuidfile = "/etc/hostid" | |
+ case "openbsd": | |
+ uuidfile = "/etc/machine-id" | |
+ // linux | |
+ default: | |
+ uuidfile = uuid | |
+ } | |
+ | |
+ bID, err := ioutil.ReadFile(uuidfile) | |
+ if err != nil { | |
+ return "", err | |
+ } | |
+ | |
+ return string(bID), nil | |
+} | |
+ | |
func generateID() (string, error) { | |
id, err := machineid.ID() | |
if err != nil { | |
log.Debugf("failed to get machine-id with usual files : %s", err) | |
} | |
if id == "" || err != nil { | |
- bID, err := ioutil.ReadFile(uuid) | |
+ id, err = readUUID() | |
if err != nil { | |
return "", errors.Wrap(err, "generating machine id") | |
} | |
- id = string(bID) | |
} | |
id = strings.ReplaceAll(id, "-", "")[:32] | |
id = fmt.Sprintf("%s%s", id, generatePassword(16)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment