File: utils.py

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (902 lines) | stat: -rw-r--r-- 30,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
"""
Utils functions consumed by SSG
"""

from __future__ import absolute_import
from __future__ import print_function

import multiprocessing
import errno
import os
import re
from collections import namedtuple
import hashlib

from .constants import (FULL_NAME_TO_PRODUCT_MAPPING,
                        MAKEFILE_ID_TO_PRODUCT_MAP,
                        MULTI_PLATFORM_LIST,
                        )


class SSGError(RuntimeError):
    pass


PRODUCT_NAME_PARSER = re.compile(r"([a-zA-Z\-]+)([0-9]+)")


class VersionSpecifierSet(set):
    """
    A set-like collection that only accepts VersionSpecifier objects.

    This class extends the built-in set and ensures that all elements are instances of the
    VersionSpecifier class. It provides additional properties to generate titles, CPE IDs, and
    OVAL IDs from the contained VersionSpecifier objects.

    Attributes:
        title (str): A string representation of the set, joining the titles of the contained
                     VersionSpecifier objects with ' and '.
        cpe_id (str): A string representation of the set, joining the CPE IDs of the contained
                      VersionSpecifier objects with ':'.
        oval_id (str): A string representation of the set, joining the OVAL IDs of the contained
                       VersionSpecifier objects with '_'.
    """
    def __init__(self, s=()):
        for el in s:
            if not isinstance(el, VersionSpecifier):
                raise ValueError('VersionSpecifierSet can only work with VersionSpecifier objects,'
                                 ' invalid object: {0}'.format(repr(el)))
        super(VersionSpecifierSet, self).__init__(s)

    @property
    def title(self):
        return ' and '.join([ver_spec.title for ver_spec in sorted(self)])

    @property
    def cpe_id(self):
        return ':'.join([ver_spec.cpe_id for ver_spec in sorted(self)])

    @property
    def oval_id(self):
        return '_'.join([ver_spec.oval_id for ver_spec in sorted(self)])


class VersionSpecifier:
    """
    A class to represent a version specifier with properties and methods to manipulate and
    retrieve version information.

    Attributes:
        op (str): The operation associated with the version specifier.
        _evr_ver_dict (dict): A dictionary containing epoch, version, and release information.
    """
    def __init__(self, op, evr_ver_dict):
        self._evr_ver_dict = evr_ver_dict
        self.op = op

    def __str__(self):
        return '{0} {1}'.format(self.op, self.ver)

    def __repr__(self):
        return '<VersionSpecifier({0},{1})>'.format(self.op, self.ver)

    def __hash__(self):
        return hash(self.op + self.ver)

    def __eq__(self, other):
        return self.op+self.ver == other.op+other.ver

    def __lt__(self, other):
        return self.op+self.ver < other.op+other.ver

    @property
    def evr_op(self):
        return comparison_to_oval(self.op)

    @property
    def ver(self):
        return VersionSpecifier.evr_dict_to_str(self._evr_ver_dict)

    @property
    def evr_ver(self):
        return VersionSpecifier.evr_dict_to_str(self._evr_ver_dict, True)

    @property
    def ev_ver(self):
        return VersionSpecifier.evr_dict_to_str(self._evr_ver_dict, True).split("-")[0]

    @property
    def title(self):
        return '{0} {1}'.format(comparison_to_oval(self.op), self.ver)

    @property
    def cpe_id(self):
        return '{0}:{1}'.format(escape_comparison(self.op), self.ver)

    @property
    def oval_id(self):
        return '{0}_{1}'.format(escape_comparison(self.op), escape_id(self.ver))

    @staticmethod
    def evr_dict_to_str(evr, fully_formed_evr_string=False):
        """
        Convert an EVR (Epoch-Version-Release) dictionary to a string representation.

        Args:
            evr (dict): A dictionary containing 'epoch', 'version', and 'release' keys.
                        'epoch' and 'release' can be None.
            fully_formed_evr_string (bool): If True, ensures that the returned string includes '0'
                                            for missing 'epoch' and '-0' for missing 'release'.

        Returns:
            str: The string representation of the EVR dictionary.
        """
        res = ''
        if evr['epoch'] is not None:
            res += evr['epoch'] + ':'
        elif fully_formed_evr_string:
            res += '0:'
        res += evr['version']
        if evr['release'] is not None:
            res += '-' + evr['release']
        elif fully_formed_evr_string:
            res += '-0'
        return res


