Skip to content

Commit 9c6e110

Browse files
authored
feat(vulnerability-policy): add admission control stage (#677)
This adds support for the "admission_control" stage in vulnerability policies. This includes the addition of two new configurable fields within the `configuration` block for the "admission_control" stage: - `failure_action`: Defines the action to take when a policy fails (e.g., "reject", "warn"). - `unknown_image_action`: Defines the action to take when an image is unknown (e.g., "reject", "rejectAndScan", "warn").
1 parent d53a13f commit 9c6e110

File tree

7 files changed

+70
-18
lines changed

7 files changed

+70
-18
lines changed

.envrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export TF_ACC=true
2+
export TF_LOG=DEBUG
3+
dotenv_if_exists .env # You can create a .env file with your env vars for this project. You can also use .secrets if you are using act. See the line below.
4+
dotenv_if_exists .secrets # Used by [act](https://nektosact.com/) to load secrets into the pipelines
5+
strict_env
6+
env_vars_required SYSDIG_SECURE_API_TOKEN SYSDIG_MONITOR_API_TOKEN

.envrc.template

Lines changed: 0 additions & 12 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
*.dll
22
*.exe
33
.DS_Store
4-
.envrc
54
.env
5+
.secrets
66
.direnv/
77
example.tf
88
terraform.tfplan
@@ -54,4 +54,4 @@ oanc
5454
# Local test folder
5555
local-terraform-test/
5656
dist/
57-
.secrets
57+

sysdig/internal/client/v2/vulnerability_policy_model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ type Stage struct {
1919
}
2020

2121
type Configuration struct {
22-
Scope string `json:"scope"`
22+
Scope string `json:"scope"`
23+
Behaviour string `json:"behaviour,omitempty"`
24+
UnknownImageAction string `json:"unknownImageAction,omitempty"`
2325
}

sysdig/resource_sysdig_secure_vulnerability_policy.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func resourceSysdigSecureVulnerabilityPolicy() *schema.Resource {
5656
"stages": {
5757
Type: schema.TypeSet,
5858
Optional: true,
59+
Set: func(a any) int {
60+
in := a.(map[string]any)
61+
return schema.HashString(in["name"])
62+
},
5963
Elem: &schema.Resource{
6064
Schema: map[string]*schema.Schema{
6165
"name": {
@@ -67,6 +71,7 @@ func resourceSysdigSecureVulnerabilityPolicy() *schema.Resource {
6771
"pipeline",
6872
"registry",
6973
"runtime",
74+
"admission_control",
7075
}, false)),
7176
},
7277
"configuration": {
@@ -79,6 +84,18 @@ func resourceSysdigSecureVulnerabilityPolicy() *schema.Resource {
7984
Required: true,
8085
Description: "Scope expression for this stage",
8186
},
87+
"failure_action": {
88+
Type: schema.TypeString,
89+
Optional: true,
90+
Description: "Required for `admission_control` stage only. Policy Failure Action. What should happen if the policy fails (aka: there's a rule vioation)",
91+
ValidateFunc: validation.StringInSlice([]string{"reject", "warn"}, false),
92+
},
93+
"unknown_image_action": {
94+
Type: schema.TypeString,
95+
Optional: true,
96+
Description: "Required for `admission_control` stage only. Unknown Image Action. What should happen if the image is unknown.",
97+
ValidateFunc: validation.StringInSlice([]string{"reject", "rejectAndScan", "warn"}, false),
98+
},
8299
},
83100
},
84101
},
@@ -193,6 +210,14 @@ func vulnerabilityPolicyStagesToMap(policyStages []v2.Stage) []map[string]any {
193210
newConfig := map[string]any{
194211
"scope": stageconfig.Scope,
195212
}
213+
214+
if stageconfig.Behaviour != "" {
215+
newConfig["failure_action"] = stageconfig.Behaviour
216+
}
217+
218+
if stageconfig.UnknownImageAction != "" {
219+
newConfig["unknown_image_action"] = stageconfig.UnknownImageAction
220+
}
196221
configsMap = append(configsMap, newConfig)
197222
}
198223

@@ -297,7 +322,19 @@ func vulnerabilityPolicyConfigsFromSet(set *schema.Set) []v2.Configuration {
297322
for _, raw := range set.List() {
298323
rawMap := raw.(map[string]any)
299324

300-
out = append(out, v2.Configuration{Scope: rawMap["scope"].(string)})
325+
config := v2.Configuration{
326+
Scope: rawMap["scope"].(string),
327+
}
328+
329+
if raw, ok := rawMap["failure_action"]; ok {
330+
config.Behaviour = raw.(string)
331+
}
332+
333+
if raw, ok := rawMap["unknown_image_action"]; ok {
334+
config.UnknownImageAction = raw.(string)
335+
}
336+
337+
out = append(out, config)
301338
}
302339

303340
return out

sysdig/resource_sysdig_secure_vulnerability_policy_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAccVulnerabilityPolicy(t *testing.T) {
3434
Check: resource.ComposeTestCheckFunc(
3535
resource.TestCheckResourceAttr("sysdig_secure_vulnerability_policy.sample", "bundles.#", "2"),
3636
resource.TestCheckResourceAttr("sysdig_secure_vulnerability_policy.sample", "bundles.0", "1"),
37-
resource.TestCheckResourceAttr("sysdig_secure_vulnerability_policy.sample", "stages.#", "3"),
37+
resource.TestCheckResourceAttr("sysdig_secure_vulnerability_policy.sample", "stages.#", "4"),
3838
),
3939
},
4040
{
@@ -90,6 +90,14 @@ resource "sysdig_secure_vulnerability_policy" "sample" {
9090
scope = "agent.tag.cluster = \"my-cluster\""
9191
}
9292
}
93+
stages {
94+
name = "admission_control"
95+
configuration {
96+
scope = "not kubernetes.namespace.name in (\"sysdig\", \"sysdig-agent\")"
97+
failure_action = "reject"
98+
unknown_image_action = "rejectAndScan"
99+
}
100+
}
93101
}
94102
`, suffix, suffix, suffix)
95103
}

website/docs/r/secure_vulnerability_policy.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ resource "sysdig_secure_vulnerability_policy" "vulnerability_policy_example" {
2626
scope = "container.image != ''"
2727
}
2828
}
29+
30+
stages {
31+
name = "admission_control"
32+
configuration {
33+
scope = "kubernetes.cluster.name = 'my-cluster'"
34+
failure_action = "reject"
35+
unknown_image_action = "rejectAndScan"
36+
}
37+
}
2938
}
3039
```
3140

@@ -38,12 +47,14 @@ resource "sysdig_secure_vulnerability_policy" "vulnerability_policy_example" {
3847

3948
### Stages block
4049

41-
* `name` - (Required) Must be one of `pipeline`, `registry`, or `runtime`.
50+
* `name` - (Required) Must be one of `pipeline`, `registry`, `runtime`, or `admission_control`.
4251
* `configuration` - (Optional) Configuration block for the stage. If no configuration is provided, it will apply to any workload in this stage.
4352

4453
### Configuration block
4554

4655
* `scope` - (Required) Scope expression defining the stage applicability.
56+
* `failure_action` - (Optional) Required for `admission_control` stage only. Policy Failure Action. What should happen if the policy fails (aka: there's a rule vioation). Must be one of `reject` or `warn`.
57+
* `unknown_image_action` - (Optional) Required for `admission_control` stage only. Unknown Image Action. What should happen if the image is unknown. Must be one of `reject`, `rejectAndScan`, or `warn`.
4758

4859
## Attributes Reference
4960

0 commit comments

Comments
 (0)