-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpolicy.go
364 lines (323 loc) · 11.3 KB
/
policy.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2020, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package api
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"time"
"github.com/lacework/go-sdk/v2/internal/array"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
// PolicyService is a service that interacts with the Custom Policies
// endpoints from the Lacework Server
type PolicyService struct {
client *Client
Exceptions *policyExceptionsService
}
func NewV2PolicyService(c *Client) *PolicyService {
return &PolicyService{c,
&policyExceptionsService{c},
}
}
type policyType int
type policyTypes map[policyType]string
const (
PolicyTypeCompliance policyType = iota
PolicyTypeManual
PolicyTypeViolation
)
var ValidPolicyTypes = policyTypes{
PolicyTypeCompliance: "Compliance",
PolicyTypeManual: "Manual",
PolicyTypeViolation: "Violation",
}
func (p policyType) String() string {
return ValidPolicyTypes[p]
}
func (pt policyTypes) String() (types []string) {
for _, v := range pt {
types = append(types, v)
}
return
}
// ValidPolicySeverities is a list of all valid policy severities
var ValidPolicySeverities = []string{"critical", "high", "medium", "low", "info"}
type NewPolicy struct {
PolicyID string `json:"policyId,omitempty" yaml:"policyId,omitempty" `
PolicyType string `json:"policyType" yaml:"policyType"`
QueryID string `json:"queryId" yaml:"queryId"`
Title string `json:"title" yaml:"title"`
Enabled bool `json:"enabled" yaml:"enabled"`
Description string `json:"description" yaml:"description"`
Remediation string `json:"remediation" yaml:"remediation"`
Severity string `json:"severity" yaml:"severity"`
Limit int `json:"limit,omitempty" yaml:"limit,omitempty"`
EvalFrequency string `json:"evalFrequency,omitempty" yaml:"evalFrequency,omitempty"`
AlertEnabled bool `json:"alertEnabled" yaml:"alertEnabled"`
AlertProfile string `json:"alertProfile,omitempty" yaml:"alertProfile,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
}
type newPoliciesYAML struct {
Policies []NewPolicy `yaml:"policies"`
}
func ParseNewPolicy(s string) (NewPolicy, error) {
var policy NewPolicy
var err error
// valid json
if err = json.Unmarshal([]byte(s), &policy); err == nil {
return policy, err
}
// nested yaml
var policies newPoliciesYAML
if err = yaml.Unmarshal([]byte(s), &policies); err == nil {
if len(policies.Policies) > 0 {
return policies.Policies[0], err
}
}
// straight yaml
policy = NewPolicy{}
err = yaml.Unmarshal([]byte(s), &policy)
if err == nil && !reflect.DeepEqual(policy, NewPolicy{}) { // empty string unmarshals w/o error
return policy, nil
}
// invalid policy
return policy, errors.New("policy must be valid JSON or YAML")
}
/*
In order to properly PATCH we need to omit items that aren't specified.
For booleans and integers Golang will omit zero values false and 0 respectively.
This would prevent someone from toggling something to disabled or 0 respectively.
As such we are using pointers instead of primitives for booleans and integers in this struct
*/
type UpdatePolicy struct {
PolicyID string `json:"policyId,omitempty" yaml:"policyId,omitempty"`
PolicyType string `json:"policyType,omitempty" yaml:"policyType,omitempty"`
QueryID string `json:"queryId,omitempty" yaml:"queryId,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Remediation string `json:"remediation,omitempty" yaml:"remediation,omitempty"`
Severity string `json:"severity,omitempty" yaml:"severity,omitempty"`
Limit *int `json:"limit,omitempty" yaml:"limit,omitempty"`
EvalFrequency string `json:"evalFrequency,omitempty" yaml:"evalFrequency,omitempty"`
AlertEnabled *bool `json:"alertEnabled,omitempty" yaml:"alertEnabled,omitempty"`
AlertProfile string `json:"alertProfile,omitempty" yaml:"alertProfile,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
}
type updatePoliciesYAML struct {
Policies []UpdatePolicy `yaml:"policies"`
}
func ParseUpdatePolicy(s string) (UpdatePolicy, error) {
var policy UpdatePolicy
var err error
// valid json
if err = json.Unmarshal([]byte(s), &policy); err == nil {
return policy, err
}
// nested yaml
var policies updatePoliciesYAML
if err = yaml.Unmarshal([]byte(s), &policies); err == nil {
if len(policies.Policies) > 0 {
return policies.Policies[0], err
}
}
// straight yaml
policy = UpdatePolicy{}
err = yaml.Unmarshal([]byte(s), &policy)
if err == nil && !reflect.DeepEqual(policy, UpdatePolicy{}) { // empty string unmarshals w/o error
return policy, nil
}
// invalid policy
return policy, errors.New("policy must be valid JSON or YAML")
}
type ExceptionConfigMap map[string][]PolicyExceptionConfigurationConstraints
type Policy struct {
PolicyID string `json:"policyId" yaml:"policyId"`
PolicyType string `json:"policyType" yaml:"-"`
QueryID string `json:"queryId" yaml:"queryId"`
Title string `json:"title" yaml:"title"`
Enabled bool `json:"enabled" yaml:"enabled"`
Description string `json:"description" yaml:"description"`
Remediation string `json:"remediation" yaml:"remediation"`
Severity string `json:"severity" yaml:"severity"`
Limit int `json:"limit" yaml:"limit"`
EvalFrequency string `json:"evalFrequency" yaml:"evalFrequency"`
AlertEnabled bool `json:"alertEnabled" yaml:"alertEnabled"`
AlertProfile string `json:"alertProfile" yaml:"alertProfile"`
Tags []string `json:"tags" yaml:"tags"`
Owner string `json:"owner" yaml:"-"`
LastUpdateTime string `json:"lastUpdateTime" yaml:"-"`
LastUpdateUser string `json:"lastUpdateUser" yaml:"-"`
ExceptionConfiguration ExceptionConfigMap `json:"exceptionConfiguration" yaml:"-"`
}
type PolicyExceptionConfigurationConstraints struct {
DataType string `json:"dataType" yaml:"dataType"`
FieldKey string `json:"fieldKey" yaml:"fieldKey"`
MultiValue bool `json:"multiValue" yaml:"multiValue"`
}
func (p *Policy) HasTag(t string) bool {
return array.ContainsStr(p.Tags, t)
}
type PolicyResponse struct {
Data Policy `json:"data"`
Message string `json:"message"`
}
type PolicyTagsResponse struct {
Data []string `json:"data"`
Message string `json:"message"`
}
type PoliciesResponse struct {
Data []Policy `json:"data"`
Message string `json:"message"`
}
func (svc *PolicyService) Create(np NewPolicy) (
response PolicyResponse,
err error,
) {
err = svc.client.RequestEncoderDecoder("POST", apiV2Policies, np, &response)
return
}
func (svc *PolicyService) List() (
response PoliciesResponse,
err error,
) {
err = svc.client.RequestDecoder("GET", apiV2Policies, nil, &response)
return
}
func (svc *PolicyService) ListTags() (
response PolicyTagsResponse,
err error,
) {
err = svc.client.RequestDecoder(
"GET",
fmt.Sprintf("%s/Tags", apiV2Policies),
nil,
&response,
)
return
}
func (svc *PolicyService) Get(policyID string) (
response PolicyResponse,
err error,
) {
if policyID == "" {
err = errors.New("policy ID must be provided")
return
}
err = svc.client.RequestDecoder(
"GET",
fmt.Sprintf("%s/%s", apiV2Policies, url.QueryEscape(policyID)),
nil,
&response,
)
return
}
func (svc *PolicyService) Update(up UpdatePolicy) (
response PolicyResponse,
err error,
) {
if up.PolicyID == "" {
err = errors.New("policy ID must be provided")
return
}
var policyID = up.PolicyID
up.PolicyID = "" // omit this for PATCH
err = svc.client.RequestEncoderDecoder(
"PATCH",
fmt.Sprintf("%s/%s", apiV2Policies, url.QueryEscape(policyID)),
up,
&response,
)
return
}
func (svc *PolicyService) Delete(policyID string) (
response PolicyResponse,
err error,
) {
if policyID == "" {
err = errors.New("policy ID must be provided")
return
}
err = svc.client.RequestDecoder(
"DELETE",
fmt.Sprintf("%s/%s", apiV2Policies, url.QueryEscape(policyID)),
nil,
&response,
)
return
}
type BulkUpdatePolicy struct {
PolicyID string `json:"policyId,omitempty" yaml:"policyId,omitempty"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
Severity string `json:"severity,omitempty" yaml:"severity,omitempty"`
}
type BulkUpdatePolicies []BulkUpdatePolicy
// UpdateMany supports updating the state(enabled/disabled) and severity of more than one
// policy using the policy bulk update api
func (svc *PolicyService) UpdateMany(policies BulkUpdatePolicies) (
response BulkPolicyUpdateResponse,
err error,
) {
if len(policies) == 0 {
err = errors.New("a list of policies must be provided")
return
}
err = svc.client.RequestEncoderDecoder(
"PATCH",
apiV2Policies,
policies,
&response,
)
return
}
type BulkPolicyUpdateResponse struct {
Data []BulkPolicyUpdateResponseData `json:"data"`
}
type BulkPolicyUpdateResponseData struct {
EvaluatorId string `json:"evaluatorId,omitempty"`
PolicyId string `json:"policyId"`
PolicyType string `json:"policyType"`
QueryId string `json:"queryId,omitempty"`
QueryText string `json:"queryText,omitempty"`
Title string `json:"title"`
Enabled bool `json:"enabled,omitempty"`
Description string `json:"description"`
Remediation string `json:"remediation"`
Severity string `json:"severity"`
Limit int `json:"limit,omitempty"`
EvalFrequency string `json:"evalFrequency,omitempty"`
AlertEnabled bool `json:"alertEnabled,omitempty"`
AlertProfile string `json:"alertProfile,omitempty"`
Owner string `json:"owner"`
LastUpdateTime time.Time `json:"lastUpdateTime"`
LastUpdateUser string `json:"lastUpdateUser"`
Tags []string `json:"tags"`
InfoLink string `json:"infoLink,omitempty"`
ExceptionConfiguration struct {
ConstraintFields []struct {
FieldKey string `json:"fieldKey"`
DataType string `json:"dataType"`
MultiValue bool `json:"multiValue"`
} `json:"constraintFields"`
} `json:"exceptionConfiguration,omitempty"`
References []string `json:"references,omitempty"`
AdditionalInformation string `json:"additionalInformation,omitempty"`
}