/
service.go
218 lines (202 loc) · 7.33 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package client
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"github.com/pkg/errors"
)
// Service represents a local Aqua Service
type Service struct {
Name string `json:"name"`
Description string `json:"description"`
Author string `json:"author"`
Containers []string `json:"containers"`
ContainersCount int `json:"containers_count"`
Evaluated bool `json:"evaluated"`
Monitoring bool `json:"monitoring"`
Policies []string `json:"policies"`
Lastupdate int `json:"lastupdate"`
Vulnerabilities VulnerabilitiesTypes `json:"vulnerabilities"`
Enforce bool `json:"enforce"`
MembershipRules MembershipRules `json:"membership_rules"`
NotEvaluatedCount int `json:"not_evaluated_count"`
UnregisteredCount int `json:"unregistered_count"`
IsRegistered bool `json:"is_registered"`
ApplicationScopes []string `json:"application_scopes"`
}
type VulnerabilitiesTypes struct {
Total int `json:"total"`
High int `json:"high"`
Medium int `json:"medium"`
Low int `json:"low"`
Sensitive int `json:"sensitive"`
Malware int `json:"malware"`
Negligible int `json:"negligible"`
ScoreAverage float64 `json:"score_average"`
}
type MembershipRules struct {
Priority int `json:"priority"`
Scope Scope `json:"scope"`
Target string `json:"target"`
}
type Scope struct {
Expression string `json:"expression"`
Variables []Variable `json:"variables"`
}
type Variable struct {
Attribute string `json:"attribute"`
Name string `json:"name,omitempty"`
Value string `json:"value"`
}
type ServiceList struct {
Count int `json:"count"`
Page int `json:"page"`
Pagesize int `json:"pagesize"`
Result []Service `json:"result"`
}
// GetServices gets all the available services
func (cli *Client) GetServices() (*ServiceList, error) {
var err error
var response ServiceList
request := cli.gorequest
apiPath := "/api/v1/applications"
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
events, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
return nil, errors.Wrap(getMergedError(errs), "failed getting list of Service")
}
if events.StatusCode == 200 {
err = json.Unmarshal([]byte(body), &response)
if err != nil {
log.Printf("Error unmarshalling response as Service list")
return nil, errors.Wrap(err, fmt.Sprintf("couldn't unmarshal list service response. Body: %v", body))
}
} else {
var errorResponse ErrorResponse
err = json.Unmarshal([]byte(body), &errorResponse)
if err != nil {
log.Printf("Error unmarshalling error response")
return nil, fmt.Errorf("failed getting service list. Status: %v, Response: %v", events.StatusCode, body)
}
return nil, fmt.Errorf("failed to list Services. Status: %v. error message: %v", events.StatusCode, errorResponse.Message)
}
return &response, nil
}
// GetService gets an Aqua service by name
func (cli *Client) GetService(name string) (*Service, error) {
var err error
var response Service
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v1/applications/%v", name)
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
events, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
return nil, errors.Wrap(getMergedError(errs), "failed getting service with name "+name)
}
if events.StatusCode == 200 {
err = json.Unmarshal([]byte(body), &response)
if err != nil {
log.Printf("Error unmarshaling response body")
return nil, errors.Wrap(err, fmt.Sprintf("couldn't unmarshal get service response. Body: %v", body))
}
} else {
var errorReponse ErrorResponse
err = json.Unmarshal([]byte(body), &errorReponse)
if err != nil {
log.Println("failed to unmarshal error response")
return nil, fmt.Errorf("failed getting service with name %v. Status: %v, Response: %v", name, events.StatusCode, body)
}
return nil, fmt.Errorf("failed getting service with name %v. Status: %v, error message: %v", name, events.StatusCode, errorReponse.Message)
}
return &response, nil
}
// CreateService creates an Aqua Service
func (cli *Client) CreateService(service *Service) error {
payload, err := json.Marshal(service)
if err != nil {
return err
}
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v1/applications")
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Post(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed creating service.")
}
if resp.StatusCode != 201 && resp.StatusCode != 204 {
var errorResponse ErrorResponse
err = json.Unmarshal([]byte(body), &errorResponse)
if err != nil {
log.Printf("Failed to Unmarshal response Body to ErrorResponse. Body: %v", body)
return fmt.Errorf("failed creating service with name %v. Status: %v, Response: %v", service.Name, resp.StatusCode, body)
}
return fmt.Errorf("failed creating service. status: %v. error message: %v", resp.Status, errorResponse.Message)
}
return nil
}
// UpdateService updates an existing service policy
func (cli *Client) UpdateService(service *Service) error {
payload, err := json.Marshal(service)
if err != nil {
return err
}
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v1/applications/%s", service.Name)
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Put(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed modifying service")
}
if resp.StatusCode != 201 && resp.StatusCode != 204 {
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to read response Body")
return err
}
var errorResponse ErrorResponse
err = json.Unmarshal(body, &errorResponse)
if err != nil {
log.Printf("Failed to Unmarshal response Body to ErrorResponse. Body: %v. error: %v", string(body), err)
return err
}
return fmt.Errorf("failed modifying service policy. status: %v. error message: %v", resp.Status, errorResponse.Message)
}
return nil
}
// DeleteService removes a Aqua Service
func (cli *Client) DeleteService(name string) error {
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v1/applications/%s", name)
err := cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Delete(cli.url + apiPath).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed deleting service")
}
if resp.StatusCode != 204 {
var errorResponse ErrorResponse
err := json.Unmarshal([]byte(body), &errorResponse)
if err != nil {
log.Printf("Failed to Unmarshal response Body to ErrorResponse. Body: %v.", body)
return err
}
return fmt.Errorf("failed deleting service, status: %v. error message: %v", resp.Status, errorResponse.Message)
}
return nil
}