def map_name(version):
    """
    Maps SSG Makefile internal product name to official product name.

    This function takes a version string and maps it to an official product name based on
    predefined mappings. It handles multi-platform versions by trimming the "multi_platform_"
    prefix and recursively mapping the trimmed version.

    Args:
        version (str): The version string to be mapped.

    Returns:
        str: The official product name corresponding to the given version.

    Raises:
        RuntimeError: If the version is invalid or cannot be mapped to any known product.
    """
    if version.startswith("multi_platform_"):
        trimmed_version = version[len("multi_platform_"):]
        if trimmed_version not in MULTI_PLATFORM_LIST:
            raise RuntimeError(
                "%s is an invalid product version. If it's multi_platform the "
                "suffix has to be from (%s)."
                % (version, ", ".join(MULTI_PLATFORM_LIST))
            )
        return map_name(trimmed_version)

    # By sorting in reversed order, keys which are a longer version of other keys are
    # visited first (e.g., rhosp vs. rhel)
    for key in sorted(MAKEFILE_ID_TO_PRODUCT_MAP, reverse=True):
        if version.startswith(key):
            return MAKEFILE_ID_TO_PRODUCT_MAP[key]

    raise RuntimeError("Can't map version '%s' to any known product!"
                       % (version))


def product_to_name(prod):
    """
    Converts a product identifier into a full product name.

    This function takes a product identifier and attempts to match it with a full product name
    from the `FULL_NAME_TO_PRODUCT_MAPPING` dictionary. If the product identifier is found in
    the dictionary, the corresponding full product name is returned. If the product identifier
    is in the `MULTI_PLATFORM_LIST` or is 'all', a string prefixed with "multi_platform_" is
    returned. If the product identifier is not recognized, a RuntimeError is raised.

    Args:
        prod (str): The product identifier to convert.

    Returns:
        str: The full product name corresponding to the given product identifier.

    Raises:
        RuntimeError: If the product identifier is not recognized.
    """
    for name, prod_type in FULL_NAME_TO_PRODUCT_MAPPING.items():
        if prod == prod_type:
            return name
    if prod in MULTI_PLATFORM_LIST or prod == 'all':
        return "multi_platform_" + prod
    raise RuntimeError("Unknown product name: %s" % prod)


def name_to_platform(names):
    """
    Converts one or more full names to a string containing one or more <platform> elements.

    Args:
        names (str or list of str): A single name as a string or a list of names.

    Returns:
        str: A string containing one or more <platform> elements.
    """
    if isinstance(names, str):
        return "<platform>%s</platform>" % names
    return "\n".join(map(name_to_platform, names))


def product_to_platform(prods):
    """
    Converts one or more product ids into a string with one or more <platform> elements.

    Args:
        prods (str or list of str): A single product id as a string or a list of product ids.

    Returns:
        str: A string containing one or more <platform> elements.
    """
    if isinstance(prods, str):
        return name_to_platform(product_to_name(prods))
    return "\n".join(map(product_to_platform, prods))


def parse_name(product):
    """
    Parses a given product string and returns a namedtuple containing the name and version.

    Args:
        product (str): The product string to parse, e.g., "rhel9".

    Returns:
        namedtuple: A namedtuple with 'name' and 'version' attributes, e.g., ("rhel", "9").

    Example:
        >>> parse_name("rhel9")
        product(name='rhel', version='9')
    """
    prod_tuple = namedtuple('product', ['name', 'version'])

    _product = product
    _product_version = None
    match = PRODUCT_NAME_PARSER.match(product)

    if match:
        _product = match.group(1)
        _product_version = match.group(2)

    return prod_tuple(_product, _product_version)


def parse_platform(platform):
    """
    Parses a comma-separated string of platforms and returns a set of trimmed platform names.

    Args:
        platform (str): A comma-separated string of platform names.

    Returns:
        set: A set of platform names with leading and trailing whitespace removed.
    """
    return set(map(lambda x: x.strip(), platform.split(',')))


def get_fixed_product_version(product, product_version):
    """
    Adjusts the product version format for specific products.

    Some product versions have a dot in between the numbers while the product ID doesn't have the
    dot, but the full product name does. This function ensures the correct format for the product
    version.

    Args:
        product (str): The name of the product (e.g., "ubuntu", "macos").
        product_version (str): The version of the product as a string.

    Returns:
        str: The adjusted product version with the correct format.
    """
    if product == "ubuntu" or product == "macos":
        product_version = product_version[:2] + "." + product_version[2:]
    return product_version


