Skip to content

Commit

Permalink
Add os-release gatherer (#283)
Browse files Browse the repository at this point in the history
* Add os-release gatherer
  • Loading branch information
rtorrero committed Oct 27, 2023
1 parent ff79cc9 commit 8a3a869
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/factsengine/gatherers/gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func StandardGatherers() FactGatherersTree {
HostsFileGathererName: map[string]FactGatherer{
"v1": NewDefaultHostsFileGatherer(),
},
OSReleaseGathererName: map[string]FactGatherer{
"v1": NewDefaultOSReleaseGatherer(),
},
PackageVersionGathererName: map[string]FactGatherer{
"v1": NewDefaultPackageVersionGatherer(),
},
Expand Down
88 changes: 88 additions & 0 deletions internal/factsengine/gatherers/osrelease.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package gatherers

import (
"os"

"github.com/hashicorp/go-envparse"
log "github.com/sirupsen/logrus"
"github.com/trento-project/agent/pkg/factsengine/entities"
)

const (
OSReleaseGathererName = "os-release"
OSReleaseFilePath = "/etc/os-release"
)

// nolint:gochecknoglobals
var (
OSReleaseFileError = entities.FactGatheringError{
Type: "os-release-file-error",
Message: "error reading /etc/os-release file",
}

OSReleaseDecodingError = entities.FactGatheringError{
Type: "os-release-decoding-error",
Message: "error decoding file content",
}
)

type OSReleaseGatherer struct {
osReleaseFilePath string
}

func NewDefaultOSReleaseGatherer() *OSReleaseGatherer {
return NewOSReleaseGatherer(OSReleaseFilePath)
}

func NewOSReleaseGatherer(path string) *OSReleaseGatherer {
return &OSReleaseGatherer{
osReleaseFilePath: path,
}
}

func (g *OSReleaseGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) {
facts := []entities.Fact{}
log.Infof("Starting %s facts gathering process", OSReleaseGathererName)

file, err := os.Open(g.osReleaseFilePath)
if err != nil {
log.Error(err)
return facts, OSReleaseFileError.Wrap(err.Error())
}
defer func() {
err := file.Close()
if err != nil {
log.Errorf("could not close os-release file %s, error: %s", g.osReleaseFilePath, err)
}
}()

osRelease, err := envparse.Parse(file)
if err != nil {
log.Error(err)
return facts, OSReleaseDecodingError.Wrap(err.Error())
}

osReleaseFactValue := mapOSReleaseToFactValue(osRelease)
if err != nil {
log.Error(err)
return facts, OSReleaseDecodingError.Wrap(err.Error())
}

for _, requestedFact := range factsRequests {
fact := entities.NewFactGatheredWithRequest(requestedFact, osReleaseFactValue)
facts = append(facts, fact)
}

log.Infof("Requested %s facts gathered", OSReleaseGathererName)
return facts, nil
}

func mapOSReleaseToFactValue(inputMap map[string]string) entities.FactValue {
factValueMap := make(map[string]entities.FactValue)

for key, value := range inputMap {
factValueMap[key] = &entities.FactValueString{Value: value}
}

return &entities.FactValueMap{Value: factValueMap}
}
88 changes: 88 additions & 0 deletions internal/factsengine/gatherers/osrelease_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package gatherers_test

import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/factsengine/gatherers"
"github.com/trento-project/agent/pkg/factsengine/entities"
"github.com/trento-project/agent/test/helpers"
)

type OSReleaseGathererTestSuite struct {
suite.Suite
}

func TestOSReleaseGathererTestSuite(t *testing.T) {
suite.Run(t, new(OSReleaseGathererTestSuite))
}

func (suite *OSReleaseGathererTestSuite) TestOSReleaseGathererSuccess() {
c := gatherers.NewOSReleaseGatherer(helpers.GetFixturePath("gatherers/os-release.basic"))

factRequests := []entities.FactRequest{
{
Name: "os-release",
Gatherer: "os-release@v1",
CheckID: "check1",
},
}

factResults, err := c.Gather(factRequests)

expectedResults := []entities.Fact{
{
Name: "os-release",
Value: &entities.FactValueMap{
Value: map[string]entities.FactValue{
"NAME": &entities.FactValueString{Value: "openSUSE Leap"},
"VERSION": &entities.FactValueString{Value: "15.2"},
"ID": &entities.FactValueString{Value: "opensuse-leap"},
"ID_LIKE": &entities.FactValueString{Value: "suse opensuse"},
"VERSION_ID": &entities.FactValueString{Value: "15.2"},
"PRETTY_NAME": &entities.FactValueString{Value: "openSUSE Leap 15.2"},
"ANSI_COLOR": &entities.FactValueString{Value: "0;32"},
"CPE_NAME": &entities.FactValueString{Value: "cpe:/o:opensuse:leap:15.2"},
"BUG_REPORT_URL": &entities.FactValueString{Value: "https://bugs.opensuse.org"},
"HOME_URL": &entities.FactValueString{Value: "https://www.opensuse.org/"},
},
},
CheckID: "check1",
},
}

suite.NoError(err)
suite.ElementsMatch(expectedResults, factResults)
}

func (suite *OSReleaseGathererTestSuite) TestOSReleaseGathererFileNotExists() {
c := gatherers.NewOSReleaseGatherer("non_existing_file")

factRequests := []entities.FactRequest{
{
Name: "os-release",
Gatherer: "os-release@v1",
},
}

_, err := c.Gather(factRequests)

suite.EqualError(err, "fact gathering error: os-release-file-error - error reading /etc/os-release file: "+
"open non_existing_file: no such file or directory")
}

func (suite *OSReleaseGathererTestSuite) TestOSReleaseGathererErrorDecoding() {

c := gatherers.NewOSReleaseGatherer(helpers.GetFixturePath("gatherers/os-release.invalid"))

factRequests := []entities.FactRequest{
{
Name: "os-release",
Gatherer: "os-release",
},
}

_, err := c.Gather(factRequests)

suite.EqualError(err, "fact gathering error: os-release-decoding-error - error decoding file content: error on line 3: missing =")
}
10 changes: 10 additions & 0 deletions test/fixtures/gatherers/os-release.basic
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME="openSUSE Leap"
VERSION="15.2"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.2"
PRETTY_NAME="openSUSE Leap 15.2"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.2"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
3 changes: 3 additions & 0 deletions test/fixtures/gatherers/os-release.invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID_ubuntu

0 comments on commit 8a3a869

Please sign in to comment.