-
Notifications
You must be signed in to change notification settings - Fork 664
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
TACACSPLUS_PASSKEY_ENCRYPTION support Part - I #3027
Open
nmoray
wants to merge
9
commits into
sonic-net:master
Choose a base branch
from
nmoray:tacacs_encrypt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9dc424c
TACACSPLUS_PASSKEY_ENCRYPTION support
nmoray 256cb7b
Removed debug prints
nmoray b710e7b
Addressed comments
nmoray bccbe6a
Addressed comments
nmoray 084e5a8
Incorporated security_cipher class into the implementation
nmoray 98648ae
Added an option to set key_encrypt flag under TACPLUS table in CONFIG…
nmoray f173660
Added a logic to remove the cipher_pass file when encryption is disabled
nmoray 485eb0e
Added try catch block to catch the getpass() abort
nmoray 1100d58
Updated class name
nmoray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import click | ||
import ipaddress | ||
import re | ||
import subprocess | ||
from swsscommon.swsscommon import ConfigDBConnector | ||
from .validated_config_db_connector import ValidatedConfigDBConnector | ||
from jsonpatch import JsonPatchConflict | ||
|
@@ -11,6 +12,41 @@ | |
RADIUS_MAXSERVERS = 8 | ||
RADIUS_PASSKEY_MAX_LEN = 65 | ||
VALID_CHARS_MSG = "Valid chars are ASCII printable except SPACE, '#', and ','" | ||
TACACS_PASSKEY_MAX_LEN = 65 | ||
TACACS_SECRET_SALT = "2e6593364d369fba925092e0c1c51466c276faa127f20d18cc5ed8ae52bedbcd" | ||
|
||
def get_salt(): | ||
file_path = "/etc/shadow" | ||
target_username = "admin" | ||
nmoray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
salt = None | ||
|
||
# Read the file and search for the "admin" username | ||
try: | ||
with open(file_path, 'r') as file: | ||
for line in file: | ||
if "admin:" in line: | ||
# Format: username:$id$salt$hashed user pass | ||
parts = line.split('$') | ||
if len(parts) == 4: | ||
salt = parts[2] | ||
break | ||
|
||
except FileNotFoundError: | ||
click.echo('File not found: ' % file_path) | ||
except Exception as e: | ||
click.echo('An error occurred: ' % str(e)) | ||
|
||
if salt == None: | ||
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. what scenarios do you anticipate no salt from shadow file? 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. When there is no admin password configured. |
||
salt = TACACS_SECRET_SALT | ||
|
||
return salt | ||
|
||
def encrypt_passkey(secret): | ||
salt = get_salt() | ||
cmd = [ 'openssl', 'enc', '-aes-128-cbc', '-A', '-a', '-salt', '-pbkdf2', '-pass', 'pass:' + salt ] | ||
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) | ||
outsecret, errs = p.communicate(input=secret) | ||
return outsecret,errs | ||
|
||
def is_secret(secret): | ||
return bool(re.match('^' + '[^ #,]*' + '$', secret)) | ||
|
@@ -240,7 +276,20 @@ def passkey(ctx, secret): | |
if ctx.obj == 'default': | ||
del_table_key('TACPLUS', 'global', 'passkey') | ||
elif secret: | ||
add_table_kv('TACPLUS', 'global', 'passkey', secret) | ||
if len(secret) > TACACS_PASSKEY_MAX_LEN: | ||
click.echo('Maximum of %d chars can be configured' % TACACS_PASSKEY_MAX_LEN) | ||
return | ||
elif not is_secret(secret): | ||
click.echo(VALID_CHARS_MSG) | ||
return | ||
config_db = ConfigDBConnector() | ||
config_db.connect() | ||
outsecret, errs = encrypt_passkey(secret) | ||
if not errs: | ||
add_table_kv('TACPLUS', 'global', 'passkey', outsecret) | ||
liuh-80 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
click.echo('Passkey configuration failed' % errs) | ||
return | ||
else: | ||
click.echo('Argument "secret" is required') | ||
tacacs.add_command(passkey) | ||
|
@@ -278,7 +327,12 @@ def add(address, timeout, key, auth_type, port, pri, use_mgmt_vrf): | |
if timeout is not None: | ||
data['timeout'] = str(timeout) | ||
if key is not None: | ||
data['passkey'] = key | ||
outsecret, errs = encrypt_passkey(key) | ||
if not errs: | ||
data['passkey'] = outsecret | ||
else: | ||
click.echo('Passkey configuration failed' % errs) | ||
return | ||
if use_mgmt_vrf : | ||
data['vrf'] = "mgmt" | ||
try: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is this hardcoded SALT? as you choose to use shadow file for SALT, what is the significance of it? shouldn't this value differ per device?
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.
It is random hash and will be fixed on all the devices unless and util an explicit admin password is configured. This a fail-safe mechanism to ensure that the encryption / decryption succeeds even if there is no admin password configured on a device.
If configured, the code will use the salt from a shadow file (from admin user credentials).