-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add os-release gatherer
- Loading branch information
Showing
5 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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} | ||
} |
This file contains 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
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 =") | ||
} |
This file contains 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
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/" |
This file contains 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
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 |