Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(secure): Add Malware, Drift, ML and AWS ML policy resources #476

Merged
merged 58 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
e6a9540
refactor: Add common schema
alexeyovriakh Jan 26, 2024
ef36744
feat: Add composite policy resource
alexeyovriakh Jan 26, 2024
f2b9d2b
feat: Add composite policy data source
alexeyovriakh Jan 26, 2024
c5a5213
docs: Add docs for composite policy resource and data source
alexeyovriakh Jan 26, 2024
ca74f33
chore: Fix formatting issues
alexeyovriakh Jan 26, 2024
df4ba5f
chore: Update TODO comments in composite policies client
alexeyovriakh Jan 26, 2024
da61a82
feat: Filter policies by type and name
alexeyovriakh Jan 26, 2024
fa03712
chore: Clean up code
alexeyovriakh Jan 26, 2024
5e3195a
chore: Update the docs
alexeyovriakh Jan 26, 2024
1099321
Merge branch 'master' into feat/composite-policy-resource
alexeyovriakh Jan 29, 2024
c3dbf45
chore: Fix formatting issue
alexeyovriakh Jan 29, 2024
11c6597
chore: Fix Sprintf args in test code
alexeyovriakh Jan 29, 2024
2aa47a6
fix: Remove ability enable or disable individual rules
alexeyovriakh Jan 29, 2024
cd04d01
chore: Fix lint issues
alexeyovriakh Jan 30, 2024
92b86f5
refactor: Introduce reducer pattern
alexeyovriakh Jan 30, 2024
c6f512d
refactor: Use a consistent way to get policy ID
alexeyovriakh Jan 30, 2024
c637e46
fix: Resolve copylock error
alexeyovriakh Jan 30, 2024
dcb69dd
refactor: Rename Composite to Malware policy
alexeyovriakh Jan 30, 2024
92de16d
chore: Fix Linter errors
alexeyovriakh Jan 30, 2024
9a53548
Fix: Resolve compilation error
alexeyovriakh Jan 30, 2024
ca7e5b6
feat: Add Drift policy resource and data source
alexeyovriakh Jan 30, 2024
bf6f5d8
test: Update Malware policy tests
alexeyovriakh Jan 30, 2024
4ffa1c3
docs: Update Malware policy docs
alexeyovriakh Jan 30, 2024
e45e331
feat: Add ML policy
alexeyovriakh Jan 31, 2024
be97fa0
fix: Ensure the version number is sent to Policies API in order to up…
alexeyovriakh Jan 31, 2024
b867581
chore: Update Drift policy docs
alexeyovriakh Jan 31, 2024
f0fd76d
fix: Resolve Lin issue
alexeyovriakh Jan 31, 2024
2e4360b
feat: Add AWS ML policy
alexeyovriakh Jan 31, 2024
2a6ffdc
chore: Fix docs typo
alexeyovriakh Jan 31, 2024
72a73fe
fix: Add AWS ML policy to TF provider
alexeyovriakh Jan 31, 2024
13166d5
fix: Resolve "Setting state: Invalid address to set" error
alexeyovriakh Jan 31, 2024
e6b9612
fix: Resolve compilation error
alexeyovriakh Jan 31, 2024
e46a372
fix: Skip version 0 to resolve resource update error
alexeyovriakh Jan 31, 2024
b497128
fix: Resolve tfproviderdocs check error
alexeyovriakh Jan 31, 2024
f16971e
fix: Resolve "Invalid address to set" in drift policy resource
alexeyovriakh Jan 31, 2024
8968587
fix: Fix resource_sysdig_secure_policy_test test failure
alexeyovriakh Jan 31, 2024
ded08a8
fix: Fix TestAccMalwarePolicy test failure
alexeyovriakh Jan 31, 2024
52b8cfd
fix: Fix TestAccDriftPolicy test failure
alexeyovriakh Jan 31, 2024
96f318f
feat: Hide tags
alexeyovriakh Jan 31, 2024
a9cea14
feat: Replace Drift rule Mode attribute with a boolean Enabled attribute
alexeyovriakh Jan 31, 2024
3874b2c
fix: Fix Drift rule reducer
alexeyovriakh Jan 31, 2024
ce79716
fix: Hide Drift rule's match_items attribute
alexeyovriakh Jan 31, 2024
2ebb337
fix: Fix "Invalid resource type" in TestAccMalwarePolicy test
alexeyovriakh Jan 31, 2024
750ef4b
refactor: Remove details block and rename rules to rule
alexeyovriakh Jan 31, 2024
f60ff47
fix: Fix "provide at least one rule name" test error
alexeyovriakh Jan 31, 2024
b61ec4b
docs: Update docs to include the latest TF resource changes
alexeyovriakh Jan 31, 2024
663f4cb
test: Update tests
alexeyovriakh Jan 31, 2024
ac29c96
chore: Update schema formatting
alexeyovriakh Jan 31, 2024
3c4b042
fix: Escape query param
alexeyovriakh Jan 31, 2024
8935262
refactor: Use const values
alexeyovriakh Jan 31, 2024
55f226d
Merge branch 'master' into feat/composite-policy-resource
alexeyovriakh Feb 1, 2024
47b7df2
tests: Generate unique policy name in data source tests
alexeyovriakh Feb 1, 2024
b6f387a
tests: Add more tests to resources
alexeyovriakh Feb 2, 2024
dd2068d
remove unnecessary changes
kmvachhani Feb 15, 2024
9a28212
more small fixes
kmvachhani Feb 15, 2024
fb3efa6
set hash alias field as optional
kmvachhani Feb 18, 2024
0c5776b
remove hash alias references
kmvachhani Feb 21, 2024
094d5c1
Merge branch 'master' into feat/composite-policy-resource
kmvachhani Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions sysdig/data_source_sysdig_secure_aws_ml_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package sysdig

