Skip to content

Commit a00be3c

Browse files
authored
Merge pull request #328 from aquasecurity/SLK-99905
fix: TP | Fix aquasec_enforcer_group for host_forensics
2 parents 621a892 + 9c71d9a commit a00be3c

8 files changed

Lines changed: 264 additions & 107 deletions

File tree

aquasec/data_enforcer_group.go

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package aquasec
22

33
import (
4+
"context"
45
"log"
56

67
"github.com/aquasecurity/terraform-provider-aquasec/client"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
810
)
911

1012
func dataSourceEnforcerGroup() *schema.Resource {
1113
return &schema.Resource{
1214
Description: "The data source `aquasec_enforcer_groups` provides an Enforcer group template that generates a configuration file, which is subsequently used to generate one or more Enforcers using a Docker command.",
13-
Read: dataEnforcerGroupRead,
15+
ReadContext: dataEnforcerGroupRead,
1416
Schema: map[string]*schema.Schema{
1517
"group_id": {
1618
Type: schema.TypeString,
@@ -80,7 +82,7 @@ func dataSourceEnforcerGroup() *schema.Resource {
8082
Description: "Select Enabled to send activity logs in your containers to the Aqua Server for forensics purposes.",
8183
Optional: true,
8284
},
83-
"host_forensics": {
85+
"host_forensics_collection": {
8486
Type: schema.TypeBool,
8587
Description: "Select Enabled to send activity logs in your host to the Aqua Server for forensics purposes.",
8688
Optional: true,
@@ -386,11 +388,42 @@ func dataSourceEnforcerGroup() *schema.Resource {
386388
Type: schema.TypeString,
387389
},
388390
},
391+
"schedule_scan_settings": {
392+
Type: schema.TypeList,
393+
Description: "Scheduling scan time for which you are creating the Enforcer group.",
394+
Computed: true,
395+
Elem: &schema.Resource{
396+
Schema: map[string]*schema.Schema{
397+
"disabled": {
398+
Type: schema.TypeBool,
399+
Computed: true,
400+
},
401+
"is_custom": {
402+
Type: schema.TypeBool,
403+
Computed: true,
404+
},
405+
"days": {
406+
Type: schema.TypeList,
407+
Computed: true,
408+
Elem: &schema.Schema{
409+
Type: schema.TypeInt,
410+
},
411+
},
412+
"time": {
413+
Type: schema.TypeList,
414+
Computed: true,
415+
Elem: &schema.Schema{
416+
Type: schema.TypeInt,
417+
},
418+
},
419+
},
420+
},
421+
},
389422
},
390423
}
391424
}
392425

393-
func dataEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
426+
func dataEnforcerGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
394427
ac := m.(*client.Client)
395428
name := d.Get("group_id").(string)
396429
group, err := ac.GetEnforcerGroup(name)
@@ -408,7 +441,7 @@ func dataEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
408441
d.Set("behavioral_engine", group.BehavioralEngine)
409442
d.Set("host_behavioral_engine", group.HostBehavioralEngine)
410443
d.Set("forensics", group.ContainerForensicsCollection)
411-
d.Set("host_forensics", group.HostForensicsCollection)
444+
d.Set("host_forensics_collection", group.HostForensicsCollection)
412445
d.Set("host_network_protection", group.HostNetworkProtection)
413446
d.Set("user_access_control", group.UserAccessControl)
414447
d.Set("image_assurance", group.ImageAssurance)
@@ -418,6 +451,7 @@ func dataEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
418451
d.Set("token", group.Token)
419452
d.Set("command", flattenCommands(group.Command))
420453
d.Set("orchestrator", flattenOrchestrators(group.Orchestrator))
454+
d.Set("schedule_scan_settings", flattenScheduleScanSettings(group.ScheduleScanSettings))
421455
d.Set("type", group.Type)
422456
d.Set("host_os", group.HostOs)
423457
d.Set("install_command", group.InstallCommand)
@@ -460,7 +494,7 @@ func dataEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
460494
log.Println("[DEBUG] setting id: ", name)
461495
d.SetId(name)
462496
} else {
463-
return err
497+
return diag.FromErr(err)
464498
}
465499
//gateways := d.Get("gateways").([]interface{})
466500

@@ -482,6 +516,21 @@ func flattenOrchestrator(Orch client.EnforcerOrchestrator) map[string]interface{
482516
}
483517
}
484518

