Skip to content

Commit

Permalink
Adding a utility to generate the encrypted data
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Oct 6, 2024
1 parent d981771 commit 2f0642e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 43 deletions.
42 changes: 0 additions & 42 deletions netmiko/cli_tools/encr_test.py

This file was deleted.

61 changes: 61 additions & 0 deletions netmiko/cli_tools/encrypt_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
import os
import argparse
from getpass import getpass

from netmiko.utilities import load_netmiko_yml
from netmiko.encryption_handling import encrypt_value


def main():
parser = argparse.ArgumentParser(
description="Encrypt data using Netmiko's encryption."
)
parser.add_argument("data", help="The data to encrypt", nargs='?')
parser.add_argument(
"--key",
help="The encryption key (if not provided, will use NETMIKO_TOOLS_KEY env variable)",
)
parser.add_argument(
"--type",
choices=["fernet", "aes128"],
help="Encryption type (if not provided, will read from .netmiko.yml)",
)

args = parser.parse_args()

if args.data:
data = args.data
else:
data = getpass("Enter the data to encrypt: ")

# Get encryption key
if args.key:
key = args.key.encode()
else:
key = os.environ.get("NETMIKO_TOOLS_KEY")
if not key:
msg = """Encryption key not provided.
Use --key or set NETMIKO_TOOLS_KEY environment variable."""
raise ValueError(msg)
key = key.encode()

# Get encryption type
if args.type:
encryption_type = args.type
else:
config_params, my_devices = load_netmiko_yml()
encryption_type = config_params.get("encryption_type", "fernet")

if not encryption_type:
msg = """Encryption type not provided.
Use --type or set 'encryption_type' in .netmiko.yml in the '__meta__' section."""
raise ValueError(msg)

# Encrypt the password
encrypted_data = encrypt_value(data, key, encryption_type)
print(f"\nEncrypted data: {encrypted_data}\n")


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion netmiko/cli_tools/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from netmiko import ConnectHandler
from netmiko.utilities import obtain_all_devices, load_netmiko_yml
from netmiko.encrypt_handling import decrypt_config, get_encryption_key
from netmiko.encryption_handling import decrypt_config, get_encryption_key
from netmiko.cli_tools import ERROR_PATTERN


Expand Down
1 change: 1 addition & 0 deletions netmiko/cli_tools/netmiko_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

COMMAND = "netmiko-show"

# FIX: --list-devices currently fails due to missing 'device/group'

def main_ep():
sys.exit(main(sys.argv[1:]))
Expand Down
File renamed without changes.

0 comments on commit 2f0642e

Please sign in to comment.