Skip to content

Commit da5b4b7

Browse files
support multi target (#517)
* support multi target * change names * change name field * fix set version constraints * add multi targets to docs * fix name field * fix * fix --------- Co-authored-by: hila1608 <[email protected]>
1 parent 844cffc commit da5b4b7

File tree

4 files changed

+196
-81
lines changed

4 files changed

+196
-81
lines changed

sysdig/common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ const (
2727
SchemaCreatedDateKey = "date_created"
2828
SchemaMinKubeVersionKey = "min_kube_version"
2929
SchemaMaxKubeVersionKey = "max_kube_version"
30+
SchemaMinVersionKey = "min_version"
31+
SchemaMaxVersionKey = "max_version"
3032
SchemaIsCustomKey = "is_custom"
3133
SchemaIsActiveKey = "is_active"
3234
SchemaPlatformKey = "platform"
35+
SchemaTargetKey = "target"
3336
SchemaZonesKey = "zones"
3437
SchemaZonesIDsKey = "zone_ids"
3538
SchemaAllZones = "all_zones"

sysdig/internal/client/v2/model.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -952,21 +952,29 @@ type PosturePolicy struct {
952952
}
953953

954954
type FullPosturePolicy struct {
955-
ID string `json:"id,omitempty"`
956-
Name string `json:"name,omitempty"`
957-
Type string `json:"type,omitempty"`
958-
Description string `json:"description,omitempty"`
959-
Version string `json:"version,omitempty"`
960-
Link string `json:"link,omitempty"`
961-
Authors string `json:"authors,omitempty"`
962-
PublishedData string `json:"publishedDate,omitempty"`
963-
RequirementsGroup []RequirementsGroup `json:"requirementFolders,omitempty"`
964-
MinKubeVersion float64 `json:"minKubeVersion,omitempty"`
965-
MaxKubeVersion float64 `json:"maxKubeVersion,omitempty"`
966-
IsCustom bool `json:"isCustom,omitempty"`
967-
IsActive bool `json:"isActive,omitempty"`
968-
Platform string `json:"platform,omitempty"`
955+
ID string `json:"id,omitempty"`
956+
Name string `json:"name,omitempty"`
957+
Type string `json:"type,omitempty"`
958+
Description string `json:"description,omitempty"`
959+
Version string `json:"version,omitempty"`
960+
Link string `json:"link,omitempty"`
961+
Authors string `json:"authors,omitempty"`
962+
PublishedData string `json:"publishedDate,omitempty"`
963+
RequirementsGroup []RequirementsGroup `json:"requirementFolders,omitempty"`
964+
MinKubeVersion float64 `json:"minKubeVersion,omitempty"`
965+
MaxKubeVersion float64 `json:"maxKubeVersion,omitempty"`
966+
IsCustom bool `json:"isCustom,omitempty"`
967+
IsActive bool `json:"isActive,omitempty"`
968+
Platform string `json:"platform,omitempty"`
969+
VersionConstraints []VersionConstraint `json:"targets,omitempty"`
970+
}
971+
972+
type VersionConstraint struct {
973+
Platform string `json:"platform"`
974+
MinVersion float64 `json:"minVersion,omitempty"`
975+
MaxVersion float64 `json:"maxVersion,omitempty"`
969976
}
977+
970978
type RequirementsGroup struct {
971979
ID string `json:"id,omitempty"`
972980
Name string `json:"name,omitempty"`
@@ -992,17 +1000,18 @@ type Control struct {
9921000
}
9931001

9941002
type CreatePosturePolicy struct {
995-
ID string `json:"id,omitempty"`
996-
Name string `json:"name,omitempty"`
997-
Description string `json:"description,omitempty"`
998-
Type string `json:"type,omitempty"`
999-
Link string `json:"link,omitempty"`
1000-
Version string `json:"version,omitempty"`
1001-
RequirementGroups []CreateRequirementsGroup `json:"groups,omitempty"`
1002-
MinKubeVersion float64 `json:"minKubeVersion,omitempty"`
1003-
MaxKubeVersion float64 `json:"maxKubeVersion,omitempty"`
1004-
IsActive bool `json:"isActive,omitempty"`
1005-
Platform string `json:"platform,omitempty"`
1003+
ID string `json:"id,omitempty"`
1004+
Name string `json:"name,omitempty"`
1005+
Description string `json:"description,omitempty"`
1006+
Type string `json:"type,omitempty"`
1007+
Link string `json:"link,omitempty"`
1008+
Version string `json:"version,omitempty"`
1009+
RequirementGroups []CreateRequirementsGroup `json:"groups,omitempty"`
1010+
MinKubeVersion float64 `json:"minKubeVersion,omitempty"`
1011+
MaxKubeVersion float64 `json:"maxKubeVersion,omitempty"`
1012+
IsActive bool `json:"isActive,omitempty"`
1013+
Platform string `json:"platform,omitempty"`
1014+
VersionConstraints []VersionConstraint `json:"targets,omitempty"`
10061015
}
10071016

10081017
type CreateRequirementsGroup struct {

sysdig/resource_sysdig_secure_posture_policy.go

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func resourceSysdigSecurePosturePolicy() *schema.Resource {
156156
SchemaTypeKey: {
157157
Type: schema.TypeString,
158158
Optional: true,
159+
Default: "Unknown",
159160
},
160161
SchemaLinkKey: {
161162
Type: schema.TypeString,
@@ -178,7 +179,26 @@ func resourceSysdigSecurePosturePolicy() *schema.Resource {
178179
SchemaPlatformKey: {
179180
Type: schema.TypeString,
180181
Optional: true,
181-
Default: "",
182+
},
183+
SchemaTargetKey: {
184+
Type: schema.TypeList,
185+
Optional: true,
186+
Elem: &schema.Resource{
187+
Schema: map[string]*schema.Schema{
188+
SchemaMinVersionKey: {
189+
Type: schema.TypeFloat,
190+
Optional: true,
191+
},
192+
SchemaMaxVersionKey: {
193+
Type: schema.TypeFloat,
194+
Optional: true,
195+
},
196+
SchemaPlatformKey: {
197+
Type: schema.TypeString,
198+
Optional: true,
199+
},
200+
},
201+
},
182202
},
183203
SchemaGroupKey: {
184204
Type: schema.TypeList,
@@ -198,18 +218,21 @@ func resourceSysdigSecurePosturePolicyCreateOrUpdate(ctx context.Context, d *sch
198218

199219
groups := extractGroupsRecursive(d.Get(SchemaGroupKey))
200220
req := &v2.CreatePosturePolicy{
201-
ID: getStringValue(d, SchemaIDKey),
202-
Name: getStringValue(d, SchemaNameKey),
203-
Type: getStringValue(d, SchemaTypeKey),
204-
Description: getStringValue(d, SchemaDescriptionKey),
205-
MinKubeVersion: getFloatValue(d, SchemaMinKubeVersionKey),
206-
MaxKubeVersion: getFloatValue(d, SchemaMaxKubeVersionKey),
207-
IsActive: getBoolValue(d, SchemaIsActiveKey),
208-
Platform: getStringValue(d, SchemaPlatformKey),
209-
Link: getStringValue(d, SchemaLinkKey),
210-
RequirementGroups: groups,
221+
ID: getStringValue(d, SchemaIDKey),
222+
Name: getStringValue(d, SchemaNameKey),
223+
Type: getStringValue(d, SchemaTypeKey),
224+
Description: getStringValue(d, SchemaDescriptionKey),
225+
MinKubeVersion: getFloatValue(d, SchemaMinKubeVersionKey),
226+
MaxKubeVersion: getFloatValue(d, SchemaMaxKubeVersionKey),
227+
IsActive: getBoolValue(d, SchemaIsActiveKey),
228+
Platform: getStringValue(d, SchemaPlatformKey),
229+
VersionConstraints: getVersionConstraintsValue(d, SchemaTargetKey),
230+
Link: getStringValue(d, SchemaLinkKey),
231+
RequirementGroups: groups,
211232
}
233+
212234
new, errStatus, err := client.CreateOrUpdatePosturePolicy(ctx, req)
235+
213236
if err != nil {
214237
return diag.Errorf("Error creating new policy with groups. error status: %s err: %s", errStatus, err)
215238
}
@@ -279,6 +302,11 @@ func resourceSysdigSecurePosturePolicyRead(ctx context.Context, d *schema.Resour
279302
return diag.FromErr(err)
280303
}
281304

305+
err = setVersionConstraints(d, SchemaTargetKey, policy.VersionConstraints)
306+
307+
if err != nil {
308+
return diag.FromErr(err)
309+
}
282310
// Set groups
283311
groupsData, err := setGroups(d, policy.RequirementsGroup)
284312
if err != nil {
@@ -378,6 +406,33 @@ func getStringValue(d *schema.ResourceData, key string) string {
378406
return ""
379407
}
380408

409+
// Helper function to retrieve version constraints value from ResourceData and handle nil case
410+
func getVersionConstraintsValue(d *schema.ResourceData, key string) []v2.VersionConstraint {
411+
pvc := []v2.VersionConstraint{}
412+
versionContraintsMap, ok := d.Get(key).([]interface{})
413+
if !ok {
414+
return nil
415+
}
416+
for _, vc := range versionContraintsMap {
417+
vcMap := vc.(map[string]interface{})
418+
minVersion := 0.0
419+
maxVersion := 0.0
420+
if vcMap["min_version"] != nil {
421+
minVersion = vcMap["min_version"].(float64)
422+
}
423+
if vcMap["max_version"] != nil {
424+
maxVersion = vcMap["max_version"].(float64)
425+
}
426+
versionConstraint := v2.VersionConstraint{
427+
MinVersion: minVersion,
428+
MaxVersion: maxVersion,
429+
Platform: vcMap["platform"].(string),
430+
}
431+
pvc = append(pvc, versionConstraint)
432+
}
433+
return pvc
434+
}
435+
381436
// Helper function to retrieve float64 value from ResourceData and handle nil case
382437
func getFloatValue(d *schema.ResourceData, key string) float64 {
383438
if value, ok := d.GetOk(key); ok {
@@ -442,3 +497,20 @@ func extractGroupsRecursive(data interface{}) []v2.CreateRequirementsGroup {
442497

443498
return groups
444499
}
500+
501+
// Helper function to set version constraints in the Terraform schema
502+
func setVersionConstraints(d *schema.ResourceData, key string, constraints []v2.VersionConstraint) error {
503+
var constraintsData []interface{}
504+
for _, vc := range constraints {
505+
constraint := map[string]interface{}{
506+
"min_version": vc.MinVersion,
507+
"max_version": vc.MaxVersion,
508+
"platform": vc.Platform,
509+
}
510+
constraintsData = append(constraintsData, constraint)
511+
}
512+
if err := d.Set(key, constraintsData); err != nil {
513+
return err
514+
}
515+
return nil
516+
}

website/docs/r/secure_posture_policy.md

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,59 @@ Creates a Sysdig Secure Posture Policy.
1616

1717
```terraform
1818
resource "sysdig_secure_posture_policy" "example" {
19-
name = "demo policy"
20-
type = "kubernetes"
21-
platform = "vanilla"
22-
max_kube_version = 2.0
23-
description = "demo create policy from terraform"
24-
group {
25-
name = "Security"
26-
description = "Security description"
27-
requirement{
28-
name = "Security Enforce access control"
29-
description = "Enforce description"
30-
control {
31-
name = "Create Pods"
32-
enabled = false
33-
}
34-
control {
35-
name = "Kubelet - Disabled AlwaysAllowed Authorization"
36-
}
37-
}
19+
name = "demo policy"
20+
type = "kubernetes"
21+
platform = "Vanilla" // Currently supported, but will be deprecated in the future
22+
min_kube_version = 1.5 // Currently supported, but will be deprecated in the future
23+
max_kube_version = 2.0 // Currently supported, but will be deprecated in the future
24+
description = "demo create policy from terraform"
25+
26+
// New targets field to specify version constraints
27+
target
28+
{
29+
platform = "Vanilla"
30+
minVersion = 1.5
31+
maxVersion = 2.0
32+
}
33+
34+
group {
35+
name = "Security"
36+
description = "Security description"
37+
38+
requirement {
39+
name = "Security Enforce access control"
40+
description = "Enforce description"
41+
42+
control {
43+
name = "Create Pods"
44+
enabled = false
3845
}
39-
group {
40-
name = "Data protection"
41-
description = "Data protection description"
42-
requirement{
43-
name = "Enforce access control"
44-
description = "Enforce description"
45-
control {
46-
name = "Create Pods"
47-
}
48-
control {
49-
name = "Kubelet - Disabled AlwaysAllowed Authorization"
50-
}
51-
}
46+
47+
control {
48+
name = "Kubelet - Disabled AlwaysAllowed Authorization"
49+
}
50+
}
51+
}
52+
53+
group {
54+
name = "Data protection"
55+
description = "Data protection description"
56+
57+
requirement {
58+
name = "Enforce access control"
59+
description = "Enforce description"
60+
61+
control {
62+
name = "Create Pods"
63+
}
64+
65+
control {
66+
name = "Kubelet - Disabled AlwaysAllowed Authorization"
5267
}
68+
}
69+
}
5370
}
71+
5472
```
5573

5674
## Argument Reference
@@ -66,19 +84,32 @@ resource "sysdig_secure_posture_policy" "example" {
6684
- Linux - `linux`
6785
- Docker - `docker`
6886
- OCI - `oci`
69-
* `min_kube_version` - (Optional) Policy minimum Kubernetes version, eg. `1.24`
70-
* `max_kube_version` - (Optional) Policy maximum Kubernetes version, eg. `1.26`
71-
* `is_active` - (Optional) Policy is active flag (active means policy is published, not active means policy is draft). by default is true.
72-
* `platform` - (Optional) Policy platform:
73-
- IKS - `iks`,
74-
- GKE - `gke`,
75-
- Vanilla - `vanilla`,
76-
- AKS - `aks`,
77-
- RKE2 - `rke2`,
78-
- OCP4 - `ocp4`,
79-
- MKE - `mke`,
80-
- EKS - `eks`,
81-
* `groups` - (Optional) Group block defines list of groups attached to Policy
87+
* `platform`: (Optional) Platform for which the policy applies. This field will be deprecated in the future, and you should use the targets field instead to describe policy platform and version. Supported platforms include:
88+
89+
IKS - iks
90+
GKE - gke
91+
Vanilla - vanilla
92+
AKS - aks
93+
RKE2 - rke2
94+
OCP4 - ocp4
95+
MKE - mke
96+
EKS - eks
97+
OCI - oci
98+
99+
* `minKubeVersion`: (Optional) Policy minimum Kubernetes version, e.g., 1.24. This field will be deprecated in the future, and you should use the targets field instead to describe policy platform and version.
100+
101+
* `maxKubeVersion`: (Optional) Policy maximum Kubernetes version, e.g., 1.26. This field will be deprecated in the future, and you should use the targets field instead to describe policy platform and version.
102+
103+
* `target`:(Optional) Specifies target platforms and version ranges. This field should replace Platform, MinKubeVersion, and MaxKubeVersion for more flexible and detailed policy descriptions.
104+
105+
Note: The fields Platform, MinKubeVersion, and MaxKubeVersion will be deprecated in the future. We recommend using the targets field now to describe policy platform and version constraints
106+
107+
* `group` - (Optional) Group block defines list of groups attached to Policy
108+
109+
### Targets block
110+
- `platform` (Optional): Name of the target platform (e.g., IKS, AWS).
111+
- `minVersion` (Optional): Minimum version of the platform.(e.g., 1.24)
112+
- `maxVersion` (Optional): Maximum version of the platform. (e.g., 1.26)
82113

83114
### Groups block
84115
- `name` - (Required) The name of the Posture Policy Group.

0 commit comments

Comments
 (0)