-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmigratepolicies.py
396 lines (351 loc) · 13.8 KB
/
migratepolicies.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import argparse
import configparser
import os
import sys
from typing import List
import library.localstore as store
import library.status.alertstatus as askeys
import library.migrationlogger as m_logger
import library.clients.alertsclient as ac
import library.utils as utils
import fetchchannels
"""Policy Migration Routines
Migrate alert policy and assigned notification channels to targetAccount.
Alert Policy and Notification Channels are created only if not present in
the targetAccount.
"""
logger = m_logger.get_logger(os.path.basename(__file__))
fetch_channels = True
def create_argument_parser():
parser = argparse.ArgumentParser(
description='Migrate Alert Policies and channels'
)
return configure_parser(parser)
def configure_parser(
parser: argparse.ArgumentParser,
is_standalone: bool = True
):
parser.add_argument(
'--fromFile',
'--policy_file',
nargs=1,
type=str,
required=False,
help='Path to file with alert policy names',
dest='policy_file'
)
parser.add_argument(
'--fromFileEntities',
'--entity_file',
nargs=1,
type=str,
required=False,
help='Path to file with entity IDs',
dest='entity_file'
)
parser.add_argument(
'--sourceAccount',
'--source_account_id',
nargs=1,
type=int,
required=is_standalone,
help='Source accountId',
dest='source_account_id'
)
parser.add_argument(
'--sourceRegion',
'--source_region',
nargs=1,
type=str,
required=False,
help='Source Account Region us(default) or eu',
dest='source_region'
)
parser.add_argument(
'--sourceApiKey',
'--source_api_key',
nargs=1,
type=str,
required=False,
help='Source account API Key or set environment variable ENV_SOURCE_API_KEY',
dest='source_api_key'
)
parser.add_argument(
'--targetAccount',
'--target_account_id',
nargs=1,
type=int,
required=is_standalone,
help='Target accountId',
dest='target_account_id'
)
parser.add_argument(
'--targetRegion',
'--target_region',
nargs=1,
type=str,
required=is_standalone,
help='Target Account Region us(default) or eu',
dest='target_region'
)
parser.add_argument(
'--targetApiKey',
'--target_api_key',
nargs=1,
type=str,
required=False,
help='Target API Key, or set environment variable ENV_TARGET_API_KEY',
dest='target_api_key'
)
parser.add_argument(
'--useLocal',
'--use_local',
required=False,
action='store_true',
help='By default channels are fetched.Pass this to use channels pre-fetched by fetchchannels',
dest='use_local'
)
return parser
# prints args and also sets the fetch_latest flag
def print_args(args, src_api_key, src_region, tgt_api_key, tgt_region):
global fetch_channels
if (args.policy_file):
logger.info("Using fromFile : " + args.policy_file[0])
if (args.entity_file):
logger.info("Using fromFileEntities : " + args.entity_file[0])
logger.info("Using sourceAccount : " + str(args.source_account_id[0]))
logger.info("sourceRegion : " + src_region)
logger.info("Using sourceApiKey : " + len(src_api_key[:-4])*"*"+src_api_key[-4:])
logger.info("Using targetAccount : " + str(args.target_account_id[0]))
logger.info("targetRegion : " + tgt_region)
logger.info("Using targetApiKey : " + len(tgt_api_key[:-4]) * "*" + tgt_api_key[-4:])
if args.use_local:
fetch_channels = False
logger.info("Using useLocal : " + str(args.use_local))
logger.info("Switched fetch_channels to :" + str(fetch_channels))
else:
logger.info("Default fetch_channels :" + str(fetch_channels))
def type_name_key(channel):
return channel['type'] + '-' + channel['name']
def get_channels_by_type_name(api_key, region):
result = ac.get_channels(api_key, region)
all_target_channels = result[askeys.CHANNELS]
target_channels_by_type_name = {}
for target_channel in all_target_channels:
target_channels_by_type_name[type_name_key(target_channel)] = target_channel
return target_channels_by_type_name
def update_notification_channels(tgt_api_key, tgt_region, source_policy, target_policy, loaded_src_channels,
tgt_channels_by_type_name, all_alert_status):
logger.info('Updating notification channels for ' + target_policy['name'])
src_policy_id = str(source_policy['id'])
if not loaded_src_channels['channels_by_policy_id']:
logger.info('No notification channel subscriptions')
return
src_channel_ids = []
if src_policy_id in loaded_src_channels['channels_by_policy_id']:
src_channel_ids = loaded_src_channels['channels_by_policy_id'][src_policy_id]
src_channels = []
for source_channel_id in src_channel_ids:
if str(source_channel_id) in loaded_src_channels['channels_by_id']:
src_channels.append(loaded_src_channels['channels_by_id'][str(source_channel_id)])
target_channel_ids = []
for src_channel in src_channels:
src_channel_type_name = type_name_key(src_channel)
if src_channel_type_name not in tgt_channels_by_type_name:
logger.info(src_channel)
result = ac.create_channel(tgt_api_key, src_channel, tgt_region)
if result['status'] == 201:
tgt_type_name = type_name_key(result['channel'])
logger.info('Created channel : ' + tgt_type_name)
tgt_channels_by_type_name[tgt_type_name] = result['channel']
else:
logger.error(result) # getting errors for channel of type user
else:
logger.info('Channel already existed : ' + src_channel_type_name)
if src_channel_type_name in tgt_channels_by_type_name:
target_channel_ids.append(tgt_channels_by_type_name[src_channel_type_name]['id'])
update_alert_status(all_alert_status, target_policy['name'], src_channel_type_name)
result = ac.put_channel_ids(tgt_api_key, target_policy['id'], target_channel_ids, tgt_region)
update_put_status(all_alert_status, result, target_policy)
def update_put_status(all_alert_status, result, target_policy):
all_alert_status[target_policy['name']][askeys.STATUS] = result['status']
if 'channel_ids' in result:
all_alert_status[target_policy['name']][askeys.PUT_CHANNELS] = result['channel_ids']
def update_alert_status(all_alert_status, policy_name, src_channel_type_name):
if askeys.CHANNELS in all_alert_status[policy_name]:
all_alert_status[policy_name][askeys.CHANNELS] = all_alert_status[policy_name][askeys.CHANNELS] \
+ ";" + src_channel_type_name
else:
all_alert_status[policy_name][askeys.CHANNELS] = src_channel_type_name
def migrate_alert_policies(policy_names: List[str],
src_account: int, src_api_key: str, src_region: str,
tgt_account: int, tgt_api_key: str, tgt_region: str):
logger.info('Alert migration started.')
policies_by_source_id = {}
all_alert_status = {}
if fetch_channels:
logger.info('Fetching latest channel info and policy assignment. This may take a while.....')
loaded_src_channels = fetchchannels.get_channels_by_id_policy(src_api_key, src_region)
else:
logger.info('Loading pre-fetched channel and policy assignment information')
loaded_src_channels = store.load_alert_channels(src_account)
tgt_channels_by_type_name = get_channels_by_type_name(tgt_api_key, tgt_region)
logger.info('Migrating the following policies:')
logger.info('%s' % policy_names)
for policy_name in policy_names:
all_alert_status[policy_name] = {}
result = ac.get_policy(src_api_key, policy_name, src_region)
if not result['policyFound']:
logger.error("Skipping as policy not found in source account " + policy_name)
all_alert_status[policy_name][askeys.ERROR] = "Policy Not found in source account"
continue
src_policy = result['policy']
result = ac.get_policy(tgt_api_key, policy_name, tgt_region)
if result['status'] in [200, 304] and result['policyFound']:
logger.info('Policy exists : ' + policy_name)
all_alert_status[policy_name] = {askeys.POLICY_EXISTED: True}
tgt_policy = result['policy']
else:
logger.info('Creating : ' + policy_name)
all_alert_status[policy_name] = {askeys.POLICY_EXISTED: False}
result = ac.create_alert_policy(tgt_api_key, src_policy, tgt_region)
update_create_status(all_alert_status, policy_name, result)
tgt_policy = result['policy']
src_policy['targetPolicyId'] = tgt_policy['id']
policies_by_source_id.setdefault(src_policy['id'], src_policy)
# update_notification_channels(tgt_api_key, tgt_region, src_policy, tgt_policy, loaded_src_channels,
# tgt_channels_by_type_name, all_alert_status)
logger.info('Alert migration complete.')
return_dict = dict()
return_dict['all_alert_status'] = all_alert_status
return_dict['policies_by_source_id'] = policies_by_source_id
return return_dict
def update_create_status(all_alert_status, policy_name, result):
all_alert_status[policy_name][askeys.STATUS] = result['status']
all_alert_status[policy_name][askeys.POLICY_CREATED] = result['entityCreated']
if 'error' in result:
all_alert_status[policy_name][askeys.ERROR] = result['error']
def migrate(
policy_file_path: str,
entity_file_path: str,
source_acct_id: int,
source_region: str,
target_acct_id: int,
target_region: str,
source_api_key: str,
target_api_key: str,
use_local: bool = False
):
policy_names = utils.load_alert_policy_names(
policy_file_path,
entity_file_path,
source_acct_id,
source_region,
source_api_key,
use_local
)
return_dict = migrate_alert_policies(
policy_names,
source_acct_id,
source_api_key,
source_region,
target_acct_id,
target_api_key,
target_region
)
policies_by_source_id = return_dict['policies_by_source_id']
status = return_dict['all_alert_status']
status_file = ac.get_alert_status_file_name(
policy_file_path,
entity_file_path,
source_acct_id,
target_acct_id,
'_policies'
)
store.save_status_csv(status_file, status, askeys)
return policies_by_source_id
class MigratePoliciesCommand:
def configure_parser(self, migrate_subparsers, global_options_parser):
# Create the parser for the "policies" command
policies_parser = migrate_subparsers.add_parser(
'policies',
help='policies help',
parents=[global_options_parser],
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
configure_parser(policies_parser, False)
policies_parser.set_defaults(func=self.run)
def run(self, config: configparser.ConfigParser, args: argparse.Namespace):
logger.info('Starting alert policy migration...')
base_config = utils.process_base_config(
config,
'migrate.policies',
args
)
policy_file_path = config.get(
'migrate.policies',
'policy_file',
fallback=None
)
if not policy_file_path:
if args.policy_file:
policy_file_path = args.policy_file[0]
entity_file_path = config.get(
'migrate.policies',
'entity_file',
fallback=None
)
if not entity_file_path:
if args.entity_file:
entity_file_path = args.entity_file[0]
if not policy_file_path and not entity_file_path:
utils.error_message_and_exit(
'Either a policy file or entity file must be specified.'
)
use_local = config.getboolean(
'migrate.conditions',
'use_local',
fallback=args.use_local
)
migrate(
policy_file_path,
entity_file_path,
base_config['source_account_id'],
base_config['source_region'],
base_config['target_account_id'],
base_config['target_region'],
base_config['source_api_key'],
base_config['target_api_key'],
use_local
)
logger.info('Completed alert policy migration.')
def main():
parser = create_argument_parser()
args = parser.parse_args()
source_api_key = utils.ensure_source_api_key(args)
if not source_api_key:
utils.error_and_exit('source_api_key', 'ENV_SOURCE_API_KEY')
target_api_key = utils.ensure_target_api_key(args)
if not target_api_key:
utils.error_and_exit('target_api_key', 'ENV_TARGET_API_KEY')
policy_file = args.policy_file[0] if args.policy_file else None
entity_file = args.entity_file[0] if args.entity_file else None
if not policy_file and not entity_file:
logger.error('Either a policy file or entity file must be specified.')
sys.exit()
sourceRegion = utils.ensure_source_region(args)
targetRegion = utils.ensure_target_region(args)
print_args(args, source_api_key, sourceRegion, target_api_key, targetRegion)
migrate(
policy_file,
entity_file,
args.source_account_id[0],
sourceRegion,
args.target_account_id[0],
targetRegion,
source_api_key,
target_api_key,
args.use_local
)
if __name__ == '__main__':
main()