-
Notifications
You must be signed in to change notification settings - Fork 0
/
netbox-cli.py
639 lines (524 loc) · 18.9 KB
/
netbox-cli.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#! /usr/bin/env python3
from cryptography.fernet import Fernet, InvalidToken
from clint import resources
import binascii
import base64
import click
import click_log
import os
import sys
import configparser
import ipaddress
import pynetbox
import json
import logging
from tools.arin.payloads import CustomerPayload, NetPayload, NetBlockPayload, TicketedRequestPayload
from tools.arin import Arin
from tools.googlemaps import GeocodeResult
from tools.googlemaps.googlemaps import GeocodeResultError
logger = logging.getLogger(__name__)
click_log.basic_config(logger)
resources.init('connectit', 'netbox-cli')
config = configparser.ConfigParser()
def write_config():
try:
with resources.user.open('config.ini', mode='w') as configFile:
config.write(configFile)
except IOError:
pass
class ContextObject:
def __init__(self, ctx):
self.ctx = ctx
self.secret = None
self.fernet = None
self._netbox = None
self._gmaps = None
self._arin = None
config['netbox'] = {
'uri': '',
'private_key_file': '',
'token': ''
}
config['arin'] = {
'api_key': '',
'parent_org_handle': '',
'uri': 'https://www.ote.arin.net/',
'origin_ases': ''
}
config['google'] = {
'api_key': '',
}
config['global'] = {
'secret': '',
}
try:
with resources.user.open('config.ini', mode='r') as configFile:
config.read_file(configFile)
except IOError:
pass
def validate_secret(self):
if not self.secret:
self.secret = click.prompt(
"Enter Configuration Secret Key", hide_input=True)
self.fernet = Fernet(self.secret.encode())
if not config['global']['secret']:
logger.error("The secret key is not set in the configuration file")
sys.exit(400)
else:
try:
self.decrypt(config['global']['secret'])
except InvalidToken:
logger.error("The secret key is invalid")
sys.exit(403)
def decrypt(self, string):
if isinstance(string, str):
string = string.encode()
try:
return base64.urlsafe_b64decode(self.fernet.decrypt(string))
except InvalidToken:
return bytes()
def encrypt(self, string):
if isinstance(string, str):
string = string.encode()
return self.fernet.encrypt(base64.urlsafe_b64encode(string))
@property
def netbox_token(self):
return self.decrypt(config['netbox']['token']).decode()
@netbox_token.setter
def netbox_token(self, value):
config['netbox']['token'] = self.encrypt(value).decode()
write_config()
@property
def netbox_uri(self):
return config['netbox']['uri']
@netbox_uri.setter
def netbox_uri(self, value):
config['netbox']['uri'] = value
write_config()
@property
def netbox_private_key_file(self):
return config['netbox']['private_key_file']
@netbox_private_key_file.setter
def netbox_private_key_file(self, value):
config['netbox']['private_key_file'] = value
write_config()
@property
def arin_api_key(self):
return self.decrypt(config['arin']['api_key']).decode()
@arin_api_key.setter
def arin_api_key(self, value):
config['arin']['api_key'] = self.encrypt(value).decode()
write_config()
@property
def arin_uri(self):
return config['arin']['uri']
@arin_uri.setter
def arin_uri(self, value):
config['arin']['uri'] = value
write_config()
@property
def arin_origin_ases(self):
return config['arin']['origin_ases'].split(',')
@arin_origin_ases.setter
def arin_origin_ases(self, value):
config['arin']['origin_ases'] = value
write_config()
@property
def arin_parent_org_handle(self):
return config['arin']['parent_org_handle']
@arin_parent_org_handle.setter
def arin_parent_org_handle(self, value):
config['arin']['parent_org_handle'] = value
write_config()
@property
def google_api_key(self):
return self.decrypt(config['google']['api_key']).decode()
@google_api_key.setter
def google_api_key(self, value):
config['google']['api_key'] = self.encrypt(value).decode()
write_config()
@property
def netbox(self):
if self._netbox is None:
self._netbox = pynetbox.api(
self.netbox_uri,
token=self.netbox_token,
private_key_file=self.netbox_private_key_file
)
logger.debug('`netbox` context object initialized')
else:
logger.debug('`netbox` context object accessed')
return self._netbox
@property
def gmaps(self):
if self._gmaps is None:
import googlemaps
self._gmaps = googlemaps.Client(key=self.google_api_key)
logger.debug('`gmaps` context object initialized')
else:
logger.debug('`gmaps` context object accessed')
return self._gmaps
@property
def arin(self):
if self._arin is None:
self._arin = Arin(apikey=self.arin_api_key, uri=self.arin_uri)
logger.debug('`arin` context object initialized')
else:
logger.debug('`arin` context object accessed')
return self._arin
class OptionPromptNull(click.Option):
_value_key = '_default_val'
def get_default(self, ctx):
if not hasattr(self, self._value_key):
default = super(OptionPromptNull, self).get_default(ctx)
setattr(self, self._value_key, default)
return getattr(self, self._value_key)
def prompt_for_value(self, ctx):
default = self.get_default(ctx)
# only prompt if the default value is None
if default is None:
return super(OptionPromptNull, self).prompt_for_value(ctx)
return default
class AliasedGroup(click.Group):
def get_command(self, ctx, cmd_name):
rv = click.Group.get_command(self, ctx, cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx)
if x.startswith(cmd_name)]
if not matches:
return None
elif len(matches) == 1:
return click.Group.get_command(self, ctx, matches[0])
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))
@click.command(cls=AliasedGroup)
@click.option('--secret', help='Secret key to encrypt sensible data with',
envvar='CONFIG_SECRET', show_envvar=True)
@click_log.simple_verbosity_option(logger)
@click.pass_context
def cli(ctx, secret):
ctx.obj = ContextObject(ctx)
ctx.obj.secret = secret
if ctx.obj.secret:
try:
ctx.obj.fernet = Fernet(ctx.obj.secret.encode())
except binascii.Error:
logger.error("The secret key is not in Base64 format")
ctx.exit(401)
# arin
@cli.command(name='arin', cls=AliasedGroup)
@click.pass_context
def arin(ctx):
obj = ctx.obj
if not obj.netbox_uri and not obj.netbox_token:
click.echo(
"netbox config settings are not properly set\n"
)
ctx.exit(1)
# arin reassign
@arin.command(name='reassign', cls=AliasedGroup)
def arin_reassign():
pass
# arin reassign simple
@arin_reassign.command(name='simple')
@click.option('-a', '--aggregate-id', required=False)
@click.option('-p', '--prefix-id', required=False)
@click.option('-r', '--replace-existing', is_flag=True)
@click.pass_context
def arin_reassign_simple(ctx, aggregate_id=None, prefix_id=None, replace_existing=False):
obj = ctx.obj
if aggregate_id and prefix_id:
click.echo(
"Either '--aggregate_id' or '--prefix_id' must be specified, not both\n"
)
click.echo(ctx.get_help())
ctx.exit(1)
if aggregate_id is None and prefix_id is None:
aggregates = obj.netbox.ipam.aggregates.all()
choices = [str(aggregate.id) for aggregate in aggregates]
for aggregate in aggregates:
aid = str(aggregate.id)
click.echo("[{aid}]: {aggregate}".format(
aggregate=aggregate,
aid=aid.rjust(4-len(aid)))
)
choice = click.prompt(
"Reassign subnets in which aggregate?",
type=click.Choice(choices)
)
aggregate_id = int(choice)
if aggregate_id and prefix_id is None:
aggregate = obj.netbox.ipam.aggregates.get(aggregate_id)
prefixes = obj.netbox.ipam.prefixes.filter(within_include=aggregate)
for prefix in prefixes:
if not replace_existing and prefix.custom_fields['RIR Handle']:
logger.info(
"%s already has an RIR Handle, skipping" % prefix)
continue
if not prefix.site:
logger.info(
"%s has no site, skipping" % prefix
)
continue
if not prefix.tenant:
logger.info(
"%s has no tenant, skipping" % prefix
)
continue
ctx.invoke(arin_reassign_simple, prefix_id=prefix.id,
replace_existing=replace_existing)
if prefix_id and not aggregate_id:
parent_org_handle = obj.arin_parent_org_handle
if not parent_org_handle:
logger.error(
"arin-parent-org-handle setting is not set, exitting"
)
ctx.exit(1)
# Get the prefix
prefix = obj.netbox.ipam.prefixes.get(prefix_id)
if not replace_existing and prefix.custom_fields['RIR Handle']:
logger.error(
"%s already has an RIR Handle, exitting" % prefix
)
ctx.exit(1)
# Get its aggregate
aggregate = obj.netbox.ipam.aggregates.get(q=prefix)
parent_net_handle = aggregate.custom_fields['RIR Handle']
if not parent_net_handle:
logger.error(
"Aggregate `%s` has no RIR Handle, exitting" % aggregate
)
ctx.exit(1)
# Query the hyperlinked endpoints
if prefix.tenant:
prefix.tenant.full_details()
else:
logger.error(
"%s has no tenant, exitting" % prefix
)
ctx.exit(1)
if prefix.site:
site = obj.netbox.dcim.sites.get(prefix.site.id)
else:
logger.error(
"%s has no site, exitting" % prefix
)
ctx.exit(1)
tenant = prefix.tenant
logger.info("Processing {prefix}@{site} for {tenant}".format(
prefix=prefix,
site=site,
tenant=tenant
))
# Build ARIN Cust Payload
customer_name = tenant.name
geocode_result = GeocodeResult(
obj.gmaps.geocode(site.physical_address))
iso3166_1_name = geocode_result.iso3166_1.name
iso3166_1_code2 = geocode_result.iso3166_1.alpha_2
iso3166_1_code3 = geocode_result.iso3166_1.alpha_3
iso3166_1_e164 = geocode_result.iso3166_1.numeric
street_address = geocode_result.street_address
city = geocode_result.city
iso3166_2 = geocode_result.short_address_components['administrative_area_level_1']
postal_code = geocode_result.postal_code
comment = ""
private_customer = 'true'
arin_customer_payload = CustomerPayload(customer_name, iso3166_1_name, iso3166_1_code2, iso3166_1_code3,
iso3166_1_e164, street_address, city, iso3166_2, postal_code, comment, parent_org_handle, private_customer)
arin_customer_response = obj.arin.create_recipient_customer(
parent_net_handle, arin_customer_payload
)
if arin_customer_response:
arin_customer_response_payload = CustomerPayload.from_xml(
str(arin_customer_response)
)
if not replace_existing and prefix.custom_fields['RIR Handle']:
logger.debug(
"%s already has an RIR Handle, skipping creation of the NET object" % prefix)
else:
ip_network = ipaddress.ip_network(prefix.prefix)
# Build ARIN NetBlock Payload
arin_netblock_payload = NetBlockPayload(start_address=ip_network.network_address,
description=prefix.role.name,
cidr_length=ip_network.prefixlen)
net_name = "CUST-%s-%s" % (
prefix.family, "-".join(
str(ip_network.network_address).split('.')
)
)
# Build ARIN Net Payload
arin_net_payload = NetPayload(version=prefix.family.value,
comment=None,
parent_net_handle=parent_net_handle,
net_name=net_name,
origin_ases=obj.arin_origin_ases,
net_blocks=[
arin_netblock_payload],
handle=None,
registration_date=None,
customer_handle=arin_customer_response_payload.handle,
poc_links=None)
arin_net_response = obj.arin.reassign_net(
parent_net_handle, arin_net_payload
)
if arin_net_response:
arin_net_response_payload = TicketedRequestPayload.from_xml(
arin_net_response)
prefix.custom_fields['RIR Handle'] = arin_net_response_payload.handle
prefix.custom_fields['RIR registration date'] = arin_net_response_payload.registration_date
prefix.custom_fields['RIR Net Name'] = arin_net_response_payload.net_name
prefix.save()
else:
import ipdb; ipdb.set_trace()
else:
import ipdb; ipdb.set_trace()
# config
@cli.command(name='config', cls=AliasedGroup)
def configuration():
pass
# config validate-secret
@configuration.command(name='validate-secret')
@click.pass_obj
def config_validate_secret(obj):
obj.validate_secret()
# config generate-secret
@configuration.command(name='generate-secret')
def config_generate_secret():
click.confirm(
'This will generate a new secret and configuration file. Continue?', abort=True)
key = Fernet.generate_key()
fernet = Fernet(key)
config['global'] = {
'secret': fernet.encrypt(key).decode(),
}
config.remove_section('netbox')
write_config()
click.echo("New secret key: %s" % key.decode())
click.echo("Please store this in a secure location")
# config get
@configuration.command(name='get', cls=AliasedGroup)
@click.pass_obj
def config_get(obj):
obj.validate_secret()
@config_get.command(name='netbox-uri')
def get_netbox_uri(obj):
if obj.netbox_uri:
click.echo(obj.netbox_uri)
else:
click.echo("NOT SET")
@config_get.command(name='netbox-private-key-file')
@click.pass_obj
def get_netbox_private_key_file(obj):
if obj.netbox_private_key_file:
click.echo(obj.netbox_private_key_file)
else:
click.echo("NOT SET")
@config_get.command(name='netbox-token')
@click.pass_obj
def get_netbox_token(obj):
if obj.netbox_token:
click.echo(obj.netbox_token)
else:
click.echo("NOT SET")
@config_get.command(name='arin-parent-org-handle')
@click.pass_obj
def get_arin_parent_org_handle(obj):
if obj.arin_private_key_file:
click.echo(obj.arin_private_key_file)
else:
click.echo("NOT SET")
@config_get.command(name='arin-origin-ases')
@click.pass_obj
def get_arin_origin_ases(obj):
if obj.arin_origin_ases:
click.echo(obj.arin_origin_ases)
else:
click.echo("NOT SET")
@config_get.command(name='arin-api-key')
@click.pass_obj
def get_arin_api_key(obj):
if obj.arin_api_key:
click.echo(obj.arin_api_key)
else:
click.echo("NOT SET")
@config_get.command(name='arin-uri')
@click.pass_obj
def get_arin_uri(obj):
if obj.arin_uri:
click.echo(obj.arin_uri)
else:
click.echo("NOT SET")
@config_get.command(name='google-api-key')
@click.pass_obj
def get_google_api_key(obj):
if obj.google_api_key:
click.echo(obj.google_api_key)
else:
click.echo("NOT SET")
# config set
@configuration.command(name='set', cls=AliasedGroup)
@click.pass_obj
def config_set(obj):
obj.validate_secret()
@config_set.command(name='netbox-uri')
@click.argument('value')
@click.pass_obj
def set_netbox_uri(obj, value):
obj.netbox_uri = value
click.echo('netbox-uri set to: {}'.format(obj.netbox_uri))
@config_set.command(name='arin-uri')
@click.argument('value')
@click.pass_obj
def set_arin_uri(obj, value):
obj.arin_uri = value
click.echo('arin-uri set to: {}'.format(obj.arin_uri))
@config_set.command(name='netbox-private-key-file')
@click.argument('file', type=click.Path(exists=True))
@click.pass_obj
def set_netbox_private_key_file(obj, file):
obj.netbox_private_key_file = file
click.echo(
'netbox-private-key-file set to: {}'.format(
obj.netbox_private_key_file)
)
@config_set.command(name='netbox-token')
@click.pass_obj
def set_netbox_token(obj):
token = click.prompt('Token')
obj.netbox_token = token
click.echo('netbox-token set')
@config_set.command(name='arin-parent-org-handle')
@click.argument('value')
@click.pass_obj
def set_arin_parent_org_handle(obj, value):
obj.arin_parent_org_handle = value
click.echo(
'arin-parent-org-handle set to: {}'.format(obj.arin_parent_org_handle)
)
@config_set.command(name='arin-origin-ases')
@click.argument('value')
@click.pass_obj
def set_arin_origin_ases(obj, value):
obj.arin_origin_ases = value
click.echo(
'arin-origin-ases set to: {}'.format(obj.arin_origin_ases)
)
@config_set.command(name='arin-api-key')
@click.pass_obj
def set_arin_api_key(obj):
key = click.prompt('API Key')
obj.arin_api_key = key
click.echo('arin-api-key set')
@config_set.command(name='google-api-key')
@click.pass_obj
def set_google_api_key(obj):
key = click.prompt('API Key')
obj.google_api_key = key
click.echo('google-api-key set')
# pylint: disable=no-value-for-parameter,unexpected-keyword-arg
if __name__ == '__main__':
try:
cli(auto_envvar_prefix='NETBOX_CLI')
except pynetbox.core.query.RequestError as e:
logger.debug(e)