Skip to content

Commit

Permalink
refactor(policies): refactor policies to use v2 client (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptubic authored May 8, 2023
1 parent 0d528b2 commit a4654a7
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 147 deletions.
5 changes: 0 additions & 5 deletions sysdig/internal/client/secure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import (
)

type SysdigSecureClient interface {
CreatePolicy(context.Context, Policy) (Policy, error)
DeletePolicy(context.Context, int) error
UpdatePolicy(context.Context, Policy) (Policy, error)
GetPolicyById(context.Context, int) (Policy, int, error)

CreateRule(context.Context, Rule) (Rule, error)
GetRuleByID(context.Context, int) (Rule, error)
UpdateRule(context.Context, Rule) (Rule, error)
Expand Down
36 changes: 0 additions & 36 deletions sysdig/internal/client/secure/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,6 @@ import (
"io"
)

// -------- Policies --------

type Policy struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Severity int `json:"severity"`
Enabled bool `json:"enabled"`
RuleNames []string `json:"ruleNames"`
Actions []Action `json:"actions"`
Scope string `json:"scope,omitempty"`
Version int `json:"version,omitempty"`
NotificationChannelIds []int `json:"notificationChannelIds"`
Type string `json:"type"`
Runbook string `json:"runbook"`
}

type Action struct {
AfterEventNs int `json:"afterEventNs,omitempty"`
BeforeEventNs int `json:"beforeEventNs,omitempty"`
Name string `json:"name,omitempty"`
IsLimitedToContainer bool `json:"isLimitedToContainer"`
Type string `json:"type"`
}

func (policy *Policy) ToJSON() io.Reader {
payload, _ := json.Marshal(policy)
return bytes.NewBuffer(payload)
}

func PolicyFromJSON(body []byte) (result Policy) {
_ = json.Unmarshal(body, &result)

return result
}

// -------- Rules --------

type Rule struct {
Expand Down
86 changes: 0 additions & 86 deletions sysdig/internal/client/secure/policies.go

This file was deleted.

23 changes: 23 additions & 0 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,29 @@ type Monitor struct {
StdDevFactor float64 `json:"stdDevFactor"`
}

type Policy struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Severity int `json:"severity"`
Enabled bool `json:"enabled"`
RuleNames []string `json:"ruleNames"`
Actions []Action `json:"actions"`
Scope string `json:"scope,omitempty"`
Version int `json:"version,omitempty"`
NotificationChannelIds []int `json:"notificationChannelIds"`
Type string `json:"type"`
Runbook string `json:"runbook"`
}

type Action struct {
AfterEventNs int `json:"afterEventNs,omitempty"`
BeforeEventNs int `json:"beforeEventNs,omitempty"`
Name string `json:"name,omitempty"`
IsLimitedToContainer bool `json:"isLimitedToContainer"`
Type string `json:"type"`
}

type List struct {
Name string `json:"name"`
Items Items `json:"items"`
Expand Down
109 changes: 109 additions & 0 deletions sysdig/internal/client/v2/policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package v2

import (
"context"
"fmt"
"net/http"
)

const (
CreatePolicyPath = "%s/api/v2/policies"
DeletePolicyPath = "%s/api/v2/policies/%d"
UpdatePolicyPath = "%s/api/v2/policies/%d"
GetPolicyPath = "%s/api/v2/policies/%d"
)

type PolicyInterface interface {
CreatePolicy(ctx context.Context, policy Policy) (Policy, error)
DeletePolicy(ctx context.Context, policyID int) error
UpdatePolicy(ctx context.Context, policy Policy) (Policy, error)
GetPolicyByID(ctx context.Context, policyID int) (Policy, int, error)
}

func (client *Client) CreatePolicy(ctx context.Context, policy Policy) (Policy, error) {
payload, err := Marshal(policy)
if err != nil {
return Policy{}, err
}

response, err := client.requester.Request(ctx, http.MethodPost, client.CreatePolicyURL(), payload)
if err != nil {
return Policy{}, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return Policy{}, client.ErrorFromResponse(response)
}

return Unmarshal[Policy](response.Body)
}

func (client *Client) DeletePolicy(ctx context.Context, policyID int) error {
response, err := client.requester.Request(ctx, http.MethodDelete, client.DeletePolicyURL(policyID), nil)
if err != nil {
return err
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
return client.ErrorFromResponse(response)
}

return err
}

func (client *Client) UpdatePolicy(ctx context.Context, policy Policy) (Policy, error) {
payload, err := Marshal(policy)
if err != nil {
return Policy{}, err
}

response, err := client.requester.Request(ctx, http.MethodPut, client.UpdatePolicyURL(policy.ID), payload)
if err != nil {
return Policy{}, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return Policy{}, client.ErrorFromResponse(response)
}

return Unmarshal[Policy](response.Body)
}

func (client *Client) GetPolicyByID(ctx context.Context, policyID int) (Policy, int, error) {
response, err := client.requester.Request(ctx, http.MethodGet, client.GetPolicyURL(policyID), nil)
if err != nil {
return Policy{}, 0, err

}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return Policy{}, response.StatusCode, client.ErrorFromResponse(response)
}

policy, err := Unmarshal[Policy](response.Body)
if err != nil {
return Policy{}, 0, err
}

return policy, http.StatusOK, nil
}

func (client *Client) CreatePolicyURL() string {
return fmt.Sprintf(CreatePolicyPath, client.config.url)
}

func (client *Client) DeletePolicyURL(policyID int) string {
return fmt.Sprintf(DeletePolicyPath, client.config.url, policyID)
}

func (client *Client) UpdatePolicyURL(policyID int) string {
return fmt.Sprintf(UpdatePolicyPath, client.config.url, policyID)
}

func (client *Client) GetPolicyURL(policyID int) string {
return fmt.Sprintf(GetPolicyPath, client.config.url, policyID)
}
1 change: 1 addition & 0 deletions sysdig/internal/client/v2/sysdig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type SysdigMonitor interface {

type SysdigSecure interface {
SysdigCommon
PolicyInterface
ListInterface
MacroInterface
}
Expand Down
Loading

0 comments on commit a4654a7

Please sign in to comment.