Skip to content
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
wants to merge 9 commits into
base: master
Choose a base branch
from
58 changes: 56 additions & 2 deletions config/aaa.py
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
Expand All @@ -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"
Copy link
Contributor

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?

Copy link
Author

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).


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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what scenarios do you anticipate no salt from shadow file?

Copy link
Author

Choose a reason for hiding this comment

The 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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
Loading