예제 #1
0
def check_cluster_config(config):
    iv = InputValidator()
    reservated_ips = {'localhost', 'NODE_IP', '0.0.0.0', '127.0.1.1'}

    if len(config['key']) == 0:
        raise WazuhException(3004, 'Unspecified key')
    elif not iv.check_name(config['key']) or not iv.check_length(
            config['key'], 32, eq):
        raise WazuhException(
            3004,
            'Key must be 32 characters long and only have alphanumeric characters'
        )

    elif config['node_type'] != 'master' and config['node_type'] != 'worker':
        raise WazuhException(
            3004,
            'Invalid node type {0}. Correct values are master and worker'.
            format(config['node_type']))

    elif not 1024 < config['port'] < 65535:
        raise WazuhException(
            3004, "Port must be higher than 1024 and lower than 65535.")

    if len(config['nodes']) > 1:
        logger.warning(
            "Found more than one node in configuration. Only master node should be specified. Using {} as master."
            .format(config['nodes'][0]))

    invalid_elements = list(reservated_ips & set(config['nodes']))

    if len(invalid_elements) != 0:
        raise WazuhException(
            3004, "Invalid elements in node fields: {0}.".format(
                ', '.join(invalid_elements)))
예제 #2
0
    def test_check_length(self):
        result = InputValidator().check_length('test')
        self.assertEqual(result, True)

        result = InputValidator().check_length('test', 3)
        self.assertEqual(result, False)

        result = InputValidator().check_length('test', 4, operator.eq)
        self.assertEqual(result, True)
예제 #3
0
    def test_check_name(self):
        result = InputValidator().check_name('test')
        self.assertEqual(result, True)

        result = InputValidator().check_name('test', '')
        self.assertEqual(result, False)

        result = InputValidator().check_name('?')
        self.assertEqual(result, False)
예제 #4
0
def check_cluster_config(config):
    """Verify that cluster configuration is correct.

    Following points are checked:
        - Cluster config block is not empty.
        - len(key) == 32 and only alphanumeric characters are used.
        - node_type is 'master' or 'worker'.
        - 1024 < port < 65535.
        - Only 1 node is specified.
        - Reserved IPs are not used.

    Parameters
    ----------
    config : dict
        Cluster configuration.

    Raises
    -------
    WazuhError
        If any of above conditions is not met.
    """
    iv = InputValidator()
    reservated_ips = {'localhost', 'NODE_IP', '0.0.0.0', '127.0.1.1'}

    if len(config['key']) == 0:
        raise WazuhError(3004, 'Unspecified key')
    elif not iv.check_name(config['key']) or not iv.check_length(
            config['key'], 32, eq):
        raise WazuhError(
            3004,
            'Key must be 32 characters long and only have alphanumeric characters'
        )

    elif config['node_type'] != 'master' and config['node_type'] != 'worker':
        raise WazuhError(
            3004,
            'Invalid node type {0}. Correct values are master and worker'.
            format(config['node_type']))

    elif not 1024 < config['port'] < 65535:
        raise WazuhError(
            3004, "Port must be higher than 1024 and lower than 65535.")

    if len(config['nodes']) > 1:
        logger.warning(
            "Found more than one node in configuration. Only master node should be specified. Using {} as master."
            .format(config['nodes'][0]))

    invalid_elements = list(reservated_ips & set(config['nodes']))

    if len(invalid_elements) != 0:
        raise WazuhError(
            3004, "Invalid elements in node fields: {0}.".format(
                ', '.join(invalid_elements)))
예제 #5
0
    def test_group(self):
        result = InputValidator().group('test')
        self.assertEqual(result, True)

        result = InputValidator().group(['test1', 'test2'])
        self.assertEqual(result, True)

        result = InputValidator().group('test')
        self.assertEqual(result, True)

        result = InputValidator().group(['test1', 'test2'])
        self.assertEqual(result, True)
예제 #6
0
def create_group(group_id):
    """Creates a group.

    :param group_id: Group ID.
    :return: Confirmation message.
    """
    # Input Validation of group_id
    if not InputValidator().group(group_id):
        raise WazuhError(1722)

    group_path = path.join(common.shared_path, group_id)

    if group_id.lower() == "default" or path.exists(group_path):
        raise WazuhError(1711, extra_message=group_id)

    # Create group in /etc/shared
    group_def_path = path.join(common.shared_path, 'agent-template.conf')
    try:
        mkdir_with_mode(group_path)
        copyfile(group_def_path, path.join(group_path, 'agent.conf'))
        chown_r(group_path, common.wazuh_uid(), common.wazuh_gid())
        chmod_r(group_path, 0o660)
        chmod(group_path, 0o770)
        msg = f"Group '{group_id}' created."
    except Exception as e:
        raise WazuhInternalError(1005, extra_message=str(e))

    return WazuhResult({'message': msg})
예제 #7
0
    def group_exists(group_id):
        """Checks if the group exists

        :param group_id: Group ID.
        :return: True if group exists, False otherwise
        """
        # Input Validation of group_id
        if not InputValidator().group(group_id):
            raise WazuhError(1722)

        if path.exists(path.join(common.shared_path, group_id)):
            return True
        else:
            return False