Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend policy endpoint with posture checks #1450

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading