-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
enabler.go
371 lines (297 loc) · 10.7 KB
/
enabler.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package inspector2
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/inspector2"
"github.com/aws/aws-sdk-go-v2/service/inspector2/types"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
func ResourceEnabler() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceEnablerCreate,
ReadWithoutTimeout: resourceEnablerRead,
UpdateWithoutTimeout: resourceEnablerCreate,
DeleteWithoutTimeout: resourceEnablerDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Minute),
Update: schema.DefaultTimeout(15 * time.Minute),
Delete: schema.DefaultTimeout(15 * time.Minute),
},
Schema: map[string]*schema.Schema{
"account_ids": {
Type: schema.TypeSet,
MinItems: 1,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: verify.ValidAccountID,
},
},
"resource_types": {
Type: schema.TypeSet,
MinItems: 1,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: enum.Validate[types.ResourceScanType](),
},
},
},
}
}
const (
ResNameEnabler = "Enabler"
)
func resourceEnablerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Inspector2Client
in := &inspector2.EnableInput{
AccountIds: flex.ExpandStringValueSet(d.Get("account_ids").(*schema.Set)),
ResourceTypes: expandResourceScanTypes(flex.ExpandStringValueSet(d.Get("resource_types").(*schema.Set))),
ClientToken: aws.String(resource.UniqueId()),
}
id := EnablerID(in.AccountIds, flex.ExpandStringValueSet(d.Get("resource_types").(*schema.Set)))
out, err := conn.Enable(ctx, in)
if err != nil {
return create.DiagError(names.Inspector2, create.ErrActionCreating, ResNameEnabler, id, err)
}
if out == nil {
return create.DiagError(names.Inspector2, create.ErrActionCreating, ResNameEnabler, id, errors.New("empty output"))
}
d.SetId(id)
if err := waitEnabled(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return create.DiagError(names.Inspector2, create.ErrActionWaitingForCreation, ResNameEnabler, d.Id(), err)
}
return resourceEnablerRead(ctx, d, meta)
}
func resourceEnablerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Inspector2Client
s, err := FindAccountStatuses(ctx, conn, d.Id())
if err != nil {
return create.DiagError(names.Inspector2, create.ErrActionReading, ResNameEnabler, d.Id(), err)
}
// probably a NotFound is not possible but including for linting/completeness
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Inspector2 Enabler (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
var enabledAccounts []string
for _, a := range s {
if a.Status == string(types.StatusEnabled) {
enabledAccounts = append(enabledAccounts, a.AccountID)
}
}
if err := d.Set("account_ids", flex.FlattenStringValueSet(enabledAccounts)); err != nil {
return create.DiagError(names.Inspector2, create.ErrActionReading, ResNameEnabler, d.Id(), err)
}
return nil
}
func resourceEnablerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Inspector2Client
in := &inspector2.DisableInput{
AccountIds: flex.ExpandStringValueSet(d.Get("account_ids").(*schema.Set)),
ResourceTypes: expandResourceScanTypes(flex.ExpandStringValueSet(d.Get("resource_types").(*schema.Set))),
}
_, err := conn.Disable(ctx, in)
if err != nil {
return create.DiagError(names.Inspector2, create.ErrActionDeleting, ResNameEnabler, d.Id(), err)
}
if err := waitDisabled(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return create.DiagError(names.Inspector2, create.ErrActionWaitingForDeletion, ResNameEnabler, d.Id(), err)
}
return nil
}
const (
StatusDisabledEnabled = string(types.StatusDisabled + types.StatusEnabled)
StatusInProgress = "IN_PROGRESS"
)
func waitEnabled(ctx context.Context, conn *inspector2.Client, id string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: append(enum.Slice(types.StatusEnabling, types.StatusDisabled), StatusDisabledEnabled, StatusInProgress),
Target: enum.Slice(types.StatusEnabled),
Refresh: statusEnable(ctx, conn, id),
Timeout: timeout,
NotFoundChecks: 20,
ContinuousTargetOccurence: 2,
Delay: 10 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
return err
}
func waitDisabled(ctx context.Context, conn *inspector2.Client, id string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: append(enum.Slice(types.StatusDisabling, types.StatusEnabled), StatusDisabledEnabled, StatusInProgress),
Target: enum.Slice(types.StatusDisabled),
Refresh: statusEnable(ctx, conn, id),
Timeout: timeout,
}
_, err := stateConf.WaitForStateContext(ctx)
return err
}
func statusEnable(ctx context.Context, conn *inspector2.Client, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
st, err := FindAccountStatuses(ctx, conn, id)
if errs.Contains(err, string(types.ErrorCodeAlreadyEnabled)) {
return 42, string(types.StatusEnabled), nil
}
if errs.Contains(err, string(types.ErrorCodeEnableInProgress)) {
return nil, StatusInProgress, nil
}
if errs.Contains(err, string(types.ErrorCodeDisableInProgress)) {
return nil, StatusInProgress, nil
}
if errs.Contains(err, string(types.ErrorCodeInternalError)) {
// Can mean DISABLE was called right after ENABLE when status hasn't changed yet.
// Otherwise, it would return a types.ErrorCodeResourceScanNotDisabled
return nil, StatusInProgress, nil
}
if err != nil {
return nil, "", err
}
hasEnabled := false
hasDisabled := false
for _, s := range st {
if s.Status != string(types.StatusEnabled) && s.Status != string(types.StatusDisabled) {
return 42, s.Status, nil
}
if !hasEnabled && s.Status == string(types.StatusEnabled) {
hasEnabled = true
}
if !hasDisabled && s.Status == string(types.StatusDisabled) {
hasDisabled = true
}
}
// can only have status of enabled and/or disabled or would have returned in for
if hasDisabled && hasEnabled {
return 42, StatusDisabledEnabled, nil
}
if hasDisabled {
return 42, string(types.StatusDisabled), nil
}
return 42, string(types.StatusEnabled), nil
}
}
type AccountStatus struct {
AccountID string
Status string
}
func FindAccountStatuses(ctx context.Context, conn *inspector2.Client, id string) ([]AccountStatus, error) {
accountIDs, resourceTypes, err := parseEnablerID(id)
if err != nil {
return nil, fmt.Errorf("parse error (%s): %s", id, err)
}
ec2 := false
ecr := false
for _, v := range resourceTypes {
if v == string(types.ResourceScanTypeEc2) {
ec2 = true
continue
}
if v == string(types.ResourceScanTypeEcr) {
ecr = true
}
}
// there's no describe/list but calling disable without a resource type returns an error
// and information about state
in := &inspector2.DisableInput{}
if len(accountIDs) > 0 {
in.AccountIds = accountIDs
}
out, err := conn.Disable(ctx, in)
if err != nil {
return nil, fmt.Errorf("calling Disable: %s", err)
}
if out == nil {
return nil, fmt.Errorf("empty response calling Disable")
}
var errs *multierror.Error
var s []AccountStatus
for _, a := range out.Accounts {
// in one situation, info is returned in Accounts (rather than FailedAccounts below): when
// everything is disabled
if a.ResourceStatus == nil {
continue
}
s = append(s, AccountStatus{
AccountID: aws.ToString(a.AccountId),
Status: compositeStatus(ec2, ecr, string(a.ResourceStatus.Ec2), string(a.ResourceStatus.Ecr)),
})
}
for _, a := range out.FailedAccounts {
if a.ErrorCode != types.ErrorCodeResourceScanNotDisabled {
// because calling disable without a resource type, information is returned in failed accounts
// with ErrorCode of types.ErrorCodeResourceScanNotDisabled
errs = multierror.Append(errs, fmt.Errorf("(%s) %s: %s (%s)", aws.ToString(a.AccountId), a.ErrorCode, aws.ToString(a.ErrorMessage), a.Status))
continue
}
if a.ResourceStatus == nil {
continue
}
s = append(s, AccountStatus{
AccountID: aws.ToString(a.AccountId),
Status: compositeStatus(ec2, ecr, string(a.ResourceStatus.Ec2), string(a.ResourceStatus.Ecr)),
})
}
return s, errs.ErrorOrNil()
}
// compositeStatus returns the status of ec2 and/or ecr scans depending on which are set by resource.
// If you configure both, compositeStatus returns the most troubling status (e.g., *ing rather than *ed).
func compositeStatus(ec2, ecr bool, ec2Status, ecrStatus string) string {
if ec2 && !ecr {
return ec2Status
}
if !ec2 && ecr {
return ecrStatus
}
// both are configured
if ec2Status == ecrStatus {
return ec2Status
}
// ING suffix beats anything (i.e., ENABLING, DISABLING, SUSPENDING)
if strings.HasSuffix(ec2Status, "ING") {
return ec2Status
}
if strings.HasSuffix(ecrStatus, "ING") {
return ecrStatus
}
// not the same & neither is *ING
if !strings.HasPrefix(ec2Status, "SUS") && !strings.HasPrefix(ecrStatus, "SUS") {
return StatusDisabledEnabled
}
return string(types.StatusSuspended)
}
func expandResourceScanTypes(s []string) []types.ResourceScanType {
vs := make([]types.ResourceScanType, 0, len(s))
for _, v := range s {
if v != "" {
vs = append(vs, types.ResourceScanType(v))
}
}
return vs
}
func EnablerID(accountIDs []string, types []string) string {
return fmt.Sprintf("%s-%s", strings.Join(accountIDs, ":"), strings.Join(types, ":"))
}
func parseEnablerID(id string) ([]string, []string, error) {
parts := strings.Split(id, "-")
if len(parts) != 2 {
return nil, nil, fmt.Errorf("unexpected ID format (%s), expected <account-ids (':' separated)>-<types (':' separated)>", id)
}
return strings.Split(parts[0], ":"), strings.Split(parts[1], ":"), nil
}