Use local machineid implementation
authorCyril Brulebois <cyril@debamax.com>
Thu, 7 Jan 2021 17:07:12 +0000 (17:07 +0000)
committerCyril Brulebois <cyril@debamax.com>
Sat, 4 Dec 2021 04:03:33 +0000 (04:03 +0000)
Let's avoid a dependency on an extra package (denisbrodbeck/machineid),
since its ID() function is mostly about trying to read from two files.

Signed-off-by: Manuel Sabban <manuel@crowdsec.net>
Signed-off-by: Cyril Brulebois <cyril@debamax.com>
Gbp-Pq: Name 0001-use-a-local-machineid-implementation.patch

cmd/crowdsec-cli/machines.go
go.mod
go.sum
pkg/machineid/machineid.go [new file with mode: 0644]

index bf23952b118b8c856cb1d04b6f70161d2496e891..d8b33e9cedb2a3043b323fa0b8b74330a9cdfaee 100644 (file)
@@ -13,7 +13,7 @@ import (
        "github.com/AlecAivazis/survey/v2"
        "github.com/crowdsecurity/crowdsec/pkg/csconfig"
        "github.com/crowdsecurity/crowdsec/pkg/database"
-       "github.com/denisbrodbeck/machineid"
+       "github.com/crowdsecurity/crowdsec/pkg/machineid"
        "github.com/enescakir/emoji"
        "github.com/go-openapi/strfmt"
        "github.com/olekukonko/tablewriter"
diff --git a/go.mod b/go.mod
index b23b928815de4d99703954d088e25406c5484250..9ac98612344a9c3e4078b94f15259df7a6966ec1 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -11,7 +11,6 @@ require (
        github.com/containerd/containerd v1.4.3 // indirect
        github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
        github.com/davecgh/go-spew v1.1.1
-       github.com/denisbrodbeck/machineid v1.0.1
        github.com/dghubble/sling v1.3.0
        github.com/docker/distribution v2.7.1+incompatible // indirect
        github.com/docker/docker v20.10.2+incompatible
diff --git a/go.sum b/go.sum
index b90a22cd9d8088b47e55614c78f3ca05c26d3b56..f02158363b9098430f9c5417b2182e0739fc0925 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -112,8 +112,6 @@ github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
-github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
 github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
 github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
 github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
diff --git a/pkg/machineid/machineid.go b/pkg/machineid/machineid.go
new file mode 100644 (file)
index 0000000..c0b3277
--- /dev/null
@@ -0,0 +1,29 @@
+package machineid
+
+import (
+       "io/ioutil"
+       "strings"
+)
+
+const (
+       // dbusPath is the default path for dbus machine id.
+       dbusPath = "/var/lib/dbus/machine-id"
+       // dbusPathEtc is the default path for dbus machine id located in /etc.
+       // Some systems (like Fedora 20) only know this path.
+       // Sometimes it's the other way round.
+       dbusPathEtc = "/etc/machine-id"
+)
+
+// idea of code is stolen from https://github.com/denisbrodbeck/machineid/
+// but here we are on Debian GNU/Linux
+func ID() (string, error) {
+       id, err := ioutil.ReadFile(dbusPath)
+       if err != nil {
+               // try fallback path
+               id, err = ioutil.ReadFile(dbusPathEtc)
+       }
+       if err != nil {
+               return "", err
+       }
+       return strings.TrimSpace(string(id)), nil
+}