Skip to content

Commit 5ea3196

Browse files
authored
feat(cloud_security_kac_policy): add custom rules to KAC policy resource (#302)
1 parent 2849926 commit 5ea3196

7 files changed

Lines changed: 1545 additions & 340 deletions

File tree

docs/resources/cloud_security_kac_policy.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ resource "crowdstrike_cloud_security_kac_policy" "example" {
6464
privileged_container = "Alert"
6565
sensitive_data_in_environment = "Disabled"
6666
}
67+
custom_rules = [
68+
{
69+
id = "123e4567-e89b-12d3-a456-426614174000"
70+
action = "Alert"
71+
}
72+
]
6773
}
6874
]
6975
default_rule_group = {
@@ -75,6 +81,12 @@ resource "crowdstrike_cloud_security_kac_policy" "example" {
7581
default_rules = {
7682
container_run_as_root = "Prevent"
7783
}
84+
custom_rules = [
85+
{
86+
id = "123e4567-e89b-12d3-a456-426614174000"
87+
action = "Disabled"
88+
}
89+
]
7890
}
7991
}
8092
@@ -108,10 +120,14 @@ output "cloud_security_kac_policy" {
108120

109121
Optional:
110122

111-
- `default_rules` (Attributes) Set the action Falcon KAC should take when assessing default rules. All default rules are set to "Alert" by default. Action must be one of:
112-
- "Disabled": Do nothing
113-
- "Alert": Send an alert
114-
- "Prevent": Prevent the object from running (see [below for nested schema](#nestedatt--default_rule_group--default_rules))
123+
- `custom_rules` (Attributes Set) Defines custom rules for the KAC policy. All custom rules attached to the policy must be included in every `rule_groups` block that defines `custom_rules`. Any `rule_groups` block that omits `custom_rules` will have all custom rules set to `"Disabled"` by default. Action must be one of:
124+
- `"Disabled"`: Do nothing
125+
- `"Alert"`: Send an alert
126+
- `"Prevent"`: Prevent the object from running (see [below for nested schema](#nestedatt--default_rule_group--custom_rules))
127+
- `default_rules` (Attributes) Set the action Falcon KAC should take when assessing default rules. All default rules are set to `"Alert"` by default. Action must be one of:
128+
- `"Disabled"`: Do nothing
129+
- `"Alert"`: Send an alert
130+
- `"Prevent"`: Prevent the object from running (see [below for nested schema](#nestedatt--default_rule_group--default_rules))
115131
- `deny_on_error` (Boolean) Defines how KAC will handle an unrecognized error or timeout when processing an admission request. If set to "false", the pod or workload will be allowed to run.
116132
- `image_assessment` (Attributes) When enabled, KAC applies image assessment policies to pods or workloads that are being created or updated on the Kubernetes cluster. (see [below for nested schema](#nestedatt--default_rule_group--image_assessment))
117133

@@ -123,6 +139,15 @@ Read-Only:
123139
- `name` (String) Name of the default KAC policy rule group.
124140
- `namespaces` (Set of String) The default rule group namespace is `"*"`, which applies to all namespaces, and is not configurable.
125141

142+
<a id="nestedatt--default_rule_group--custom_rules"></a>
143+
### Nested Schema for `default_rule_group.custom_rules`
144+
145+
Required:
146+
147+
- `action` (String) Determines what action Falcon KAC takes when assessing the custom rule.
148+
- `id` (String) Identifier for the KAC custom rule.
149+
150+
126151
<a id="nestedatt--default_rule_group--default_rules"></a>
127152
### Nested Schema for `default_rule_group.default_rules`
128153

@@ -188,10 +213,14 @@ Required:
188213

189214
Optional:
190215

191-
- `default_rules` (Attributes) Set the action Falcon KAC should take when assessing default rules. All default rules are set to "Alert" by default. Action must be one of:
192-
- "Disabled": Do nothing
193-
- "Alert": Send an alert
194-
- "Prevent": Prevent the object from running (see [below for nested schema](#nestedatt--rule_groups--default_rules))
216+
- `custom_rules` (Attributes Set) Defines custom rules for the KAC policy. All custom rules attached to the policy must be included in every `rule_groups` block that defines `custom_rules`. Any `rule_groups` block that omits `custom_rules` will have all custom rules set to `"Disabled"` by default. Action must be one of:
217+
- `"Disabled"`: Do nothing
218+
- `"Alert"`: Send an alert
219+
- `"Prevent"`: Prevent the object from running (see [below for nested schema](#nestedatt--rule_groups--custom_rules))
220+
- `default_rules` (Attributes) Set the action Falcon KAC should take when assessing default rules. All default rules are set to `"Alert"` by default. Action must be one of:
221+
- `"Disabled"`: Do nothing
222+
- `"Alert"`: Send an alert
223+
- `"Prevent"`: Prevent the object from running (see [below for nested schema](#nestedatt--rule_groups--default_rules))
195224
- `deny_on_error` (Boolean) Defines how KAC will handle an unrecognized error or timeout when processing an admission request. If set to "false", the pod or workload will be allowed to run.
196225
- `description` (String) Description of the KAC policy rule group.
197226
- `image_assessment` (Attributes) When enabled, KAC applies image assessment policies to pods or workloads that are being created or updated on the Kubernetes cluster. (see [below for nested schema](#nestedatt--rule_groups--image_assessment))
@@ -202,6 +231,15 @@ Read-Only:
202231

203232
- `id` (String) Identifier for the KAC policy rule group.
204233

234+
<a id="nestedatt--rule_groups--custom_rules"></a>
235+
### Nested Schema for `rule_groups.custom_rules`
236+
237+
Required:
238+
239+
- `action` (String) Determines what action Falcon KAC takes when assessing the custom rule.
240+
- `id` (String) Identifier for the KAC custom rule.
241+
242+
205243
<a id="nestedatt--rule_groups--default_rules"></a>
206244
### Nested Schema for `rule_groups.default_rules`
207245

examples/resources/crowdstrike_cloud_security_kac_policy/resource.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ resource "crowdstrike_cloud_security_kac_policy" "example" {
4040
privileged_container = "Alert"
4141
sensitive_data_in_environment = "Disabled"
4242
}
43+
custom_rules = [
44+
{
45+
id = "123e4567-e89b-12d3-a456-426614174000"
46+
action = "Alert"
47+
}
48+
]
4349
}
4450
]
4551
default_rule_group = {
@@ -51,6 +57,12 @@ resource "crowdstrike_cloud_security_kac_policy" "example" {
5157
default_rules = {
5258
container_run_as_root = "Prevent"
5359
}
60+
custom_rules = [
61+
{
62+
id = "123e4567-e89b-12d3-a456-426614174000"
63+
action = "Disabled"
64+
}
65+
]
5466
}
5567
}
5668

internal/cloud_security/kac_policy_resource.go

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type ruleGroupTFModel struct {
8383
Namespaces types.Set `tfsdk:"namespaces"`
8484
Labels types.Set `tfsdk:"labels"`
8585
DefaultRules types.Object `tfsdk:"default_rules"`
86+
CustomRules types.Set `tfsdk:"custom_rules"`
8687
}
8788

8889
type imageAssessmentTFModel struct {
@@ -96,6 +97,11 @@ type labelTFModel struct {
9697
Operator types.String `tfsdk:"operator"`
9798
}
9899

100+
type customRuleTFModel struct {
101+
ID types.String `tfsdk:"id"`
102+
Action types.String `tfsdk:"action"`
103+
}
104+
99105
func (m *cloudSecurityKacPolicyResourceModel) wrap(
100106
ctx context.Context,
101107
policy *models.ModelsKACPolicy,
@@ -366,6 +372,7 @@ func (r *cloudSecurityKacPolicyResource) Schema(
366372
},
367373
},
368374
"default_rules": defaultRulesSchema,
375+
"custom_rules": customRulesSchema,
369376
},
370377
},
371378
},
@@ -390,6 +397,7 @@ func (r *cloudSecurityKacPolicyResource) Schema(
390397
),
391398
"namespaces": types.SetUnknown(types.StringType),
392399
"labels": types.SetUnknown(types.ObjectType{AttrTypes: labelsAttrMap}),
400+
"custom_rules": types.SetNull(types.ObjectType{AttrTypes: customRulesAttrMap}),
393401
"default_rules": defaultRulesDefaultValue,
394402
},
395403
),
@@ -482,6 +490,7 @@ func (r *cloudSecurityKacPolicyResource) Schema(
482490
},
483491
},
484492
"default_rules": defaultRulesSchema,
493+
"custom_rules": customRulesSchema,
485494
},
486495
},
487496
"last_updated": schema.StringAttribute{
@@ -788,56 +797,66 @@ func (r *cloudSecurityKacPolicyResource) ValidateConfig(
788797
) {
789798
var kacPolicyConfig cloudSecurityKacPolicyResourceModel
790799
resp.Diagnostics.Append(req.Config.Get(ctx, &kacPolicyConfig)...)
800+
if resp.Diagnostics.HasError() {
801+
return
802+
}
803+
804+
resp.Diagnostics.Append(r.validateCustomRulesPropagation(ctx, kacPolicyConfig)...)
791805
}
792806

793807
func (r *cloudSecurityKacPolicyResource) ModifyPlan(
794808
ctx context.Context,
795809
req resource.ModifyPlanRequest,
796810
resp *resource.ModifyPlanResponse,
797811
) {
798-
if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() {
799-
return
800-
}
801-
802-
if req.Plan.Raw.Equal(req.State.Raw) {
812+
if req.Plan.Raw.IsNull() || req.Plan.Raw.Equal(req.State.Raw) {
803813
return
804814
}
805815

806-
var originalPlan, plan, state cloudSecurityKacPolicyResourceModel
807-
808-
resp.Diagnostics.Append(req.Plan.Get(ctx, &originalPlan)...)
816+
var plan, state cloudSecurityKacPolicyResourceModel
817+
var diags diag.Diagnostics
809818
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
810-
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
819+
820+
plan, diags = r.propagateCustomRules(ctx, plan)
821+
resp.Diagnostics.Append(diags...)
811822
if resp.Diagnostics.HasError() {
812823
return
813824
}
814825

815-
// Computed+Optional object attributes (default_rule_group) cause constant plan diffs
816-
// This causes computed values that need to be set on update (LastUpdated) to be marked
817-
// as Unknown, resulting in constant plan diffs.
818-
// When there are no plan changes and LastUpdated is Unknown revert it to prior state value.
819-
if plan.LastUpdated.IsUnknown() {
820-
plan.LastUpdated = state.LastUpdated
821-
822-
resp.Diagnostics.Append(resp.Plan.Set(ctx, &plan)...)
826+
if !req.State.Raw.IsNull() {
827+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
823828
if resp.Diagnostics.HasError() {
824829
return
825830
}
826831

827-
// Revert if LastUpdated is not the only change
828-
if !resp.Plan.Raw.Equal(req.State.Raw) {
829-
plan = originalPlan
830-
resp.Diagnostics.Append(resp.Plan.Set(ctx, originalPlan)...)
832+
// Computed+Optional object attributes (default_rule_group) cause constant plan diffs
833+
// This causes computed values that need to be set on update (LastUpdated) to be marked
834+
// as Unknown, resulting in constant plan diffs.
835+
// When there are no plan changes and LastUpdated is Unknown revert it to prior state value.
836+
if plan.LastUpdated.IsUnknown() {
837+
planLastUpdated := plan.LastUpdated
838+
plan.LastUpdated = state.LastUpdated
839+
840+
resp.Diagnostics.Append(resp.Plan.Set(ctx, &plan)...)
841+
if resp.Diagnostics.HasError() {
842+
return
843+
}
844+
845+
// Revert if LastUpdated is not the only change
846+
if !resp.Plan.Raw.Equal(req.State.Raw) {
847+
plan.LastUpdated = planLastUpdated
848+
resp.Diagnostics.Append(resp.Plan.Set(ctx, plan)...)
849+
}
831850
}
832-
}
833851

834-
modifiedPlan, modifyDiags := r.matchRuleGroupIDsByName(ctx, plan, state)
835-
resp.Diagnostics.Append(modifyDiags...)
836-
if resp.Diagnostics.HasError() {
837-
return
852+
plan, diags = r.matchRuleGroupIDsByName(ctx, plan, state)
853+
resp.Diagnostics.Append(diags...)
854+
if resp.Diagnostics.HasError() {
855+
return
856+
}
838857
}
839858

840-
resp.Diagnostics.Append(resp.Plan.Set(ctx, modifiedPlan)...)
859+
resp.Diagnostics.Append(resp.Plan.Set(ctx, plan)...)
841860
}
842861

843862
func (r *cloudSecurityKacPolicyResource) matchRuleGroupIDsByName(
@@ -1164,6 +1183,15 @@ func (r *cloudSecurityKacPolicyResource) reconcileRuleGroupUpdates(
11641183
apiKacPolicy = updatedApiKacPolicy
11651184
}
11661185

1186+
updatedApiKacPolicy, customRulesDiags := r.reconcileCustomRules(ctx, plan.ID.ValueString(), planTFRuleGroups, apiKacPolicy)
1187+
diags.Append(customRulesDiags...)
1188+
if diags.HasError() {
1189+
return nil, diags
1190+
}
1191+
if updatedApiKacPolicy != nil {
1192+
apiKacPolicy = updatedApiKacPolicy
1193+
}
1194+
11671195
return apiKacPolicy, diags
11681196
}
11691197

0 commit comments

Comments
 (0)