From bfc284ac31b56e2ae2676bd28da218e6848d85dd Mon Sep 17 00:00:00 2001 From: Azmy Ali Date: Thu, 2 May 2024 15:29:31 +0300 Subject: [PATCH] Added CLI commands to configure Local Users' Passwords Reset feature and display current configuration --- config/main.py | 13 ++++++++ doc/Command-Reference.md | 40 ++++++++++++++++++++++- show/main.py | 20 ++++++++++++ tests/local_users_passwords_reset_test.py | 34 +++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/local_users_passwords_reset_test.py diff --git a/config/main.py b/config/main.py index 500718e2b8..6474a181f8 100644 --- a/config/main.py +++ b/config/main.py @@ -7667,5 +7667,18 @@ def notice(db, category_list, max_events, namespace): handle_asic_sdk_health_suppress(db, 'notice', category_list, max_events, namespace) +# +# 'local_users_passwords_reset' command ('config local-users-passwords-reset ...') +# +@config.command('local-users-passwords-reset') +@click.argument('state', metavar='', required=True, type=click.Choice(['enabled', 'disabled'])) +@clicommon.pass_db +def state(db, state): + """Set local-users-passwords-reset feature state""" + + config_db = db.cfgdb + config_db.mod_entry(swsscommon.CFG_LOCAL_USERS_PASSWORDS_RESET, 'global', {'state': state}) + + if __name__ == '__main__': config() diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 457436354e..766e432f48 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -217,7 +217,9 @@ * [Static DNS show command](#static-dns-show-command) * [Wake-on-LAN Commands](#wake-on-lan-commands) * [Send Wake-on-LAN Magic Packet command](#send-wake-on-lan-magic-packet-command) - +* [Local Users' Passwords Reset Commands](#local-users-passwords-reset-commands) + * [Local Users' Passwords Config Command](#local-users-passwords-reset-config-command) + * [Reset Local Users' Passwords Show command](#local-users-passwords-reset-show-command) ## Document History | Version | Modification Date | Details | @@ -13587,3 +13589,39 @@ Sending 3 magic packet to 11:33:55:77:99:bb via interface Vlan1000 ``` For the 4th example, it specifise 2 target MAC addresses and `count` is 3. So it'll send 6 magic packets in total. + +# Local Users Passwords Reset Commands + +This sub-section explains the list of the configuration options available for Local Users' Passwords Reset feature. + +Please note, The commands will not have any effect if the feature is disabled in `rules/config`. + +## Local Users Passwords Reset Config Command + +- Set Local Users' Passwords Reset feature state + +``` + admin@sonic:~$ config local-users-passwords-reset + Usage: config config local-users-passwords-reset + Set local-users-passwords-reset feature state + Options: + -?, -h, --help Show this message and exit. + ``` + + ## Local Users Passwords Reset Show Command + +- show local-users-passwords-reset + +``` + admin@sonic:~$ show local-users-passwords-reset + Usage: show local-users-passwords-reset + Show local-users-passwords-reset + Options: + -h, -?, --help Show this message and exit. + ``` + ``` + admin@sonic:~$ show local-users-passwords-reset + state + ------- + enabled + ``` \ No newline at end of file diff --git a/show/main.py b/show/main.py index 077547fa6a..a4357f30f8 100755 --- a/show/main.py +++ b/show/main.py @@ -2254,6 +2254,26 @@ def received(db, namespace): ctx.fail("ASIC/SDK health event is not supported on the platform") +# +# 'local-users-passwords-reset' command group ("show local-users-passwords-reset ...") +# +@cli.command('local-users-passwords-reset') +@clicommon.pass_db +def local_users_passwords_reset(db): + """Show local-users-passwords-reset state""" + + feature_table = db.cfgdb.get_entry('LOCAL_USERS_PASSWORDS_RESET', 'global') + + hdrs = ['state'] + data = [] + + for key in hdrs: + data.append(feature_table.get(key, '')) + + messages = [data] + click.echo(tabulate(messages, headers=hdrs, tablefmt='simple', missingval='')) + + # Load plugins and register them helper = util_base.UtilHelper() helper.load_and_register_plugins(plugins, cli) diff --git a/tests/local_users_passwords_reset_test.py b/tests/local_users_passwords_reset_test.py new file mode 100644 index 0000000000..1c178067ce --- /dev/null +++ b/tests/local_users_passwords_reset_test.py @@ -0,0 +1,34 @@ +from click.testing import CliRunner + +import config.main as config +import show.main as show +from utilities_common.db import Db + + +class TestLocalUsersPasswordsReset: + def test_config_command(self): + runner = CliRunner() + + db = Db() + + result = runner.invoke(config.config.commands['local-users-passwords-reset'], ['enabled'], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry('LOCAL_USERS_PASSWORDS_RESET', 'global')['state'] == 'enabled' + + result = runner.invoke(show.cli.commands['local-users-passwords-reset'], obj=db) + assert result.exit_code == 0 + assert 'enabled' in result.output + + result = runner.invoke(config.config.commands['local-users-passwords-reset'], ['disabled'], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry('LOCAL_USERS_PASSWORDS_RESET', 'global')['state'] == 'disabled' + + result = runner.invoke(show.cli.commands['local-users-passwords-reset'], obj=db) + assert result.exit_code == 0 + assert 'disabled' in result.output + + result = runner.invoke(config.config.commands['local-users-passwords-reset'], ['invalid-input'], obj=db) + print(result.output) + assert result.exit_code != 0