Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/proto/events/event.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/extensions/nginx-app-protect/nap/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nap

const (
FILE_NOT_FOUND = "The following file could not be found - %s"
FILE_NOT_FOUND = "the following file could not be found - %s"
UNABLE_TO_MATCH_NAP_BUILD_VERSION = "Couldn't match the NAP build version (%s) to a supported NAP release..."
UNABLE_TO_FIND_RELEASE_VERSION_INFO = "Unable to find NAP release info for supplied NAP release version - %s"
UNSUPPORTED_NAP_RELEASE_VERSION = "The supplied release version (%s) is not within the supported release versions - %v"
Expand Down
25 changes: 13 additions & 12 deletions src/extensions/nginx-app-protect/nap/nap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"strings"
"time"

"github.com/nginx/agent/v2/src/core"
log "github.com/sirupsen/logrus"

"github.com/nginx/agent/v2/src/core"
)

const (
Expand Down Expand Up @@ -40,22 +41,22 @@ func NewNginxAppProtect(optDirPath, symLinkDir string) (*NginxAppProtect, error)
}

// Get status of NAP on the system
napStatus, err := napStatus(requiredNAPFiles)
status, err := napStatus(requiredNAPFiles)
if err != nil {
return nil, err
}

// Get the release version of NAP on the system if NAP is installed
var napRelease *NAPRelease
if napStatus != MISSING {
if status != MISSING {
napRelease, err = installedNAPRelease(NAP_VERSION_FILE)
if err != nil {
return nil, err
}
}

// Update the NAP object with the values from NAP on the system
nap.Status = napStatus.String()
nap.Status = status.String()
if napRelease != nil {
nap.Release = *napRelease
}
Expand All @@ -65,7 +66,7 @@ func NewNginxAppProtect(optDirPath, symLinkDir string) (*NginxAppProtect, error)

// Monitor starts a goroutine responsible for monitoring the system for any NAP related
// changes and communicates those changes with a report message sent via the channel this
// function returns. Additionally if any changes are detected the NAP object that called
// function returns. Additionally, if any changes are detected the NAP object that called
// this monitoring function will have its attributes updated to the new changes. Here are
// examples of NAP changes that would be detected and communicated:
// - NAP installed/version changed
Expand Down Expand Up @@ -106,7 +107,7 @@ func (nap *NginxAppProtect) monitor(msgChannel chan NAPReportBundle, pollInterva

// Check if there has been any change in the NAP report
if nap.napReportIsEqual(newNAPReport) {
log.Infof("No change in NAP detected... Checking NAP again in %v seconds", pollInterval.Seconds())
log.Debugf("No change in NAP detected... Checking NAP again in %v seconds", pollInterval.Seconds())
break
}

Expand Down Expand Up @@ -183,7 +184,7 @@ func (nap *NginxAppProtect) syncSymLink(previousVersion, newVersion string) erro
}

// removeNAPSymlinks walks the NAP symlink directory and removes any existing NAP
// symlinks found in the directory except for ones that match the ignore pattern.
// symlinks found in the directory except for ones that match to ignore pattern.
func (nap *NginxAppProtect) removeNAPSymlinks(symlinkPatternToIgnore string) error {
// Check if the necessary directory exists
_, err := os.Stat(nap.symLinkDir)
Expand Down Expand Up @@ -235,7 +236,7 @@ func (nap *NginxAppProtect) napReportIsEqual(incomingNAPReport NAPReport) bool {

// napInstalled determines if NAP is installed on the system. If NAP is NOT installed on the
// system then the bool will be false and the error will be nil, if the error is not nil then
// it's possible NAP might be installed but an error verifying it's installation has occurred.
// it's possible NAP might be installed but an error verifying its installation has occurred.
func napInstalled(requiredFiles []string) (bool, error) {
log.Debugf("Checking for the required NAP files - %v\n", requiredFiles)
return core.FilesExists(requiredFiles)
Expand Down Expand Up @@ -267,16 +268,16 @@ func napRunning() (bool, error) {
func napStatus(requiredFiles []string) (Status, error) {

// Check if NAP is installed
napInstalled, err := napInstalled(requiredFiles)
if !napInstalled && err == nil {
installed, err := napInstalled(requiredFiles)
if !installed && err == nil {
return MISSING, nil
} else if err != nil {
return UNDEFINED, err
}

// It's installed, but is running?
napRunning, err := napRunning()
if !napRunning && err == nil {
running, err := napRunning()
if !running && err == nil {
return INSTALLED, nil
} else if err != nil {
return UNDEFINED, err
Expand Down
59 changes: 4 additions & 55 deletions src/extensions/nginx-app-protect/nap/nap_release.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,23 @@
package nap

import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/nginx/agent/v2/src/core"
)

// NewNAPReleaseMap is responsible for creating a NAPReleaseMap object that is contains
// info about each support NAP release.
func NewNAPReleaseMap() *NAPReleaseMap {
return &NAPReleaseMap{
ReleaseMap: map[string]NAPRelease{
"3.12.2": NAPRelease3_12_2(),
"3.12": NAPRelease3_12(),
"3.11": NAPRelease3_11(),
"3.10": NAPRelease3_10(),
"3.9.1": NAPRelease3_9_1(),
"3.9": NAPRelease3_9(),
"3.8": NAPRelease3_8(),
"3.7": NAPRelease3_7(),
"3.6": NAPRelease3_6(),
"3.5": NAPRelease3_5(),
"3.4": NAPRelease3_4(),
"3.3": NAPRelease3_3(),
"3.2": NAPRelease3_2(),
"3.1": NAPRelease3_1(),
"3.0": NAPRelease3_0(),
},
}
}

// NAPReleaseInfo get the NAP release information for a specified NAP release version.
func NAPReleaseInfo(napReleaseVersion string) (*NAPRelease, error) {
napRelease, exists := NewNAPReleaseMap().ReleaseMap[napReleaseVersion]
if !exists {
// Couldn't find details for supplied version
msg := fmt.Sprintf(UNABLE_TO_FIND_RELEASE_VERSION_INFO, napReleaseVersion)
logger.Error(msg)
return nil, errors.New(msg)
}

return &napRelease, nil
}

// installedNAPRelease gets the NAP release version based off the Nginx App Protect installed
// on the system.
func installedNAPRelease(versionFile string) (*NAPRelease, error) {
// Get build version of NAP so we can determine the release details
// Get build version of NAP, so we can determine the release details
napBuildVersion, err := installedNAPBuildVersion(versionFile)
if err != nil {
return nil, err
}

// Try to match NAP system build version to a build version in the NAP version mapping obj
for releaseVersion, napRelease := range NewNAPReleaseMap().ReleaseMap {
if napBuildVersion == napRelease.VersioningDetails.NAPBuild {
logger.Debugf("Matched the NAP build version (%s) to the NAP release version (%s)\n", napBuildVersion, releaseVersion)
return &napRelease, nil
}
}

// No match found but we'll return a release with a build version
logger.Errorf(UNABLE_TO_MATCH_NAP_BUILD_VERSION, napBuildVersion)
logger.Warnf("Returning NAP release with only build number - %s", napBuildVersion)

unmappedRelease := NAPReleaseUnmappedBuild(napBuildVersion)

unmappedRelease := ReleaseUnmappedBuild(napBuildVersion)
return &unmappedRelease, nil
}

Expand All @@ -83,7 +32,7 @@ func installedNAPBuildVersion(versionFile string) (string, error) {
return "", err
}

versionBytes, err := ioutil.ReadFile(versionFile)
versionBytes, err := os.ReadFile(versionFile)
if err != nil {
return "", err
}
Expand Down
42 changes: 6 additions & 36 deletions src/extensions/nginx-app-protect/nap/nap_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,9 @@ const (
)

var (
napRelease3_9 = NAPRelease3_9()
testUnmappedBuildRelease = NAPReleaseUnmappedBuild(testUnsupportedVersion)
testUnmappedBuildRelease = ReleaseUnmappedBuild(testUnsupportedVersion)
)

func TestNAPReleaseInfo(t *testing.T) {
testCases := []struct {
testName string
napReleaseVersion string
expReleaseVersion *NAPRelease
expError error
}{
{
testName: "ValidNAPRelease",
napReleaseVersion: "3.9",
expReleaseVersion: &napRelease3_9,
expError: nil,
},
{
testName: "InvalidNAPRelease",
napReleaseVersion: "invalid-release",
expReleaseVersion: nil,
expError: fmt.Errorf(UNABLE_TO_FIND_RELEASE_VERSION_INFO, "invalid-release"),
},
}

for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
// Get release version info
releaseVersion, err := NAPReleaseInfo(tc.napReleaseVersion)

// Validate returned release info
assert.Equal(t, err, tc.expError)
assert.Equal(t, releaseVersion, tc.expReleaseVersion)
})
}
}

func TestInstalledNAPBuildVersion(t *testing.T) {
testCases := []struct {
testName string
Expand Down Expand Up @@ -100,6 +66,10 @@ func TestInstalledNAPBuildVersion(t *testing.T) {
}
}

func buildFromPTR(v string) *NAPRelease {
version := ReleaseUnmappedBuild(v)
return &version
}
func TestInstalledNAPRelease(t *testing.T) {
testCases := []struct {
testName string
Expand All @@ -119,7 +89,7 @@ func TestInstalledNAPRelease(t *testing.T) {
testName: "SuccessfullyGetNAPReleaseVersion",
versionFile: testNAPVersionFile,
version: testNAPVersion,
expReleaseVersion: &napRelease3_9,
expReleaseVersion: buildFromPTR(testNAPVersion),
expError: nil,
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/nginx-app-protect/nap/nap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ func TestGenerateNAPReport(t *testing.T) {
testName: "NAPInstalled",
nap: NginxAppProtect{
Status: INSTALLED.String(),
Release: napRelease3_9,
Release: testUnmappedBuildRelease,
AttackSignaturesVersion: "2022.02.24",
ThreatCampaignsVersion: "2022.03.01",
},
expNAPReport: NAPReport{
Status: INSTALLED.String(),
NAPVersion: napRelease3_9.VersioningDetails.NAPRelease,
NAPVersion: testUnmappedBuildRelease.VersioningDetails.NAPRelease,
AttackSignaturesVersion: "2022.02.24",
ThreatCampaignsVersion: "2022.03.01",
},
Expand Down
Loading