Skip to content

Commit

Permalink
Post rebase fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
surik committed Jan 22, 2024
1 parent 34ee987 commit 952478a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
37 changes: 21 additions & 16 deletions management/server/posture/os_version.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package posture

import (
"fmt"

"github.com/hashicorp/go-version"
nbpeer "github.com/netbirdio/netbird/management/server/peer"
log "github.com/sirupsen/logrus"
)

type MinVersionCheck struct {
Expand All @@ -25,7 +24,7 @@ type OSVersionCheck struct {

var _ Check = (*OSVersionCheck)(nil)

func (c *OSVersionCheck) Check(peer nbpeer.Peer) error {
func (c *OSVersionCheck) Check(peer nbpeer.Peer) (bool, error) {
peerGoOS := peer.Meta.GoOS
switch peerGoOS {
case "android":
Expand All @@ -39,53 +38,59 @@ func (c *OSVersionCheck) Check(peer nbpeer.Peer) error {
case "windows":
return checkMinKernelVersion(peerGoOS, peer.Meta.KernelVersion, c.Windows)
}
return nil
return true, nil
}

func (c *OSVersionCheck) Name() string {
return OSVersionCheckName
}

func checkMinVersion(peerGoOS, peerVersion string, check *MinVersionCheck) error {
func checkMinVersion(peerGoOS, peerVersion string, check *MinVersionCheck) (bool, error) {
if check == nil {
return fmt.Errorf("peer %s OS is not allowed", peerGoOS)
log.Debugf("peer %s OS is not allowed in the check", peerGoOS)
return false, nil
}

peerNBVersion, err := version.NewVersion(peerVersion)
if err != nil {
return err
return false, err
}

constraints, err := version.NewConstraint(">= " + check.MinVersion)
if err != nil {
return err
return false, err
}

if constraints.Check(peerNBVersion) {
return nil
return true, nil
}

return fmt.Errorf("peer %s OS version %s is older than minimum allowed version %s", peerGoOS, peerVersion, check.MinVersion)
log.Debugf("peer %s OS version %s is older than minimum allowed version %s", peerGoOS, peerVersion, check.MinVersion)

return false, nil
}

func checkMinKernelVersion(peerGoOS, peerVersion string, check *MinKernelVersionCheck) error {
func checkMinKernelVersion(peerGoOS, peerVersion string, check *MinKernelVersionCheck) (bool, error) {
if check == nil {
return fmt.Errorf("peer %s OS is not allowed", peerGoOS)
log.Debugf("peer %s OS is not allowed in the check", peerGoOS)
return false, nil
}

peerNBVersion, err := version.NewVersion(peerVersion)
if err != nil {
return err
return false, err
}

constraints, err := version.NewConstraint(">= " + check.MinKernelVersion)
if err != nil {
return err
return false, err
}

if constraints.Check(peerNBVersion) {
return nil
return true, nil
}

return fmt.Errorf("peer %s kernel version %s is older than minimum allowed version %s", peerGoOS, peerVersion, check.MinKernelVersion)
log.Debugf("peer %s kernel version %s is older than minimum allowed version %s", peerGoOS, peerVersion, check.MinKernelVersion)

return false, nil
}
15 changes: 11 additions & 4 deletions management/server/posture/os_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestOSVersionCheck_Check(t *testing.T) {
input peer.Peer
check OSVersionCheck
wantErr bool
isValid bool
}{
{
name: "Valid Peer Linux Kernel version",
Expand All @@ -29,6 +30,7 @@ func TestOSVersionCheck_Check(t *testing.T) {
},
},
wantErr: false,
isValid: true,
},
{
name: "Not valid Peer macOS version",
Expand All @@ -43,7 +45,8 @@ func TestOSVersionCheck_Check(t *testing.T) {
MinVersion: "15",
},
},
wantErr: true,
wantErr: false,
isValid: false,
},
{
name: "Valid Peer ios version allowed by any rule",
Expand All @@ -59,6 +62,7 @@ func TestOSVersionCheck_Check(t *testing.T) {
},
},
wantErr: false,
isValid: true,
},
{
name: "Valid Peer android version not allowed by rule",
Expand All @@ -69,7 +73,8 @@ func TestOSVersionCheck_Check(t *testing.T) {
},
},
check: OSVersionCheck{},
wantErr: true,
wantErr: false,
isValid: false,
},
{
name: "Valid Peer Linux Kernel version not allowed by rule",
Expand All @@ -80,18 +85,20 @@ func TestOSVersionCheck_Check(t *testing.T) {
},
},
check: OSVersionCheck{},
wantErr: true,
wantErr: false,
isValid: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.check.Check(tt.input)
isValid, err := tt.check.Check(tt.input)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.isValid, isValid)
})
}
}

0 comments on commit 952478a

Please sign in to comment.