519+
func flattenScheduleScanSetting(setting client.EnforcerScheduleScanSettings) map[string]interface{} {
520+
return map[string]interface{}{
521+
"disabled": setting.Disabled,
522+
"is_custom": setting.IsCustom,
523+
"days": setting.Days,
524+
"time": setting.Time,
525+
}
526+
}
527+
528+
func flattenScheduleScanSettings(setting client.EnforcerScheduleScanSettings) []map[string]interface{} {
529+
set := make([]map[string]interface{}, 1)
530+
set[0] = flattenScheduleScanSetting(setting)
531+
return set
532+
}
533+
485534
func flattenCommands(Command client.EnforcerCommand) []map[string]interface{} {
486535
comm := make([]map[string]interface{}, 1)
487536
comm[0] = flattenCommand(Command)

aquasec/data_enforcer_group_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ func TestAquasecEnforcerGroupDatasource(t *testing.T) {
2020
Gateways: []string{
2121
"3ef9a43f2693_gateway",
2222
},
23-
Type: "agent",
24-
EnforcerImageName: "registry.aquasec.com/enforcer:6.5.22034",
25-
Orchestrator: client.EnforcerOrchestrator{},
23+
Type: "agent",
24+
EnforcerImageName: "registry.aquasec.com/enforcer:6.5.22034",
25+
Orchestrator: client.EnforcerOrchestrator{},
26+
ScheduleScanSettings: client.EnforcerScheduleScanSettings{},
2627
}
2728

2829
resource.Test(t, resource.TestCase{
@@ -53,6 +54,12 @@ func testAccCheckAquasecEnforcerGroupDataSource(enforcerGroup client.EnforcerGro
5354
namespace = "%s"
5455
master = "%v"
5556
}
57+
schedule_scan_settings {
58+
disabled = %v
59+
is_custom = %v
60+
days = [0, 1, 2, 3, 4, 5, 6]
61+
time = [3, 0]
62+
}
5663
}
5764
data "aquasec_enforcer_groups" "testegdata" {
5865
group_id = aquasec_enforcer_groups.testegdata.group_id
@@ -70,7 +77,9 @@ func testAccCheckAquasecEnforcerGroupDataSource(enforcerGroup client.EnforcerGro
7077
enforcerGroup.Orchestrator.Type,
7178
enforcerGroup.Orchestrator.ServiceAccount,
7279
enforcerGroup.Orchestrator.Namespace,
73-
enforcerGroup.Orchestrator.Master)
80+
enforcerGroup.Orchestrator.Master,
81+
enforcerGroup.ScheduleScanSettings.Disabled,
82+
enforcerGroup.ScheduleScanSettings.IsCustom)
7483

7584
}
7685

aquasec/resource_enforcer_group.go

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package aquasec
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"strings"
78
"time"
89

910
"github.com/aquasecurity/terraform-provider-aquasec/client"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214
)
1315

