forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_guardduty_threatintelset.go
211 lines (183 loc) · 6.12 KB
/
resource_aws_guardduty_threatintelset.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package aws
import (
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/guardduty"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsGuardDutyThreatintelset() *schema.Resource {
return &schema.Resource{
Create: resourceAwsGuardDutyThreatintelsetCreate,
Read: resourceAwsGuardDutyThreatintelsetRead,
Update: resourceAwsGuardDutyThreatintelsetUpdate,
Delete: resourceAwsGuardDutyThreatintelsetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"detector_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"format": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateGuardDutyThreatIntelSetFormat,
},
"location": {
Type: schema.TypeString,
Required: true,
},
"activate": {
Type: schema.TypeBool,
Required: true,
},
},
}
}
func resourceAwsGuardDutyThreatintelsetCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
detectorID := d.Get("detector_id").(string)
input := &guardduty.CreateThreatIntelSetInput{
DetectorId: aws.String(detectorID),
Name: aws.String(d.Get("name").(string)),
Format: aws.String(d.Get("format").(string)),
Location: aws.String(d.Get("location").(string)),
Activate: aws.Bool(d.Get("activate").(bool)),
}
resp, err := conn.CreateThreatIntelSet(input)
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{guardduty.ThreatIntelSetStatusActivating, guardduty.ThreatIntelSetStatusDeactivating},
Target: []string{guardduty.ThreatIntelSetStatusActive, guardduty.ThreatIntelSetStatusInactive},
Refresh: guardDutyThreatintelsetRefreshStatusFunc(conn, *resp.ThreatIntelSetId, detectorID),
Timeout: 5 * time.Minute,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("[WARN] Error waiting for GuardDuty ThreatIntelSet status to be \"%s\" or \"%s\": %s",
guardduty.ThreatIntelSetStatusActive, guardduty.ThreatIntelSetStatusInactive, err)
}
d.SetId(fmt.Sprintf("%s:%s", detectorID, *resp.ThreatIntelSetId))
return resourceAwsGuardDutyThreatintelsetRead(d, meta)
}
func resourceAwsGuardDutyThreatintelsetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
threatIntelSetId, detectorId, err := decodeGuardDutyThreatintelsetID(d.Id())
if err != nil {
return err
}
input := &guardduty.GetThreatIntelSetInput{
DetectorId: aws.String(detectorId),
ThreatIntelSetId: aws.String(threatIntelSetId),
}
resp, err := conn.GetThreatIntelSet(input)
if err != nil {
if isAWSErr(err, guardduty.ErrCodeBadRequestException, "The request is rejected because the input detectorId is not owned by the current account.") {
log.Printf("[WARN] GuardDuty ThreatIntelSet %q not found, removing from state", threatIntelSetId)
d.SetId("")
return nil
}
return err
}
d.Set("detector_id", detectorId)
d.Set("format", resp.Format)
d.Set("location", resp.Location)
d.Set("name", resp.Name)
d.Set("activate", *resp.Status == guardduty.ThreatIntelSetStatusActive)
return nil
}
func resourceAwsGuardDutyThreatintelsetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
threatIntelSetID, detectorId, err := decodeGuardDutyThreatintelsetID(d.Id())
if err != nil {
return err
}
input := &guardduty.UpdateThreatIntelSetInput{
DetectorId: aws.String(detectorId),
ThreatIntelSetId: aws.String(threatIntelSetID),
}
if d.HasChange("name") {
input.Name = aws.String(d.Get("name").(string))
}
if d.HasChange("location") {
input.Location = aws.String(d.Get("location").(string))
}
if d.HasChange("activate") {
input.Activate = aws.Bool(d.Get("activate").(bool))
}
_, err = conn.UpdateThreatIntelSet(input)
if err != nil {
return err
}
return resourceAwsGuardDutyThreatintelsetRead(d, meta)
}
func resourceAwsGuardDutyThreatintelsetDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).guarddutyconn
threatIntelSetID, detectorId, err := decodeGuardDutyThreatintelsetID(d.Id())
if err != nil {
return err
}
input := &guardduty.DeleteThreatIntelSetInput{
DetectorId: aws.String(detectorId),
ThreatIntelSetId: aws.String(threatIntelSetID),
}
_, err = conn.DeleteThreatIntelSet(input)
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{
guardduty.ThreatIntelSetStatusActive,
guardduty.ThreatIntelSetStatusActivating,
guardduty.ThreatIntelSetStatusInactive,
guardduty.ThreatIntelSetStatusDeactivating,
guardduty.ThreatIntelSetStatusDeletePending,
},
Target: []string{guardduty.ThreatIntelSetStatusDeleted},
Refresh: guardDutyThreatintelsetRefreshStatusFunc(conn, threatIntelSetID, detectorId),
Timeout: 5 * time.Minute,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("[WARN] Error waiting for GuardDuty ThreatIntelSet status to be \"%s\": %s", guardduty.ThreatIntelSetStatusDeleted, err)
}
return nil
}
func guardDutyThreatintelsetRefreshStatusFunc(conn *guardduty.GuardDuty, threatIntelSetID, detectorID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &guardduty.GetThreatIntelSetInput{
DetectorId: aws.String(detectorID),
ThreatIntelSetId: aws.String(threatIntelSetID),
}
resp, err := conn.GetThreatIntelSet(input)
if err != nil {
return nil, "failed", err
}
return resp, *resp.Status, nil
}
}
func decodeGuardDutyThreatintelsetID(id string) (threatIntelSetID, detectorID string, err error) {
parts := strings.Split(id, ":")
if len(parts) != 2 {
err = fmt.Errorf("GuardDuty ThreatIntelSet ID must be of the form <Detector ID>:<ThreatIntelSet ID>, was provided: %s", id)
return
}
threatIntelSetID = parts[1]
detectorID = parts[0]
return
}