2024-02-20 09:59:56 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-02-20 09:59:56 +01:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-02-22 17:22:43 +01:00
|
|
|
"net/netip"
|
2024-02-20 09:59:56 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/geolocation"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
var berlin = "Berlin"
|
|
|
|
var losAngeles = "Los Angeles"
|
|
|
|
|
|
|
|
func initPostureChecksTestData(postureChecks ...*posture.Checks) *PostureChecksHandler {
|
|
|
|
testPostureChecks := make(map[string]*posture.Checks, len(postureChecks))
|
|
|
|
for _, postureCheck := range postureChecks {
|
|
|
|
testPostureChecks[postureCheck.ID] = postureCheck
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PostureChecksHandler{
|
|
|
|
accountManager: &mock_server.MockAccountManager{
|
2024-07-03 11:33:02 +02:00
|
|
|
GetPostureChecksFunc: func(_ context.Context, accountID, postureChecksID, userID string) (*posture.Checks, error) {
|
2024-02-20 09:59:56 +01:00
|
|
|
p, ok := testPostureChecks[postureChecksID]
|
|
|
|
if !ok {
|
|
|
|
return nil, status.Errorf(status.NotFound, "posture checks not found")
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
},
|
2024-07-03 11:33:02 +02:00
|
|
|
SavePostureChecksFunc: func(_ context.Context, accountID, userID string, postureChecks *posture.Checks) error {
|
2024-02-20 09:59:56 +01:00
|
|
|
postureChecks.ID = "postureCheck"
|
|
|
|
testPostureChecks[postureChecks.ID] = postureChecks
|
2024-06-13 13:24:24 +02:00
|
|
|
|
|
|
|
if err := postureChecks.Validate(); err != nil {
|
2024-08-14 12:30:10 +02:00
|
|
|
return status.Errorf(status.InvalidArgument, err.Error()) //nolint
|
2024-06-13 13:24:24 +02:00
|
|
|
}
|
|
|
|
|
2024-02-20 09:59:56 +01:00
|
|
|
return nil
|
|
|
|
},
|
2024-07-03 11:33:02 +02:00
|
|
|
DeletePostureChecksFunc: func(_ context.Context, accountID, postureChecksID, userID string) error {
|
2024-02-20 09:59:56 +01:00
|
|
|
_, ok := testPostureChecks[postureChecksID]
|
|
|
|
if !ok {
|
|
|
|
return status.Errorf(status.NotFound, "posture checks not found")
|
|
|
|
}
|
|
|
|
delete(testPostureChecks, postureChecksID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2024-07-03 11:33:02 +02:00
|
|
|
ListPostureChecksFunc: func(_ context.Context, accountID, userID string) ([]*posture.Checks, error) {
|
2024-02-20 09:59:56 +01:00
|
|
|
accountPostureChecks := make([]*posture.Checks, len(testPostureChecks))
|
|
|
|
for _, p := range testPostureChecks {
|
|
|
|
accountPostureChecks = append(accountPostureChecks, p)
|
|
|
|
}
|
|
|
|
return accountPostureChecks, nil
|
|
|
|
},
|
2024-09-22 22:44:10 +02:00
|
|
|
GetAccountIDFromTokenFunc: func(_ context.Context, claims jwtclaims.AuthorizationClaims) (string, string, error) {
|
|
|
|
return claims.AccountId, claims.UserId, nil
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
geolocationManager: &geolocation.Geolocation{},
|
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithFromRequestContext(func(r *http.Request) jwtclaims.AuthorizationClaims {
|
|
|
|
return jwtclaims.AuthorizationClaims{
|
|
|
|
UserId: "test_user",
|
|
|
|
Domain: "hotmail.com",
|
|
|
|
AccountId: "test_id",
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPostureCheck(t *testing.T) {
|
|
|
|
postureCheck := &posture.Checks{
|
|
|
|
ID: "postureCheck",
|
|
|
|
Name: "nbVersion",
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "1.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
osPostureCheck := &posture.Checks{
|
|
|
|
ID: "osPostureCheck",
|
|
|
|
Name: "osVersion",
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
OSVersionCheck: &posture.OSVersionCheck{
|
|
|
|
Linux: &posture.MinKernelVersionCheck{
|
|
|
|
MinKernelVersion: "6.0.0",
|
|
|
|
},
|
|
|
|
Darwin: &posture.MinVersionCheck{
|
|
|
|
MinVersion: "14",
|
|
|
|
},
|
|
|
|
Ios: &posture.MinVersionCheck{
|
|
|
|
MinVersion: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
geoPostureCheck := &posture.Checks{
|
|
|
|
ID: "geoPostureCheck",
|
|
|
|
Name: "geoLocation",
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
GeoLocationCheck: &posture.GeoLocationCheck{
|
|
|
|
Locations: []posture.Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: posture.CheckActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
privateNetworkCheck := &posture.Checks{
|
|
|
|
ID: "privateNetworkPostureCheck",
|
|
|
|
Name: "privateNetwork",
|
|
|
|
Checks: posture.ChecksDefinition{
|
2024-02-27 11:59:48 +01:00
|
|
|
PeerNetworkRangeCheck: &posture.PeerNetworkRangeCheck{
|
2024-02-22 17:22:43 +01:00
|
|
|
Ranges: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("192.168.0.0/24"),
|
|
|
|
},
|
|
|
|
Action: posture.CheckActionAllow,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
checkName string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
requestBody io.Reader
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "GetPostureCheck NBVersion OK",
|
|
|
|
expectedBody: true,
|
|
|
|
id: postureCheck.ID,
|
|
|
|
checkName: postureCheck.Name,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetPostureCheck OSVersion OK",
|
|
|
|
expectedBody: true,
|
|
|
|
id: osPostureCheck.ID,
|
|
|
|
checkName: osPostureCheck.Name,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetPostureCheck GeoLocation OK",
|
|
|
|
expectedBody: true,
|
|
|
|
id: geoPostureCheck.ID,
|
|
|
|
checkName: geoPostureCheck.Name,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
{
|
|
|
|
name: "GetPostureCheck PrivateNetwork OK",
|
|
|
|
expectedBody: true,
|
|
|
|
id: privateNetworkCheck.ID,
|
|
|
|
checkName: privateNetworkCheck.Name,
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
},
|
2024-02-20 09:59:56 +01:00
|
|
|
{
|
|
|
|
name: "GetPostureCheck Not Found",
|
|
|
|
id: "not-exists",
|
|
|
|
expectedStatus: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:22:43 +01:00
|
|
|
p := initPostureChecksTestData(postureCheck, osPostureCheck, geoPostureCheck, privateNetworkCheck)
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/posture-checks/"+tc.id, tc.requestBody)
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/posture-checks/{postureCheckId}", p.GetPostureCheck).Methods("GET")
|
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if status := recorder.Code; status != tc.expectedStatus {
|
|
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, tc.expectedStatus)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.expectedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var got api.PostureCheck
|
|
|
|
if err = json.Unmarshal(content, &got); err != nil {
|
|
|
|
t.Fatalf("Sent content is not in correct json format; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, got.Id, tc.id)
|
|
|
|
assert.Equal(t, got.Name, tc.checkName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostureCheckUpdate(t *testing.T) {
|
|
|
|
str := func(s string) *string { return &s }
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
expectedStatus int
|
|
|
|
expectedBody bool
|
|
|
|
expectedPostureCheck *api.PostureCheck
|
|
|
|
requestType string
|
|
|
|
requestPath string
|
|
|
|
requestBody io.Reader
|
|
|
|
setupHandlerFunc func(handler *PostureChecksHandler)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Create Posture Checks NB version",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"description": "default",
|
|
|
|
"checks": {
|
|
|
|
"nb_version_check": {
|
|
|
|
"min_version": "1.2.3"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str("default"),
|
|
|
|
Checks: api.Checks{
|
|
|
|
NbVersionCheck: &api.NBVersionCheck{
|
|
|
|
MinVersion: "1.2.3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Create Posture Checks NB version with No geolocation DB",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"description": "default",
|
|
|
|
"checks": {
|
|
|
|
"nb_version_check": {
|
|
|
|
"min_version": "1.2.3"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str("default"),
|
|
|
|
Checks: api.Checks{
|
|
|
|
NbVersionCheck: &api.NBVersionCheck{
|
|
|
|
MinVersion: "1.2.3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setupHandlerFunc: func(handler *PostureChecksHandler) {
|
|
|
|
handler.geolocationManager = nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Create Posture Checks OS version",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"description": "default",
|
|
|
|
"checks": {
|
|
|
|
"os_version_check": {
|
|
|
|
"android": {
|
|
|
|
"min_version": "9.0.0"
|
|
|
|
},
|
|
|
|
"ios": {
|
|
|
|
"min_version": "17.0"
|
|
|
|
},
|
|
|
|
"linux": {
|
|
|
|
"min_kernel_version": "6.0.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str("default"),
|
|
|
|
Checks: api.Checks{
|
|
|
|
OsVersionCheck: &api.OSVersionCheck{
|
|
|
|
Android: &api.MinVersionCheck{
|
|
|
|
MinVersion: "9.0.0",
|
|
|
|
},
|
|
|
|
Ios: &api.MinVersionCheck{
|
|
|
|
MinVersion: "17.0",
|
|
|
|
},
|
|
|
|
Linux: &api.MinKernelVersionCheck{
|
|
|
|
MinKernelVersion: "6.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Create Posture Checks Geo Location",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"description": "default",
|
|
|
|
"checks": {
|
|
|
|
"geo_location_check": {
|
|
|
|
"locations": [
|
|
|
|
{
|
|
|
|
"city_name": "Berlin",
|
|
|
|
"country_code": "DE"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"action": "allow"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str("default"),
|
|
|
|
Checks: api.Checks{
|
|
|
|
GeoLocationCheck: &api.GeoLocationCheck{
|
|
|
|
Locations: []api.Location{
|
|
|
|
{
|
|
|
|
CityName: &berlin,
|
|
|
|
CountryCode: "DE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: api.GeoLocationCheckActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
{
|
2024-02-27 11:59:48 +01:00
|
|
|
name: "Create Posture Checks Peer Network Range",
|
2024-02-22 17:22:43 +01:00
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"description": "default",
|
|
|
|
"checks": {
|
2024-02-27 11:59:48 +01:00
|
|
|
"peer_network_range_check": {
|
2024-02-22 17:22:43 +01:00
|
|
|
"action": "allow",
|
|
|
|
"ranges": [
|
|
|
|
"10.0.0.0/8"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str("default"),
|
|
|
|
Checks: api.Checks{
|
2024-02-27 11:59:48 +01:00
|
|
|
PeerNetworkRangeCheck: &api.PeerNetworkRangeCheck{
|
2024-02-22 17:22:43 +01:00
|
|
|
Ranges: []string{
|
|
|
|
"10.0.0.0/8",
|
|
|
|
},
|
2024-02-27 11:59:48 +01:00
|
|
|
Action: api.PeerNetworkRangeCheckActionAllow,
|
2024-02-22 17:22:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-20 09:59:56 +01:00
|
|
|
{
|
|
|
|
name: "Create Posture Checks Geo Location with No geolocation DB",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"description": "default",
|
|
|
|
"checks": {
|
|
|
|
"geo_location_check": {
|
|
|
|
"locations": [
|
|
|
|
{
|
|
|
|
"city_name": "Berlin",
|
|
|
|
"country_code": "DE"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"action": "allow"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusPreconditionFailed,
|
|
|
|
expectedBody: false,
|
|
|
|
setupHandlerFunc: func(handler *PostureChecksHandler) {
|
|
|
|
handler.geolocationManager = nil
|
|
|
|
},
|
|
|
|
},
|
2024-06-13 13:24:24 +02:00
|
|
|
{
|
|
|
|
name: "Create Posture Checks Process Check",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"description": "default",
|
|
|
|
"checks": {
|
|
|
|
"process_check": {
|
|
|
|
"processes": [
|
|
|
|
{
|
|
|
|
"linux_path": "/usr/local/bin/netbird",
|
|
|
|
"mac_path": "/Applications/NetBird.app/Contents/MacOS/netbird",
|
|
|
|
"windows_path": "C:\\ProgramData\\NetBird\\netbird.exe"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str("default"),
|
|
|
|
Checks: api.Checks{
|
|
|
|
ProcessCheck: &api.ProcessCheck{
|
|
|
|
Processes: []api.Process{
|
|
|
|
{
|
|
|
|
LinuxPath: str("/usr/local/bin/netbird"),
|
|
|
|
MacPath: str("/Applications/NetBird.app/Contents/MacOS/netbird"),
|
|
|
|
WindowsPath: str("C:\\ProgramData\\NetBird\\netbird.exe"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-20 09:59:56 +01:00
|
|
|
{
|
|
|
|
name: "Create Posture Checks Invalid Check",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"non_existing_check": {
|
|
|
|
"min_version": "1.2.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Create Posture Checks Invalid Name",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"checks": {
|
|
|
|
"nb_version_check": {
|
|
|
|
"min_version": "1.2.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Create Posture Checks Invalid NetBird's Min Version",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"nb_version_check": {}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Create Posture Checks Invalid Geo Location",
|
|
|
|
requestType: http.MethodPost,
|
|
|
|
requestPath: "/api/posture-checks",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"geo_location_check": {}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks NB Version",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/postureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"nb_version_check": {
|
|
|
|
"min_version": "1.9.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str(""),
|
|
|
|
Checks: api.Checks{
|
|
|
|
NbVersionCheck: &api.NBVersionCheck{
|
|
|
|
MinVersion: "1.9.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks OS Version",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/osPostureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"os_version_check": {
|
|
|
|
"linux": {
|
|
|
|
"min_kernel_version": "6.9.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str(""),
|
|
|
|
Checks: api.Checks{
|
|
|
|
OsVersionCheck: &api.OSVersionCheck{
|
|
|
|
Linux: &api.MinKernelVersionCheck{
|
|
|
|
MinKernelVersion: "6.9.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks OS Version with No geolocation DB",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/osPostureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"os_version_check": {
|
|
|
|
"linux": {
|
|
|
|
"min_kernel_version": "6.9.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str(""),
|
|
|
|
Checks: api.Checks{
|
|
|
|
OsVersionCheck: &api.OSVersionCheck{
|
|
|
|
Linux: &api.MinKernelVersionCheck{
|
|
|
|
MinKernelVersion: "6.9.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setupHandlerFunc: func(handler *PostureChecksHandler) {
|
|
|
|
handler.geolocationManager = nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks Geo Location",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/geoPostureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"geo_location_check": {
|
|
|
|
"locations": [
|
|
|
|
{
|
|
|
|
"city_name": "Los Angeles",
|
|
|
|
"country_code": "US"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"action": "allow"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str(""),
|
|
|
|
Checks: api.Checks{
|
|
|
|
GeoLocationCheck: &api.GeoLocationCheck{
|
|
|
|
Locations: []api.Location{
|
|
|
|
{
|
|
|
|
CityName: &losAngeles,
|
|
|
|
CountryCode: "US",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: api.GeoLocationCheckActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks Geo Location with No geolocation DB",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/geoPostureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"geo_location_check": {
|
|
|
|
"locations": [
|
|
|
|
{
|
|
|
|
"city_name": "Los Angeles",
|
|
|
|
"country_code": "US"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"action": "allow"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusPreconditionFailed,
|
|
|
|
expectedBody: false,
|
|
|
|
setupHandlerFunc: func(handler *PostureChecksHandler) {
|
|
|
|
handler.geolocationManager = nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks Geo Location with not valid action",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/geoPostureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"geo_location_check": {
|
|
|
|
"locations": [
|
|
|
|
{
|
|
|
|
"city_name": "Los Angeles",
|
|
|
|
"country_code": "US"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"action": "not-valid"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks Invalid Check",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/postureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"non_existing_check": {
|
|
|
|
"min_version": "1.2.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks Invalid Name",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/postureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"checks": {
|
|
|
|
"nb_version_check": {
|
|
|
|
"min_version": "1.2.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Posture Checks Invalid NetBird's Min Version",
|
|
|
|
requestType: http.MethodPut,
|
|
|
|
requestPath: "/api/posture-checks/postureCheck",
|
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
|
|
|
"nb_version_check": {}
|
|
|
|
}
|
|
|
|
}`)),
|
2024-06-13 13:24:24 +02:00
|
|
|
expectedStatus: http.StatusUnprocessableEntity,
|
2024-02-20 09:59:56 +01:00
|
|
|
expectedBody: false,
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
{
|
2024-02-27 11:59:48 +01:00
|
|
|
name: "Update Posture Checks Peer Network Range",
|
2024-02-22 17:22:43 +01:00
|
|
|
requestType: http.MethodPut,
|
2024-02-27 11:59:48 +01:00
|
|
|
requestPath: "/api/posture-checks/peerNetworkRangePostureCheck",
|
2024-02-22 17:22:43 +01:00
|
|
|
requestBody: bytes.NewBuffer(
|
|
|
|
[]byte(`{
|
|
|
|
"name": "default",
|
|
|
|
"checks": {
|
2024-02-27 11:59:48 +01:00
|
|
|
"peer_network_range_check": {
|
2024-02-22 17:22:43 +01:00
|
|
|
"action": "deny",
|
|
|
|
"ranges": [
|
|
|
|
"192.168.1.0/24"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)),
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: true,
|
|
|
|
expectedPostureCheck: &api.PostureCheck{
|
|
|
|
Id: "postureCheck",
|
|
|
|
Name: "default",
|
|
|
|
Description: str(""),
|
|
|
|
Checks: api.Checks{
|
2024-02-27 11:59:48 +01:00
|
|
|
PeerNetworkRangeCheck: &api.PeerNetworkRangeCheck{
|
2024-02-22 17:22:43 +01:00
|
|
|
Ranges: []string{
|
|
|
|
"192.168.1.0/24",
|
|
|
|
},
|
2024-02-27 11:59:48 +01:00
|
|
|
Action: api.PeerNetworkRangeCheckActionDeny,
|
2024-02-22 17:22:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p := initPostureChecksTestData(&posture.Checks{
|
|
|
|
ID: "postureCheck",
|
|
|
|
Name: "postureCheck",
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "1.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&posture.Checks{
|
|
|
|
ID: "osPostureCheck",
|
|
|
|
Name: "osPostureCheck",
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
OSVersionCheck: &posture.OSVersionCheck{
|
|
|
|
Linux: &posture.MinKernelVersionCheck{
|
|
|
|
MinKernelVersion: "5.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&posture.Checks{
|
|
|
|
ID: "geoPostureCheck",
|
|
|
|
Name: "geoLocation",
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
GeoLocationCheck: &posture.GeoLocationCheck{
|
|
|
|
Locations: []posture.Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: posture.CheckActionDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&posture.Checks{
|
2024-02-27 11:59:48 +01:00
|
|
|
ID: "peerNetworkRangePostureCheck",
|
|
|
|
Name: "peerNetworkRange",
|
2024-02-22 17:22:43 +01:00
|
|
|
Checks: posture.ChecksDefinition{
|
2024-02-27 11:59:48 +01:00
|
|
|
PeerNetworkRangeCheck: &posture.PeerNetworkRangeCheck{
|
2024-02-22 17:22:43 +01:00
|
|
|
Ranges: []netip.Prefix{
|
|
|
|
netip.MustParsePrefix("192.168.0.0/24"),
|
|
|
|
},
|
|
|
|
Action: posture.CheckActionAllow,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody)
|
|
|
|
|
|
|
|
defaultHandler := *p
|
|
|
|
if tc.setupHandlerFunc != nil {
|
|
|
|
tc.setupHandlerFunc(&defaultHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/posture-checks", defaultHandler.CreatePostureCheck).Methods("POST")
|
|
|
|
router.HandleFunc("/api/posture-checks/{postureCheckId}", defaultHandler.UpdatePostureCheck).Methods("PUT")
|
|
|
|
router.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
res := recorder.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
content, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("I don't know what I expected; %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status := recorder.Code; status != tc.expectedStatus {
|
|
|
|
t.Errorf("handler returned wrong status code: got %v want %v, content: %s",
|
|
|
|
status, tc.expectedStatus, string(content))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.expectedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expected, err := json.Marshal(tc.expectedPostureCheck)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("marshal expected posture check: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Trim(string(content), " \n"), string(expected), "content mismatch")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|