Skip to content

Commit

Permalink
Extend policy endpoint with posture checks (#1450)
Browse files Browse the repository at this point in the history
* Implement posture and version checks in API models

* go mod tidy

* Allow attaching posture checks to policy

* Update error message for linked posture check on deleting

* Refactor PostureCheck and Checks structures

* go mod tidy
  • Loading branch information
bcmmbaga authored Jan 9, 2024
1 parent d64beaa commit b308f34
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
8 changes: 7 additions & 1 deletion management/server/http/api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,12 @@ components:
- $ref: '#/components/schemas/PolicyMinimum'
- type: object
properties:
source_posture_checks:
description: Posture checks ID's applied to policy source groups
type: array
items:
type: string
example: "chacdk86lnnboviihd70"
rules:
description: Policy rule object for policy UI editor
type: array
Expand Down Expand Up @@ -825,7 +831,7 @@ components:
required:
- id
- name
- check
- checks
Checks:
description: List of objects that perform the actual checks
type: object
Expand Down
5 changes: 4 additions & 1 deletion management/server/http/api/types.gen.go

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

18 changes: 18 additions & 0 deletions management/server/http/policies_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func (h *Policies) savePolicy(
policy.Rules = append(policy.Rules, &pr)
}

if req.SourcePostureChecks != nil {
policy.SourcePostureChecks = sourcePostureChecksToStrings(account, *req.SourcePostureChecks)
}

if err := h.accountManager.SavePolicy(account.Id, user.Id, &policy); err != nil {
util.WriteError(err, w)
return
Expand Down Expand Up @@ -352,3 +356,17 @@ func groupMinimumsToStrings(account *server.Account, gm []string) []string {
}
return result
}

func sourcePostureChecksToStrings(account *server.Account, postureChecksIds []string) []string {
result := make([]string, 0, len(postureChecksIds))
for _, id := range postureChecksIds {
for _, postureCheck := range account.PostureChecks {
if id == postureCheck.ID {
result = append(result, id)
continue
}
}

}
return result
}
2 changes: 1 addition & 1 deletion management/server/http/posture_checks_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,6 @@ func toPostureChecksResponse(postureChecks *posture.Checks) *api.PostureCheck {
Id: postureChecks.ID,
Name: postureChecks.Name,
Description: &postureChecks.Description,
Checks: &checks,
Checks: checks,
}
}
4 changes: 2 additions & 2 deletions management/server/http/posture_checks_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestPostureCheckUpdate(t *testing.T) {
Id: "postureCheck",
Name: "default",
Description: str("default"),
Checks: &api.Checks{
Checks: api.Checks{
NbVersionCheck: &api.NBVersionCheck{
Enabled: true,
MinVersion: "1.2.3",
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestPostureCheckUpdate(t *testing.T) {
Id: "postureCheck",
Name: "default",
Description: str(""),
Checks: &api.Checks{
Checks: api.Checks{
NbVersionCheck: &api.NBVersionCheck{
Enabled: true,
MinVersion: "1.9.0",
Expand Down
3 changes: 2 additions & 1 deletion management/server/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ func (p *Policy) Copy() *Policy {
Description: p.Description,
Enabled: p.Enabled,
Rules: make([]*PolicyRule, len(p.Rules)),
SourcePostureChecks: p.SourcePostureChecks,
SourcePostureChecks: make([]string, len(p.SourcePostureChecks)),
}
for i, r := range p.Rules {
c.Rules[i] = r.Copy()
}
copy(c.SourcePostureChecks, p.SourcePostureChecks)
return c
}

Expand Down
4 changes: 1 addition & 3 deletions management/server/posture_checks.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package server

import (
"fmt"

"github.com/netbirdio/netbird/management/server/activity"
"github.com/netbirdio/netbird/management/server/posture"
"github.com/netbirdio/netbird/management/server/status"
Expand Down Expand Up @@ -134,7 +132,7 @@ func (am *DefaultAccountManager) deletePostureChecks(account *Account, postureCh
for _, policy := range account.Policies {
for _, id := range policy.SourcePostureChecks {
if id == postureChecksID {
return nil, fmt.Errorf("posture checks have been linked to policy: %s", policy.Name)
return nil, status.Errorf(status.PreconditionFailed, "posture checks have been linked to policy: %s", policy.Name)
}
}
}
Expand Down

0 comments on commit b308f34

Please sign in to comment.