1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-03-12 04:58:34 +03:00

Refactor config update method to handle non-existing config values.

Added the option to strip domain part from radius
This commit is contained in:
Adolfo Gómez García 2024-10-17 18:18:24 +02:00
parent c96193f755
commit 7da008ba53
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
5 changed files with 50 additions and 15 deletions

View File

@ -39,6 +39,7 @@ from uds.REST import Handler
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Enclosed methods under /config path # Enclosed methods under /config path
class Config(Handler): class Config(Handler):
needs_admin = True # By default, staff is lower level needed needs_admin = True # By default, staff is lower level needed
@ -59,6 +60,21 @@ class Config(Handler):
def put(self): def put(self):
for section, secDict in self._params.items(): for section, secDict in self._params.items():
for key, vals in secDict.items(): for key, vals in secDict.items():
logger.info('Updating config value %s.%s to %s by %s', section, key, vals['value'], self._user.name) config = CfgConfig.update(section, key, vals['value'])
CfgConfig.update(section, key, vals['value']) if config is not None:
logger.info(
'Updating config value %s.%s to %s by %s',
section,
key,
'********' if config.isCrypted() else vals['value'],
self._user.name,
)
else:
logger.error(
'Non existing config value %s.%s to %s by %s',
section,
key,
vals['value'],
self._user.name,
)
return 'done' return 'done'

View File

@ -268,21 +268,23 @@ class Config:
yield val yield val
@staticmethod @staticmethod
def update(section, key, value, checkType=False) -> bool: def update(section, key, value, checkType=False) -> typing.Optional['Config.Value']:
# If cfg value does not exists, simply ignore request # If cfg value does not exists, simply ignore request
try: try:
cfg = DBConfig.objects.filter(section=section, key=key)[0] # @UndefinedVariable cfg = DBConfig.objects.filter(section=section, key=key)[0] # @UndefinedVariable
if checkType and cfg.field_type in (Config.READ_FIELD, Config.HIDDEN_FIELD): if checkType and cfg.field_type in (Config.READ_FIELD, Config.HIDDEN_FIELD):
return False # Skip non writable elements return None # Skip non writable elements
if cfg.crypt: if cfg.crypt:
value = cryptoManager().encrypt(value) value = cryptoManager().encrypt(value)
cfg.value = value cfg.value = value
cfg.save() cfg.save()
logger.debug('Updated value for %s.%s to %s', section, key, value) logger.debug('Updated value for %s.%s to %s', section, key, value)
return True if cfg.crypt:
return Config.section(section).valueCrypt(key)
return Config.section(section).value(key)
except Exception: except Exception:
return False return None
@staticmethod @staticmethod
def getConfigValues( def getConfigValues(

View File

@ -58,9 +58,8 @@ class Command(BaseCommand):
mod, name = first mod, name = first
else: else:
mod, name = GLOBAL_SECTION, first[0] mod, name = GLOBAL_SECTION, first[0]
if ( if not Config.update(mod, name, value):
Config.update(mod, name, value) is False # If not exists, try to store value without any special parameters
): # If not exists, try to store value without any special parameters
Config.section(mod).value(name, value).get() Config.section(mod).value(name, value).get()
except Exception as e: except Exception as e:
self.stderr.write('The command could not be processed: {}'.format(e)) self.stderr.write('The command could not be processed: {}'.format(e))

View File

@ -158,6 +158,18 @@ class RadiusOTP(mfas.MFA):
tab=_('Config'), tab=_('Config'),
) )
send_just_username = gui.CheckBoxField(
label=_('Send only username (without domain) to radius server'),
order=34,
defvalue=False,
tooltip=_(
'If unchecked, username will be sent as is to radius server. \n'
'If checked, domain part will be removed from username before sending it to radius server.'
),
required=False,
tab=_('Config'),
)
def initialize(self, values: 'Module.ValuesType') -> None: def initialize(self, values: 'Module.ValuesType') -> None:
return super().initialize(values) return super().initialize(values)
@ -250,11 +262,14 @@ class RadiusOTP(mfas.MFA):
''' '''
if self.askForOTP(request) is False: if self.askForOTP(request) is False:
return mfas.MFA.RESULT.ALLOWED return mfas.MFA.RESULT.ALLOWED
# if we are in a "all-users-otp" policy, avoid this step and go directly to ask for OTP # if we are in a "all-users-otp" policy, avoid this step and go directly to ask for OTP
if self.all_users_otp.isTrue(): if self.all_users_otp.isTrue():
return mfas.MFA.RESULT.OK return mfas.MFA.RESULT.OK
if self.send_just_username.isTrue():
username = username.strip().split('@')[0].split('\\')[-1]
web_pwd = webPassword(request) web_pwd = webPassword(request)
try: try:
connection = self.radiusClient() connection = self.radiusClient()
@ -314,6 +329,9 @@ class RadiusOTP(mfas.MFA):
''' '''
try: try:
if self.send_just_username.isTrue():
username = username.strip().split('@')[0].split('\\')[-1]
err = _('Invalid OTP code') err = _('Invalid OTP code')
web_pwd = webPassword(request) web_pwd = webPassword(request)

View File

@ -485,17 +485,17 @@ class XenServer: # pylint: disable=too-many-public-methods
if not all_VIFs: if not all_VIFs:
raise XenException('No Network interfaces found!') raise XenException('No Network interfaces found!')
found = (all_VIFs[0], self.VIF.get_record(all_VIFs[0])) found = (all_VIFs[0], self.VIF.get_record(all_VIFs[0]))
for vifId in all_VIFs: for vif_id in all_VIFs:
vif = self.VIF.get_record(vifId) vif = self.VIF.get_record(vif_id)
logger.info('VIF: %s', vif) logger.info('VIF: %s', vif)
if vif['network'] == mac['network']: if vif['network'] == mac['network']:
found = (vifId, vif) found = (vif_id, vif)
break break
logger.debug('Found VIF: %s', found[1]) logger.debug('Found VIF: %s', found[1])
vifId, vif = found vif_id, vif = found
self.VIF.destroy(vifId) self.VIF.destroy(vif_id)
vif['MAC'] = mac['mac'] vif['MAC'] = mac['mac']
vif['network'] = mac['network'] vif['network'] = mac['network']