import (
"context"
"time"

v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceSysdigSecureAWSMLPolicy() *schema.Resource {
timeout := 5 * time.Minute

return &schema.Resource{
ReadContext: dataSourceSysdigSecureAWSMLPolicyRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(timeout),
},

Schema: createAWSMLPolicyDataSourceSchema(),
}
}

func dataSourceSysdigSecureAWSMLPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return awsMLPolicyDataSourceRead(ctx, d, meta, "custom policy", isCustomCompositePolicy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the message be more clear here "custom AWS ML policy"? Similarly for other types

}

func createAWSMLPolicyDataSourceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
// IMPORTANT: Type is implicit: It's automatically added upon conversion to JSON
"type": {
Type: schema.TypeString,
Computed: true,
},
"name": NameSchema(),
"description": DescriptionComputedSchema(),
"enabled": EnabledComputedSchema(),
"severity": SeverityComputedSchema(),
"scope": ScopeComputedSchema(),
"version": VersionSchema(),
"notification_channels": NotificationChannelsComputedSchema(),
"runbook": RunbookComputedSchema(),
"rule": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": ReadOnlyIntSchema(),
"name": ReadOnlyStringSchema(),
"description": DescriptionComputedSchema(),
"tags": TagsSchema(),
"version": VersionSchema(),
"anomalous_console_login": MLRuleThresholdAndSeverityComputedSchema(),
},
},
},
}
}

func awsMLPolicyDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}, resourceName string, validationFunc func(v2.PolicyRulesComposite) bool) diag.Diagnostics {
client, err := getSecureCompositePolicyClient(meta.(SysdigClients))
if err != nil {
return diag.FromErr(err)
}

policyName := d.Get("name").(string)
policyType := policyTypeAWSML

policies, _, err := client.FilterCompositePoliciesByNameAndType(ctx, policyType, policyName)
if err != nil {
return diag.FromErr(err)
}

var policy v2.PolicyRulesComposite
for _, existingPolicy := range policies {
tflog.Debug(ctx, "Filtered policies", map[string]interface{}{"name": existingPolicy.Policy.Name})

if existingPolicy.Policy.Name == policyName && existingPolicy.Policy.Type == policyType {
if !validationFunc(existingPolicy) {
return diag.Errorf("policy is not a %s", resourceName)
}
policy = existingPolicy
break
}
}

if policy.Policy == nil {
return diag.Errorf("unable to find policy %s", resourceName)
}

if policy.Policy.ID == 0 {
return diag.Errorf("unable to find %s", resourceName)
}

err = awsMLPolicyToResourceData(&policy, d)
if err != nil {
return diag.FromErr(err)
}

return nil
}
64 changes: 64 additions & 0 deletions sysdig/data_source_sysdig_secure_aws_ml_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//go:build tf_acc_sysdig_secure || tf_acc_policies || tf_acc_onprem_secure

package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/draios/terraform-provider-sysdig/sysdig"
)

func TestAccAWSMLPolicyDataSource(t *testing.T) {
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
}
},
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
},
},
Steps: []resource.TestStep{
{
Config: awsAWSMLPolicyDataSource(rText),
},
},
})
}

