Skip to content

Commit

Permalink
feat: Add policy types support (#93)
Browse files Browse the repository at this point in the history
* refactor: Move data marshalling to it's own method

* chore: Increase the Secure policy timeout to 5 minutes

* chore: Increase all timeouts to 5 minutes

* feat: Add policy types support
  • Loading branch information
tembleking authored Mar 29, 2021
1 parent 1435887 commit 7a76793
Show file tree
Hide file tree
Showing 32 changed files with 81 additions and 58 deletions.
2 changes: 1 addition & 1 deletion sysdig/data_source_sysdig_current_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func dataSourceSysdigCurrentUser() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
ReadContext: dataSourceSysdigCurrentUserRead,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/data_source_sysdig_secure_notification_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
)

func dataSourceSysdigSecureNotificationChannel() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
ReadContext: dataSourceSysdigNotificationChannelRead,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/data_source_sysdig_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func dataSourceSysdigUser() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
ReadContext: dataSourceSysdigUserRead,
Expand Down
1 change: 1 addition & 0 deletions sysdig/internal/client/secure/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Policy struct {
Scope string `json:"scope,omitempty"`
Version int `json:"version,omitempty"`
NotificationChannelIds []int `json:"notificationChannelIds"`
Type string `json:"type"`
}

type Action struct {
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_alert_anomaly.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func resourceSysdigMonitorAlertAnomaly() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigAlertAnomalyCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_alert_downtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func resourceSysdigMonitorAlertDowntime() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigAlertDowntimeCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_alert_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func resourceSysdigMonitorAlertEvent() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigAlertEventCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_alert_group_outlier.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func resourceSysdigMonitorAlertGroupOutlier() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigAlertGroupOutlierCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_alert_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func resourceSysdigMonitorAlertMetric() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigAlertMetricCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func resourceSysdigMonitorDashboard() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigDashboardCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func resourceSysdigMonitorNotificationChannelEmail() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorNotificationChannelEmailCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func resourceSysdigMonitorNotificationChannelOpsGenie() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorNotificationChannelOpsGenieCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigMonitorNotificationChannelPagerduty() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorNotificationChannelPagerdutyCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func resourceSysdigMonitorNotificationChannelSlack() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorNotificationChannelSlackCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_notification_channel_sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigMonitorNotificationChannelSNS() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorNotificationChannelSNSCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func resourceSysdigMonitorNotificationChannelVictorOps() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorNotificationChannelVictorOpsCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigMonitorNotificationChannelWebhook() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorNotificationChannelWebhookCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigMonitorTeam() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigMonitorTeamCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func resourceSysdigSecureNotificationChannelEmail() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureNotificationChannelEmailCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func resourceSysdigSecureNotificationChannelOpsGenie() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureNotificationChannelOpsGenieCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureNotificationChannelPagerduty() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureNotificationChannelPagerdutyCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureNotificationChannelSlack() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureNotificationChannelSlackCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_secure_notification_channel_sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func resourceSysdigSecureNotificationChannelSNS() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureNotificationChannelSNSCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureNotificationChannelVictorOps() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureNotificationChannelVictorOpsCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureNotificationChannelWebhook() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureNotificationChannelWebhookCreate,
Expand Down
78 changes: 50 additions & 28 deletions sysdig/resource_sysdig_secure_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var matchActions = map[string]string{
}

func resourceSysdigSecurePolicy() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigPolicyCreate,
Expand All @@ -54,6 +54,12 @@ func resourceSysdigSecurePolicy() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: "falco",
ValidateDiagFunc: validateDiagFunc(validation.StringInSlice([]string{"falco", "list_matching", "k8s_audit"}, false)),
},
"severity": {
Type: schema.TypeInt,
Default: 4,
Expand Down Expand Up @@ -135,18 +141,56 @@ func resourceSysdigPolicyCreate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

d.SetId(strconv.Itoa(policy.ID))
d.Set("version", policy.Version)
policyToResourceData(&policy, d)

return nil
}

func policyToResourceData(policy *secure.Policy, d *schema.ResourceData) {
if policy.ID != 0 {
d.SetId(strconv.Itoa(policy.ID))
}

d.Set("name", policy.Name)
d.Set("description", policy.Description)
d.Set("scope", policy.Scope)
d.Set("enabled", policy.Enabled)
d.Set("version", policy.Version)
d.Set("severity", policy.Severity)

if policy.Type != "" {
d.Set("type", policy.Type)
} else {
d.Set("type", "falco")
}

actions := []map[string]interface{}{{}}
for _, action := range policy.Actions {
if action.Type != "POLICY_ACTION_CAPTURE" {
action := strings.Replace(action.Type, "POLICY_ACTION_", "", 1)
actions[0]["container"] = strings.ToLower(action)
d.Set("actions", actions)
//d.Set("actions.0.container", strings.ToLower(action))
} else {
actions[0]["capture"] = []map[string]interface{}{{
"seconds_after_event": action.AfterEventNs / 1000000000,
"seconds_before_event": action.BeforeEventNs / 1000000000,
}}
d.Set("actions", actions)
}
}

d.Set("notification_channels", policy.NotificationChannelIds)
d.Set("rule_names", policy.RuleNames)
}

func policyFromResourceData(d *schema.ResourceData) secure.Policy {
policy := secure.Policy{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Severity: d.Get("severity").(int),
Enabled: d.Get("enabled").(bool),
Type: d.Get("type").(string),
}

scope := d.Get("scope").(string)
Expand Down Expand Up @@ -211,33 +255,10 @@ func resourceSysdigPolicyRead(ctx context.Context, d *schema.ResourceData, meta

if err != nil {
d.SetId("")
return diag.FromErr(err)
}

d.Set("name", policy.Name)
d.Set("description", policy.Description)
d.Set("scope", policy.Scope)
d.Set("enabled", policy.Enabled)
d.Set("version", policy.Version)
d.Set("severity", policy.Severity)

actions := []map[string]interface{}{{}}
for _, action := range policy.Actions {
if action.Type != "POLICY_ACTION_CAPTURE" {
action := strings.Replace(action.Type, "POLICY_ACTION_", "", 1)
actions[0]["container"] = strings.ToLower(action)
d.Set("actions", actions)
//d.Set("actions.0.container", strings.ToLower(action))
} else {
actions[0]["capture"] = []map[string]interface{}{{
"seconds_after_event": action.AfterEventNs / 1000000000,
"seconds_before_event": action.BeforeEventNs / 1000000000,
}}
d.Set("actions", actions)
}
}

d.Set("notification_channels", policy.NotificationChannelIds)
d.Set("rule_names", policy.RuleNames)
policyToResourceData(&policy, d)

return nil
}
Expand All @@ -254,6 +275,7 @@ func resourceSysdigPolicyDelete(ctx context.Context, d *schema.ResourceData, met
if err != nil {
return diag.FromErr(err)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_secure_rule_filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureRuleFilesystem() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigRuleFilesystemCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_secure_rule_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureRuleNetwork() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigRuleNetworkCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_secure_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureTeam() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigSecureTeamCreate,
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_secure_vulnerability_exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func resourceSysdigSecureVulnerabilityException() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigVulnerabilityExceptionCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func resourceSysdigSecureVulnerabilityExceptionList() *schema.Resource {
timeout := 30 * time.Second
timeout := 5 * time.Minute

return &schema.Resource{
CreateContext: resourceSysdigVulnerabilityExceptionListCreate,
Expand Down
Loading

0 comments on commit 7a76793

Please sign in to comment.