-
Notifications
You must be signed in to change notification settings - Fork 759
kdump-Remote-SSH-Configurations #3400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8e1a4b4
3893fc9
03c2b63
6f34f88
18caaba
7d7226c
49b511a
a67a777
2094bc7
7ae0635
518837e
d74f909
17e0790
999d298
361bb75
48eb48d
194f333
45fe33e
8c97669
2453d25
cfbdd13
1b7091b
82f8000
7efce5d
12f0279
3c22c75
6d2f7ce
f4856c7
2d0cacb
c1f2076
d3e975a
18895d3
4fe15b1
8588324
65a07b0
247cb24
edd60cf
c9d4b83
146c2c8
668a357
16927d0
301b953
d32c898
26530b9
fb3c04c
557d2da
88f72c5
d52d4fd
871bc9b
1a94b1e
e6af5ca
c5b1917
eb35995
8d20e9c
af76fe1
efbfa46
8eaf182
bd1d2f1
657d913
9a95762
91c24c2
adda82e
6810b25
ac0b0e6
82f94b9
486a486
f709f03
ebaefc0
bc04bb7
6f9c74f
1bbf78a
98f3e14
bc87384
617523b
d8fe5b8
a5c5f34
318f658
53a3530
dd13c18
9f21cf6
4e66ca1
2400b33
91763ef
2ea0e24
76eb141
1eec069
ba6af36
5eedadd
3e9b1a3
71a42f7
b73360e
51648ac
0c87f96
f05ce6b
f252fb8
7f96d23
1194a45
f3e8098
9e657d8
8d760b1
35ced45
5aaab85
b8eb084
9144810
41a3a41
50847d1
b928199
2510c85
886d21c
ec1cc1a
036257f
48d1cae
d1c5543
c620c18
b30391d
05c420f
fcc77a7
ef95754
06ccd35
d8ac24c
8401e77
adc256a
b1f9379
5527452
bf91f71
368328b
b3b5d18
46fb9d5
94d2609
5f91081
3604182
e5ccfcb
18809cb
762a163
b231c11
ba8172a
60c067b
e05c85d
a2c829d
6549fe1
ab53019
5a6cf43
9912125
f68fa48
e5a93ac
0343e5b
2dede5b
3fdee51
3bda604
d7efd0d
a966efa
cc6c765
3970ec8
a6ec1ce
3951942
1d64ffc
5741922
6abe2f9
eebdd79
e93041a
5bdc5c9
077d336
ea02048
e6968ae
a1d4077
d8fb143
2db858c
d9c8f39
41c0be3
00e7c5f
623f822
e5357c8
6f1d7fc
89cc594
bf6102d
2a7595b
88dd4f0
9fae7d0
96ecc6d
826bb11
9f18a6a
3bc371f
4109593
d897d79
6670418
d1b6c32
dbafbac
ef1252d
e4d24c5
eaf3efa
b2911e2
b57b1ed
2898e78
49b674e
72cbd6a
db43785
7f1f2f4
0ea1d54
e1f2c7a
8f70056
57982a0
3200dea
956c0dc
1f83d6a
eb1a597
bc212a5
5a55046
43d9caf
5cf9b60
90cf8f1
98384cf
9f224a1
3279763
87db7f6
5a701d2
f255133
3bbf3f8
4ff72b0
6b0fa30
ba80960
f6ff197
7646f4b
322e80a
701d3bf
cd0042f
ba9bb0f
e6b1efb
7eaf2ef
50a1053
ea5ee5b
f839e1c
8628355
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,18 @@ | ||
| import sys | ||
|
|
||
| import click | ||
| import os | ||
| from utilities_common.cli import AbbreviationGroup, pass_db | ||
|
|
||
|
|
||
| from ipaddress import ip_address, AddressValueError | ||
| import re | ||
| # | ||
| # 'kdump' group ('sudo config kdump ...') | ||
| # | ||
|
|
||
| @click.group(cls=AbbreviationGroup, name="kdump") | ||
| def kdump(): | ||
| """Configure the KDUMP mechanism""" | ||
| pass | ||
|
|
||
|
|
||
| def check_kdump_table_existence(kdump_table): | ||
| """Checks whether the 'KDUMP' table is configured in Config DB. | ||
|
|
@@ -32,40 +32,46 @@ def check_kdump_table_existence(kdump_table): | |
| sys.exit(2) | ||
|
|
||
|
|
||
| def echo_reboot_warning(): | ||
| """Prints the warning message about reboot requirements.""" | ||
| click.echo("KDUMP configuration changes may require a reboot to take effect.") | ||
| click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") | ||
| # | ||
| # 'disable' command ('sudo config kdump disable') | ||
| # | ||
| @kdump.command(name="disable", short_help="Disable the KDUMP mechanism") | ||
|
|
||
|
|
||
| @kdump.command(name="disable", help="Disable the KDUMP mechanism") | ||
| @pass_db | ||
| def kdump_disable(db): | ||
| """Disable the KDUMP mechanism""" | ||
| kdump_table = db.cfgdb.get_table("KDUMP") | ||
| check_kdump_table_existence(kdump_table) | ||
|
|
||
| db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "false"}) | ||
| click.echo("KDUMP configuration changes may require a reboot to take effect.") | ||
| click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") | ||
|
|
||
| echo_reboot_warning() | ||
|
|
||
| # | ||
| # 'enable' command ('sudo config kdump enable') | ||
| # | ||
| @kdump.command(name="enable", short_help="Enable the KDUMP mechanism") | ||
|
|
||
|
|
||
| @kdump.command(name="enable", help="Enable the KDUMP mechanism") | ||
| @pass_db | ||
| def kdump_enable(db): | ||
| """Enable the KDUMP mechanism""" | ||
| kdump_table = db.cfgdb.get_table("KDUMP") | ||
| check_kdump_table_existence(kdump_table) | ||
|
|
||
| db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "true"}) | ||
| click.echo("KDUMP configuration changes may require a reboot to take effect.") | ||
| click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") | ||
|
|
||
| echo_reboot_warning() | ||
|
|
||
| # | ||
| # 'memory' command ('sudo config kdump memory ...') | ||
| # | ||
| @kdump.command(name="memory", short_help="Configure the memory for KDUMP mechanism") | ||
|
|
||
|
|
||
| @kdump.command(name="memory", help="Configure the memory for KDUMP mechanism") | ||
| @click.argument('kdump_memory', metavar='<kdump_memory>', required=True) | ||
| @pass_db | ||
| def kdump_memory(db, kdump_memory): | ||
|
|
@@ -74,14 +80,14 @@ def kdump_memory(db, kdump_memory): | |
| check_kdump_table_existence(kdump_table) | ||
|
|
||
| db.cfgdb.mod_entry("KDUMP", "config", {"memory": kdump_memory}) | ||
| click.echo("KDUMP configuration changes may require a reboot to take effect.") | ||
| click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") | ||
|
|
||
| echo_reboot_warning() | ||
|
|
||
| # | ||
| # 'num_dumps' command ('sudo config keump num_dumps ...') | ||
| # 'num_dumps' command ('sudo config kdump num_dumps ...') | ||
| # | ||
| @kdump.command(name="num_dumps", short_help="Configure the maximum dump files of KDUMP mechanism") | ||
|
|
||
|
|
||
| @kdump.command(name="num_dumps", help="Configure the maximum dump files of KDUMP mechanism") | ||
| @click.argument('kdump_num_dumps', metavar='<kdump_num_dumps>', required=True, type=int) | ||
| @pass_db | ||
| def kdump_num_dumps(db, kdump_num_dumps): | ||
|
|
@@ -90,3 +96,160 @@ def kdump_num_dumps(db, kdump_num_dumps): | |
| check_kdump_table_existence(kdump_table) | ||
|
|
||
| db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) | ||
| echo_reboot_warning() | ||
|
|
||
|
|
||
| @kdump.command(name="remote", help="Configure the remote enable/disable for KDUMP mechanism") | ||
| @click.argument('action', metavar='<enable/disable>', required=True, type=click.STRING) # Corrected this line | ||
| @pass_db | ||
| def remote(db, action): | ||
| """Enable or disable remote kdump feature""" | ||
| kdump_table = db.cfgdb.get_table("KDUMP") | ||
| check_kdump_table_existence(kdump_table) | ||
|
|
||
| # Get the current status of the remote feature as string | ||
| current_status = kdump_table["config"].get("remote", "false").lower() | ||
|
|
||
| if action.lower() == 'enable': | ||
| if current_status == "true": | ||
| click.echo("Remote kdump feature is already enabled.") | ||
| else: | ||
| db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) | ||
| click.echo("Remote kdump feature enabled.") | ||
| echo_reboot_warning() | ||
| elif action.lower() == 'disable': | ||
| if current_status == "false": | ||
| click.echo("Remote kdump feature is already disabled.") | ||
| else: | ||
| db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) | ||
| click.echo("Remote kdump feature disabled.") | ||
| echo_reboot_warning() | ||
| else: | ||
| click.echo("Invalid action. Use 'enable' or 'disable'.") | ||
|
|
||
|
|
||
| @kdump.group(name="add", help="Add configuration items to KDUMP") | ||
| def add(): | ||
| """Group of commands to add configuration items to KDUMP""" | ||
| pass | ||
|
|
||
|
|
||
| @add.command(name="ssh_string", help="Add an SSH string to the KDUMP configuration") | ||
| @click.argument('ssh_string', metavar='<ssh_key>', required=True) | ||
| @pass_db | ||
| def add_ssh_key(db, ssh_string): | ||
| """Add an SSH string to KDUMP configuration""" | ||
|
|
||
| def is_valid_ssh_key(ssh_string): | ||
| """Validate the SSH key format""" | ||
| # Check if it contains username and hostname/IP (format: username@host) | ||
| if "@" not in ssh_string: | ||
| return "Invalid format. SSH key must be in 'username@host' format." | ||
|
|
||
| username, host = ssh_string.split("@", 1) | ||
|
|
||
| # Validate username | ||
| if not username or not username.isalnum(): | ||
| return "Invalid username. Ensure it contains only alphanumeric characters." | ||
|
|
||
| # Validate host (IP or hostname) | ||
| try: | ||
| # Check if it's a valid IP address | ||
| ip_address(host) | ||
| except AddressValueError: | ||
| # If not an IP, validate hostname | ||
| hostname_regex = r'^[a-zA-Z0-9.-]+$' | ||
| if not re.match(hostname_regex, host) or host.startswith('-') or host.endswith('-'): | ||
| return "Invalid host. Must be a valid IP or hostname." | ||
|
|
||
| return None # Validation successful | ||
|
|
||
| kdump_table = db.cfgdb.get_table("KDUMP") | ||
| check_kdump_table_existence(kdump_table) | ||
| current_status = kdump_table["config"].get("remote", "false").lower() | ||
|
|
||
| if current_status == 'false': | ||
| click.echo("Remote feature is not enabled. Please enable the remote feature first.") | ||
| return | ||
|
|
||
| # Validate SSH key | ||
| validation_error = is_valid_ssh_key(ssh_string) | ||
| if validation_error: | ||
| click.echo(f"Error: {validation_error}") | ||
| return | ||
|
|
||
| # Add or update the 'ssh_key' entry in the KDUMP table | ||
| db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ssh_string}) | ||
| click.echo(f"SSH string added to KDUMP configuration: {ssh_string}") | ||
|
|
||
|
|
||
| @add.command(name="ssh_path", help="Add an SSH path to the KDUMP configuration") | ||
| @click.argument('ssh_path', metavar='<ssh_path>', required=True) | ||
| @pass_db | ||
| def add_ssh_path(db, ssh_path): | ||
| """Add an SSH path to KDUMP configuration""" | ||
|
|
||
| def is_valid_ssh_path(ssh_path): | ||
| """Validate the SSH path""" | ||
| # Check if the path is absolute | ||
| if not os.path.isabs(ssh_path): | ||
| return "Invalid path. SSH path must be an absolute path." | ||
|
|
||
| # (Optional) Check if the path exists on the system | ||
| if not os.path.exists(ssh_path): | ||
| return f"Invalid path. The path '{ssh_path}' does not exist." | ||
|
|
||
| return None # Validation successful | ||
|
|
||
| kdump_table = db.cfgdb.get_table("KDUMP") | ||
| check_kdump_table_existence(kdump_table) | ||
| current_status = kdump_table["config"].get("remote", "false").lower() | ||
| if current_status == 'false': | ||
| click.echo("Remote feature is not enabled. Please enable the remote feature first.") | ||
| return | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No validation for SSH path?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The validation for the SSH path has been implemented in the code. The necessary checks are now in place to ensure the SSH key is properly validated before proceeding with the operation |
||
| # Validate SSH path | ||
| validation_error = is_valid_ssh_path(ssh_path) | ||
| if validation_error: | ||
| click.echo(f"Error: {validation_error}") | ||
| return | ||
|
|
||
| # Add or update the 'ssh_path' entry in the KDUMP table | ||
| db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": ssh_path}) | ||
| click.echo(f"SSH path added to KDUMP configuration: {ssh_path}") | ||
|
|
||
|
|
||
| @kdump.group(name="remove", help="remove configuration items to KDUMP") | ||
| def remove(): | ||
| """Group of commands to remove configuration items to KDUMP""" | ||
| pass | ||
|
|
||
|
|
||
| @remove.command(name="ssh_string", help="Remove the SSH string from the KDUMP configuration") | ||
| @pass_db | ||
| def remove_ssh_string(db): | ||
| """Remove the SSH string from KDUMP configuration""" | ||
| kdump_table = db.cfgdb.get_table("KDUMP") | ||
| check_kdump_table_existence(kdump_table) | ||
|
|
||
| # Check if ssh_string exists | ||
| if "ssh_string" in kdump_table["config"]: | ||
| db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": None}) | ||
| click.echo("SSH string removed from KDUMP configuration.") | ||
| else: | ||
| click.echo("SSH string not found in KDUMP configuration.") | ||
|
|
||
|
|
||
| @remove.command(name="ssh_path", help="Remove the SSH path from the KDUMP configuration") | ||
| @pass_db | ||
| def remove_ssh_path(db): | ||
| """Remove the SSH path from KDUMP configuration""" | ||
| kdump_table = db.cfgdb.get_table("KDUMP") | ||
| check_kdump_table_existence(kdump_table) | ||
|
|
||
| # Check if ssh_string exists | ||
| if "ssh_path" in kdump_table["config"]: | ||
| db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": None}) | ||
| click.echo("SSH path removed from KDUMP configuration.") | ||
| else: | ||
| click.echo("SSH path not found in KDUMP configuration.") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No validation for SSH key?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validation for the SSH key has been implemented in the code. The necessary checks are now in place to ensure the SSH key is properly validated before proceeding with the operation