def is_applicable_for_product(platform, product):
    """
    Determines if a remediation script is applicable for a given product based on the platform specifier.

    The function checks if the platform is either a general multi-platform specifier or matches
    the specific product name and version.

    Args:
        platform (str): The platform specifier of the remediation script.
        product (str): The product name and version.

    Returns:
        bool: True if the remediation script is applicable for the product, False otherwise.
    """
    # If the platform is None, platform must not exist in the config, so return False.
    if not platform:
        return False

    product, product_version = parse_name(product)

    # Define general platforms
    multi_platforms = ['multi_platform_all',
                       'multi_platform_' + product]

    # First test if platform isn't for 'multi_platform_all' or 'multi_platform_' + product
    for _platform in multi_platforms:
        if _platform in platform and product in MULTI_PLATFORM_LIST:
            return True

    product_name = ""
    # Get official name for product
    if product_version is not None:
        product_name = map_name(product) + ' ' + get_fixed_product_version(
            product, product_version
        )
    else:
        product_name = map_name(product)

    # Test if this is for the concrete product version
    for _name_part in platform.split(','):
        if product_name == _name_part.strip():
            return True

    # Remediation script isn't neither a multi platform one, nor isn't
    # applicable for this product => return False to indicate that
    return False


def is_applicable(platform, product):
    """
    Check if a platform is applicable for the given product.

    This function determines whether a specified platform or a list of platforms is applicable
    for a given product. It handles cases where the platform is specified as 'all' or
    'multi_platform_all', as well as cases where the platform is a comma-separated list of
    products.

    Args:
        platform (str): The platform or list of platforms to check.
        product (str): The product to check applicability for.

    Returns:
        bool: True if the product is applicable for the platform or list of platforms, False otherwise.
    """
    if platform == 'all' or platform == 'multi_platform_all':
        return True

    if is_applicable_for_product(platform, product):
        return True

    if 'osp7' in product and 'osp7' in platform:
        return True

    return product in platform.split(',')


def required_key(_dict, _key):
    """
    Returns the value associated with the specified key in the given dictionary.

    Parameters:
        _dict (dict): The dictionary to search for the key.
        _key: The key to look for in the dictionary.

    Returns:
        The value associated with the specified key in the dictionary.

    Raises:
        ValueError: If the key is not found in the dictionary, an exception is raised with a
                    message indicating that the key is required but was not found.
    """
    if _key in _dict:
        return _dict[_key]

    raise ValueError("%s is required but was not found in:\n%s" %
                     (_key, repr(_dict)))


def get_cpu_count():
    """
    Returns the most likely estimate of the number of CPUs in the machine for threading purposes,
    gracefully handling errors and possible exceptions.

    Returns:
        int: The number of CPUs available. If the number of CPUs cannot be determined, returns 2
             as a default value.
    """
    try:
        return max(1, multiprocessing.cpu_count())

    except NotImplementedError:
        return 2


def merge_dicts(left, right):
    """
    Merges two dictionaries, keeping left and right as passed.

    If there are any common keys between left and right, the value from right is used.

    Args:
        left (dict): The first dictionary.
        right (dict): The second dictionary.

    Returns:
        dict: The merged dictionary containing keys and values from both left and right dictionaries.
    """
    result = left.copy()
    result.update(right)
    return result


def subset_dict(dictionary, keys):
    """
    Restricts a dictionary to only include specified keys.

    This function creates a new dictionary that contains only the key-value pairs from the
    original dictionary where the key is present in the provided list of keys. Neither the
    original dictionary nor the list of keys is modified.

    Args:
        dictionary (dict): The original dictionary from which to create the subset.
        keys (iterable): An iterable of keys that should be included in the subset.

    Returns:
        dict: A new dictionary containing only the key-value pairs where the key is in the
              provided list of keys.
    """
    result = dictionary.copy()
    for original_key in dictionary:
        if original_key not in keys:
            del result[original_key]

    return result


def read_file_list(path):
    """
    Reads the given file path and returns the contents as a list.

    Args:
        path (str): The path to the file to be read.

    Returns:
        list: A list containing the contents of the file, split by lines or other delimiters as
              defined by `split_string_content`.

    Raises:
        FileNotFoundError: If the file at the given path does not exist.
        IOError: If an I/O error occurs while reading the file.
    """
    with open(path, 'r') as f:
        return split_string_content(f.read())


