Skip to content

Commit

Permalink
Improve configuration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-stefani committed Apr 19, 2024
1 parent 9b7acf1 commit ce66b3f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
2 changes: 1 addition & 1 deletion framework/wazuh/core/cluster/hap_helper/hap_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,4 @@ async def start(cls):
except Exception as unexpected_exc:
logger.critical(f'Unexpected exception: {unexpected_exc}', exc_info=True)
finally:
logger.info('Process ended')
logger.info('Task ended')
77 changes: 65 additions & 12 deletions framework/wazuh/core/cluster/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
}


def validate_haproxy_helper_config(helper_config: dict) -> dict:
"""Validate HAProxy helper configuration section.
def validate_haproxy_helper_integer_values(helper_config: dict) -> dict:
"""Validate HAProxy helper integer values.
Parameters
----------
Expand All @@ -73,22 +73,13 @@ def validate_haproxy_helper_config(helper_config: dict) -> dict:
Returns
-------
dict
Validated configuration for HAProxy Helper.
Validated configuration with integer values.
Raises
------
WazuhError (3004)
If some value has an invalid type.
"""
# If any value is missing from user's cluster configuration, add the default one.
for value_name in set(HELPER_DEFAULTS.keys()) - set(helper_config.keys()):
helper_config[value_name] = HELPER_DEFAULTS[value_name]

if helper_config[HAPROXY_DISABLED] == NO:
helper_config[HAPROXY_DISABLED] = False
elif helper_config[HAPROXY_DISABLED] == YES:
helper_config[HAPROXY_DISABLED] = True

for field in [
HAPROXY_PORT,
FREQUENCY,
Expand All @@ -102,13 +93,75 @@ def validate_haproxy_helper_config(helper_config: dict) -> dict:
helper_config[field] = int(helper_config[field])
except ValueError:
raise WazuhError(3004, extra_message=f"HAProxy Helper {field} must be an integer.")
return helper_config


def validate_haproxy_helper_float_values(helper_config: dict) -> dict:
"""Validate HAProxy helper float values.
Parameters
----------
helper_config : dict
Configuration to validate.
Returns
-------
dict
Validated configuration with float values.
Raises
------
WazuhError (3004)
If some value has an invalid type.
"""
for field in [IMBALANCE_TOLERANCE]:
if helper_config.get(field):
try:
helper_config[field] = float(helper_config[field])
except ValueError:
raise WazuhError(3004, extra_message=f"HAProxy Helper {field} must be a float.")
return helper_config


def validate_haproxy_helper_config(helper_config: dict) -> dict:
"""Validate HAProxy helper configuration section.
Parameters
----------
helper_config : dict
Configuration to validate.
Returns
-------
dict
Validated configuration for HAProxy Helper.
Raises
------
WazuhError (3004)
If some value has an invalid type.
"""
# If any value is missing from user's cluster configuration, add the default one.
for value_name in set(HELPER_DEFAULTS.keys()) - set(helper_config.keys()):
helper_config[value_name] = HELPER_DEFAULTS[value_name]

if helper_config[HAPROXY_DISABLED] == NO:
helper_config[HAPROXY_DISABLED] = False
elif helper_config[HAPROXY_DISABLED] == YES:
helper_config[HAPROXY_DISABLED] = True

helper_config = validate_haproxy_helper_integer_values(helper_config)
helper_config = validate_haproxy_helper_float_values(helper_config)

if helper_config[HAPROXY_PROTOCOL] not in ['http', 'https']:
raise WazuhError(
3004,
f'Invalid protocol type {helper_config[HAPROXY_PROTOCOL]}. Correct values are `http` and `https`'
)

if helper_config[HAPROXY_PORT] is not None:
if not 1024 < helper_config[HAPROXY_PORT] < 65535:
raise WazuhError(3004, "HAPRoxy port must be higher than 1024 and lower than 65535.")

return helper_config

Expand Down

0 comments on commit ce66b3f

Please sign in to comment.