|
| 1 | +#!/usr/bin/env python -u |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import click |
| 5 | +import netaddr |
| 6 | +from swsssdk import ConfigDBConnector |
| 7 | + |
| 8 | + |
| 9 | +def is_ipaddress(val): |
| 10 | + if not val: |
| 11 | + return False |
| 12 | + try: |
| 13 | + netaddr.IPAddress(str(val)) |
| 14 | + except: |
| 15 | + return False |
| 16 | + return True |
| 17 | + |
| 18 | + |
| 19 | +def add_table_kv(table, entry, key, val): |
| 20 | + config_db = ConfigDBConnector() |
| 21 | + config_db.connect() |
| 22 | + config_db.mod_entry(table, entry, {key:val}) |
| 23 | + |
| 24 | + |
| 25 | +def del_table_key(table, entry, key): |
| 26 | + config_db = ConfigDBConnector() |
| 27 | + config_db.connect() |
| 28 | + data = config_db.get_entry(table, entry) |
| 29 | + if data: |
| 30 | + if key in data: |
| 31 | + del data[key] |
| 32 | + config_db.set_entry(table, entry, data) |
| 33 | + |
| 34 | + |
| 35 | +@click.group() |
| 36 | +def aaa(): |
| 37 | + """AAA command line""" |
| 38 | + pass |
| 39 | + |
| 40 | + |
| 41 | +# cmd: aaa authentication |
| 42 | +@click.group() |
| 43 | +def authentication(): |
| 44 | + """User authentication""" |
| 45 | + pass |
| 46 | +aaa.add_command(authentication) |
| 47 | + |
| 48 | + |
| 49 | +# cmd: aaa authentication failthrough |
| 50 | +@click.command() |
| 51 | +@click.argument('option', type=click.Choice(["enable", "disable", "default"])) |
| 52 | +def failthrough(option): |
| 53 | + """Allow AAA fail-through [enable | disable | default]""" |
| 54 | + if option == 'default': |
| 55 | + del_table_key('AAA', 'authentication', 'failthrough') |
| 56 | + else: |
| 57 | + if option == 'enable': |
| 58 | + add_table_kv('AAA', 'authentication', 'failthrough', True) |
| 59 | + elif option == 'disable': |
| 60 | + add_table_kv('AAA', 'authentication', 'failthrough', False) |
| 61 | +authentication.add_command(failthrough) |
| 62 | + |
| 63 | + |
| 64 | +# cmd: aaa authentication fallback |
| 65 | +@click.command() |
| 66 | +@click.argument('option', type=click.Choice(["enable", "disable", "default"])) |
| 67 | +def fallback(option): |
| 68 | + """Allow AAA fallback [enable | disable | default]""" |
| 69 | + if option == 'default': |
| 70 | + del_table_key('AAA', 'authentication', 'fallback') |
| 71 | + else: |
| 72 | + if option == 'enable': |
| 73 | + add_table_kv('AAA', 'authentication', 'fallback', True) |
| 74 | + elif option == 'disable': |
| 75 | + add_table_kv('AAA', 'authentication', 'fallback', False) |
| 76 | +authentication.add_command(fallback) |
| 77 | + |
| 78 | + |
| 79 | +@click.command() |
| 80 | +@click.argument('auth_protocol', nargs=-1, type=click.Choice(["tacacs+", "local", "default"])) |
| 81 | +def login(auth_protocol): |
| 82 | + """Switch login authentication [ {tacacs+, local} | default ]""" |
| 83 | + if len(auth_protocol) is 0: |
| 84 | + print 'Not support empty argument' |
| 85 | + return |
| 86 | + |
| 87 | + if 'default' in auth_protocol: |
| 88 | + del_table_key('AAA', 'authentication', 'login') |
| 89 | + else: |
| 90 | + val = auth_protocol[0] |
| 91 | + if len(auth_protocol) == 2: |
| 92 | + val += ',' + auth_protocol[1] |
| 93 | + add_table_kv('AAA', 'authentication', 'login', val) |
| 94 | +authentication.add_command(login) |
| 95 | + |
| 96 | + |
| 97 | +@click.group() |
| 98 | +def tacacs(): |
| 99 | + """TACACS+ server configuration""" |
| 100 | + pass |
| 101 | + |
| 102 | + |
| 103 | +@click.group() |
| 104 | +@click.pass_context |
| 105 | +def default(ctx): |
| 106 | + """set its default configuration""" |
| 107 | + ctx.obj = 'default' |
| 108 | +tacacs.add_command(default) |
| 109 | + |
| 110 | + |
| 111 | +@click.command() |
| 112 | +@click.argument('second', metavar='<time_second>', type=click.IntRange(0, 60), required=False) |
| 113 | +@click.pass_context |
| 114 | +def timeout(ctx, second): |
| 115 | + """Specify TACACS+ server global timeout <0 - 60>""" |
| 116 | + if ctx.obj == 'default': |
| 117 | + del_table_key('TACPLUS', 'global', 'timeout') |
| 118 | + elif second: |
| 119 | + add_table_kv('TACPLUS', 'global', 'timeout', second) |
| 120 | + else: |
| 121 | + click.echo('Not support empty argument') |
| 122 | +tacacs.add_command(timeout) |
| 123 | +default.add_command(timeout) |
| 124 | + |
| 125 | + |
| 126 | +@click.command() |
| 127 | +@click.argument('type', metavar='<type>', type=click.Choice(["chap", "pap", "mschap"]), required=False) |
| 128 | +@click.pass_context |
| 129 | +def authtype(ctx, type): |
| 130 | + """Specify TACACS+ server global auth_type [chap | pap | mschap]""" |
| 131 | + if ctx.obj == 'default': |
| 132 | + del_table_key('TACPLUS', 'global', 'auth_type') |
| 133 | + elif type: |
| 134 | + add_table_kv('TACPLUS', 'global', 'auth_type', type) |
| 135 | + else: |
| 136 | + click.echo('Not support empty argument') |
| 137 | +tacacs.add_command(authtype) |
| 138 | +default.add_command(authtype) |
| 139 | + |
| 140 | + |
| 141 | +@click.command() |
| 142 | +@click.argument('secret', metavar='<secret_string>', required=False) |
| 143 | +@click.pass_context |
| 144 | +def passkey(ctx, secret): |
| 145 | + """Specify TACACS+ server global passkey <STRING>""" |
| 146 | + if ctx.obj == 'default': |
| 147 | + del_table_key('TACPLUS', 'global', 'passkey') |
| 148 | + elif secret: |
| 149 | + add_table_kv('TACPLUS', 'global', 'passkey', secret) |
| 150 | + else: |
| 151 | + click.echo('Not support empty argument') |
| 152 | +tacacs.add_command(passkey) |
| 153 | +default.add_command(passkey) |
| 154 | + |
| 155 | + |
| 156 | +# cmd: tacacs add <ip_address> --timeout SECOND --key SECRET --type TYPE --port PORT --pri PRIORITY |
| 157 | +@click.command() |
| 158 | +@click.argument('address', metavar='<ip_address>') |
| 159 | +@click.option('-t', '--timeout', help='Transmission timeout interval, default 5', type=int) |
| 160 | +@click.option('-k', '--key', help='Shared secret') |
| 161 | +@click.option('-a', '--auth_type', help='Authentication type, default pap', type=click.Choice(["chap", "pap", "mschap"])) |
| 162 | +@click.option('-o', '--port', help='TCP port range is 1 to 65535, default 49', type=click.IntRange(1, 65535), default=49) |
| 163 | +@click.option('-p', '--pri', help="Priority, default 1", type=click.IntRange(1, 64), default=1) |
| 164 | +def add(address, timeout, key, auth_type, port, pri): |
| 165 | + """Specify a TACACS+ server""" |
| 166 | + if not is_ipaddress(address): |
| 167 | + click.echo('Invalid ip address') |
| 168 | + return |
| 169 | + |
| 170 | + config_db = ConfigDBConnector() |
| 171 | + config_db.connect() |
| 172 | + old_data = config_db.get_entry('TACPLUS_SERVER', address) |
| 173 | + if old_data != {}: |
| 174 | + click.echo('server %s already exists' % address) |
| 175 | + else: |
| 176 | + data = { |
| 177 | + 'tcp_port': str(port), |
| 178 | + 'priority': pri |
| 179 | + } |
| 180 | + if auth_type is not None: |
| 181 | + data['auth_type'] = auth_type |
| 182 | + if timeout is not None: |
| 183 | + data['timeout'] = str(timeout) |
| 184 | + if key is not None: |
| 185 | + data['passkey'] = key |
| 186 | + config_db.set_entry('TACPLUS_SERVER', address, data) |
| 187 | +tacacs.add_command(add) |
| 188 | + |
| 189 | + |
| 190 | +# cmd: tacacs delete <ip_address> |
| 191 | +# 'del' is keyword, replace with 'delete' |
| 192 | +@click.command() |
| 193 | +@click.argument('address', metavar='<ip_address>') |
| 194 | +def delete(address): |
| 195 | + """Delete a TACACS+ server""" |
| 196 | + if not is_ipaddress(address): |
| 197 | + click.echo('Invalid ip address') |
| 198 | + return |
| 199 | + |
| 200 | + config_db = ConfigDBConnector() |
| 201 | + config_db.connect() |
| 202 | + config_db.set_entry('TACPLUS_SERVER', address, None) |
| 203 | +tacacs.add_command(delete) |
| 204 | + |
| 205 | + |
| 206 | +if __name__ == "__main__": |
| 207 | + aaa() |
| 208 | + |
0 commit comments