From 103f90383f02b72a6f045e7263c41ea4719ac458 Mon Sep 17 00:00:00 2001 From: Ivan Davydenko Date: Tue, 20 Feb 2024 12:39:05 +0200 Subject: [PATCH 1/4] SONIC CLI for CLI-Sessions feature --- config/main.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ show/main.py | 39 ++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/config/main.py b/config/main.py index b750b49820..04976b476f 100644 --- a/config/main.py +++ b/config/main.py @@ -7750,5 +7750,69 @@ def notice(db, category_list, max_events, namespace): handle_asic_sdk_health_suppress(db, 'notice', category_list, max_events, namespace) +# +# 'serial_console' group ('config serial_console') +# +@config.group() +def serial_console(): + """Configuring system serial-console behavior""" + pass + + +@serial_console.command('sysrq-capabilities') +@click.argument('sysrq_capabilities', metavar='', required=True, + type=click.Choice(['enabled', 'disabled'])) +def sysrq_capabilities(sysrq_capabilities): + """Set serial console sysrq-capabilities state""" + + config_db = ConfigDBConnector() + config_db.connect() + config_db.mod_entry("SERIAL_CONSOLE", 'POLICIES', + {'sysrq_capabilities': sysrq_capabilities}) + + +@serial_console.command('inactivity-timeout') +@click.argument('inactivity_timeout', metavar='', required=True, + type=click.IntRange(0,35000)) +def inactivity_timeout_serial(inactivity_timeout): + """Set serial console inactivity timeout""" + + config_db = ConfigDBConnector() + config_db.connect() + config_db.mod_entry("SERIAL_CONSOLE", 'POLICIES', + {'inactivity_timeout': inactivity_timeout}) + + +# +# 'ssh' group ('config ssh') +# +@config.group() +def ssh(): + """Configuring system ssh behavior""" + pass + +@ssh.command('inactivity-timeout') +@click.argument('inactivity_timeout', metavar='', required=True, + type=click.IntRange(0,35000)) +def inactivity_timeout_ssh(inactivity_timeout): + """Set ssh inactivity timeout""" + + config_db = ConfigDBConnector() + config_db.connect() + config_db.mod_entry("SSH_SERVER", 'POLICIES', + {'inactivity_timeout': inactivity_timeout}) + +@ssh.command('max-sessions') +@click.argument('max-sessions', metavar='', required=True, + type=click.IntRange(0,100)) +def max_sessions(max_sessions): + """Set max number of concurrent logins""" + + config_db = ConfigDBConnector() + config_db.connect() + config_db.mod_entry("SSH_SERVER", 'POLICIES', + {'max_sessions': max_sessions}) + + if __name__ == '__main__': config() diff --git a/show/main.py b/show/main.py index cfdf30d3c6..314a304bc6 100755 --- a/show/main.py +++ b/show/main.py @@ -2254,6 +2254,45 @@ def received(db, namespace): ctx.fail("ASIC/SDK health event is not supported on the platform") +# +# 'serial_console' command group ("show serial_console ...") +# +@cli.group('serial_console', invoke_without_command=True) +@clicommon.pass_db +def serial_console(db): + """Show serial_console configuration""" + + serial_console_table = db.cfgdb.get_entry('SERIAL_CONSOLE', 'POLICIES') + + hdrs = ['inactivity-timeout', 'sysrq-capabilities'] + data = [] + + data.append(serial_console_table.get('inactivity_timeout', '900 ')) + data.append(serial_console_table.get('sysrq_capabilities', 'disabled ')) + + configuration = [data] + click.echo(tabulate(configuration, headers=hdrs, tablefmt='simple', missingval='')) + +# +# 'ssh' command group ("show ssh ...") +# +@cli.group('ssh', invoke_without_command=True) +@clicommon.pass_db +def ssh(db): + """Show ssh configuration""" + + serial_console_table = db.cfgdb.get_entry('SSH_SERVER', 'POLICIES') + + hdrs = ['inactivity-timeout', 'max-sessions'] + data = [] + + data.append(serial_console_table.get('inactivity_timeout', '900 ')) + data.append(serial_console_table.get('max_session', '0 ')) + + configuration = [data] + click.echo(tabulate(configuration, headers=hdrs, tablefmt='simple', missingval='')) + + # Load plugins and register them helper = util_base.UtilHelper() helper.load_and_register_plugins(plugins, cli) From bdbf65647c5e726a3751c02d7b6e84d9f6da9e0e Mon Sep 17 00:00:00 2001 From: Ivan Davydenko Date: Wed, 22 May 2024 01:23:18 +0300 Subject: [PATCH 2/4] [cli-sessions] beautify changes in config and show utilities. --- config/main.py | 14 ++++++++------ show/main.py | 9 +++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/config/main.py b/config/main.py index 04976b476f..b3fa72370c 100644 --- a/config/main.py +++ b/config/main.py @@ -7760,7 +7760,7 @@ def serial_console(): @serial_console.command('sysrq-capabilities') -@click.argument('sysrq_capabilities', metavar='', required=True, +@click.argument('sysrq_capabilities', metavar='', required=True, type=click.Choice(['enabled', 'disabled'])) def sysrq_capabilities(sysrq_capabilities): """Set serial console sysrq-capabilities state""" @@ -7772,8 +7772,8 @@ def sysrq_capabilities(sysrq_capabilities): @serial_console.command('inactivity-timeout') -@click.argument('inactivity_timeout', metavar='', required=True, - type=click.IntRange(0,35000)) +@click.argument('inactivity_timeout', metavar='', required=True, + type=click.IntRange(0, 35000)) def inactivity_timeout_serial(inactivity_timeout): """Set serial console inactivity timeout""" @@ -7791,8 +7791,9 @@ def ssh(): """Configuring system ssh behavior""" pass + @ssh.command('inactivity-timeout') -@click.argument('inactivity_timeout', metavar='', required=True, +@click.argument('inactivity_timeout', metavar='', required=True, type=click.IntRange(0,35000)) def inactivity_timeout_ssh(inactivity_timeout): """Set ssh inactivity timeout""" @@ -7802,9 +7803,10 @@ def inactivity_timeout_ssh(inactivity_timeout): config_db.mod_entry("SSH_SERVER", 'POLICIES', {'inactivity_timeout': inactivity_timeout}) + @ssh.command('max-sessions') -@click.argument('max-sessions', metavar='', required=True, - type=click.IntRange(0,100)) +@click.argument('max-sessions', metavar='', required=True, + type=click.IntRange(0, 100)) def max_sessions(max_sessions): """Set max number of concurrent logins""" diff --git a/show/main.py b/show/main.py index 314a304bc6..940ee0b583 100755 --- a/show/main.py +++ b/show/main.py @@ -2268,11 +2268,12 @@ def serial_console(db): data = [] data.append(serial_console_table.get('inactivity_timeout', '900 ')) - data.append(serial_console_table.get('sysrq_capabilities', 'disabled ')) - + data.append(serial_console_table.get('sysrq_capabilities', 'disabled ')) + configuration = [data] click.echo(tabulate(configuration, headers=hdrs, tablefmt='simple', missingval='')) + # # 'ssh' command group ("show ssh ...") # @@ -2287,8 +2288,8 @@ def ssh(db): data = [] data.append(serial_console_table.get('inactivity_timeout', '900 ')) - data.append(serial_console_table.get('max_session', '0 ')) - + data.append(serial_console_table.get('max_session', '0 ')) + configuration = [data] click.echo(tabulate(configuration, headers=hdrs, tablefmt='simple', missingval='')) From d5713671ec8bd3417c41c4f508aaa00a20c6edf9 Mon Sep 17 00:00:00 2001 From: Ivan Davydenko Date: Wed, 22 May 2024 02:00:39 +0300 Subject: [PATCH 3/4] [cli-sessions] beautifier fixes --- config/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index b3fa72370c..905ab7a050 100644 --- a/config/main.py +++ b/config/main.py @@ -7794,7 +7794,7 @@ def ssh(): @ssh.command('inactivity-timeout') @click.argument('inactivity_timeout', metavar='', required=True, - type=click.IntRange(0,35000)) + type=click.IntRange(0, 35000)) def inactivity_timeout_ssh(inactivity_timeout): """Set ssh inactivity timeout""" From 9eda124070ffe3ea753730ac8aeba1f585f42a4b Mon Sep 17 00:00:00 2001 From: Ivan Davydenko Date: Fri, 24 May 2024 23:08:01 +0300 Subject: [PATCH 4/4] [cli-sessions] unittests for ssh-server and serial-console commands. --- config/main.py | 4 ++-- tests/cli_sessions_test.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/cli_sessions_test.py diff --git a/config/main.py b/config/main.py index 905ab7a050..dc27ef50d5 100644 --- a/config/main.py +++ b/config/main.py @@ -7753,7 +7753,7 @@ def notice(db, category_list, max_events, namespace): # # 'serial_console' group ('config serial_console') # -@config.group() +@config.group(cls=clicommon.AbbreviationGroup, name='serial_console') def serial_console(): """Configuring system serial-console behavior""" pass @@ -7786,7 +7786,7 @@ def inactivity_timeout_serial(inactivity_timeout): # # 'ssh' group ('config ssh') # -@config.group() +@config.group(cls=clicommon.AbbreviationGroup, name='ssh') def ssh(): """Configuring system ssh behavior""" pass diff --git a/tests/cli_sessions_test.py b/tests/cli_sessions_test.py new file mode 100644 index 0000000000..755b232708 --- /dev/null +++ b/tests/cli_sessions_test.py @@ -0,0 +1,32 @@ +from click.testing import CliRunner + +import config.main as config +import show.main as show +from utilities_common.db import Db + + +class TestCliSessionsCommands: + def test_config_command(self): + runner = CliRunner() + + db = Db() + + result = runner.invoke(config.config.commands['serial_console'].commands['sysrq-capabilities'], + ['enabled'], obj=db) + assert result.exit_code == 0 + + result = runner.invoke(config.config.commands['serial_console'].commands['inactivity-timeout'], + ['180'], obj=db) + assert result.exit_code == 0 + + result = runner.invoke(show.cli.commands['serial_console'], obj=db) + assert result.exit_code == 0 + + result = runner.invoke(config.config.commands['ssh'].commands['inactivity-timeout'], ['190'], obj=db) + assert result.exit_code == 0 + + result = runner.invoke(config.config.commands['ssh'].commands['max-sessions'], ['60'], obj=db) + assert result.exit_code == 0 + + result = runner.invoke(show.cli.commands['ssh'], obj=db) + assert result.exit_code == 0