|
@@ -0,0 +1,144 @@
|
|
|
|
+import click
|
|
|
|
+import hashlib
|
|
|
|
+import json
|
|
|
|
+from sqlalchemy import create_engine
|
|
|
|
+from kamcli.ioutils import ioutils_dbres_print
|
|
|
|
+from kamcli.cli import pass_context
|
|
|
|
+from kamcli.cli import parse_user_spec
|
|
|
|
+from kamcli.iorpc import command_ctl
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('address', help='Manage permissions address records')
|
|
|
|
+@pass_context
|
|
|
|
+def cli(ctx):
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('add', short_help='Add a new record to address table')
|
|
|
|
[email protected]('mask', '--mask', type=int, default=32,
|
|
|
|
+ help='Mask value (default 32)')
|
|
|
|
[email protected]('port', '--port', type=int, default=0,
|
|
|
|
+ help='Port value (default 0)')
|
|
|
|
[email protected]('tag', '--tag', default='',
|
|
|
|
+ help='Tag value (default: "")')
|
|
|
|
[email protected]('group', metavar='<group>', type=int)
|
|
|
|
[email protected]('address', metavar='<address>')
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_add(ctx, mask, port, tag, group, address):
|
|
|
|
+ """Add a new record to address db table
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <group> - group id
|
|
|
|
+ <address> - IP address
|
|
|
|
+ """
|
|
|
|
+ ctx.vlog('Adding to group id [%d] address [%s]', group, address)
|
|
|
|
+ e = create_engine(ctx.gconfig.get('db', 'rwurl'))
|
|
|
|
+ e.execute('insert into address (grp, ip_addr, mask, port, tag) values ({0}, {1!r}, {2}, {3}, {4!r})'.format(group, address.encode('ascii','ignore'), mask, pport, tag.encode('ascii','ignore')))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('rm', short_help='Remove a record from address db table')
|
|
|
|
[email protected]('mask', '--mask', type=int,
|
|
|
|
+ help='Mask value')
|
|
|
|
[email protected]('port', '--port', type=int,
|
|
|
|
+ help='Port value')
|
|
|
|
[email protected]('group', metavar='<group>', type=int)
|
|
|
|
[email protected]('address', metavar='<address>')
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_rm(ctx, mask, port, group, address):
|
|
|
|
+ """Remove a record from address db table
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <group> - group id
|
|
|
|
+ <address> - IP address
|
|
|
|
+ """
|
|
|
|
+ e = create_engine(ctx.gconfig.get('db', 'rwurl'))
|
|
|
|
+ if not mask:
|
|
|
|
+ if not port:
|
|
|
|
+ e.execute('delete from dispatcher where setid={0} and destination={1!r}'.format(setid, destination.encode('ascii','ignore')))
|
|
|
|
+ else:
|
|
|
|
+ e.execute('delete from dispatcher where setid={0} and destination={1!r} and port={2}'.format(setid, destination.encode('ascii','ignore'),port))
|
|
|
|
+ else:
|
|
|
|
+ if not port:
|
|
|
|
+ e.execute('delete from dispatcher where setid={0} and destination={1!r} and mask={2}'.format(setid, destination.encode('ascii','ignore'),mask))
|
|
|
|
+ else:
|
|
|
|
+ e.execute('delete from dispatcher where setid={0} and destination={1!r} and mask={2} and port={3}'.format(setid, destination.encode('ascii','ignore'),mask, port))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('showdb', short_help='Show address records in database')
|
|
|
|
[email protected]('oformat', '--output-format', '-F',
|
|
|
|
+ type=click.Choice(['raw', 'json', 'table', 'dict']),
|
|
|
|
+ default=None, help='Format the output')
|
|
|
|
[email protected]('ostyle', '--output-style', '-S',
|
|
|
|
+ default=None, help='Style of the output (tabulate table format)')
|
|
|
|
[email protected]('group', nargs=-1, metavar='[<group>]', type=int)
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_showdb(ctx, oformat, ostyle, group):
|
|
|
|
+ """Show details for records in address db table
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <group> - address group
|
|
|
|
+ """
|
|
|
|
+ e = create_engine(ctx.gconfig.get('db', 'rwurl'))
|
|
|
|
+ if not group:
|
|
|
|
+ ctx.vlog('Showing all address records')
|
|
|
|
+ res = e.execute('select * from address')
|
|
|
|
+ else:
|
|
|
|
+ ctx.vlog('Showing address records for group')
|
|
|
|
+ res = e.execute('select * from address where group=%d', group)
|
|
|
|
+ ioutils_dbres_print(ctx, oformat, ostyle, res)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('list', short_help='Show details for address records in memory')
|
|
|
|
[email protected]('tag', '--mode', default='all',
|
|
|
|
+ help='What to be printed (all, addresses, subnets, domains)')
|
|
|
|
[email protected]('group', nargs=-1, metavar='[<group>]', type=int)
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_list(ctx, mode, group):
|
|
|
|
+ """Show details for address records in memory
|
|
|
|
+
|
|
|
|
+ \b
|
|
|
|
+ Parameters:
|
|
|
|
+ <group> - address group
|
|
|
|
+ """
|
|
|
|
+ if mode == "all":
|
|
|
|
+ command_ctl(ctx, 'permissions.addressDump', [ ])
|
|
|
|
+ command_ctl(ctx, 'permissions.subnetDump', [ ])
|
|
|
|
+ command_ctl(ctx, 'permissions.domainDump', [ ])
|
|
|
|
+ elif mode == "addresses":
|
|
|
|
+ command_ctl(ctx, 'permissions.addressDump', [ ])
|
|
|
|
+ elif mode == "subnets":
|
|
|
|
+ command_ctl(ctx, 'permissions.subnetDump', [ ])
|
|
|
|
+ elif mode == "domains":
|
|
|
|
+ command_ctl(ctx, 'permissions.domainDump', [ ])
|
|
|
|
+ else:
|
|
|
|
+ command_ctl(ctx, 'permissions.addressDump', [ ])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+##
|
|
|
|
+#
|
|
|
|
+#
|
|
|
|
[email protected]('reload', short_help='Reload address records from database into memory')
|
|
|
|
+@pass_context
|
|
|
|
+def dispatcher_reload(ctx):
|
|
|
|
+ """Reload address records from database into memory
|
|
|
|
+ """
|
|
|
|
+ command_ctl(ctx, 'permissions.addressReload', [ ])
|
|
|
|
+
|