From f79c2aa0e70ff25d0346029c39e37dfcf9fc7a83 Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Fri, 5 Jan 2024 10:35:32 +0300 Subject: [PATCH] Refactored posture checks implementation to align with the new API schema --- management/server/http/api/openapi.yml | 4 +- management/server/http/api/types.gen.go | 6 +- .../server/http/posture_checks_handler.go | 16 +- .../http/posture_checks_handler_test.go | 153 +++++++++++------- 4 files changed, 104 insertions(+), 75 deletions(-) diff --git a/management/server/http/api/openapi.yml b/management/server/http/api/openapi.yml index 26160f38989..c7a640b9e91 100644 --- a/management/server/http/api/openapi.yml +++ b/management/server/http/api/openapi.yml @@ -862,8 +862,8 @@ components: description: Posture check friendly description type: string example: This checks if the peer is running required NetBird's version - nb_version_check: - $ref: '#/components/schemas/NBVersionCheck' + checks: + $ref: '#/components/schemas/Checks' required: - name - description diff --git a/management/server/http/api/types.gen.go b/management/server/http/api/types.gen.go index 2015551034d..a3a0df91d53 100644 --- a/management/server/http/api/types.gen.go +++ b/management/server/http/api/types.gen.go @@ -762,14 +762,14 @@ type PostureCheck struct { // PostureCheckUpdate defines model for PostureCheckUpdate. type PostureCheckUpdate struct { + // Checks List of objects that perform the actual checks + Checks *Checks `json:"checks,omitempty"` + // Description Posture check friendly description Description string `json:"description"` // Name Posture check name identifier Name string `json:"name"` - - // NbVersionCheck Posture check for the version of NetBird - NbVersionCheck *NBVersionCheck `json:"nb_version_check,omitempty"` } // Route defines model for Route. diff --git a/management/server/http/posture_checks_handler.go b/management/server/http/posture_checks_handler.go index c946b9b0a95..44fbc42cec9 100644 --- a/management/server/http/posture_checks_handler.go +++ b/management/server/http/posture_checks_handler.go @@ -167,15 +167,15 @@ func (p *PostureChecksHandler) savePostureChecks( Checks: make([]posture.Check, 0), } - if req.NbVersionCheck != nil { + if nbVersionCheck := req.Checks.NbVersionCheck; nbVersionCheck != nil { var maxVersion string - if req.NbVersionCheck.MaxVersion != nil { - maxVersion = *req.NbVersionCheck.MaxVersion + if nbVersionCheck.MaxVersion != nil { + maxVersion = *nbVersionCheck.MaxVersion } postureChecks.Checks = append(postureChecks.Checks, &posture.NBVersionCheck{ - Enabled: req.NbVersionCheck.Enabled, - MinVersion: req.NbVersionCheck.MinVersion, + Enabled: nbVersionCheck.Enabled, + MinVersion: nbVersionCheck.MinVersion, MaxVersion: maxVersion, }) } @@ -193,11 +193,11 @@ func validatePostureChecksUpdate(req api.PostureCheckUpdate) error { return status.Errorf(status.InvalidArgument, "posture checks name shouldn't be empty") } - if req.NbVersionCheck == nil { - return status.Errorf(status.InvalidArgument, "posture checks list shouldn't be empty") + if req.Checks == nil { + return status.Errorf(status.InvalidArgument, "posture checks shouldn't be empty") } - if req.NbVersionCheck != nil && req.NbVersionCheck.MinVersion == "" { + if req.Checks.NbVersionCheck != nil && req.Checks.NbVersionCheck.MinVersion == "" { return status.Errorf(status.InvalidArgument, "minimum version for NetBird's version check shouldn't be empty") } diff --git a/management/server/http/posture_checks_handler_test.go b/management/server/http/posture_checks_handler_test.go index aa2ca4ee15b..c14fe73ed65 100644 --- a/management/server/http/posture_checks_handler_test.go +++ b/management/server/http/posture_checks_handler_test.go @@ -36,6 +36,7 @@ func initPostureChecksTestData(postureChecks ...*posture.Checks) *PostureChecksH return p, nil }, SavePostureChecksFunc: func(accountID, userID string, postureChecks *posture.Checks) error { + postureChecks.ID = "postureCheck" testPostureChecks[postureChecks.ID] = postureChecks return nil }, @@ -98,11 +99,11 @@ func TestGetPostureCheck(t *testing.T) { name: "GetPostureCheck OK", expectedBody: true, requestType: http.MethodGet, - requestPath: "/api/posture-checks/postureCheck1", + requestPath: "/api/posture-checks/postureCheck", expectedStatus: http.StatusOK, }, { - name: "GetPostureCheck not found", + name: "GetPostureCheck Not Found", requestType: http.MethodGet, requestPath: "/api/posture-checks/not-exists", expectedStatus: http.StatusNotFound, @@ -110,8 +111,8 @@ func TestGetPostureCheck(t *testing.T) { } postureCheck := &posture.Checks{ - ID: "postureCheck1", - Name: "postureCheck1", + ID: "postureCheck", + Name: "name", Checks: []posture.Check{ &posture.NBVersionCheck{ Enabled: true, @@ -161,6 +162,7 @@ func TestGetPostureCheck(t *testing.T) { } func TestPostureCheckUpdate(t *testing.T) { + str := func(s string) *string { return &s } tt := []struct { name string expectedStatus int @@ -170,36 +172,49 @@ func TestPostureCheckUpdate(t *testing.T) { requestPath string requestBody io.Reader }{ - //{ - // name: "Create Posture Checks", - // requestType: http.MethodPost, - // requestPath: "/api/posture-checks", - // requestBody: bytes.NewBuffer( - // []byte(`{ - // "name": "default", - // "nb_version_check": { - // "enabled": true, - // "min_version": "1.2.3" - // } - // }`)), - // expectedStatus: http.StatusOK, - // expectedBody: true, - // expectedPostureCheck: &api.PostureCheck{ - // Id: "id-was-set", - // Name: "default", - // Checks: []api.Checks{}, - // }, - //}, + { + name: "Create Posture Checks", + requestType: http.MethodPost, + requestPath: "/api/posture-checks", + requestBody: bytes.NewBuffer( + []byte(`{ + "name": "default", + "description": "default", + "checks": { + "nb_version_check": { + "enabled": true, + "min_version": "1.2.3", + "max_version": "2.0.0" + } + } + }`)), + expectedStatus: http.StatusOK, + expectedBody: true, + expectedPostureCheck: &api.PostureCheck{ + Id: "postureCheck", + Name: "default", + Description: str("default"), + Checks: &api.Checks{ + NbVersionCheck: &api.NBVersionCheck{ + Enabled: true, + MinVersion: "1.2.3", + MaxVersion: str("2.0.0"), + }, + }, + }, + }, { name: "Create Posture Checks Invalid Name", requestType: http.MethodPost, requestPath: "/api/posture-checks", requestBody: bytes.NewBuffer( []byte(`{ - "nb_version_check": { - "enabled": true, - "min_version": "1.2.3" - } + "checks": { + "nb_version_check": { + "enabled": true, + "min_version": "1.2.0" + } + } }`)), expectedStatus: http.StatusBadRequest, expectedBody: false, @@ -211,43 +226,55 @@ func TestPostureCheckUpdate(t *testing.T) { requestBody: bytes.NewBuffer( []byte(`{ "name": "default", - "nb_version_check": { - "enabled": true, - } + "checks": { + "nb_version_check": { + "enabled": true, + } + } }`)), expectedStatus: http.StatusBadRequest, expectedBody: false, }, - //{ - // name: "Update Posture Checks", - // requestType: http.MethodPut, - // requestPath: "/api/posture-checks/postureCheck1", - // requestBody: bytes.NewBuffer( - // []byte(`{ - // "name": "default", - // "nb_version_check": { - // "enabled": true, - // "min_version": "1.2.3" - // } - // }`)), - // expectedStatus: http.StatusOK, - // expectedBody: true, - // expectedPostureCheck: &api.PostureCheck{ - // Id: "id-was-set", - // Name: "default", - // Checks: []api.Checks{}, - // }, - //}, + { + name: "Update Posture Checks", + requestType: http.MethodPut, + requestPath: "/api/posture-checks/postureCheck", + requestBody: bytes.NewBuffer( + []byte(`{ + "name": "default", + "checks": { + "nb_version_check": { + "enabled": true, + "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{ + Enabled: true, + MinVersion: "1.9.0", + MaxVersion: str(""), + }, + }, + }, + }, { name: "Update Posture Checks Invalid Name", requestType: http.MethodPut, - requestPath: "/api/posture-checks/postureCheck1", + requestPath: "/api/posture-checks/postureCheck", requestBody: bytes.NewBuffer( []byte(`{ - "nb_version_check": { - "enabled": true, - "min_version": "1.2.3" - } + "checks": { + "nb_version_check": { + "enabled": true, + } + } }`)), expectedStatus: http.StatusBadRequest, expectedBody: false, @@ -255,13 +282,15 @@ func TestPostureCheckUpdate(t *testing.T) { { name: "Update Posture Checks Invalid NetBird's Min Version", requestType: http.MethodPut, - requestPath: "/api/posture-checks/postureCheck1", + requestPath: "/api/posture-checks/postureCheck", requestBody: bytes.NewBuffer( []byte(`{ "name": "default", - "nb_version_check": { - "enabled": true, - } + "checks": { + "nb_version_check": { + "enabled": false, + } + } }`)), expectedStatus: http.StatusBadRequest, expectedBody: false, @@ -269,8 +298,8 @@ func TestPostureCheckUpdate(t *testing.T) { } p := initPostureChecksTestData(&posture.Checks{ - ID: "postureCheck1", - Name: "postureCheck1", + ID: "postureCheck", + Name: "postureCheck", Checks: []posture.Check{ &posture.NBVersionCheck{ Enabled: true,