-
Notifications
You must be signed in to change notification settings - Fork 177
/
sns.go
181 lines (155 loc) · 4.91 KB
/
sns.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
package sdk
import (
"context"
"encoding/gob"
"github.com/BishopFox/cloudfox/internal"
"github.com/aws/aws-sdk-go-v2/service/sns"
snsTypes "github.com/aws/aws-sdk-go-v2/service/sns/types"
"github.com/patrickmn/go-cache"
)
type AWSSNSClientInterface interface {
ListTopics(ctx context.Context, params *sns.ListTopicsInput, optFns ...func(*sns.Options)) (*sns.ListTopicsOutput, error)
ListSubscriptions(ctx context.Context, params *sns.ListSubscriptionsInput, optFns ...func(*sns.Options)) (*sns.ListSubscriptionsOutput, error)
ListSubscriptionsByTopic(ctx context.Context, params *sns.ListSubscriptionsByTopicInput, optFns ...func(*sns.Options)) (*sns.ListSubscriptionsByTopicOutput, error)
GetTopicAttributes(ctx context.Context, params *sns.GetTopicAttributesInput, optFns ...func(*sns.Options)) (*sns.GetTopicAttributesOutput, error)
}
func init() {
gob.Register([]string{})
gob.Register(snsTypes.Topic{})
gob.Register(snsTypes.Subscription{})
}
func CachedSNSListTopics(SNSClient AWSSNSClientInterface, accountID string, region string) ([]string, error) {
var PaginationControl *string
var topics []string
cacheKey := "sns-ListTopics-" + accountID + "-" + region
cached, found := internal.Cache.Get(cacheKey)
if found {
sharedLogger.Debug("Using cached SNS topics data")
return cached.([]string), nil
}
for {
ListTopics, err := SNSClient.ListTopics(
context.TODO(),
&sns.ListTopicsInput{
NextToken: PaginationControl,
},
func(o *sns.Options) {
o.Region = region
},
)
if err != nil {
sharedLogger.Error(err.Error())
break
}
for _, topic := range ListTopics.Topics {
topics = append(topics, *topic.TopicArn)
}
// Pagination control.
if ListTopics.NextToken != nil {
PaginationControl = ListTopics.NextToken
} else {
PaginationControl = nil
break
}
}
internal.Cache.Set(cacheKey, topics, cache.DefaultExpiration)
return topics, nil
}
func CachedSNSListSubscriptions(SNSClient AWSSNSClientInterface, accountID string, region string) ([]string, error) {
var PaginationControl *string
var subscriptions []string
cacheKey := "sns-ListSubscriptions-" + accountID + "-" + region
cached, found := internal.Cache.Get(cacheKey)
if found {
sharedLogger.Debug("Using cached SNS subscriptions data")
return cached.([]string), nil
}
for {
ListSubscriptions, err := SNSClient.ListSubscriptions(
context.TODO(),
&sns.ListSubscriptionsInput{
NextToken: PaginationControl,
},
func(o *sns.Options) {
o.Region = region
},
)
if err != nil {
sharedLogger.Error(err.Error())
break
}
for _, subscription := range ListSubscriptions.Subscriptions {
subscriptions = append(subscriptions, *subscription.SubscriptionArn)
}
// Pagination control.
if ListSubscriptions.NextToken != nil {
PaginationControl = ListSubscriptions.NextToken
} else {
PaginationControl = nil
break
}
}
internal.Cache.Set(cacheKey, subscriptions, cache.DefaultExpiration)
return subscriptions, nil
}
func CachedSNSListSubscriptionsByTopic(SNSClient AWSSNSClientInterface, accountID string, region string, topicArn string) ([]string, error) {
var PaginationControl *string
var subscriptions []string
cacheKey := "sns-ListSubscriptionsByTopic-" + accountID + "-" + region + "-" + topicArn
cached, found := internal.Cache.Get(cacheKey)
if found {
sharedLogger.Debug("Using cached SNS subscriptions data")
return cached.([]string), nil
}
for {
ListSubscriptionsByTopic, err := SNSClient.ListSubscriptionsByTopic(
context.TODO(),
&sns.ListSubscriptionsByTopicInput{
NextToken: PaginationControl,
TopicArn: &topicArn,
},
func(o *sns.Options) {
o.Region = region
},
)
if err != nil {
sharedLogger.Error(err.Error())
break
}
for _, subscription := range ListSubscriptionsByTopic.Subscriptions {
subscriptions = append(subscriptions, *subscription.SubscriptionArn)
}
// Pagination control.
if ListSubscriptionsByTopic.NextToken != nil {
PaginationControl = ListSubscriptionsByTopic.NextToken
} else {
PaginationControl = nil
break
}
}
internal.Cache.Set(cacheKey, subscriptions, cache.DefaultExpiration)
return subscriptions, nil
}
func CachedSNSGetTopicAttributes(SNSClient AWSSNSClientInterface, accountID string, region string, topicArn string) (map[string]string, error) {
cacheKey := "sns-GetTopicAttributes-" + accountID + "-" + region + "-" + topicArn
cached, found := internal.Cache.Get(cacheKey)
if found {
sharedLogger.Debug("Using cached SNS topic attributes data")
return cached.(map[string]string), nil
}
GetTopicAttributes, err := SNSClient.GetTopicAttributes(
context.TODO(),
&sns.GetTopicAttributesInput{
TopicArn: &topicArn,
},
func(o *sns.Options) {
o.Region = region
},
)
if err != nil {
sharedLogger.Error(err.Error())
return nil, err
}
internal.Cache.Set(cacheKey, GetTopicAttributes.Attributes, cache.DefaultExpiration)
return GetTopicAttributes.Attributes, nil
}