1416
func resourceEnforcerGroup() *schema.Resource {
1517
return &schema.Resource{
16-
Create: resourceEnforcerGroupCreate,
17-
Read: resourceEnforcerGroupRead,
18-
Update: resourceEnforcerGroupUpdate,
19-
Delete: resourceEnforcerGroupDelete,
18+
CreateContext: resourceEnforcerGroupCreate,
19+
ReadContext: resourceEnforcerGroupRead,
20+
UpdateContext: resourceEnforcerGroupUpdate,
21+
DeleteContext: resourceEnforcerGroupDelete,
2022
Importer: &schema.ResourceImporter{
2123
StateContext: schema.ImportStatePassthroughContext,
2224
},
@@ -204,7 +206,7 @@ func resourceEnforcerGroup() *schema.Resource {
204206
Description: "Set `True` to enable these Host Runtime Policy controls: `OS Users and Groups Allowed` and `OS Users and Groups Blocked`",
205207
Optional: true,
206208
},
207-
"host_forensics": {
209+
"host_forensics_collection": {
208210
Type: schema.TypeBool,
209211
Description: "Select Enabled to send activity logs in your host to the Aqua Server for forensics purposes.",
210212
Optional: true,
@@ -372,6 +374,37 @@ func resourceEnforcerGroup() *schema.Resource {
372374
Optional: true,
373375
ValidateFunc: validation.StringInSlice([]string{"docker", "crio", "containerd", "garden"}, false),
374376
},
377+
"schedule_scan_settings": {
378+
Type: schema.TypeList,
379+
Description: "Scheduling scan time for which you are creating the Enforcer group.",
380+
Optional: true,
381+
Elem: &schema.Resource{
382+
Schema: map[string]*schema.Schema{
383+
"disabled": {
384+
Type: schema.TypeBool,
385+
Optional: true,
386+
},
387+
"is_custom": {
388+
Type: schema.TypeBool,
389+
Optional: true,
390+
},
391+
"days": {
392+
Type: schema.TypeList,
393+
Optional: true,
394+
Elem: &schema.Schema{
395+
Type: schema.TypeInt,
396+
},
397+
},
398+
"time": {
399+
Type: schema.TypeList,
400+
Optional: true,
401+
Elem: &schema.Schema{
402+
Type: schema.TypeInt,
403+
},
404+
},
405+
},
406+
},
407+
},
375408
"sync_host_images": {
376409
Type: schema.TypeBool,
377410
Description: "Set `True` to configure Enforcers to discover local host images. Discovered images will be listed under Images > Host Images, as well as under Infrastructure (in the Images tab for applicable hosts).",
@@ -403,28 +436,24 @@ func resourceEnforcerGroup() *schema.Resource {
403436
}
404437
}
405438

406-
func resourceEnforcerGroupCreate(d *schema.ResourceData, m interface{}) error {
439+
func resourceEnforcerGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
407440
ac := m.(*client.Client)
408441

409442
group := expandEnforcerGroup(d)
410443
err := ac.CreateEnforcerGroup(group)
411444

412445
if err != nil {
413-
return err
446+
return diag.FromErr(err)
414447
}
415448

416-
err = resourceEnforcerGroupRead(d, m)
417-
418-
if err == nil {
419-
d.SetId(d.Get("group_id").(string))
420-
} else {
421-
return err
449+
if diags := resourceEnforcerGroupRead(ctx, d, m); diags.HasError() {
450+
return diags
422451
}
423-
452+
d.SetId(d.Get("group_id").(string))
424453
return nil
425454
}
426455

427-
func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
456+
func resourceEnforcerGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
428457
var name string
429458
ac := m.(*client.Client)
430459
groupId, ok := d.GetOk("group_id")
@@ -442,7 +471,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
442471
d.SetId("")
443472
return nil
444473
}
445-
return err
474+
return diag.FromErr(err)
446475
}
447476

448477
d.Set("group_id", r.ID)
@@ -460,7 +489,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
460489
d.Set("behavioral_engine", r.BehavioralEngine)
461490
d.Set("host_behavioral_engine", r.BehavioralEngine)
462491
d.Set("forensics", r.ContainerForensicsCollection)
463-
d.Set("host_forensics", r.HostForensicsCollection)
492+
d.Set("host_forensics_collection", r.HostForensicsCollection)
464493
d.Set("host_network_protection", r.HostNetworkProtection)
465494
d.Set("user_access_control", r.UserAccessControl)
466495
d.Set("image_assurance", r.ImageAssurance)
@@ -470,6 +499,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
470499
d.Set("token", r.Token)
471500
d.Set("command", flattenCommands(r.Command))
472501
d.Set("orchestrator", flattenOrchestrators(r.Orchestrator))
502+
d.Set("schedule_scan_settings", flattenScheduleScanSettings(r.ScheduleScanSettings))
473503
d.Set("host_os", r.HostOs)
474504
d.Set("install_command", r.InstallCommand)
475505
d.Set("hosts_count", r.HostsCount)
@@ -511,8 +541,7 @@ func resourceEnforcerGroupRead(d *schema.ResourceData, m interface{}) error {
511541
return nil
512542
}
513543

514-
func resourceEnforcerGroupUpdate(d *schema.ResourceData, m interface{}) error {
515-
544+
func resourceEnforcerGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
516545
if d.HasChanges("admission_control",
517546
"allow_kube_enforcer_audit",
518547
"allowed_applications",
@@ -537,6 +566,7 @@ func resourceEnforcerGroupUpdate(d *schema.ResourceData, m interface{}) error {
537566
"host_os",
538567
"host_protection",
539568
"host_user_protection",
569+
"host_forensics_collection",
540570
"image_assurance",
541571
"kube_bench_image_name",
542572
"logical_name",
@@ -553,6 +583,7 @@ func resourceEnforcerGroupUpdate(d *schema.ResourceData, m interface{}) error {
553583
"type",
554584
"user_access_control",
555585
"orchestrator",
586+
"schedule_scan_settings",
556587
) {
557588

558589
ac := m.(*client.Client)
@@ -561,23 +592,23 @@ func resourceEnforcerGroupUpdate(d *schema.ResourceData, m interface{}) error {
561592
err := ac.UpdateEnforcerGroup(group)
562593

563594
if err == nil {
564-
_ = d.Set("last_updated", time.Now().Format(time.RFC850))
595+
_ = d.Set("last_update", time.Now().Unix())
565596
} else {
566597
log.Println("[DEBUG] error while updating enforcer r: ", err)
567-
return err
598+
return diag.FromErr(err)
568599
}
569600
}
570601
return nil
571602
}
572603

573-
func resourceEnforcerGroupDelete(d *schema.ResourceData, m interface{}) error {
604+
func resourceEnforcerGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
574605
ac := m.(*client.Client)
575606
name := d.Id()
576607
err := ac.DeleteEnforcerGroup(name)
577608
if err != nil {
578-
return err
609+
return diag.FromErr(err)
579610
}
580-
return err
611+
return diag.FromErr(err)
581612
}
582613

583614
func expandEnforcerGroup(d *schema.ResourceData) client.EnforcerGroup {
@@ -714,7 +745,7 @@ func expandEnforcerGroup(d *schema.ResourceData) client.EnforcerGroup {
714745
enforcerGroup.HostBehavioralEngine = hostBehavioralEngine.(bool)
715746
}
716747

717-
hostForensics, ok := d.GetOk("host_forensics")
748+
hostForensics, ok := d.GetOk("host_forensics_collection")
718749
if ok {
719750
enforcerGroup.HostForensicsCollection = hostForensics.(bool)
720751
}
@@ -827,5 +858,33 @@ func expandEnforcerGroup(d *schema.ResourceData) client.EnforcerGroup {
827858
}
828859
}
829860

861+
if v, ok := d.GetOk("schedule_scan_settings"); ok {
862+
scanSettingsList := v.([]interface{})
863+
if len(scanSettingsList) > 0 && scanSettingsList[0] != nil {
864+
catData := scanSettingsList[0].(map[string]interface{})
865+
866+
sDisabled := catData["disabled"].(bool)
867+
sIsCustom := catData["is_custom"].(bool)
868+
869+
rawDays := catData["days"].([]interface{})
870+
sDays := make([]int, len(rawDays))
871+
for i, v := range rawDays {
872+
sDays[i] = v.(int)
873+
}
874+
875+
rawTime := catData["time"].([]interface{})
876+
sTime := make([]int, len(rawTime))
877+
for i, v := range rawTime {
878+
sTime[i] = v.(int)
879+
}
880+
881+
enforcerGroup.ScheduleScanSettings = client.EnforcerScheduleScanSettings{
882+
Disabled: sDisabled,
883+
IsCustom: sIsCustom,
884+
Days: sDays,
885+
Time: sTime,
886+
}
887+
}
888+
}
830889
return enforcerGroup
831890
}

0 commit comments

Comments
 (0)