def split_string_content(content):
    """
    Splits the input string content by newline characters and returns the result as a list of strings.

    Args:
        content (str): The string content to be split.

    Returns:
        list: A list of strings, each representing a line from the input content.
              The last element will be removed if it is an empty string.
    """
    file_contents = content.split("\n")
    if file_contents[-1] == '':
        file_contents = file_contents[:-1]
    return file_contents


def write_list_file(path, contents):
    """
    Writes the given contents to the specified file path.

    Args:
        path (str): The file path where the contents will be written.
        contents (list of str): A list of strings to be written to the file.

    Returns:
        None
    """
    _contents = "\n".join(contents) + "\n"
    _f = open(path, 'w')
    _f.write(_contents)
    _f.flush()
    _f.close()


# Taken from https://stackoverflow.com/a/600612/592892
def mkdir_p(path):
    """
    Create a directory and all intermediate-level directories if they do not exist.

    Args:
        path (str): The directory path to create.

    Returns:
        bool: True if the directory was created, False if it already exists.

    Raises:
        OSError: If the directory cannot be created and it does not already exist.
    """
    if os.path.isdir(path):
        return False
    # Python >=3.4.1
    # os.makedirs(path, exist_ok=True)
    try:
        os.makedirs(path)
        return True
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            return False
        else:
            raise


def escape_regex(text):
    """
    Escapes special characters in a given text to make it safe for use in regular expressions.

    This function mimics the behavior of re.escape() in Python 3.7, which escapes a reasonable set
    of characters.
    Specifically, it escapes the following characters: #, $, &, *, +, ., ^, `, |, ~, :, (, ), and -.
    Note that the characters '!', '"', '%', "'", ',', '/', ':', ';', '<', '=', '>', '@', and "`"
    are not escaped.

    Args:
        text (str): The input string containing characters to be escaped.

    Returns:
        str: A new string with special characters escaped.
    """
    # We could use re.escape(), but it escapes too many characters, including plain white space.
    # In python 3.7 the set of characters escaped by re.escape is reasonable, so lets mimic it.
    # See https://docs.python.org/3/library/re.html#re.sub
    # '!', '"', '%', "'", ',', '/', ':', ';', '<', '=', '>', '@', and "`" are not escaped.
    return re.sub(r"([#$&*+.^`|~:()\[\]-])", r"\\\1", text)


def escape_id(text):
    """
    Converts a given text into a string that is more readable and compatible with OSCAP/XCCDF/OVAL
    entity IDs by replacing non-word characters with underscores and stripping leading/trailing
    underscores.

    Args:
        text (str): The input string to be converted.

    Returns:
        str: The converted string with non-word characters replaced by underscores and
             leading/trailing underscores removed.
    """
    # Make a string used as an Id for OSCAP/XCCDF/OVAL entities more readable
    # and compatible with:
    # OVAL: r'oval:[A-Za-z0-9_\-\.]+:ste:[1-9][0-9]*'
    return re.sub(r"[^\w]+", "_", text).strip("_")


def escape_yaml_key(text):
    """
    Escapes uppercase letters and caret symbols in a YAML key by prefixing them with a caret
    and converts the entire key to lowercase.

    This function is used to handle the limitation of OVAL's name argument of the field type,
    which requires avoiding uppercase letters for keys. The probe would escape them with the
    '^' symbol.

    Example:
        myCamelCase^Key -> my^camel^case^^^key

    Args:
        text (str): The YAML key to be escaped.

    Returns:
        str: The escaped and lowercased YAML key.
    """
    return re.sub(r'([A-Z^])', '^\\1', text).lower()


def _map_comparison_op(op, table):
    """
    Maps a comparison operator to its corresponding function or value from a given table.

    Args:
        op (str): The comparison operator to be mapped.
        table (dict): A dictionary where keys are comparison operators and values are the
                      corresponding functions or values.

    Returns:
        The function or value corresponding to the given comparison operator.

    Raises:
        KeyError: If the given comparison operator is not found in the table.
    """
    if op not in table:
        raise KeyError("Invalid comparison operator: %s (expected one of: %s)",
                       op, ', '.join(table.keys()))
    return table[op]