func awsAWSMLPolicyDataSource(name string) string {
return fmt.Sprintf(`
resource "sysdig_secure_aws_ml_policy" "policy_1" {
name = "Test AWS ML Policy %s"
description = "Test AWS ML Policy Description %s"
enabled = true
severity = 4

rule {
description = "Test AWS ML Rule Description"

anomalous_console_login {
enabled = true
threshold = 2
severity = 1
}
}

}

data "sysdig_secure_aws_ml_policy" "policy_2" {
name = sysdig_secure_aws_ml_policy.policy_1.name
depends_on = [sysdig_secure_aws_ml_policy.policy_1]
}
`, name, name)
}
117 changes: 117 additions & 0 deletions sysdig/data_source_sysdig_secure_drift_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package sysdig

import (
"context"
"time"

v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceSysdigSecureDriftPolicy() *schema.Resource {
timeout := 5 * time.Minute

return &schema.Resource{
ReadContext: dataSourceSysdigSecureDriftPolicyRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(timeout),
},

Schema: createDriftPolicyDataSourceSchema(),
}
}

func dataSourceSysdigSecureDriftPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return driftPolicyDataSourceRead(ctx, d, meta, "custom policy", isCustomCompositePolicy)
}

func createDriftPolicyDataSourceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
// IMPORTANT: Type is implicit: It's automatically added upon conversion to JSON
"type": {
Type: schema.TypeString,
Computed: true,
},
"name": NameSchema(),
"description": DescriptionComputedSchema(),
"enabled": EnabledComputedSchema(),
"severity": SeverityComputedSchema(),
"scope": ScopeComputedSchema(),
"version": VersionSchema(),
"notification_channels": NotificationChannelsComputedSchema(),
"runbook": RunbookComputedSchema(),
"rule": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": ReadOnlyIntSchema(),
"name": ReadOnlyStringSchema(),
"description": DescriptionComputedSchema(),
"tags": TagsSchema(),
"version": VersionSchema(),
"enabled": BoolComputedSchema(),
"exceptions": ExceptionsComputedSchema(),
"prohibited_binaries": ExceptionsComputedSchema(),
},
},
},
"actions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"prevent_drift": PreventActionComputedSchema(),
"container": ContainerActionComputedSchema(),
"capture": CaptureActionComputedSchema(),
},
},
},
}
}

func driftPolicyDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}, resourceName string, validationFunc func(v2.PolicyRulesComposite) bool) diag.Diagnostics {
client, err := getSecureCompositePolicyClient(meta.(SysdigClients))
if err != nil {
return diag.FromErr(err)
}

policyName := d.Get("name").(string)
policyType := policyTypeDrift

policies, _, err := client.FilterCompositePoliciesByNameAndType(ctx, policyType, policyName)
if err != nil {
return diag.FromErr(err)
}

var policy v2.PolicyRulesComposite
for _, existingPolicy := range policies {
tflog.Debug(ctx, "Filtered policies", map[string]interface{}{"name": existingPolicy.Policy.Name})

if existingPolicy.Policy.Name == policyName && existingPolicy.Policy.Type == policyType {
if !validationFunc(existingPolicy) {
return diag.Errorf("policy is not a %s", resourceName)
}
policy = existingPolicy
break
}
}

if policy.Policy == nil {
return diag.Errorf("unable to find policy %s", resourceName)
}

if policy.Policy.ID == 0 {
return diag.Errorf("unable to find %s", resourceName)
}

err = driftPolicyToResourceData(&policy, d)
if err != nil {
return diag.FromErr(err)
}

return nil
}
70 changes: 70 additions & 0 deletions sysdig/data_source_sysdig_secure_drift_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//go:build tf_acc_sysdig_secure || tf_acc_policies || tf_acc_onprem_secure

package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/draios/terraform-provider-sysdig/sysdig"
)

func TestAccDriftPolicyDataSource(t *testing.T) {
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
}
},
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
},
},
Steps: []resource.TestStep{
{
Config: driftPolicyDataSource(rText),
},
},
})
}

func driftPolicyDataSource(name string) string {
return fmt.Sprintf(`
resource "sysdig_secure_drift_policy" "policy_1" {
name = "Test Drift Policy %s"
description = "Test Drift Policy Description %s"
enabled = true
severity = 4

rule {
description = "Test Drift Rule Description"
enabled = true

exceptions {
items = ["/usr/bin/sh"]
}
prohibited_binaries {
items = ["/usr/bin/curl"]
}
}

actions {
prevent_drift = true
}

}

data "sysdig_secure_drift_policy" "policy_2" {
name = sysdig_secure_drift_policy.policy_1.name
depends_on = [sysdig_secure_drift_policy.policy_1]
}
`, name, name)
}
Loading
Loading