-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_create_ses.py
executable file
·241 lines (201 loc) · 8.05 KB
/
run_create_ses.py
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
#!/usr/bin/env python3.11
import json
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
aws_cli = AWSCli('us-east-1')
ses = env['ses']
def create_email_identity():
cmd = ['ses', 'list-identities']
identities_results = dict(aws_cli.run(cmd))
for identity in ses['IDENTITIES']:
if identity['EMAIL'] in identities_results['Identities']:
continue
cmd = ['ses', 'verify-email-identity',
'--email-address', identity['EMAIL']]
aws_cli.run(cmd)
sns_topic_name = identity['SNS_TOPICS_NAME']
print_message('check topic exists: %s' % sns_topic_name)
region, topic_name = sns_topic_name.split('/')
topic_arn = AWSCli(region).get_topic_arn(topic_name)
if not topic_arn:
print('sns topic: "%s" is not exists in %s' % (sns_topic_name, region))
raise Exception()
cmd = ['ses', 'set-identity-notification-topic',
'--identity', identity['EMAIL'],
'--notification-type', 'Bounce',
'--sns-topic', topic_arn]
aws_cli.run(cmd)
cmd = ['ses', 'set-identity-headers-in-notifications-enabled',
'--identity', identity['EMAIL'],
'--notification-type', 'Bounce',
'--enabled']
aws_cli.run(cmd)
cmd = ['ses', 'set-identity-notification-topic',
'--identity', identity['EMAIL'],
'--notification-type', 'Complaint',
'--sns-topic', topic_arn]
aws_cli.run(cmd)
cmd = ['ses', 'set-identity-headers-in-notifications-enabled',
'--identity', identity['EMAIL'],
'--notification-type', 'Complaint',
'--enabled']
aws_cli.run(cmd)
def create_config_set():
cmd = ['ses', 'list-configuration-sets']
exist_config_sets = aws_cli.run(cmd)['ConfigurationSets']
exist_config_names = [exist_config_set['Name'] for exist_config_set in exist_config_sets]
for config_set in ses['CONFIGURATION_SETS']:
config = dict()
config['NAME'] = config_set['NAME']
ll = list()
for event_destination in config_set['EVENT_DESTINATIONS']:
if event_destination['TYPE'] == 'cloudwatch':
ll.append({
"Name": event_destination['NAME'],
"Enabled": event_destination['ENABLED'],
"MatchingEventTypes": event_destination['MATCHING_TYPE'],
"CloudWatchDestination": {
"DimensionConfigurations": [
{
"DimensionName": event_destination['DIMENSION_CONFIG_NAME'],
"DimensionValueSource": event_destination['DIMENSION_CONFIG_VALUE_SOURCE'],
"DefaultDimensionValue": event_destination['DIMENSION_CONFIG_VALUE']
}
]
}
})
elif event_destination['TYPE'] == 'sns':
sns_topic_name = event_destination['SNS_TOPICS_NAME']
region, topic_name = sns_topic_name.split('/')
topic_arn = AWSCli(region).get_topic_arn(topic_name)
ll.append({
"Name": event_destination['NAME'],
"Enabled": event_destination['ENABLED'],
"MatchingEventTypes": event_destination['MATCHING_TYPE'],
"SNSDestination": {
"TopicARN": topic_arn
}
})
config['EVENT_DESTINATIONS'] = ll
if config_set['NAME'] not in exist_config_names:
cmd = ['ses', 'create-configuration-set',
'--configuration-set', 'Name=%s' % config['NAME']]
aws_cli.run(cmd)
for event_destination in config['EVENT_DESTINATIONS']:
cmd = ['ses', 'create-configuration-set-event-destination',
'--configuration-set-name', config_set['NAME'],
'--event-destination', json.dumps(event_destination)
]
aws_cli.run(cmd)
def verify_ses_domain():
for ii in ses['DOMAIN_IDENTITIES']:
cmd = ['ses', 'get-identity-mail-from-domain-attributes',
'--identities', ii['DOMAIN']]
rr = dict(aws_cli.run(cmd))
if rr['MailFromDomainAttributes']:
continue
cmd = ['ses', 'verify-domain-identity',
'--domain', ii['DOMAIN']]
aws_cli.run(cmd)
def create_rule_set():
for ii in ses['DOMAIN_IDENTITIES']:
cmd = ['ses', 'describe-receipt-rule-set',
'--rule-set-name', ii['RULE_SET_NAME']]
rr = aws_cli.run(cmd, ignore_error=True)
if rr:
continue
cmd = ['ses', 'create-receipt-rule-set',
'--rule-set-name', ii['RULE_SET_NAME']]
aws_cli.run(cmd)
cmd = ['ses', 'set-active-receipt-rule-set',
'--rule-set-name', ii['RULE_SET_NAME']]
aws_cli.run(cmd)
for rule in ii['RULES']:
cmd = ['sns', 'list-topics']
rr = dict(aws_cli.run(cmd))
sns_topic = __find_sns_topic(rr, rule)
bucket_name = __find_s3_bucket(rule)
rule_action = {
'Name': rule['NAME'],
'Enabled': True,
'Actions': [{
'S3Action': {
'BucketName': bucket_name,
'ObjectKeyPrefix': env['common']['PHASE'],
'TopicArn': sns_topic
}
}]
}
cmd = ['ses', 'create-receipt-rule',
'--rule-set-name', ii['RULE_SET_NAME'],
'--rule', json.dumps(rule_action)]
aws_cli.run(cmd)
def __find_s3_bucket(rule):
bucket_name = None
cmd = ['s3api', 'list-buckets']
rr = dict(aws_cli.run(cmd))
for bb in rr['Buckets']:
if rule['BUCKET_NAME'] == bb['Name']:
bucket_name = rule['BUCKET_NAME']
break
if not bucket_name:
cmd = ['s3api', 'create-bucket',
'--bucket', rule['BUCKET_NAME']]
bucket_name = rule['BUCKET_NAME']
aws_cli.run(cmd)
s3_policy = {
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ses.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::%s/*" % bucket_name
}
]
}
cmd = ['s3api', 'put-bucket-policy',
'--bucket', bucket_name,
'--policy', json.dumps(s3_policy)]
aws_cli.run(cmd)
s3_lifecycle_rule = {
"Rules": [
{
"ID": "%s-lifecycle" % bucket_name,
"Prefix": env['common']['PHASE'],
"Status": "Enabled",
"Expiration": {
"Days": rule['BUCKET_LIFECYCLE_EXPIRATION']
}
}
]
}
cmd = ['s3api', 'put-bucket-lifecycle-configuration',
'--bucket', bucket_name,
'--lifecycle-configuration', json.dumps(s3_lifecycle_rule)]
aws_cli.run(cmd)
return bucket_name
def __find_sns_topic(rr, rule):
sns_topic = None
for tt in rr['Topics']:
if rule['SNS_NAME'] in tt['TopicArn']:
sns_topic = tt['TopicArn']
break
return sns_topic
if __name__ == "__main__":
from run_common import parse_args
parse_args()
################################################################################
#
# start
#
################################################################################
print_session('create ses')
################################################################################
create_email_identity()
create_config_set()
verify_ses_domain()
create_rule_set()