From 4d95205e87bb4ab6a4eb6ff954c97ef0d6ce196d Mon Sep 17 00:00:00 2001 From: Cyril Brulebois Date: Thu, 7 Jan 2021 17:07:12 +0000 Subject: [PATCH] Use local machineid implementation 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 Signed-off-by: Cyril Brulebois Gbp-Pq: Name 0001-use-a-local-machineid-implementation.patch --- cmd/crowdsec-cli/machines.go | 2 +- go.mod | 1 - go.sum | 2 -- pkg/machineid/machineid.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 pkg/machineid/machineid.go diff --git a/cmd/crowdsec-cli/machines.go b/cmd/crowdsec-cli/machines.go index bf23952..d8b33e9 100644 --- a/cmd/crowdsec-cli/machines.go +++ b/cmd/crowdsec-cli/machines.go @@ -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 b23b928..9ac9861 100644 --- 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 b90a22c..f021583 100644 --- 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 index 0000000..c0b3277 --- /dev/null +++ b/pkg/machineid/machineid.go @@ -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 +} -- 2.30.2