Skip to content

Commit e01afb1

Browse files
committed
Update Terraform provider to v2 + Add acceptance tests
Signed-off-by: Federico Barcelona <[email protected]>
1 parent 847cf02 commit e01afb1

36 files changed

+2241
-1688
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
resource "sysdig_secure_notification_channel" "sample-email" {
3+
name = "Example Channel - Email"
4+
enabled = true
5+
type = "EMAIL"
6+
recipients = "[email protected]"
7+
notify_when_ok = false
8+
notify_when_resolved = false
9+
}
10+
11+
resource "sysdig_secure_notification_channel" "sample-amazon-sns" {
12+
name = "Example Channel - Amazon SNS"
13+
enabled = true
14+
type = "SNS"
15+
topics = "arn:aws:sns:us-east-1:273107874544:my-alerts,arn:aws:sns:us-east-1:273107874544:my-alerts2"
16+
notify_when_ok = false
17+
notify_when_resolved = false
18+
}
19+
20+
resource "sysdig_secure_notification_channel" "sample-victorops" {
21+
name = "Example Channel - VictorOps"
22+
enabled = true
23+
type = "VICTOROPS"
24+
api_key = "1234342-4234243-4234-2"
25+
routing_key = "My team"
26+
notify_when_ok = false
27+
notify_when_resolved = false
28+
}
29+
30+
resource "sysdig_secure_notification_channel" "sample-opsgenie" {
31+
name = "Example Channel - OpsGenie"
32+
enabled = true
33+
type = "OPSGENIE"
34+
api_key = "2349324-342354353-5324-23"
35+
notify_when_ok = false
36+
notify_when_resolved = false
37+
}
38+
39+
resource "sysdig_secure_notification_channel" "sample-webhook" {
40+
name = "Example Channel - Webhook"
41+
enabled = true
42+
type = "WEBHOOK"
43+
url = "localhost:8080"
44+
notify_when_ok = false
45+
notify_when_resolved = false
46+
}
47+
48+
resource "sysdig_secure_notification_channel" "sample-slack" {
49+
name = "Example Channel - Slack"
50+
enabled = true
51+
type = "SLACK"
52+
url = "https://hooks.slack.cwom/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
53+
channel = "#sysdig"
54+
notify_when_ok = true
55+
notify_when_resolved = true
56+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
resource "sysdig_secure_policy" "sample" {
3+
name = "Other example of Policy"
4+
description = "this is other example of policy"
5+
enabled = true
6+
severity = 4
7+
scope = "container.id != \"\""
8+
rule_names = ["Terminal shell in container"]
9+
10+
actions {
11+
container = "stop"
12+
capture {
13+
seconds_before_event = 5
14+
seconds_after_event = 10
15+
}
16+
}
17+
18+
notification_channels = [10000]
19+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
resource "sysdig_secure_rule_container" "sample" {
2+
name = "Other example of Policy"
3+
description = "this is other example of policy"
4+
tags = ["container", "cis"]
5+
6+
matching = true // default
7+
containers = ["foo", "foo:bar"]
8+
}
9+
10+
resource "sysdig_secure_rule_filesystem" "foo" {
11+
name = "Other example of Policy"
12+
description = "this is other example of policy"
13+
tags = ["filesystem", "cis"]
14+
15+
read_only {
16+
matching = true // default
17+
paths = ["/etc"]
18+
}
19+
20+
read_write {
21+
matching = true // default
22+
paths = ["/tmp"]
23+
}
24+
}
25+
26+
resource "sysdig_secure_rule_network" "foo" {
27+
name = "Other example of Policy" // ID
28+
description = "this is other example of policy"
29+
tags = ["network", "cis"]
30+
31+
block_inbound = true
32+
block_outbound = true
33+
34+
tcp {
35+
matching = true // default
36+
ports = [80, 443]
37+
}
38+
39+
udp {
40+
matching = true // default
41+
ports = [80, 443]
42+
}
43+
}
44+
45+
resource "sysdig_secure_rule_process" "foo" {
46+
name = "Other example of Policy" // ID
47+
description = "this is other example of policy"
48+
49+
matching = true // default
50+
processes = ["bash"]
51+
}
52+
53+
resource "sysdig_secure_rule_syscall" "foo" {
54+
name = "Other example of Policy" // ID
55+
description = "this is other example of policy"
56+
57+
matching = true // default
58+
syscalls = ["open", "execve"]
59+
}
60+
61+
resource "sysdig_secure_rule_falco" "foo" {
62+
name = "Other example of Policy" // ID
63+
description = "this is other example of policy"
64+
tags = ["container", "shell", "mitre_execution"]
65+
66+
condition = "spawned_process and container and shell_procs and proc.tty != 0 and container_entrypoint"
67+
output = "A shell was spawned in a container with an attached terminal (user=%user.name %container.info shell=%proc.name parent=%proc.pname cmdline=%proc.cmdline terminal=%proc.tty container_id=%container.id image=%container.image.repository)"
68+
priority = "notice"
69+
source = "syscall" // syscall or k8s_audit
70+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/draios/terraform-provider-sysdig
22

33
go 1.12
44

5-
require github.com/hashicorp/terraform v0.12.8
5+
require github.com/hashicorp/terraform-plugin-sdk v1.0.0

go.sum

Lines changed: 116 additions & 271 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"github.com/draios/terraform-provider-sysdig/sysdig"
5-
"github.com/hashicorp/terraform/plugin"
5+
"github.com/hashicorp/terraform-plugin-sdk/plugin"
66
)
77

88
func main() {

sysdig/provider.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package sysdig
22

33
import (
4-
"github.com/hashicorp/terraform/helper/schema"
5-
"github.com/hashicorp/terraform/terraform"
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
66

77
"github.com/draios/terraform-provider-sysdig/sysdig/secure"
88
)
@@ -13,6 +13,7 @@ func Provider() terraform.ResourceProvider {
1313
"sysdig_secure_api_token": {
1414
Type: schema.TypeString,
1515
Required: true,
16+
Sensitive: true,
1617
DefaultFunc: schema.EnvDefaultFunc("SYSDIG_SECURE_API_TOKEN", nil),
1718
},
1819
"sysdig_secure_url": {
@@ -23,9 +24,13 @@ func Provider() terraform.ResourceProvider {
2324
},
2425
ResourcesMap: map[string]*schema.Resource{
2526
"sysdig_secure_policy": resourceSysdigSecurePolicy(),
26-
"sysdig_secure_user_rules_file": resourceSysdigSecureUserRulesFile(),
2727
"sysdig_secure_notification_channel": resourceSysdigSecureNotificationChannel(),
28-
"sysdig_secure_policies_priority": resourceSysdigSecurePoliciesPriority(),
28+
"sysdig_secure_rule_container": resourceSysdigSecureRuleContainer(),
29+
"sysdig_secure_rule_filesystem": resourceSysdigSecureRuleFilesystem(),
30+
"sysdig_secure_rule_network": resourceSysdigSecureRuleNetwork(),
31+
"sysdig_secure_rule_process": resourceSysdigSecureRuleProcess(),
32+
"sysdig_secure_rule_syscall": resourceSysdigSecureRuleSyscall(),
33+
"sysdig_secure_rule_falco": resourceSysdigSecureRuleFalco(),
2934
},
3035
ConfigureFunc: providerConfigure,
3136
}

sysdig/resource_sysdig_secure_notification_channel.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/hashicorp/terraform/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1111

1212
"github.com/draios/terraform-provider-sysdig/sysdig/secure"
1313
)
@@ -87,6 +87,11 @@ func resourceSysdigSecureNotificationChannel() *schema.Resource {
8787
Type: schema.TypeInt,
8888
Computed: true,
8989
},
90+
"send_test_notification": {
91+
Type: schema.TypeBool,
92+
Optional: true,
93+
Default: false,
94+
},
9095
},
9196
}
9297
}
@@ -136,6 +141,7 @@ func resourceSysdigNotificationChannelRead(d *schema.ResourceData, meta interfac
136141
d.Set("routing_key", nc.Options.RoutingKey)
137142
d.Set("notify_when_ok", nc.Options.NotifyOnOk)
138143
d.Set("notify_when_resolved", nc.Options.NotifyOnResolve)
144+
d.Set("send_test_notification", nc.Options.SendTestNotification)
139145

140146
// When we receive a notification channel of type OpsGenie,
141147
// the API sends us the URL, but we are configuring the API
@@ -200,8 +206,9 @@ func notificationChannelFromResourceData(d *schema.ResourceData) (nc secure.Noti
200206
Enabled: d.Get("enabled").(bool),
201207
Type: channelType,
202208
Options: secure.NotificationChannelOptions{
203-
NotifyOnOk: d.Get("notify_when_ok").(bool),
204-
NotifyOnResolve: d.Get("notify_when_resolved").(bool),
209+
NotifyOnOk: d.Get("notify_when_ok").(bool),
210+
NotifyOnResolve: d.Get("notify_when_resolved").(bool),
211+
SendTestNotification: d.Get("send_test_notification").(bool),
205212
},
206213
}
207214

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package sysdig_test
2+
3+
import (
4+
"fmt"
5+
"github.com/draios/terraform-provider-sysdig/sysdig"
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
9+
"os"
10+
"testing"
11+
)
12+
13+
func TestAccNotificationChannel(t *testing.T) {
14+
//var ncBefore, ncAfter secure.NotificationChannel
15+
16+
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) }
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
21+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
22+
}
23+
},
24+
Providers: map[string]terraform.ResourceProvider{
25+
"sysdig": sysdig.Provider(),
26+
},
27+
Steps: []resource.TestStep{
28+
{
29+
Config: notificationChannelEmailWithName(rText()),
30+
},
31+
{
32+
Config: notificationChannelAmazonSNSWithName(rText()),
33+
},
34+
{
35+
Config: notificationChannelOpsGenieWithName(rText()),
36+
},
37+
{
38+
Config: notificationChannelVictorOpsWithName(rText()),
39+
},
40+
{
41+
Config: notificationChannelWebhookWithName(rText()),
42+
},
43+
{
44+
Config: notificationChannelSlackWithName(rText()),
45+
},
46+
},
47+
})
48+
}
49+
50+
func notificationChannelEmailWithName(name string) string {
51+
return fmt.Sprintf(`
52+
resource "sysdig_secure_notification_channel" "sample_email" {
53+
name = "%s"
54+
enabled = true
55+
type = "EMAIL"
56+
recipients = "[email protected]"
57+
notify_when_ok = false
58+
notify_when_resolved = false
59+
}`, name)
60+
}
61+
62+
func notificationChannelAmazonSNSWithName(name string) string {
63+
return fmt.Sprintf(`
64+
resource "sysdig_secure_notification_channel" "sample-amazon-sns" {
65+
name = "Example Channel %s - Amazon SNS"
66+
enabled = true
67+
type = "SNS"
68+
topics = "arn:aws:sns:us-east-1:273489009834:my-alerts,arn:aws:sns:us-east-1:279948934544:my-alerts2"
69+
notify_when_ok = false
70+
notify_when_resolved = false
71+
}`, name)
72+
}
73+
74+
func notificationChannelVictorOpsWithName(name string) string {
75+
return fmt.Sprintf(`
76+
resource "sysdig_secure_notification_channel" "sample-victorops" {
77+
name = "Example Channel %s - VictorOps"
78+
enabled = true
79+
type = "VICTOROPS"
80+
api_key = "1234342-4234243-4234-2"
81+
routing_key = "My team"
82+
notify_when_ok = false
83+
notify_when_resolved = false
84+
}`, name)
85+
}
86+
87+
func notificationChannelOpsGenieWithName(name string) string {
88+
return fmt.Sprintf(`
89+
resource "sysdig_secure_notification_channel" "sample-opsgenie" {
90+
name = "Example Channel %s - OpsGenie"
91+
enabled = true
92+
type = "OPSGENIE"
93+
api_key = "2349324-342354353-5324-23"
94+
notify_when_ok = false
95+
notify_when_resolved = false
96+
}`, name)
97+
}
98+
99+
func notificationChannelWebhookWithName(name string) string {
100+
return fmt.Sprintf(`
101+
resource "sysdig_secure_notification_channel" "sample-webhook" {
102+
name = "Example Channel %s - Webhook"
103+
enabled = true
104+
type = "WEBHOOK"
105+
url = "localhost:8080"
106+
notify_when_ok = false
107+
notify_when_resolved = false
108+
}`, name)
109+
}
110+
111+
func notificationChannelSlackWithName(name string) string {
112+
return fmt.Sprintf(`
113+
resource "sysdig_secure_notification_channel" "sample-slack" {
114+
name = "Example Channel %s - Slack"
115+
enabled = true
116+
type = "SLACK"
117+
url = "https://hooks.slack.cwom/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
118+
channel = "#sysdig"
119+
notify_when_ok = true
120+
notify_when_resolved = true
121+
}`, name)
122+
}

0 commit comments

Comments
 (0)