Skip to content

Commit a4654a7

Browse files
authored
refactor(policies): refactor policies to use v2 client (#312)
1 parent 0d528b2 commit a4654a7

File tree

8 files changed

+151
-147
lines changed

8 files changed

+151
-147
lines changed

sysdig/internal/client/secure/client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import (
1212
)
1313

1414
type SysdigSecureClient interface {
15-
CreatePolicy(context.Context, Policy) (Policy, error)
16-
DeletePolicy(context.Context, int) error
17-
UpdatePolicy(context.Context, Policy) (Policy, error)
18-
GetPolicyById(context.Context, int) (Policy, int, error)
19-
2015
CreateRule(context.Context, Rule) (Rule, error)
2116
GetRuleByID(context.Context, int) (Rule, error)
2217
UpdateRule(context.Context, Rule) (Rule, error)

sysdig/internal/client/secure/models.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,6 @@ import (
66
"io"
77
)
88

9-
// -------- Policies --------
10-
11-
type Policy struct {
12-
ID int `json:"id,omitempty"`
13-
Name string `json:"name"`
14-
Description string `json:"description"`
15-
Severity int `json:"severity"`
16-
Enabled bool `json:"enabled"`
17-
RuleNames []string `json:"ruleNames"`
18-
Actions []Action `json:"actions"`
19-
Scope string `json:"scope,omitempty"`
20-
Version int `json:"version,omitempty"`
21-
NotificationChannelIds []int `json:"notificationChannelIds"`
22-
Type string `json:"type"`
23-
Runbook string `json:"runbook"`
24-
}
25-
26-
type Action struct {
27-
AfterEventNs int `json:"afterEventNs,omitempty"`
28-
BeforeEventNs int `json:"beforeEventNs,omitempty"`
29-
Name string `json:"name,omitempty"`
30-
IsLimitedToContainer bool `json:"isLimitedToContainer"`
31-
Type string `json:"type"`
32-
}
33-
34-
func (policy *Policy) ToJSON() io.Reader {
35-
payload, _ := json.Marshal(policy)
36-
return bytes.NewBuffer(payload)
37-
}
38-
39-
func PolicyFromJSON(body []byte) (result Policy) {
40-
_ = json.Unmarshal(body, &result)
41-
42-
return result
43-
}
44-
459
// -------- Rules --------
4610

4711
type Rule struct {

sysdig/internal/client/secure/policies.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

sysdig/internal/client/v2/model.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,29 @@ type Monitor struct {
177177
StdDevFactor float64 `json:"stdDevFactor"`
178178
}
179179

180+
type Policy struct {
181+
ID int `json:"id,omitempty"`
182+
Name string `json:"name"`
183+
Description string `json:"description"`
184+
Severity int `json:"severity"`
185+
Enabled bool `json:"enabled"`
186+
RuleNames []string `json:"ruleNames"`
187+
Actions []Action `json:"actions"`
188+
Scope string `json:"scope,omitempty"`
189+
Version int `json:"version,omitempty"`
190+
NotificationChannelIds []int `json:"notificationChannelIds"`
191+
Type string `json:"type"`
192+
Runbook string `json:"runbook"`
193+
}
194+
195+
type Action struct {
196+
AfterEventNs int `json:"afterEventNs,omitempty"`
197+
BeforeEventNs int `json:"beforeEventNs,omitempty"`
198+
Name string `json:"name,omitempty"`
199+
IsLimitedToContainer bool `json:"isLimitedToContainer"`
200+
Type string `json:"type"`
201+
}
202+
180203
type List struct {
181204
Name string `json:"name"`
182205
Items Items `json:"items"`
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const (
10+
CreatePolicyPath = "%s/api/v2/policies"
11+
DeletePolicyPath = "%s/api/v2/policies/%d"
12+
UpdatePolicyPath = "%s/api/v2/policies/%d"
13+
GetPolicyPath = "%s/api/v2/policies/%d"
14+
)
15+
16+
type PolicyInterface interface {
17+
CreatePolicy(ctx context.Context, policy Policy) (Policy, error)
18+
DeletePolicy(ctx context.Context, policyID int) error
19+
UpdatePolicy(ctx context.Context, policy Policy) (Policy, error)
20+
GetPolicyByID(ctx context.Context, policyID int) (Policy, int, error)
21+
}
22+
23+
func (client *Client) CreatePolicy(ctx context.Context, policy Policy) (Policy, error) {
24+
payload, err := Marshal(policy)
25+
if err != nil {
26+
return Policy{}, err
27+
}
28+
29+
response, err := client.requester.Request(ctx, http.MethodPost, client.CreatePolicyURL(), payload)
30+
if err != nil {
31+
return Policy{}, err
32+
}
33+
defer response.Body.Close()
34+
35+
if response.StatusCode != http.StatusOK {
36+
return Policy{}, client.ErrorFromResponse(response)
37+
}
38+
39+
return Unmarshal[Policy](response.Body)
40+
}
41+
42+
func (client *Client) DeletePolicy(ctx context.Context, policyID int) error {
43+
response, err := client.requester.Request(ctx, http.MethodDelete, client.DeletePolicyURL(policyID), nil)
44+
if err != nil {
45+
return err
46+
}
47+
defer response.Body.Close()
48+
49+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
50+
return client.ErrorFromResponse(response)
51+
}
52+
53+
return err
54+
}
55+
56+
func (client *Client) UpdatePolicy(ctx context.Context, policy Policy) (Policy, error) {
57+
payload, err := Marshal(policy)
58+
if err != nil {
59+
return Policy{}, err
60+
}
61+
62+
response, err := client.requester.Request(ctx, http.MethodPut, client.UpdatePolicyURL(policy.ID), payload)
63+
if err != nil {
64+
return Policy{}, err
65+
}
66+
defer response.Body.Close()
67+
68+
if response.StatusCode != http.StatusOK {
69+
return Policy{}, client.ErrorFromResponse(response)
70+
}
71+
72+
return Unmarshal[Policy](response.Body)
73+
}
74+
75+
func (client *Client) GetPolicyByID(ctx context.Context, policyID int) (Policy, int, error) {
76+
response, err := client.requester.Request(ctx, http.MethodGet, client.GetPolicyURL(policyID), nil)
77+
if err != nil {
78+
return Policy{}, 0, err
79+
80+
}
81+
defer response.Body.Close()
82+
83+
if response.StatusCode != http.StatusOK {
84+
return Policy{}, response.StatusCode, client.ErrorFromResponse(response)
85+
}
86+
87+
policy, err := Unmarshal[Policy](response.Body)
88+
if err != nil {
89+
return Policy{}, 0, err
90+
}
91+
92+
return policy, http.StatusOK, nil
93+
}
94+
95+
func (client *Client) CreatePolicyURL() string {
96+
return fmt.Sprintf(CreatePolicyPath, client.config.url)
97+
}
98+
99+
func (client *Client) DeletePolicyURL(policyID int) string {
100+
return fmt.Sprintf(DeletePolicyPath, client.config.url, policyID)
101+
}
102+
103+
func (client *Client) UpdatePolicyURL(policyID int) string {
104+
return fmt.Sprintf(UpdatePolicyPath, client.config.url, policyID)
105+
}
106+
107+
func (client *Client) GetPolicyURL(policyID int) string {
108+
return fmt.Sprintf(GetPolicyPath, client.config.url, policyID)
109+
}

sysdig/internal/client/v2/sysdig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type SysdigMonitor interface {
2828

2929
type SysdigSecure interface {
3030
SysdigCommon
31+
PolicyInterface
3132
ListInterface
3233
MacroInterface
3334
}

0 commit comments

Comments
 (0)