def escape_comparison(op):
    """
    Maps a comparison operator to its corresponding string representation.

    Args:
        op (str): The comparison operator to be mapped.
                  Expected values are '==', '!=', '>', '<', '>=', '<='.

    Returns:
        str: The string representation of the comparison operator.
             Possible return values are 'eq', 'ne', 'gt', 'le', 'gt_or_eq', 'le_or_eq'.

    Raises:
        KeyError: If the provided operator is not in the mapping dictionary.
    """
    return _map_comparison_op(op, {
        '==': 'eq',       '!=': 'ne',
        '>': 'gt',        '<': 'le',
        '>=': 'gt_or_eq', '<=': 'le_or_eq',
    })


def comparison_to_oval(op):
    """
    Converts a comparison operator to its corresponding OVAL string representation.

    Args:
        op (str): The comparison operator to convert.
                  Expected values are '==', '!=', '>', '<', '>=', '<='.

    Returns:
        str: The OVAL string representation of the comparison operator.

    Raises:
        KeyError: If the provided operator is not one of the expected values.
    """
    return _map_comparison_op(op, {
        '==': 'equals',                '!=': 'not equal',
        '>': 'greater than',           '<': 'less than',
        '>=': 'greater than or equal', '<=': 'less than or equal',
    })


def sha256(text):
    """
    Generate a SHA-256 hash for the given text.

    Args:
        text (str): The input text to be hashed.

    Returns:
        str: The SHA-256 hash of the input text as a hexadecimal string.
    """
    return hashlib.sha256(text.encode('utf-8')).hexdigest()


def banner_regexify(banner_text):
    """
    Converts a banner text into a regex pattern.

    This function escapes special regex characters in the input text and then performs the
    following transformations:
    - Replaces newline characters ("\n") with a placeholder string "BFLMPSVZ".
    - Replaces spaces with a regex pattern that matches one or more whitespace characters or
      newline characters.
    - Replaces the placeholder string "BFLMPSVZ" with a regex pattern that matches one or more
      newline characters or escaped newline sequences.

    Args:
        banner_text (str): The banner text to be converted into a regex pattern.

    Returns:
        str: The resulting regex pattern.
    """
    return escape_regex(banner_text) \
        .replace("\n", "BFLMPSVZ") \
        .replace(" ", "[\\s\\n]+") \
        .replace("BFLMPSVZ", "(?:[\\n]+|(?:\\\\n)+)")


def banner_anchor_wrap(banner_text):
    """
    Wraps the given banner text with '^' at the beginning and '$' at the end.

    Args:
        banner_text (str): The text to be wrapped.

    Returns:
        str: The banner text wrapped with '^' at the beginning and '$' at the end.
    """
    return "^" + banner_text + "$"


def parse_template_boolean_value(data, parameter, default_value):
    """
    Parses a boolean value from a template parameter.

    Args:
        data (dict): The dictionary containing the template data.
        parameter (str): The key for the parameter to parse.
        default_value (bool): The default value to return if the parameter is not found or is None.

    Returns:
        bool: The parsed boolean value.

    Raises:
        ValueError: If the parameter value is not "true" or "false".
    """
    value = data.get(parameter)
    if not value:
        return default_value
    if value == "true":
        return True
    elif value == "false":
        return False
    else:
        raise ValueError(
            "Template parameter {} used in rule {} cannot accept the "
            "value {}".format(parameter, data["_rule_id"], value))


def check_conflict_regex_directory(data):
    """
    Validate that either all paths are directories or the 'file_regex' key exists.

    This function checks the following conditions:
    1. If the 'is_directory' key is present in the data dictionary, it ensures that all file paths
       in the 'filepath' list are either directories or files. Mixing directories and files is not
       allowed.
    2. If the 'file_regex' key is present in the data dictionary, it ensures that all file paths
       in the 'filepath' list are directories.

    Args:
        data (dict): A dictionary containing the following keys:
            - 'filepath' (list): A list of file paths to be validated.
            - 'is_directory' (bool, optional): A flag indicating whether the paths are directories.
            - 'file_regex' (str, optional): A regular expression pattern for file matching.
            - '_rule_id' (str): An identifier for the rule being validated.

    Raises:
        ValueError: If the file paths are a mix of directories and files, or if the 'file_regex' key
                    is used but the file paths are not directories.
    """
    for f in data["filepath"]:
        if "is_directory" in data and data["is_directory"] != f.endswith("/"):
            raise ValueError(
                "If passing a list of filepaths, all of them need to be "
                "either directories or files. Mixing is not possible. "
                "Please fix rules '{0}' filepath '{1}'".format(data["_rule_id"], f))

        data["is_directory"] = f.endswith("/")

        if "file_regex" in data and not data["is_directory"]:
            raise ValueError(
                "Used 'file_regex' key in rule '{0}' but filepath '{1}' does not "
                "specify a directory. Append '/' to the filepath or remove the "
                "'file_regex' key.".format(data["_rule_id"], f))


