Skip to content

Commit ded29ee

Browse files
committed
Bulk encryption solution for .netmiko.yml
1 parent 95fbbdc commit ded29ee

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

netmiko/cli_tools/bulk_encrypt.py renamed to netmiko/cli_tools/netmiko_bulk_encrypt.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
#!/usr/bin/env python3
22
import argparse
3-
import yaml
43
import sys
54
from pathlib import Path
5+
from ruamel.yaml import YAML
66

77
from netmiko.encryption_handling import encrypt_value, get_encryption_key
88

9+
yaml = YAML()
10+
yaml.preserve_quotes = True
11+
yaml.indent(mapping=2, sequence=4, offset=2)
12+
913

1014
def encrypt_netmiko_yml(
1115
input_file: str, output_file: str | None, encryption_type: str
1216
) -> None:
1317
# Read the input YAML file
1418
input_path = Path(input_file).expanduser()
1519
with input_path.open("r") as f:
16-
config = yaml.safe_load(f)
20+
config = yaml.load(f)
1721

1822
# Get the encryption key
1923
key = get_encryption_key()
@@ -27,7 +31,7 @@ def encrypt_netmiko_yml(
2731
)
2832
params["password"] = encrypted_value
2933
if "secret" in params:
30-
# Use the same encrypted value for secret if it's identical to password
34+
encrypted_value = encrypt_value(params["secret"], key, encryption_type)
3135
params["secret"] = encrypted_value
3236

3337
# Write the updated config to the output file or stdout
@@ -39,6 +43,10 @@ def encrypt_netmiko_yml(
3943
yaml.dump(config, sys.stdout)
4044

4145

46+
def main_ep():
47+
sys.exit(main())
48+
49+
4250
def main():
4351
parser = argparse.ArgumentParser(
4452
description="Encrypt passwords in .netmiko.yml file"
@@ -69,6 +77,8 @@ def main():
6977
file=sys.stderr,
7078
)
7179

80+
return 0
81+
7282

7383
if __name__ == "__main__":
74-
main()
84+
sys.exit(main())

netmiko/cli_tools/netmiko_encrypt.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#!/usr/bin/env python3
22
import os
3+
import sys
34
import argparse
45
from getpass import getpass
56

67
from netmiko.utilities import load_netmiko_yml
78
from netmiko.encryption_handling import encrypt_value
89

910

11+
def main_ep():
12+
sys.exit(main())
13+
14+
1015
def main():
1116
parser = argparse.ArgumentParser(
1217
description="Encrypt data using Netmiko's encryption."
@@ -56,6 +61,8 @@ def main():
5661
encrypted_data = encrypt_value(data, key, encryption_type)
5762
print(f"\nEncrypted data: {encrypted_data}\n")
5863

64+
return 0
65+
5966

6067
if __name__ == "__main__":
61-
main()
68+
sys.exit(main())

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ python = ">=3.9,<4.0"
2424
setuptools = ">=65.0.0"
2525
paramiko = ">=2.9.5"
2626
scp = ">=0.13.6"
27-
pyyaml = ">=5.3"
27+
pyyaml = ">=6.0.2"
2828
textfsm = ">=1.1.3"
2929
ntc-templates = ">=3.1.0"
3030
pyserial = ">=3.3"
3131
cffi = ">=1.17.0rc1"
3232
rich = ">=13.8"
33+
ruamel.yaml = ">=0.17"
3334

3435
[tool.poetry.group.dev.dependencies]
3536
black = "24.8.0"
3637
mypy = "1.11.2"
3738
mypy-extensions = "1.0.0"
38-
PyYAML = "6.0.2"
3939
pytest = "8.3.3"
4040
pyflakes = "3.2.0"
4141
pylama = "8.4.1"
@@ -58,6 +58,8 @@ ttp = ">=0.9.5"
5858
"netmiko-grep" = "netmiko.cli_tools.netmiko_grep:main_ep"
5959
"netmiko-show" = "netmiko.cli_tools.netmiko_show:main_ep"
6060
"netmiko-cfg" = "netmiko.cli_tools.netmiko_cfg:main_ep"
61+
"netmiko-encrypt" = "netmiko.cli_tools.netmiko_encrypt:main_ep"
62+
"netmiko-bulk-encrypt" = "netmiko.cli_tools.netmiko_bulk_encrypt:main_ep"
6163

6264
[tool.black]
6365
exclude = '''

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ ignore_errors = True
5858

5959
[mypy-netmiko.cli_tools.netmiko_encrypt]
6060
ignore_errors = True
61+
62+
[mypy-netmiko.cli_tools.netmiko_bulk_encrypt]
63+
ignore_errors = True

0 commit comments

Comments
 (0)