-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_ruleset_test.go
160 lines (135 loc) · 4.64 KB
/
resource_ruleset_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/jfcantu/threatstack-golang/threatstack"
)
func init() {
resource.AddTestSweepers("threatstack_ruleset", &resource.Sweeper{
Name: "threatstack_ruleset",
F: sweepRulesets,
})
}
func TestAccThreatstackRuleset_basic(test *testing.T) {
testRulesetName := fmt.Sprintf("tf%s", acctest.RandString(5))
testRulesetDesc := fmt.Sprintf("tf%s", acctest.RandString(5))
testRulesetName2 := fmt.Sprintf("tf%s", acctest.RandString(5))
testRulesetDesc2 := fmt.Sprintf("tf%s", acctest.RandString(5))
testRuleName := fmt.Sprintf("tf%s", acctest.RandString(5))
resource.Test(test, resource.TestCase{
PreCheck: func() { testAccPreCheck(test) },
Providers: testAccProviders,
CheckDestroy: testAccCheckThreatstackRulesetDestroyed,
Steps: []resource.TestStep{
// Step 1: Create ruleset
{
Config: testAccThreatstackRulesetSimpleRuleset(testRulesetName, testRulesetDesc),
Check: resource.ComposeTestCheckFunc(
testAccCheckThreatstackRulesetExists("threatstack_ruleset.test"),
resource.TestCheckResourceAttr("threatstack_ruleset.test", "name", testRulesetName),
resource.TestCheckResourceAttr("threatstack_ruleset.test", "description", testRulesetDesc),
),
},
// Step 2: Add rule
{
Config: testAccThreatstackRulesetSimpleRulesetWithRule(testRulesetName, testRulesetDesc, testRuleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckThreatstackRulesetHasRule("threatstack_ruleset.test", "threatstack_host_rule.test"),
),
},
// Step 3: Rename rule
{
Config: testAccThreatstackRulesetSimpleRulesetWithRule(testRulesetName2, testRulesetDesc2, testRuleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckThreatstackRulesetHasRule("threatstack_ruleset.test", "threatstack_host_rule.test"),
resource.TestCheckResourceAttr("threatstack_ruleset.test", "name", testRulesetName2),
resource.TestCheckResourceAttr("threatstack_ruleset.test", "description", testRulesetDesc2),
),
},
},
})
}
func testAccCheckThreatstackRulesetExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
cli := testAccProvider.Meta().(*threatstack.Client)
res, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
if res.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
if _, err := cli.Rulesets.Get(res.Primary.ID); err != nil {
return fmt.Errorf("Error retrieving rule: %s", err.Error())
}
return nil
}
}
func testAccCheckThreatstackRulesetHasRule(rulesetName string, ruleName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rsResource := s.RootModule().Resources[rulesetName]
ruleResource := s.RootModule().Resources[ruleName]
ruleset, err := testAccProvider.Meta().(*threatstack.Client).Rulesets.Get(rsResource.Primary.ID)
if err != nil {
return err
}
for _, v := range ruleset.RuleIDs {
if v == ruleResource.Primary.ID {
return nil
}
}
return fmt.Errorf("Rule ID %s not found in rules for %s", ruleResource.Primary.ID, rulesetName)
}
}
func testAccCheckThreatstackRulesetDestroyed(s *terraform.State) error {
cli := testAccProvider.Meta().(*threatstack.Client)
for _, res := range s.RootModule().Resources {
if res.Type != "threatstack_ruleset" {
continue
}
_, err := cli.Rulesets.Get(res.Primary.ID)
if err == nil {
return fmt.Errorf("Ruleset ID %s still exists", res.Primary.ID)
}
if !strings.Contains(err.Error(), "404") {
return err
}
}
return nil
}
// Test definitions
// Test 1: Define simple ruleset
func testAccThreatstackRulesetSimpleRuleset(rsName, rsDesc string) string {
return fmt.Sprintf(`
resource "threatstack_ruleset" "test" {
name = "%s"
description = "%s"
}
`, rsName, rsDesc)
}
// Test 2: Add simple rule to ruleset
func testAccThreatstackRulesetSimpleRulesetWithRule(rsName, rsDesc, ruleName string) string {
return fmt.Sprintf(`
resource "threatstack_ruleset" "test" {
name = "%s"
description = "%s"
}
resource "threatstack_host_rule" "test" {
name = "%s"
title = "TEST"
description = "TEST"
ruleset = threatstack_ruleset.test.id
severity = 1
aggregate_fields = ["user"]
filter = "event_type = \"host\""
window = 86400
threshold = 1
suppressions = ["event_type != \"host\""]
enabled = true
}
`, rsName, rsDesc, ruleName)
}