forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot-policies.go
162 lines (125 loc) · 3.01 KB
/
iot-policies.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
package resources
import (
"context"
"github.com/gotidy/ptr"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)
const IoTPolicyResource = "IoTPolicy"
func init() {
registry.Register(®istry.Registration{
Name: IoTPolicyResource,
Scope: nuke.Account,
Lister: &IoTPolicyLister{},
})
}
type IoTPolicyLister struct{}
func (l *IoTPolicyLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := iot.New(opts.Session)
resources := make([]resource.Resource, 0)
params := &iot.ListPoliciesInput{
PageSize: aws.Int64(25),
}
for {
output, err := svc.ListPolicies(params)
if err != nil {
return nil, err
}
for _, policy := range output.Policies {
p := &IoTPolicy{
svc: svc,
name: policy.PolicyName,
}
p, err = listIoTPolicyTargets(p)
if err != nil {
return nil, err
}
p, err = listIoTPolicyDeprecatedVersions(p)
if err != nil {
return nil, err
}
resources = append(resources, p)
}
if output.NextMarker == nil {
break
}
params.Marker = output.NextMarker
}
return resources, nil
}
func listIoTPolicyTargets(f *IoTPolicy) (*IoTPolicy, error) {
var targets []*string
params := &iot.ListTargetsForPolicyInput{
PolicyName: f.name,
PageSize: aws.Int64(25),
}
for {
output, err := f.svc.ListTargetsForPolicy(params)
if err != nil {
return nil, err
}
targets = append(targets, output.Targets...)
if output.NextMarker == nil {
break
}
params.Marker = output.NextMarker
}
f.targets = targets
return f, nil
}
func listIoTPolicyDeprecatedVersions(f *IoTPolicy) (*IoTPolicy, error) {
var deprecatedVersions []*string
params := &iot.ListPolicyVersionsInput{
PolicyName: f.name,
}
output, err := f.svc.ListPolicyVersions(params)
if err != nil {
return nil, err
}
for _, policyVersion := range output.PolicyVersions {
if !ptr.ToBool(policyVersion.IsDefaultVersion) {
deprecatedVersions = append(deprecatedVersions, policyVersion.VersionId)
}
}
f.deprecatedVersions = deprecatedVersions
return f, nil
}
type IoTPolicy struct {
svc *iot.IoT
name *string
targets []*string
deprecatedVersions []*string
}
func (f *IoTPolicy) Remove(_ context.Context) error {
// detach attached targets first
for _, target := range f.targets {
_, err := f.svc.DetachPolicy(&iot.DetachPolicyInput{
PolicyName: f.name,
Target: target,
})
if err != nil {
return err
}
}
// delete deprecated versions
for _, version := range f.deprecatedVersions {
_, err := f.svc.DeletePolicyVersion(&iot.DeletePolicyVersionInput{
PolicyName: f.name,
PolicyVersionId: version,
})
if err != nil {
return err
}
}
_, err := f.svc.DeletePolicy(&iot.DeletePolicyInput{
PolicyName: f.name,
})
return err
}
func (f *IoTPolicy) String() string {
return *f.name
}