2024-02-20 09:59:56 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-02-20 09:59:56 +01:00
|
|
|
"testing"
|
2024-10-23 12:05:02 +02:00
|
|
|
"time"
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-11-25 16:26:24 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-05-16 18:28:37 +02:00
|
|
|
|
2024-10-31 19:24:15 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/group"
|
2024-12-11 12:58:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/store"
|
|
|
|
"github.com/netbirdio/netbird/management/server/types"
|
2024-10-31 19:24:15 +01:00
|
|
|
|
2024-05-16 18:28:37 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
2024-02-20 09:59:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
adminUserID = "adminUserID"
|
|
|
|
regularUserID = "regularUserID"
|
|
|
|
postureCheckName = "Existing check"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultAccountManager_PostureCheck(t *testing.T) {
|
|
|
|
am, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestPostureChecksAccount(am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Generic posture check flow", func(t *testing.T) {
|
|
|
|
// regular users can not create checks
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = am.SavePostureChecks(context.Background(), account.Id, regularUserID, &posture.Checks{})
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// regular users cannot list check
|
2024-07-03 11:33:02 +02:00
|
|
|
_, err = am.ListPostureChecks(context.Background(), account.Id, regularUserID)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// should be possible to create posture check with uniq name
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheck, err := am.SavePostureChecks(context.Background(), account.Id, adminUserID, &posture.Checks{
|
2024-02-20 09:59:56 +01:00
|
|
|
Name: postureCheckName,
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.26.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// admin users can list check
|
2024-07-03 11:33:02 +02:00
|
|
|
checks, err := am.ListPostureChecks(context.Background(), account.Id, adminUserID)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, checks, 1)
|
|
|
|
|
|
|
|
// should not be possible to create posture check with non uniq name
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = am.SavePostureChecks(context.Background(), account.Id, adminUserID, &posture.Checks{
|
2024-02-20 09:59:56 +01:00
|
|
|
Name: postureCheckName,
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
GeoLocationCheck: &posture.GeoLocationCheck{
|
|
|
|
Locations: []posture.Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// admins can update posture checks
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheck.Checks = posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.27.0",
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
}
|
|
|
|
_, err = am.SavePostureChecks(context.Background(), account.Id, adminUserID, postureCheck)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// users should not be able to delete posture checks
|
2024-11-25 16:26:24 +01:00
|
|
|
err = am.DeletePostureChecks(context.Background(), account.Id, postureCheck.ID, regularUserID)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// admin should be able to delete posture checks
|
2024-11-25 16:26:24 +01:00
|
|
|
err = am.DeletePostureChecks(context.Background(), account.Id, postureCheck.ID, adminUserID)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.NoError(t, err)
|
2024-07-03 11:33:02 +02:00
|
|
|
checks, err = am.ListPostureChecks(context.Background(), account.Id, adminUserID)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, checks, 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
func initTestPostureChecksAccount(am *DefaultAccountManager) (*types.Account, error) {
|
2024-02-20 09:59:56 +01:00
|
|
|
accountID := "testingAccount"
|
|
|
|
domain := "example.com"
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
admin := &types.User{
|
2024-02-20 09:59:56 +01:00
|
|
|
Id: adminUserID,
|
2024-12-11 12:58:45 +01:00
|
|
|
Role: types.UserRoleAdmin,
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
2024-12-11 12:58:45 +01:00
|
|
|
user := &types.User{
|
2024-02-20 09:59:56 +01:00
|
|
|
Id: regularUserID,
|
2024-12-11 12:58:45 +01:00
|
|
|
Role: types.UserRoleUser,
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account := newAccountWithId(context.Background(), accountID, groupAdminUserID, domain)
|
2024-02-20 09:59:56 +01:00
|
|
|
account.Users[admin.Id] = admin
|
|
|
|
account.Users[user.Id] = user
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err := am.Store.SaveAccount(context.Background(), account)
|
2024-02-20 09:59:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
return am.Store.GetAccount(context.Background(), account.Id)
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
2024-10-23 12:05:02 +02:00
|
|
|
|
|
|
|
func TestPostureCheckAccountPeersUpdate(t *testing.T) {
|
|
|
|
manager, account, peer1, peer2, peer3 := setupNetworkMapTest(t)
|
|
|
|
|
|
|
|
err := manager.SaveGroups(context.Background(), account.Id, userID, []*group.Group{
|
|
|
|
{
|
|
|
|
ID: "groupA",
|
|
|
|
Name: "GroupA",
|
|
|
|
Peers: []string{peer1.ID, peer2.ID, peer3.ID},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "groupB",
|
|
|
|
Name: "GroupB",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "groupC",
|
|
|
|
Name: "GroupC",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
updMsg := manager.peersUpdateManager.CreateChannel(context.Background(), peer1.ID)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
manager.peersUpdateManager.CloseChannel(context.Background(), peer1.ID)
|
|
|
|
})
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheckA := &posture.Checks{
|
|
|
|
Name: "postureCheckA",
|
|
|
|
AccountID: account.Id,
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
ProcessCheck: &posture.ProcessCheck{
|
|
|
|
Processes: []posture.Process{
|
|
|
|
{LinuxPath: "/usr/bin/netbird", MacPath: "/usr/local/bin/netbird"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
postureCheckA, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckA)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
postureCheckB := &posture.Checks{
|
|
|
|
Name: "postureCheckB",
|
2024-10-23 12:05:02 +02:00
|
|
|
AccountID: account.Id,
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.28.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Saving unused posture check should not update account peers and not send peer update
|
|
|
|
t.Run("saving unused posture check", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldNotReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheckB, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckB)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Updating unused posture check should not update account peers and not send peer update
|
|
|
|
t.Run("updating unused posture check", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldNotReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheckB.Checks = posture.ChecksDefinition{
|
2024-10-23 12:05:02 +02:00
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.29.0",
|
|
|
|
},
|
|
|
|
}
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckB)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
policy := &types.Policy{
|
2024-10-23 12:05:02 +02:00
|
|
|
Enabled: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Rules: []*types.PolicyRule{
|
2024-10-23 12:05:02 +02:00
|
|
|
{
|
|
|
|
Enabled: true,
|
|
|
|
Sources: []string{"groupA"},
|
|
|
|
Destinations: []string{"groupA"},
|
|
|
|
Bidirectional: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Action: types.PolicyTrafficActionAccept,
|
2024-10-23 12:05:02 +02:00
|
|
|
},
|
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
SourcePostureChecks: []string{postureCheckB.ID},
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Linking posture check to policy should trigger update account peers and send peer update
|
|
|
|
t.Run("linking posture check to policy with peers", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-26 10:46:05 +01:00
|
|
|
policy, err = manager.SavePolicy(context.Background(), account.Id, userID, policy)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Updating linked posture checks should update account peers and send peer update
|
|
|
|
t.Run("updating linked to posture check with peers", func(t *testing.T) {
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheckB.Checks = posture.ChecksDefinition{
|
2024-10-23 12:05:02 +02:00
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.29.0",
|
|
|
|
},
|
|
|
|
ProcessCheck: &posture.ProcessCheck{
|
|
|
|
Processes: []posture.Process{
|
|
|
|
{LinuxPath: "/usr/bin/netbird", MacPath: "/usr/local/bin/netbird"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckB)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Removing posture check from policy should trigger account peers update and send peer update
|
|
|
|
t.Run("removing posture check from policy", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
policy.SourcePostureChecks = []string{}
|
2024-11-26 10:46:05 +01:00
|
|
|
_, err := manager.SavePolicy(context.Background(), account.Id, userID, policy)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Deleting unused posture check should not trigger account peers update and not send peer update
|
|
|
|
t.Run("deleting unused posture check", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldNotReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
err := manager.DeletePostureChecks(context.Background(), account.Id, postureCheckA.ID, userID)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckB)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Updating linked posture check to policy with no peers should not trigger account peers update and not send peer update
|
|
|
|
t.Run("updating linked posture check to policy with no peers", func(t *testing.T) {
|
2024-12-11 12:58:45 +01:00
|
|
|
_, err = manager.SavePolicy(context.Background(), account.Id, userID, &types.Policy{
|
2024-10-23 12:05:02 +02:00
|
|
|
Enabled: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Rules: []*types.PolicyRule{
|
2024-10-23 12:05:02 +02:00
|
|
|
{
|
|
|
|
Enabled: true,
|
|
|
|
Sources: []string{"groupB"},
|
|
|
|
Destinations: []string{"groupC"},
|
|
|
|
Bidirectional: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Action: types.PolicyTrafficActionAccept,
|
2024-10-23 12:05:02 +02:00
|
|
|
},
|
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
SourcePostureChecks: []string{postureCheckB.ID},
|
2024-11-26 10:46:05 +01:00
|
|
|
})
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldNotReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheckB.Checks = posture.ChecksDefinition{
|
2024-10-23 12:05:02 +02:00
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.29.0",
|
|
|
|
},
|
|
|
|
}
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckB)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Updating linked posture check to policy where destination has peers but source does not
|
|
|
|
// should trigger account peers update and send peer update
|
|
|
|
t.Run("updating linked posture check to policy where destination has peers but source does not", func(t *testing.T) {
|
|
|
|
updMsg1 := manager.peersUpdateManager.CreateChannel(context.Background(), peer2.ID)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
manager.peersUpdateManager.CloseChannel(context.Background(), peer2.ID)
|
|
|
|
})
|
2024-11-26 10:46:05 +01:00
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
_, err = manager.SavePolicy(context.Background(), account.Id, userID, &types.Policy{
|
2024-10-23 12:05:02 +02:00
|
|
|
Enabled: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Rules: []*types.PolicyRule{
|
2024-10-23 12:05:02 +02:00
|
|
|
{
|
|
|
|
Enabled: true,
|
|
|
|
Sources: []string{"groupB"},
|
|
|
|
Destinations: []string{"groupA"},
|
|
|
|
Bidirectional: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Action: types.PolicyTrafficActionAccept,
|
2024-10-23 12:05:02 +02:00
|
|
|
},
|
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
SourcePostureChecks: []string{postureCheckB.ID},
|
2024-11-26 10:46:05 +01:00
|
|
|
})
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg1)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheckB.Checks = posture.ChecksDefinition{
|
2024-10-23 12:05:02 +02:00
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.29.0",
|
|
|
|
},
|
|
|
|
}
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckB)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Updating linked client posture check to policy where source has peers but destination does not,
|
|
|
|
// should trigger account peers update and send peer update
|
2024-10-31 19:24:15 +01:00
|
|
|
t.Run("updating linked posture check to policy where source has peers but destination does not", func(t *testing.T) {
|
2024-12-11 12:58:45 +01:00
|
|
|
_, err = manager.SavePolicy(context.Background(), account.Id, userID, &types.Policy{
|
2024-10-23 12:05:02 +02:00
|
|
|
Enabled: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Rules: []*types.PolicyRule{
|
2024-10-23 12:05:02 +02:00
|
|
|
{
|
|
|
|
Enabled: true,
|
|
|
|
Sources: []string{"groupA"},
|
|
|
|
Destinations: []string{"groupB"},
|
|
|
|
Bidirectional: true,
|
2024-12-11 12:58:45 +01:00
|
|
|
Action: types.PolicyTrafficActionAccept,
|
2024-10-23 12:05:02 +02:00
|
|
|
},
|
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
SourcePostureChecks: []string{postureCheckB.ID},
|
2024-11-26 10:46:05 +01:00
|
|
|
})
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
postureCheckB.Checks = posture.ChecksDefinition{
|
2024-10-23 12:05:02 +02:00
|
|
|
ProcessCheck: &posture.ProcessCheck{
|
|
|
|
Processes: []posture.Process{
|
|
|
|
{
|
|
|
|
LinuxPath: "/usr/bin/netbird",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-11-25 16:26:24 +01:00
|
|
|
_, err = manager.SavePostureChecks(context.Background(), account.Id, userID, postureCheckB)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
func TestArePostureCheckChangesAffectPeers(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
require.NoError(t, err, "failed to create account manager")
|
|
|
|
|
|
|
|
account, err := initTestPostureChecksAccount(manager)
|
|
|
|
require.NoError(t, err, "failed to init testing account")
|
|
|
|
|
|
|
|
groupA := &group.Group{
|
|
|
|
ID: "groupA",
|
|
|
|
AccountID: account.Id,
|
|
|
|
Peers: []string{"peer1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
groupB := &group.Group{
|
|
|
|
ID: "groupB",
|
|
|
|
AccountID: account.Id,
|
|
|
|
Peers: []string{},
|
|
|
|
}
|
2024-12-11 12:58:45 +01:00
|
|
|
err = manager.Store.SaveGroups(context.Background(), store.LockingStrengthUpdate, []*group.Group{groupA, groupB})
|
2024-11-25 16:26:24 +01:00
|
|
|
require.NoError(t, err, "failed to save groups")
|
|
|
|
|
|
|
|
postureCheckA := &posture.Checks{
|
|
|
|
Name: "checkA",
|
|
|
|
AccountID: account.Id,
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{MinVersion: "0.33.1"},
|
2024-10-23 12:05:02 +02:00
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
}
|
|
|
|
postureCheckA, err = manager.SavePostureChecks(context.Background(), account.Id, adminUserID, postureCheckA)
|
|
|
|
require.NoError(t, err, "failed to save postureCheckA")
|
|
|
|
|
|
|
|
postureCheckB := &posture.Checks{
|
|
|
|
Name: "checkB",
|
|
|
|
AccountID: account.Id,
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{MinVersion: "0.33.1"},
|
2024-10-23 12:05:02 +02:00
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
}
|
|
|
|
postureCheckB, err = manager.SavePostureChecks(context.Background(), account.Id, adminUserID, postureCheckB)
|
|
|
|
require.NoError(t, err, "failed to save postureCheckB")
|
|
|
|
|
2024-12-11 12:58:45 +01:00
|
|
|
policy := &types.Policy{
|
2024-11-25 16:26:24 +01:00
|
|
|
AccountID: account.Id,
|
2024-12-11 12:58:45 +01:00
|
|
|
Rules: []*types.PolicyRule{
|
2024-10-23 12:05:02 +02:00
|
|
|
{
|
2024-11-25 16:26:24 +01:00
|
|
|
Enabled: true,
|
|
|
|
Sources: []string{"groupA"},
|
|
|
|
Destinations: []string{"groupA"},
|
2024-10-23 12:05:02 +02:00
|
|
|
},
|
|
|
|
},
|
2024-11-25 16:26:24 +01:00
|
|
|
SourcePostureChecks: []string{postureCheckA.ID},
|
2024-10-23 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 10:46:05 +01:00
|
|
|
policy, err = manager.SavePolicy(context.Background(), account.Id, adminUserID, policy)
|
2024-11-25 16:26:24 +01:00
|
|
|
require.NoError(t, err, "failed to save policy")
|
|
|
|
|
2024-10-23 12:05:02 +02:00
|
|
|
t.Run("posture check exists and is linked to policy with peers", func(t *testing.T) {
|
2024-11-25 16:26:24 +01:00
|
|
|
result, err := arePostureCheckChangesAffectPeers(context.Background(), manager.Store, account.Id, postureCheckA.ID)
|
|
|
|
require.NoError(t, err)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.True(t, result)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("posture check exists but is not linked to any policy", func(t *testing.T) {
|
2024-11-25 16:26:24 +01:00
|
|
|
result, err := arePostureCheckChangesAffectPeers(context.Background(), manager.Store, account.Id, postureCheckB.ID)
|
|
|
|
require.NoError(t, err)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.False(t, result)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("posture check does not exist", func(t *testing.T) {
|
2024-11-25 16:26:24 +01:00
|
|
|
result, err := arePostureCheckChangesAffectPeers(context.Background(), manager.Store, account.Id, "unknown")
|
|
|
|
require.NoError(t, err)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.False(t, result)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("posture check is linked to policy with no peers in source groups", func(t *testing.T) {
|
2024-11-25 16:26:24 +01:00
|
|
|
policy.Rules[0].Sources = []string{"groupB"}
|
|
|
|
policy.Rules[0].Destinations = []string{"groupA"}
|
2024-11-26 10:46:05 +01:00
|
|
|
_, err = manager.SavePolicy(context.Background(), account.Id, adminUserID, policy)
|
2024-11-25 16:26:24 +01:00
|
|
|
require.NoError(t, err, "failed to update policy")
|
|
|
|
|
|
|
|
result, err := arePostureCheckChangesAffectPeers(context.Background(), manager.Store, account.Id, postureCheckA.ID)
|
|
|
|
require.NoError(t, err)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.True(t, result)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("posture check is linked to policy with no peers in destination groups", func(t *testing.T) {
|
2024-11-25 16:26:24 +01:00
|
|
|
policy.Rules[0].Sources = []string{"groupA"}
|
|
|
|
policy.Rules[0].Destinations = []string{"groupB"}
|
2024-11-26 10:46:05 +01:00
|
|
|
_, err = manager.SavePolicy(context.Background(), account.Id, adminUserID, policy)
|
2024-11-25 16:26:24 +01:00
|
|
|
require.NoError(t, err, "failed to update policy")
|
|
|
|
|
|
|
|
result, err := arePostureCheckChangesAffectPeers(context.Background(), manager.Store, account.Id, postureCheckA.ID)
|
|
|
|
require.NoError(t, err)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.True(t, result)
|
|
|
|
})
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
t.Run("posture check is linked to policy but no peers in groups", func(t *testing.T) {
|
|
|
|
groupA.Peers = []string{}
|
2024-12-11 12:58:45 +01:00
|
|
|
err = manager.Store.SaveGroup(context.Background(), store.LockingStrengthUpdate, groupA)
|
2024-11-25 16:26:24 +01:00
|
|
|
require.NoError(t, err, "failed to save groups")
|
|
|
|
|
|
|
|
result, err := arePostureCheckChangesAffectPeers(context.Background(), manager.Store, account.Id, postureCheckA.ID)
|
|
|
|
require.NoError(t, err)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.False(t, result)
|
|
|
|
})
|
|
|
|
|
2024-11-25 16:26:24 +01:00
|
|
|
t.Run("posture check is linked to policy with non-existent group", func(t *testing.T) {
|
|
|
|
policy.Rules[0].Sources = []string{"nonExistentGroup"}
|
|
|
|
policy.Rules[0].Destinations = []string{"nonExistentGroup"}
|
2024-11-26 10:46:05 +01:00
|
|
|
_, err = manager.SavePolicy(context.Background(), account.Id, adminUserID, policy)
|
2024-11-25 16:26:24 +01:00
|
|
|
require.NoError(t, err, "failed to update policy")
|
|
|
|
|
|
|
|
result, err := arePostureCheckChangesAffectPeers(context.Background(), manager.Store, account.Id, postureCheckA.ID)
|
|
|
|
require.NoError(t, err)
|
2024-10-23 12:05:02 +02:00
|
|
|
assert.False(t, result)
|
|
|
|
})
|
|
|
|
}
|