def enum(*args):
    """
    Creates an enumeration with the given arguments.

    Args:
        *args: A variable length argument list of strings representing the names of the
               enumeration members.

    Returns:
        type: A new enumeration type with the given member names as attributes, each assigned a
              unique integer value starting from 0.

    Example:
        >>> Colors = enum('RED', 'GREEN', 'BLUE')
        >>> Colors.RED
        0
        >>> Colors.GREEN
        1
        >>> Colors.BLUE
        2
    """
    enums = dict(zip(args, range(len(args))))
    return type('Enum', (), enums)


def recurse_or_substitute_or_do_nothing(v, string_dict, ignored_keys=frozenset()):
    """
    Recursively applies string formatting to dictionary values, substitutes strings, or returns
    the value unchanged.

    Args:
        v (Any): The value to process. Can be a dictionary, string, or any other type.
        string_dict (dict): A dictionary containing string keys and values to be used for formatting.
        ignored_keys (frozenset, optional): A set of keys to ignore when processing dictionaries.
                                            Defaults to an empty frozenset.

    Returns:
        Any: The processed value. If `v` is a dictionary, returns a dictionary with formatted values.
             If `v` is a string, returns the formatted string. Otherwise, returns `v` unchanged.
    """
    if isinstance(v, dict):
        return apply_formatting_on_dict_values(v, string_dict, ignored_keys)
    elif isinstance(v, str):
        return v.format(**string_dict)
    else:
        return v


def apply_formatting_on_dict_values(source_dict, string_dict, ignored_keys=frozenset()):
    """
    Apply string formatting on dictionary values.

    This function uses Python's built-in string replacement to replace strings marked by {token}
    if "token" is a key in the string_dict parameter. It skips keys in source_dict which are
    listed in the ignored_keys parameter. This function works only for dictionaries whose values
    are dictionaries or strings.

    Args:
        source_dict (dict): The source dictionary whose values need formatting.
        string_dict (dict): A dictionary containing replacement strings for tokens.
        ignored_keys (frozenset, optional): A set of keys to be ignored during formatting.
                                            Defaults to an empty frozenset.

    Returns:
    dict: A new dictionary with formatted values.
   """
    new_dict = {}
    for k, v in source_dict.items():
        if k not in ignored_keys:
            new_dict[k] = recurse_or_substitute_or_do_nothing(
                v, string_dict, ignored_keys)
        else:
            new_dict[k] = v
    return new_dict


def ensure_file_paths_and_file_regexes_are_correctly_defined(data):
    """
    Ensures that the data structure for file paths and file regexes is correctly defined.

    This function is used for the file_owner, file_groupowner, and file_permissions templates.
    It ensures that:
    - The 'filepath' item in the data is a list.
    - The number of items in 'file_regex' matches the number of items in 'filepath'.

    Note:
        - If 'filepath' is a string, it will be converted to a list containing that string.
        - If 'file_regex' is a string, it will be converted to a list with the same length as
          'filepath', with each element being the original 'file_regex' string.
        - If there are multiple regexes for a single filepath, the filepath must be declared
          multiple times.

    Args:
        data (dict): A dictionary containing file path and file regex information.
                     It must contain the keys:
                     - 'filepath': A string or list of strings representing file paths.
                     - 'file_regex' (optional): A string or list of strings representing file regexes.
                     - '_rule_id': A string representing the rule ID.

    Raises:
        ValueError: If the number of items in 'file_regex' does not match the number of items in 'filepath'.
    """
    # this avoids code duplicates
    if isinstance(data["filepath"], str):
        data["filepath"] = [data["filepath"]]

    if "file_regex" in data:
        # we can have a list of filepaths, but only one regex
        # instead of declaring the same regex multiple times
        if isinstance(data["file_regex"], str):
            data["file_regex"] = [data["file_regex"]] * len(data["filepath"])

        # if the length of filepaths and file_regex are not the same, then error.
        # in case we have multiple regexes for just one filepath, than we need
        # to declare that filepath multiple times
        if len(data["filepath"]) != len(data["file_regex"]):
            raise ValueError(
                "You should have one file_path per file_regex. Please check "
                "rule '{0}'".format(data["_rule_id"]))

    check_conflict_regex_directory(data)