Skip to content

Commit

Permalink
Refactored posture checks implementation to align with the new API sc…
Browse files Browse the repository at this point in the history
…hema
  • Loading branch information
bcmmbaga committed Jan 5, 2024
1 parent feec7fc commit f79c2aa
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 75 deletions.
4 changes: 2 additions & 2 deletions management/server/http/api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions management/server/http/api/types.gen.go

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

16 changes: 8 additions & 8 deletions management/server/http/posture_checks_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand All @@ -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")
}

Expand Down
153 changes: 91 additions & 62 deletions management/server/http/posture_checks_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -98,20 +99,20 @@ 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,
},
}

postureCheck := &posture.Checks{
ID: "postureCheck1",
Name: "postureCheck1",
ID: "postureCheck",
Name: "name",
Checks: []posture.Check{
&posture.NBVersionCheck{
Enabled: true,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -211,66 +226,80 @@ 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,
},
{
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,
},
}

p := initPostureChecksTestData(&posture.Checks{
ID: "postureCheck1",
Name: "postureCheck1",
ID: "postureCheck",
Name: "postureCheck",
Checks: []posture.Check{
&posture.NBVersionCheck{
Enabled: true,
Expand Down

0 comments on commit f79c2aa

Please sign in to comment.