Skip to content

[policies] manage mounted volume toggle in drift policies #602

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

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 9 additions & 8 deletions sysdig/data_source_sysdig_secure_drift_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ func createDriftPolicyDataSourceSchema() map[string]*schema.Schema {
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(),
"id": ReadOnlyIntSchema(),
"name": ReadOnlyStringSchema(),
"description": DescriptionComputedSchema(),
"tags": TagsSchema(),
"version": VersionSchema(),
"enabled": BoolComputedSchema(),
"exceptions": ExceptionsComputedSchema(),
"prohibited_binaries": ExceptionsComputedSchema(),
"mounted_volume_drift_enabled": BoolComputedSchema(),
},
},
},
Expand Down
15 changes: 8 additions & 7 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,14 @@ type RuntimePolicyRuleList struct {
}

type DriftRuleDetails struct {
RuleType ElementType `json:"ruleType"`
Exceptions *RuntimePolicyRuleList `json:"exceptionList"`
ProcessBasedExceptions *RuntimePolicyRuleList `json:"allowlistProcess"`
ProcessBasedDenylist *RuntimePolicyRuleList `json:"denylistProcess"`
ProhibitedBinaries *RuntimePolicyRuleList `json:"prohibitedBinaries"`
Mode string `json:"mode"`
Details `json:"-"`
RuleType ElementType `json:"ruleType"`
Exceptions *RuntimePolicyRuleList `json:"exceptionList"`
ProcessBasedExceptions *RuntimePolicyRuleList `json:"allowlistProcess"`
ProcessBasedDenylist *RuntimePolicyRuleList `json:"denylistProcess"`
ProhibitedBinaries *RuntimePolicyRuleList `json:"prohibitedBinaries"`
Mode string `json:"mode"`
MountedVolumeDriftEnabled bool `json:"mountedVolumeDriftEnabled"`
Details `json:"-"`
}

func (p DriftRuleDetails) GetRuleType() ElementType {
Expand Down
1 change: 1 addition & 0 deletions sysdig/resource_sysdig_secure_drift_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func resourceSysdigSecureDriftPolicy() *schema.Resource {
"prohibited_binaries": ExceptionsSchema(),
"process_based_exceptions": ExceptionsSchema(),
"process_based_prohibited_binaries": ExceptionsSchema(),
"mounted_volume_drift_enabled": BoolSchema(),
},
},
},
Expand Down
31 changes: 31 additions & 0 deletions sysdig/resource_sysdig_secure_drift_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func TestAccDriftPolicy(t *testing.T) {
{
Config: driftPolicyWithoutExceptions(rText()),
},
{
Config: driftPolicyWithMountedVolumeDriftEnabled(rText()),
},
},
})
}
Expand Down Expand Up @@ -212,3 +215,31 @@ resource "sysdig_secure_drift_policy" "sample" {

`, secureNotificationChannelEmailWithName(name), name)
}

func driftPolicyWithMountedVolumeDriftEnabled(name string) string {
return fmt.Sprintf(`
resource "sysdig_secure_drift_policy" "sample" {

name = "Test Drift Policy %s"
description = "Test Drift Policy Description"
enabled = true
severity = 4

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

exceptions {
items = ["/usr/bin/sh"]
}
prohibited_binaries {
items = ["/usr/bin/curl"]
}
process_based_exceptions {
items = ["/usr/bin/curl"]
}
}
}
`, name)
}
28 changes: 16 additions & 12 deletions sysdig/tfresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,13 @@ func setTFResourcePolicyRulesDrift(d *schema.ResourceData, policy v2.PolicyRules
enabled := (mode != "disabled")

ruleMap := map[string]interface{}{
"id": rule.Id,
"name": rule.Name,
"description": rule.Description,
"version": rule.Version,
"tags": rule.Tags,
"enabled": enabled,
"id": rule.Id,
"name": rule.Name,
"description": rule.Description,
"version": rule.Version,
"tags": rule.Tags,
"enabled": enabled,
"mounted_volume_drift_enabled": driftDetails.MountedVolumeDriftEnabled,
}

if exceptionsBlock != nil {
Expand Down Expand Up @@ -495,18 +496,21 @@ func setPolicyRulesDrift(policy *v2.PolicyRulesComposite, d *schema.ResourceData
mode = "disabled"
}

mountedVolumeDriftEnabled := d.Get("rule.0.mounted_volume_drift_enabled").(bool)

rule := &v2.RuntimePolicyRule{
// TODO: Do not hardcode the indexes
Name: d.Get("rule.0.name").(string),
Description: d.Get("rule.0.description").(string),
Tags: tags,
Details: v2.DriftRuleDetails{
RuleType: v2.ElementType(driftElementType), // TODO: Use const
Mode: mode,
Exceptions: &exceptions,
ProhibitedBinaries: &prohibitedBinaries,
ProcessBasedExceptions: &processBasedExceptions,
ProcessBasedDenylist: &processBasedProhibitedBinaries,
RuleType: v2.ElementType(driftElementType), // TODO: Use const
Mode: mode,
Exceptions: &exceptions,
ProhibitedBinaries: &prohibitedBinaries,
ProcessBasedExceptions: &processBasedExceptions,
ProcessBasedDenylist: &processBasedProhibitedBinaries,
MountedVolumeDriftEnabled: mountedVolumeDriftEnabled,
},
}

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/secure_drift_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ The rule block is required and supports:
* `items` - (Required) Specify comma separated list of processes, e.g. `/usr/bin/rm, /usr/bin/curl`.
* `process_based_prohibited_binaries` - (Optional) List of processes that will be prohibited to execute a drifted file
* `items` - (Required) Specify comma separated list of processes, e.g. `/usr/bin/rm, /usr/bin/curl`.
* `mounted_volume_drift_enabled` - (Optional) Treat all binaries from mounted volumes as drifted. Default value is false/disabled.



Loading