/
runtime_policy.go
418 lines (383 loc) · 19.6 KB
/
runtime_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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"log"
"time"
)
type RuntimePolicy struct {
AllowedExecutables AllowedExecutables `json:"allowed_executables"`
AllowedRegistries AllowedRegistries `json:"allowed_registries"`
ApplicationScopes []string `json:"application_scopes"`
AuditBruteForceLogin bool `json:"audit_brute_force_login"`
AuditOnFailure bool `json:"audit_on_failure,omitempty"`
Auditing Auditing `json:"auditing"`
Author string `json:"author"`
BlacklistedOsUsers BlacklistedOsUsers `json:"blacklisted_os_users,omitempty"`
BlockDisallowedImages bool `json:"block_disallowed_images,omitempty"`
BlockFailed bool `json:"block_failed,omitempty"`
BlockFilelessExec bool `json:"block_fileless_exec"`
BlockNonCompliantWorkloads bool `json:"block_non_compliant_workloads"`
BlockNonK8sContainers bool `json:"block_non_k8s_containers"`
BlockNwUnlinkCont bool `json:"block_nw_unlink_cont,omitempty"`
BypassScope BypassScope `json:"bypass_scope"`
ContainerExec ContainerExec `json:"container_exec"`
Created string `json:"created,omitempty"`
Cve string `json:"cve"`
DefaultSecurityProfile string `json:"default_security_profile"`
Description string `json:"description"`
Digest string `json:"digest"`
Domain string `json:"domain,omitempty"`
DomainName string `json:"domain_name,omitempty"`
DriftPrevention DriftPrevention `json:"drift_prevention"`
EnableCryptoMiningDns bool `json:"enable_crypto_mining_dns,omitempty"`
EnableForkGuard bool `json:"enable_fork_guard"`
EnableIPReputation bool `json:"enable_ip_reputation"`
EnablePortScanProtection bool `json:"enable_port_scan_protection"`
Enabled bool `json:"enabled"`
Enforce bool `json:"enforce"`
EnforceAfterDays int `json:"enforce_after_days"`
EnforceSchedulerAddedOn int `json:"enforce_scheduler_added_on,omitempty"`
ExecutableBlacklist ExecutableBlacklist `json:"executable_blacklist"`
FailCicd bool `json:"fail_cicd,omitempty"`
FailedKubernetesChecks FailedKubernetesChecks `json:"failed_kubernetes_checks"`
FileBlock FileBlock `json:"file_block"`
FileIntegrityMonitoring FileIntegrityMonitoring `json:"file_integrity_monitoring"`
ForkGuardProcessLimit int `json:"fork_guard_process_limit"`
HeuristicRefID int `json:"heuristic_ref_id,omitempty"`
ImageID int `json:"image_id,omitempty"`
ImageName string `json:"image_name"`
IsAuditChecked bool `json:"is_audit_checked"`
IsAutoGenerated bool `json:"is_auto_generated"`
Lastupdate int `json:"lastupdate,omitempty"`
LimitContainerPrivileges LimitContainerPrivileges `json:"limit_container_privileges"`
LinuxCapabilities LinuxCapabilities `json:"linux_capabilities"`
MalwareScanOptions MalwareScanOptions `json:"malware_scan_options"`
Name string `json:"name"`
NoNewPrivileges bool `json:"no_new_privileges"`
OnlyRegisteredImages bool `json:"only_registered_images,omitempty"`
PackageBlock PackageBlock `json:"package_block"`
Permission string `json:"permission,omitempty"`
PortBlock PortBlock `json:"port_block"`
//PreventOverrideDefaultConfig PreventOverrideDefaultConfig `json:"prevent_override_default_config,omitempty"`
ReadonlyFiles ReadonlyFiles `json:"readonly_files"`
ReadonlyRegistry ReadonlyRegistry `json:"readonly_registry"`
Registry string `json:"registry"`
RegistryAccessMonitoring RegistryAccessMonitoring `json:"registry_access_monitoring"`
RepoID int `json:"repo_id,omitempty"`
RepoName string `json:"repo_name"`
ResourceName string `json:"resource_name"`
ResourceType string `json:"resource_type"`
RestrictedVolumes RestrictedVolumes `json:"restricted_volumes"`
ReverseShell ReverseShell `json:"reverse_shell"`
RuntimeType string `json:"runtime_type"`
Scope Scope `json:"scope"`
SystemIntegrityProtection SystemIntegrityProtection `json:"system_integrity_protection"`
Tripwire Tripwire `json:"tripwire"`
Type string `json:"type"`
Updated time.Time `json:"updated"`
Version string `json:"version"`
VpatchVersion string `json:"vpatch_version"`
VulnID int `json:"vuln_id,omitempty"`
WhitelistedOsUsers WhitelistedOsUsers `json:"whitelisted_os_users"`
//JSON
//EnableCryptoMiningDNS bool `json:"enable_crypto_mining_dns"`
BlockContainerExec bool `json:"block_container_exec,omitempty"`
IsOOTBPolicy bool `json:"is_ootb_policy,omitempty"`
RuntimeMode int `json:"runtime_mode,omitempty"`
ExcludeApplicationScopes []string `json:"exclude_application_scopes,omitempty"`
}
type AllowedExecutables struct {
AllowExecutables []string `json:"allow_executables,omitempty"`
AllowRootExecutables []string `json:"allow_root_executables,omitempty"`
Enabled bool `json:"enabled"`
SeparateExecutables bool `json:"separate_executables,omitempty"`
}
type AllowedRegistries struct {
AllowedRegistries []string `json:"allowed_registries"`
Enabled bool `json:"enabled"`
}
type ExecutableBlacklist struct {
Enabled bool `json:"enabled"`
Executables []string `json:"executables"`
}
type FailedKubernetesChecks struct {
Enabled bool `json:"enabled"`
FailedChecks []string `json:"failed_checks"`
}
type DriftPrevention struct {
Enabled bool `json:"enabled"`
ExecLockdown bool `json:"exec_lockdown"`
ImageLockdown bool `json:"image_lockdown"`
PreventPrivileged bool `json:"prevent_privileged,omitempty"`
ExecLockdownWhiteList []string `json:"exec_lockdown_white_list"`
}
type RestrictedVolumes struct {
Enabled bool `json:"enabled"`
Volumes []string `json:"volumes"`
}
type BypassScope struct {
Enabled bool `json:"enabled"`
Scope Scope `json:"scope"`
}
type LimitContainerPrivileges struct {
Enabled bool `json:"enabled"`
Privileged bool `json:"privileged,omitempty"`
Netmode bool `json:"netmode,omitempty"`
Pidmode bool `json:"pidmode,omitempty"`
Utsmode bool `json:"utsmode,omitempty"`
Usermode bool `json:"usermode,omitempty"`
Ipcmode bool `json:"ipcmode,omitempty"`
PreventRootUser bool `json:"prevent_root_user,omitempty"`
PreventLowPortBinding bool `json:"prevent_low_port_binding,omitempty"`
BlockAddCapabilities bool `json:"block_add_capabilities,omitempty"`
UseHostUser bool `json:"use_host_user,omitempty"`
}
type PreventOverrideDefaultConfig struct {
Enabled bool `json:"enabled,omitempty"`
EnforceSelinux bool `json:"enforce_selinux,omitempty"`
EnforceSeccomp bool `json:"enforce_seccomp,omitempty"`
EnforceApparmor bool `json:"enforce_apparmor,omitempty"`
}
type Auditing struct {
AuditAllNetwork bool `json:"audit_all_network"`
AuditAllProcesses bool `json:"audit_all_processes"`
AuditFailedLogin bool `json:"audit_failed_login"`
AuditOsUserActivity bool `json:"audit_os_user_activity"`
AuditProcessCmdline bool `json:"audit_process_cmdline"`
AuditSuccessLogin bool `json:"audit_success_login"`
AuditUserAccountManagement bool `json:"audit_user_account_management"`
Enabled bool `json:"enabled"`
}
type BlacklistedOsUsers struct {
Enabled bool `json:"enabled"`
UserBlackList []string `json:"user_black_list"`
GroupBlackList []string `json:"group_black_list"`
}
type WhitelistedOsUsers struct {
Enabled bool `json:"enabled"`
UserWhiteList []string `json:"user_white_list"`
GroupWhiteList []string `json:"group_white_list"`
}
type FileBlock struct {
Enabled bool `json:"enabled"`
FilenameBlockList []string `json:"filename_block_list"`
ExceptionalBlockFiles []string `json:"exceptional_block_files"`
BlockFilesUsers []string `json:"block_files_users"`
BlockFilesProcesses []string `json:"block_files_processes"`
ExceptionalBlockFilesUsers []string `json:"exceptional_block_files_users"`
ExceptionalBlockFilesProcesses []string `json:"exceptional_block_files_processes"`
}
type PackageBlock struct {
Enabled bool `json:"enabled"`
PackagesBlackList []string `json:"packages_black_list"`
ExceptionalBlockPackagesFiles []string `json:"exceptional_block_packages_files"`
BlockPackagesUsers []string `json:"block_packages_users"`
BlockPackagesProcesses []string `json:"block_packages_processes"`
ExceptionalBlockPackagesUsers []string `json:"exceptional_block_packages_users"`
ExceptionalBlockPackagesProcesses []string `json:"exceptional_block_packages_processes"`
}
type LinuxCapabilities struct {
Enabled bool `json:"enabled"`
RemoveLinuxCapabilities []string `json:"remove_linux_capabilities"`
}
type MalwareScanOptions struct {
Action string `json:"action"`
Enabled bool `json:"enabled"`
ExcludeDirectories []string `json:"exclude_directories"`
ExcludeProcesses []string `json:"exclude_processes"`
IncludeDirectories []string `json:"include_directories"`
}
type PortBlock struct {
Enabled bool `json:"enabled"`
BlockInboundPorts []string `json:"block_inbound_ports"`
BlockOutboundPorts []string `json:"block_outbound_ports"`
}
type Tripwire struct {
Enabled bool `json:"enabled"`
UserID string `json:"user_id"`
UserPassword string `json:"user_password"`
ApplyOn []string `json:"apply_on"`
ServerlessApp string `json:"serverless_app"`
}
type FileIntegrityMonitoring struct {
Enabled bool `json:"enabled"`
MonitoredFiles []string `json:"monitored_files"`
ExceptionalMonitoredFiles []string `json:"exceptional_monitored_files"`
MonitoredFilesProcesses []string `json:"monitored_files_processes"`
ExceptionalMonitoredFilesProcesses []string `json:"exceptional_monitored_files_processes"`
MonitoredFilesUsers []string `json:"monitored_files_users"`
ExceptionalMonitoredFilesUsers []string `json:"exceptional_monitored_files_users"`
MonitoredFilesCreate bool `json:"monitored_files_create"`
MonitoredFilesRead bool `json:"monitored_files_read"`
MonitoredFilesModify bool `json:"monitored_files_modify"`
MonitoredFilesDelete bool `json:"monitored_files_delete"`
MonitoredFilesAttributes bool `json:"monitored_files_attributes"`
}
type RegistryAccessMonitoring struct {
Enabled bool `json:"enabled"`
ExceptionalMonitoredRegistryPaths []string `json:"exceptional_monitored_registry_paths"`
ExceptionalMonitoredRegistryProcesses []string `json:"exceptional_monitored_registry_processes"`
ExceptionalMonitoredRegistryUsers []string `json:"exceptional_monitored_registry_users"`
MonitoredRegistryAttributes bool `json:"monitored_registry_attributes"`
MonitoredRegistryCreate bool `json:"monitored_registry_create"`
MonitoredRegistryDelete bool `json:"monitored_registry_delete"`
MonitoredRegistryModify bool `json:"monitored_registry_modify"`
MonitoredRegistryPaths []string `json:"monitored_registry_paths"`
MonitoredRegistryProcesses []string `json:"monitored_registry_processes"`
MonitoredRegistryRead bool `json:"monitored_registry_read"`
MonitoredRegistryUsers []string `json:"monitored_registry_users"`
}
type SystemIntegrityProtection struct {
AuditSystemtimeChange bool `json:"audit_systemtime_change"`
Enabled bool `json:"enabled"`
MonitorAuditLogIntegrity bool `json:"monitor_audit_log_integrity"`
WindowsServicesMonitoring bool `json:"windows_services_monitoring"`
}
type ReadonlyFiles struct {
Enabled bool `json:"enabled"`
ReadonlyFiles []string `json:"readonly_files"`
ExceptionalReadonlyFiles []string `json:"exceptional_readonly_files"`
ReadonlyFilesProcesses []string `json:"readonly_files_processes"`
ExceptionalReadonlyFilesProcesses []string `json:"exceptional_readonly_files_processes"`
ReadonlyFilesUsers []string `json:"readonly_files_users"`
ExceptionalReadonlyFilesUsers []string `json:"exceptional_readonly_files_users"`
}
type ReadonlyRegistry struct {
Enabled bool `json:"enabled"`
ExceptionalReadonlyRegistryPaths []string `json:"exceptional_readonly_registry_paths"`
ExceptionalReadonlyRegistryProcesses []string `json:"exceptional_readonly_registry_processes"`
ExceptionalReadonlyRegistryUsers []string `json:"exceptional_readonly_registry_users"`
ReadonlyRegistryPaths []string `json:"readonly_registry_paths"`
ReadonlyRegistryProcesses []string `json:"readonly_registry_processes"`
ReadonlyRegistryUsers []string `json:"readonly_registry_users"`
}
type ContainerExec struct {
Enabled bool `json:"enabled"`
BlockContainerExec bool `json:"block_container_exec"`
ContainerExecProcWhiteList []string `json:"container_exec_proc_white_list"`
}
type ReverseShell struct {
Enabled bool `json:"enabled"`
BlockReverseShell bool `json:"block_reverse_shell"`
ReverseShellProcWhiteList []string `json:"reverse_shell_proc_white_list"`
ReverseShellIpWhiteList []string `json:"reverse_shell_ip_white_list"`
}
// JSON
// CreateRuntimePolicy creates an Aqua RuntimePolicy
func (cli *Client) CreateRuntimePolicy(runtimePolicy *RuntimePolicy) error {
payload, err := json.Marshal(runtimePolicy)
if err != nil {
return err
}
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v2/runtime_policies")
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
log.Println(string(payload))
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 runtime policy.")
}
if resp.StatusCode != 200 && 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 runtime policy with name %v. Status: %v, Response: %v", runtimePolicy.Name, resp.StatusCode, body)
}
return fmt.Errorf("failed creating runtime policy. status: %v. error message: %v", resp.Status, errorResponse.Message)
}
return nil
}
// GetRuntimePolicy gets an Aqua runtime policy by name
func (cli *Client) GetRuntimePolicy(name string) (*RuntimePolicy, error) {
var err error
var response RuntimePolicy
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v2/runtime_policies/%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 runtime policy 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 runtime policy 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 runtime policy with name %v. Status: %v, Response: %v", name, events.StatusCode, body)
}
return nil, fmt.Errorf("failed getting runtime policy with name %v. Status: %v, error message: %v", name, events.StatusCode, errorReponse.Message)
}
return &response, nil
}
// UpdateRuntimePolicy updates an existing runtime policy policy
func (cli *Client) UpdateRuntimePolicy(runtimePolicy *RuntimePolicy) error {
payload, err := json.Marshal(runtimePolicy)
if err != nil {
return err
}
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v2/runtime_policies/%s", runtimePolicy.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 runtime policy")
}
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 runtime policy. status: %v. error message: %v", resp.Status, errorResponse.Message)
}
return nil
}
// DeleteRuntimePolicy removes a Aqua runtime policy
func (cli *Client) DeleteRuntimePolicy(name string) error {
request := cli.gorequest
apiPath := fmt.Sprintf("/api/v2/runtime_policies/%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 runtime policy")
}
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 runtime policy, status: %v. error message: %v", resp.Status, errorResponse.Message)
}
return nil
}