Skip to content

Commit

Permalink
Move password helper names to be namespaced with vendor information. …
Browse files Browse the repository at this point in the history
…Assign new name to old name to not break semver. (networktocode#286)
  • Loading branch information
itdependsnetworks authored Jun 9, 2023
1 parent 8a878c8 commit 92c041c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 91 deletions.
20 changes: 10 additions & 10 deletions docs/dev/attribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ Influencers

In many instances variables and function names were reused, but the code was built from scratch to avoid any potential licensing issues. Functions that were known to be rewritten and their known origin.

| Function | Origin |
| ---------------- | ------- |
| asn_to_int | NAPALM |
| is_ip | IPCal |
| ip_to_bin | IPCal |
| get_usable_range | IPCal |
| encrypt_type7 | unknown |
| decrypt_type7 | unknown |
| vlan_to_list | Ansible |
| sanitize_config | NAPALM |
| Function | Origin |
| ------------------- | ------- |
| asn_to_int | NAPALM |
| is_ip | IPCal |
| ip_to_bin | IPCal |
| get_usable_range | IPCal |
| encrypt_cisco_type7 | unknown |
| decrypt_cisco_type7 | unknown |
| vlan_to_list | Ansible |
| sanitize_config | NAPALM |

Relevant PR's

Expand Down
13 changes: 9 additions & 4 deletions docs/user/include_jinja_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@
| mac_to_int | netutils.mac.mac_to_int |
| mac_type | netutils.mac.mac_type |
| get_upgrade_path | netutils.os_version.get_upgrade_path |
| compare_cisco_type5 | netutils.password.compare_cisco_type5 |
| compare_cisco_type7 | netutils.password.compare_cisco_type7 |
| compare_cisco_type9 | netutils.password.compare_cisco_type9 |
| compare_type5 | netutils.password.compare_type5 |
| compare_type7 | netutils.password.compare_type7 |
| compare_type9 | netutils.password.compare_type9 |
| decrypt_juniper | netutils.password.decrypt_juniper |
| decrypt_cisco_type7 | netutils.password.decrypt_cisco_type7 |
| decrypt_juniper_type9 | netutils.password.decrypt_juniper_type9 |
| decrypt_type7 | netutils.password.decrypt_type7 |
| encrypt_juniper | netutils.password.encrypt_juniper |
| encrypt_cisco_type5 | netutils.password.encrypt_cisco_type5 |
| encrypt_cisco_type7 | netutils.password.encrypt_cisco_type7 |
| encrypt_cisco_type9 | netutils.password.encrypt_cisco_type9 |
| encrypt_juniper_type9 | netutils.password.encrypt_juniper_type9 |
| encrypt_type5 | netutils.password.encrypt_type5 |
| encrypt_type7 | netutils.password.encrypt_type7 |
| encrypt_type9 | netutils.password.encrypt_type9 |
| get_hash_salt | netutils.password.get_hash_salt |
| tcp_ping | netutils.ping.tcp_ping |
| longest_prefix_match | netutils.route.longest_prefix_match |
Expand Down
6 changes: 3 additions & 3 deletions docs/user/lib_use_cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ The following function will help in deploying list of VLANs and match the config
You may want to compare a known password with a given encrypted password. This can help in verifying if the passwords are as expected for compliance reasons.

```python
>>> from netutils.password import compare_type5
>>> from netutils.password import compare_cisco_type5
>>>
>>> compare_type5("cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
>>> compare_cisco_type5("cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
True
>>>
>>> compare_type5("not_cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
>>> compare_cisco_type5("not_cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
False
>>>
```
Expand Down
76 changes: 42 additions & 34 deletions netutils/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def decorated(*args: t.Any, **kwargs: t.Any) -> t.Any:
return decorated


def compare_type5(
def compare_cisco_type5(
unencrypted_password: str, encrypted_password: str, return_original: bool = False
) -> t.Union[str, bool]:
"""Given an encrypted and unencrypted password of Cisco Type 5 password, compare if they are a match.
Expand All @@ -126,22 +126,22 @@ def compare_type5(
Whether or not the password is as compared to.
Examples:
>>> from netutils.password import compare_type5
>>> compare_type5("cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
>>> from netutils.password import compare_cisco_type5
>>> compare_cisco_type5("cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
True
>>> compare_type5("not_cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
>>> compare_cisco_type5("not_cisco","$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.")
False
>>>
"""
salt = get_hash_salt(encrypted_password)
if encrypt_type5(unencrypted_password, salt) == encrypted_password:
if encrypt_cisco_type5(unencrypted_password, salt) == encrypted_password:
if return_original is True:
return encrypted_password
return True
return False


def compare_type7(
def compare_cisco_type7(
unencrypted_password: str, encrypted_password: str, return_original: bool = False
) -> t.Union[str, bool]:
"""Given an encrypted and unencrypted password of Cisco Type 7 password, compare if they are a match.
Expand All @@ -155,24 +155,24 @@ def compare_type7(
Whether or not the password is as compared to.
Examples:
>>> from netutils.password import compare_type7
>>> compare_type7("cisco","121A0C041104")
>>> from netutils.password import compare_cisco_type7
>>> compare_cisco_type7("cisco","121A0C041104")
True
>>> compare_type7("not_cisco","121A0C041104")
>>> compare_cisco_type7("not_cisco","121A0C041104")
False
>>>
"""
if decrypt_type7(encrypted_password) == unencrypted_password:
if decrypt_cisco_type7(encrypted_password) == unencrypted_password:
if return_original is True:
return encrypted_password
return True
return False


def compare_type9(
def compare_cisco_type9(
unencrypted_password: str, encrypted_password: str, return_original: bool = False
) -> t.Union[str, bool]:
"""Given an encrypted and unencrypted password of Cisco Type 7 password, compare if they are a match.
"""Given an encrypted and unencrypted password of Cisco Type 9 password, compare if they are a match.
Args:
unencrypted_password: A password that has not been encrypted, and will be compared against.
Expand All @@ -183,22 +183,22 @@ def compare_type9(
Whether or not the password is as compared to.
Examples:
>>> from netutils.password import compare_type9
>>> compare_type9("cisco","$9$588|P!iWqEx=Wf$nadLmT9snc6V9QAeUuATSOoCAZMQIHqixJfZpQj5EU2")
>>> from netutils.password import compare_cisco_type9
>>> compare_cisco_type9("cisco","$9$588|P!iWqEx=Wf$nadLmT9snc6V9QAeUuATSOoCAZMQIHqixJfZpQj5EU2")
True
>>> compare_type9("not_cisco","$9$588|P!iWqEx=Wf$nadLmT9snc6V9QAeUuATSOoCAZMQIHqixJfZpQj5EU2")
>>> compare_cisco_type9("not_cisco","$9$588|P!iWqEx=Wf$nadLmT9snc6V9QAeUuATSOoCAZMQIHqixJfZpQj5EU2")
False
>>>
"""
salt = get_hash_salt(encrypted_password)
if encrypt_type9(unencrypted_password, salt) == encrypted_password:
if encrypt_cisco_type9(unencrypted_password, salt) == encrypted_password:
if return_original is True:
return encrypted_password
return True
return False


def decrypt_type7(encrypted_password: str) -> str:
def decrypt_cisco_type7(encrypted_password: str) -> str:
"""Given an unencrypted password of Cisco Type 7 password decrypt it.
Args:
Expand All @@ -208,8 +208,8 @@ def decrypt_type7(encrypted_password: str) -> str:
The unencrypted_password password.
Examples:
>>> from netutils.password import decrypt_type7
>>> decrypt_type7("121A0C041104")
>>> from netutils.password import decrypt_cisco_type7
>>> decrypt_cisco_type7("121A0C041104")
'cisco'
>>>
"""
Expand All @@ -229,7 +229,7 @@ def decrypt_type7(encrypted_password: str) -> str:


@_fail_on_mac
def encrypt_type5(unencrypted_password: str, salt: t.Optional[str] = None, salt_len: int = 4) -> str:
def encrypt_cisco_type5(unencrypted_password: str, salt: t.Optional[str] = None, salt_len: int = 4) -> str:
"""Given an unencrypted password of Cisco Type 5 password, encrypt it.
Args:
Expand All @@ -241,8 +241,8 @@ def encrypt_type5(unencrypted_password: str, salt: t.Optional[str] = None, salt_
The encrypted password.
Examples:
>>> from netutils.password import encrypt_type5
>>> encrypt_type5("cisco") # doctest: +SKIP
>>> from netutils.password import encrypt_cisco_type5
>>> encrypt_cisco_type5("cisco") # doctest: +SKIP
'$1$MHkb$v2MFmDkQX66TTxLkFF50K/'
>>>
"""
Expand All @@ -253,7 +253,7 @@ def encrypt_type5(unencrypted_password: str, salt: t.Optional[str] = None, salt_
return crypt.crypt(unencrypted_password, f"$1${salt}$")


def encrypt_type7(unencrypted_password: str, salt: t.Optional[int] = None) -> str:
def encrypt_cisco_type7(unencrypted_password: str, salt: t.Optional[int] = None) -> str:
"""Given an unencrypted password of Cisco Type 7 password, encypt it.
Args:
Expand All @@ -264,8 +264,8 @@ def encrypt_type7(unencrypted_password: str, salt: t.Optional[int] = None) -> st
The encrypted password.
Examples:
>>> from netutils.password import encrypt_type7
>>> encrypt_type7("cisco", 11)
>>> from netutils.password import encrypt_cisco_type7
>>> encrypt_cisco_type7("cisco", 11)
'110A1016141D'
>>>
"""
Expand All @@ -290,7 +290,7 @@ def encrypt_type7(unencrypted_password: str, salt: t.Optional[int] = None) -> st
return encrypted_password


def encrypt_type9(unencrypted_password: str, salt: t.Optional[str] = None) -> str:
def encrypt_cisco_type9(unencrypted_password: str, salt: t.Optional[str] = None) -> str:
"""Given an unencrypted password of Cisco Type 9 password, encrypt it.
Note: This uses the built-in Python `scrypt` function to generate the password
Expand All @@ -306,8 +306,8 @@ def encrypt_type9(unencrypted_password: str, salt: t.Optional[str] = None) -> st
The encrypted password.
Examples:
>>> from netutils.password import encrypt_type9
>>> encrypt_type9("123456", "cvWdfQlRRDKq/U")
>>> from netutils.password import encrypt_cisco_type9
>>> encrypt_cisco_type9("123456", "cvWdfQlRRDKq/U")
'$9$cvWdfQlRRDKq/U$VFTPha5VHTCbSgSUAo.nPoh50ZiXOw1zmljEjXkaq1g'
Raises:
Expand Down Expand Up @@ -364,7 +364,7 @@ def get_hash_salt(encrypted_password: str) -> str:
return split_password[2]


def decrypt_juniper(encrypted_password: str) -> str:
def decrypt_juniper_type9(encrypted_password: str) -> str:
"""Given an encrypted Junos $9$ type password, decrypt it.
Args:
Expand All @@ -374,8 +374,8 @@ def decrypt_juniper(encrypted_password: str) -> str:
The unencrypted_password password.
Examples:
>>> from netutils.password import decrypt_juniper
>>> decrypt_juniper("$9$7YdwgGDkTz6oJz69A1INdb")
>>> from netutils.password import decrypt_juniper_type9
>>> decrypt_juniper_type9("$9$7YdwgGDkTz6oJz69A1INdb")
'juniper'
>>>
"""
Expand Down Expand Up @@ -409,7 +409,7 @@ def decrypt_juniper(encrypted_password: str) -> str:
return decrypted_password


def encrypt_juniper(unencrypted_password: str, salt: t.Optional[int] = None) -> str:
def encrypt_juniper_type9(unencrypted_password: str, salt: t.Optional[int] = None) -> str:
"""Given an unencrypted password, encrypt to Juniper $9$ type password.
Args:
Expand All @@ -420,8 +420,8 @@ def encrypt_juniper(unencrypted_password: str, salt: t.Optional[int] = None) ->
The encrypted password.
Examples:
>>> from netutils.password import encrypt_juniper
>>> encrypt_juniper("juniper", 35) # doctest: +SKIP
>>> from netutils.password import encrypt_juniper_type9
>>> encrypt_juniper_type9("juniper", 35) # doctest: +SKIP
'$9$7YdwgGDkTz6oJz69A1INdb'
>>>
"""
Expand Down Expand Up @@ -454,3 +454,11 @@ def encrypt_juniper(unencrypted_password: str, salt: t.Optional[int] = None) ->
encrypted_password += new_character

return encrypted_password


# Provide until transition to 2.0
compare_type5 = compare_cisco_type5
compare_type7 = compare_cisco_type7
decrypt_type7 = decrypt_cisco_type7
encrypt_type5 = encrypt_cisco_type5
encrypt_type7 = encrypt_cisco_type7
13 changes: 9 additions & 4 deletions netutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@
"get_oui": "mac.get_oui",
"compare_type5": "password.compare_type5",
"compare_type7": "password.compare_type7",
"compare_type9": "password.compare_type9",
"compare_cisco_type5": "password.compare_cisco_type5",
"compare_cisco_type7": "password.compare_cisco_type7",
"compare_cisco_type9": "password.compare_cisco_type9",
"decrypt_type7": "password.decrypt_type7",
"decrypt_cisco_type7": "password.decrypt_cisco_type7",
"decrypt_juniper_type9": "password.decrypt_juniper_type9",
"encrypt_type5": "password.encrypt_type5",
"encrypt_type7": "password.encrypt_type7",
"encrypt_type9": "password.encrypt_type9",
"encrypt_cisco_type5": "password.encrypt_cisco_type5",
"encrypt_cisco_type7": "password.encrypt_cisco_type7",
"encrypt_cisco_type9": "password.encrypt_cisco_type9",
"encrypt_juniper_type9": "password.encrypt_juniper_type9",
"get_hash_salt": "password.get_hash_salt",
"encrypt_juniper": "password.encrypt_juniper",
"decrypt_juniper": "password.decrypt_juniper",
"tcp_ping": "ping.tcp_ping",
"longest_prefix_match": "route.longest_prefix_match",
"vlanlist_to_config": "vlan.vlanlist_to_config",
Expand Down
Loading

0 comments on commit 92c041c

Please sign in to comment.