-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
331 lines (315 loc) · 10 KB
/
client.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package iprepd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// Client is the iprepd service client
type Client struct {
hostURL string
authStr string
httpClient *http.Client
}
// in an effort to keep the client's error handling consistent and for ease of
// testing, all error messages should be listed below and reused where possible
const (
// input validation errors
clientErrURLEmpty = "url cannot be empty"
clientErrAuthEmpty = "auth credentials cannot be empty"
clientErrObjectEmpty = "object cannot be empty"
clientErrObjectTypeEmpty = "object type cannot be empty"
clientErrBadType = "object provided does not match type"
clientErrViolationEmpty = "violation cannot be empty"
clientErrReputationNil = "reputation cannot be nil"
clientErrViolationRequestNil = "violation request cannot be nil"
clientErrMarshal = "could not marshal payload"
// http client errors
clientErrBuildRequest = "could not build http request"
clientErrSendRequest = "could not send http request"
// http reponse payload errors
clientErrReadResponse = "could not read response body"
clientErrNon200 = "non 200 status code received"
clientErrUnmarshal = "could not unmarshal response body"
)
// NewClient is the default constructor for the client
func NewClient(url, token string, httpClient *http.Client) (*Client, error) {
if url == "" {
return nil, errors.New(clientErrURLEmpty)
}
if token == "" {
return nil, errors.New(clientErrAuthEmpty)
}
if httpClient == nil {
httpClient = http.DefaultClient
}
return &Client{
hostURL: url,
authStr: token,
httpClient: httpClient,
}, nil
}
func (c *Client) addAuth(r *http.Request) {
r.Header.Set("Authorization", c.authStr)
}
// Dump retrieves all reputation entries
func (c *Client) Dump() ([]Reputation, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/dump", c.hostURL), nil)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
c.addAuth(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrReadResponse, err)
}
var ret []Reputation
if err = json.Unmarshal(bodyBytes, &ret); err != nil {
return nil, fmt.Errorf("%s: %s", clientErrUnmarshal, err)
}
return ret, nil
}
// Heartbeat checks whether an IPrepd deployment is healthy / reachable
func (c *Client) Heartbeat() (bool, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/__heartbeat__", c.hostURL), nil)
if err != nil {
return false, fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return false, fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
return (resp.StatusCode == http.StatusOK), nil
}
// LBHeartbeat checks whether an IPrepd LB is healthy / reachable
func (c *Client) LBHeartbeat() (bool, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/__lbheartbeat__", c.hostURL), nil)
if err != nil {
return false, fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return false, fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
return (resp.StatusCode == http.StatusOK), nil
}
// GetReputation fetches the reputation of a given object and type
func (c *Client) GetReputation(objectType, object string) (*Reputation, error) {
if object == "" {
return nil, errors.New(clientErrObjectEmpty)
}
if objectType == "" {
return nil, errors.New(clientErrObjectTypeEmpty)
}
if err := validateType(objectType, object); err != nil {
return nil, errors.New(clientErrBadType)
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/type/%s/%s", c.hostURL, objectType, object), nil)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
c.addAuth(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
byt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrReadResponse, err)
}
var r *Reputation
if err := json.Unmarshal(byt, &r); err != nil {
return nil, fmt.Errorf("%s: %s", clientErrUnmarshal, err)
}
return r, nil
}
// SetReputation updates the reputation of a given object and type to a given score
func (c *Client) SetReputation(r *Reputation) error {
if r == nil {
return errors.New(clientErrReputationNil)
}
if r.Object == "" {
return errors.New(clientErrObjectEmpty)
}
if r.Type == "" {
return errors.New(clientErrObjectTypeEmpty)
}
if err := validateType(r.Type, r.Object); err != nil {
return errors.New(clientErrBadType)
}
byt, err := json.Marshal(&r)
if err != nil {
return fmt.Errorf("%s: %s", clientErrMarshal, err)
}
req, err := http.NewRequest(http.MethodPut,
fmt.Sprintf("%s/type/%s/%s", c.hostURL, r.Type, r.Object), bytes.NewBuffer(byt))
if err != nil {
return fmt.Errorf("%s: %s", clientErrMarshal, err)
}
c.addAuth(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
return nil
}
// DeleteReputation deletes the reputation of a given object and type
func (c *Client) DeleteReputation(objectType, object string) error {
if object == "" {
return errors.New(clientErrObjectEmpty)
}
if objectType == "" {
return errors.New(clientErrObjectTypeEmpty)
}
if err := validateType(objectType, object); err != nil {
return errors.New(clientErrBadType)
}
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/type/%s/%s", c.hostURL, objectType, object), nil)
if err != nil {
return fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
c.addAuth(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
return nil
}
// VersionResponse is the response payload from the /__version__ endpoint
type VersionResponse struct {
Commit string `json:"commit"`
Version string `json:"version"`
Source string `json:"source"`
Build string `json:"build"`
}
// Version retrieves the version of the IPrepd deployment
func (c *Client) Version() (*VersionResponse, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/__version__", c.hostURL), nil)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrReadResponse, err)
}
var vr *VersionResponse
if err = json.Unmarshal(bodyBytes, &vr); err != nil {
return nil, fmt.Errorf("%s: %s", clientErrUnmarshal, err)
}
return vr, nil
}
// GetViolations gets all existing violations on the server
func (c *Client) GetViolations() ([]Violation, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/violations", c.hostURL), nil)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
c.addAuth(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
bodyByt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%s: %s", clientErrReadResponse, err)
}
var v []Violation
if err = json.Unmarshal(bodyByt, &v); err != nil {
return nil, fmt.Errorf("%s: %s", clientErrUnmarshal, err)
}
return v, nil
}
// ApplyViolation submits a ViolationRequest to iprepd
func (c *Client) ApplyViolation(vr *ViolationRequest) error {
if vr == nil {
return errors.New(clientErrViolationRequestNil)
}
if vr.Object == "" {
return errors.New(clientErrObjectEmpty)
}
if vr.Type == "" {
return errors.New(clientErrObjectTypeEmpty)
}
if err := validateType(vr.Type, vr.Object); err != nil {
return errors.New(clientErrBadType)
}
if vr.Violation == "" {
return errors.New(clientErrViolationEmpty)
}
byt, err := json.Marshal(&vr)
if err != nil {
return fmt.Errorf("%s: %s", clientErrMarshal, err)
}
req, err := http.NewRequest(http.MethodPut,
fmt.Sprintf("%s/violations/type/%s/%s", c.hostURL, vr.Type, vr.Object), bytes.NewBuffer(byt))
if err != nil {
return fmt.Errorf("%s: %s", clientErrBuildRequest, err)
}
c.addAuth(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
return nil
}
// BatchApplyViolation submits a batch of ViolationRequests to iprepd
func (c *Client) BatchApplyViolation(typ string, vrs []ViolationRequest) error {
if typ == "" {
return errors.New(clientErrObjectTypeEmpty)
}
if len(vrs) == 0 {
return nil
}
byt, err := json.Marshal(&vrs)
if err != nil {
return fmt.Errorf("%s: %s", clientErrMarshal, err)
}
req, err := http.NewRequest(http.MethodPut,
fmt.Sprintf("%s/violations/type/%s", c.hostURL, typ), bytes.NewBuffer(byt))
if err != nil {
return fmt.Errorf("%s: %s", clientErrMarshal, err)
}
c.addAuth(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("%s: %s", clientErrSendRequest, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %d", clientErrNon200, resp.StatusCode)
}
return nil
}