From a3e86d97499367a46cec4e014b108bdd820ea814 Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 16 Sep 2025 11:20:38 +0200 Subject: [PATCH 1/4] deps: replace osinfo with gopsutil, use go 1.25 --- .github/workflows/tests.yml | 2 +- go.mod | 15 +++++++++++---- go.sum | 32 ++++++++++++++++++++++++++------ version/os.go | 13 ++++++++----- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 587bff9..9fbf8e7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,7 +26,7 @@ jobs: - name: Run unit tests run: | - go install gotest.tools/gotestsum@v1.12.1 + go install gotest.tools/gotestsum@v1.12.3 gotestsum --format github-actions env: RICHGO_FORCE_COLOR: 1 diff --git a/go.mod b/go.mod index 5bd53b6..ed94712 100644 --- a/go.mod +++ b/go.mod @@ -1,22 +1,29 @@ module github.com/crowdsecurity/go-cs-lib -go 1.23 +go 1.25 require ( - github.com/blackfireio/osinfo v1.1.0 github.com/coreos/go-systemd/v22 v22.5.0 github.com/goccy/go-yaml v1.18.0 + github.com/shirou/gopsutil/v4 v4.25.8 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.9.1 - github.com/stretchr/testify v1.10.0 + github.com/stretchr/testify v1.11.1 gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/ebitengine/purego v0.8.4 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/spf13/pflag v1.0.6 // indirect - golang.org/x/sys v0.7.0 // indirect + github.com/tklauser/go-sysconf v0.3.15 // indirect + github.com/tklauser/numcpus v0.10.0 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect + golang.org/x/sys v0.35.0 // indirect ) diff --git a/go.sum b/go.sum index 826f733..7870f66 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,30 @@ -github.com/blackfireio/osinfo v1.1.0 h1:1LMkMiFL42+Brx7r3MKuf7UTlXBRgebFLJQAfoFafj8= -github.com/blackfireio/osinfo v1.1.0/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA= github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= 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/ebitengine/purego v0.8.4 h1:CF7LEKg5FFOsASUj0+QwaXf8Ht6TlFxg09+S9wz0omw= +github.com/ebitengine/purego v0.8.4/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shirou/gopsutil/v4 v4.25.8 h1:NnAsw9lN7587WHxjJA9ryDnqhJpFH6A+wagYWTOH970= +github.com/shirou/gopsutil/v4 v4.25.8/go.mod h1:q9QdMmfAOVIw7a+eF86P7ISEU6ka+NLgkUxlopV4RwI= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo= @@ -22,11 +33,20 @@ github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4= +github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4= +github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso= +github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XVuEeem8LsQ= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= +golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/version/os.go b/version/os.go index a725ad9..0846ccb 100644 --- a/version/os.go +++ b/version/os.go @@ -1,18 +1,21 @@ package version import ( - "github.com/blackfireio/osinfo" + "github.com/shirou/gopsutil/v4/host" ) func DetectOS() (string, string) { - osInfo, err := osinfo.GetOSInfo() + info, err := host.Info() if err != nil { return System, "???" } - if osInfo.Name != "" && System == "docker" { - return osInfo.Name + " (docker)", osInfo.Version + name := info.Platform + version := info.PlatformVersion + + if name != "" && System == "docker" { + return name + " (docker)", version } - return osInfo.Name, osInfo.Version + return name, version } From 9424c5c1616496e0d607b587708b8cdd0cfc7c82 Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 16 Sep 2025 13:06:25 +0200 Subject: [PATCH 2/4] detect platform family --- version/os.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/version/os.go b/version/os.go index 0846ccb..137fdf4 100644 --- a/version/os.go +++ b/version/os.go @@ -4,18 +4,19 @@ import ( "github.com/shirou/gopsutil/v4/host" ) -func DetectOS() (string, string) { +func DetectOS() (string, string, string) { info, err := host.Info() if err != nil { - return System, "???" + return System, "???", "???" } - name := info.Platform + platform := info.Platform + family := info.PlatformFamily version := info.PlatformVersion - if name != "" && System == "docker" { - return name + " (docker)", version + if platform != "" && System == "docker" { + return platform + " (docker)", family, version } - return name, version + return platform, family, version } From 8a70cfb92b2b9648627d892980c203198161a07e Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 16 Sep 2025 13:15:44 +0200 Subject: [PATCH 3/4] lint --- .github/workflows/tests.yml | 2 +- .golangci.yml | 8 ++++---- cstest/aws.go | 4 +++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9fbf8e7..16d9be7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -34,6 +34,6 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0 with: - version: v2.1 + version: v2.4 args: --issues-exit-code=1 --timeout 10m only-new-issues: false diff --git a/.golangci.yml b/.golangci.yml index 9d99f7a..1c3b7c5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,7 @@ linters: - inamedparam # reports interfaces with unnamed method parameters - wrapcheck # Checks that errors returned from external packages are wrapped - err113 # Go linter to check the errors handling expressions - #- noinlineerr + - noinlineerr - paralleltest # Detects missing usage of t.Parallel() method in your Go test - testpackage # linter that makes you use a separate _test package - exhaustruct # Checks if all structure fields are initialized @@ -35,7 +35,7 @@ linters: # - nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity - #- wsl_v5 # add or remove empty lines + - wsl_v5 # add or remove empty lines settings: @@ -104,8 +104,8 @@ linters: - 43 - name: defer disabled: true - #- name: enforce-switch-style - # disabled: true + - name: enforce-switch-style + disabled: true - name: flag-parameter disabled: true - name: function-length diff --git a/cstest/aws.go b/cstest/aws.go index 990af90..482ef09 100644 --- a/cstest/aws.go +++ b/cstest/aws.go @@ -30,7 +30,9 @@ func SetAWSTestEnv(t *testing.T) string { t.Setenv("AWS_ACCESS_KEY_ID", "test") t.Setenv("AWS_SECRET_ACCESS_KEY", "test") - _, err := net.Dial("tcp", strings.TrimPrefix(endpoint, "http://")) + dialer := &net.Dialer{} + + _, err := dialer.DialContext(t.Context(), "tcp", strings.TrimPrefix(endpoint, "http://")) if err != nil { t.Fatalf("%s: make sure localstack is running and retry", err) } From 35dfeee04f93cfdbc62f2e43c51cdc54d28f7a6f Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 30 Sep 2025 16:11:33 +0200 Subject: [PATCH 4/4] update golangci-lint and actions --- .github/workflows/tests.yml | 6 +++--- cstest/filenotfound_unix.go | 3 +-- csyaml/merge.go | 19 +++++++++---------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 16d9be7..1dca567 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,10 +17,10 @@ jobs: steps: - name: Check out code into the Go module directory - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 - name: Set up Go - uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 + uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 with: go-version-file: go.mod @@ -34,6 +34,6 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0 with: - version: v2.4 + version: v2.5 args: --issues-exit-code=1 --timeout 10m only-new-issues: false diff --git a/cstest/filenotfound_unix.go b/cstest/filenotfound_unix.go index 6543060..4884cb4 100644 --- a/cstest/filenotfound_unix.go +++ b/cstest/filenotfound_unix.go @@ -3,7 +3,6 @@ package cstest const ( - // these are the same on unix. FileNotFoundMessage = "no such file or directory" - PathNotFoundMessage = FileNotFoundMessage + PathNotFoundMessage = FileNotFoundMessage // these are the same on unix. ) diff --git a/csyaml/merge.go b/csyaml/merge.go index 044f3cc..4ca10e0 100644 --- a/csyaml/merge.go +++ b/csyaml/merge.go @@ -1,10 +1,3 @@ -// Package merge implements a deep-merge over multiple YAML documents, -// preserving key order and rejecting invalid documents. -// -// Maps are deep-merged; sequences and scalars are replaced by later inputs. -// Type mismatches result in an error. -// -// Adapted from https://github.com/uber-go/config/tree/master/internal/merge package csyaml import ( @@ -17,9 +10,15 @@ import ( "github.com/goccy/go-yaml" ) -// Merge reads each YAML source in inputs, merges them in order (later -// sources override earlier), and returns the result as a bytes.Buffer. -// Always runs in strict mode: type mismatches or duplicate keys cause an error. +// Merge implements a deep-merge over multiple YAML documents, preserving key +// order and rejecting invalid documents. +// Always runs in strict mode: type mismatches or duplicate keys cause an +// error. +// +// Maps are deep-merged; sequences and scalars are replaced by later inputs. +// Type mismatches result in an error. +// +// Adapted from https://github.com/uber-go/config/tree/master/internal/merge func Merge(inputs [][]byte) (*bytes.Buffer, error) { var merged any