glasswall.content_management.policies.word_search

  1import glasswall
  2from glasswall.content_management.config_elements.config_element import ConfigElement
  3from glasswall.content_management.policies.policy import Policy
  4from glasswall.content_management.switches.switch import Switch
  5
  6
  7class WordSearch(Policy):
  8    """ A content management policy for Word Search.
  9
 10    WordSearch(default="allow", config={
 11        "textSearchConfig": {
 12            "@libVersion": "core2",
 13            "textList": [
 14                {"name": "textItem", "switches": [
 15                    {"name": "text", "value": "generic"},
 16                    {"name": "textSetting", "@replacementChar": "*", "value": "redact"},
 17                ]},
 18            ]
 19        }
 20    })
 21    """
 22
 23    def __init__(self, default: str = "allow", config: dict = {}):
 24        self.default = default
 25        self.default_config_elements = [
 26            ConfigElement(
 27                name="sysConfig",
 28                switches=[Switch(name="interchange_type", value="xml")],
 29            ),
 30        ]
 31        self.config = config
 32
 33        super().__init__(
 34            default=self.default,
 35            default_config_elements=self.default_config_elements,
 36            config=self.config,
 37        )
 38
 39    def add_textItem(self, text: str, replacementChar: str, textSetting: str = "redact", **kwargs):
 40        """ Adds a textItem to the textSearchConfig textList subelements. """
 41        textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None)
 42        if not textSearchConfig:
 43            # Add textSearchConfig to ConfigElements
 44            textSearchConfig = ConfigElement(
 45                name="textSearchConfig",
 46                attributes={"libVersion": kwargs.get("libVersion", "core2")},
 47                subelements=[ConfigElement(name="textList")]
 48            )
 49            self.add_config_element(
 50                config_element=textSearchConfig,
 51            )
 52
 53        # Select the ConfigElement named textList
 54        textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList"))
 55
 56        # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates.
 57        #   (cannot redact "generic" with "*" and also redact "generic" with "@")
 58        for textItem in textList.subelements.copy():
 59            for switch in textItem.switches:
 60                if switch.name == "text" and switch.value.lower() == text.lower():
 61                    # Remove textItem from tetList.subelements using the builtin list .remove method
 62                    textList.subelements.remove(textItem)
 63                    break
 64
 65        # Add textItem to end of textList.subelements
 66        textList.subelements.append(
 67            ConfigElement(
 68                name="textItem",
 69                switches=[
 70                    Switch(name="text", value=text),
 71                    Switch(name="textSetting", attributes={"replacementChar": replacementChar}, value=textSetting)
 72                ],
 73            )
 74        )
 75
 76        # Don't sort textList: this preserves top-down order for redaction settings.
 77
 78    def remove_textItem(self, text: str):
 79        """ Removes a textItem from the textSearchConfig textList subelements. """
 80        textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None)
 81        if not textSearchConfig:
 82            raise glasswall.content_management.errors.config_elements.ConfigElementNotFound("textSearchConfig")
 83
 84        # Select the ConfigElement named textList
 85        textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList"))
 86
 87        all_textItem_texts = [
 88            switch.value.lower()
 89            for textItem in textList.subelements
 90            for switch in textItem.switches
 91            if switch.name == "text"
 92        ]
 93        if text not in all_textItem_texts:
 94            raise glasswall.content_management.errors.switches.SwitchNotFound(f"No switch found with name: 'text' and value: '{text}'")
 95
 96        # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates.
 97        #   (cannot redact "generic" with "*" and also redact "GeNeRiC" with "@")
 98        for textItem in textList.subelements.copy():
 99            for switch in textItem.switches:
100                if switch.name == "text" and switch.value.lower() == text.lower():
101                    textList.subelements.remove(textItem)
102                    break
 10class WordSearch(Policy):
 11    """ A content management policy for Word Search.
 12
 13    WordSearch(default="allow", config={
 14        "textSearchConfig": {
 15            "@libVersion": "core2",
 16            "textList": [
 17                {"name": "textItem", "switches": [
 18                    {"name": "text", "value": "generic"},
 19                    {"name": "textSetting", "@replacementChar": "*", "value": "redact"},
 20                ]},
 21            ]
 22        }
 23    })
 24    """
 25
 26    def __init__(self, default: str = "allow", config: dict = {}):
 27        self.default = default
 28        self.default_config_elements = [
 29            ConfigElement(
 30                name="sysConfig",
 31                switches=[Switch(name="interchange_type", value="xml")],
 32            ),
 33        ]
 34        self.config = config
 35
 36        super().__init__(
 37            default=self.default,
 38            default_config_elements=self.default_config_elements,
 39            config=self.config,
 40        )
 41
 42    def add_textItem(self, text: str, replacementChar: str, textSetting: str = "redact", **kwargs):
 43        """ Adds a textItem to the textSearchConfig textList subelements. """
 44        textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None)
 45        if not textSearchConfig:
 46            # Add textSearchConfig to ConfigElements
 47            textSearchConfig = ConfigElement(
 48                name="textSearchConfig",
 49                attributes={"libVersion": kwargs.get("libVersion", "core2")},
 50                subelements=[ConfigElement(name="textList")]
 51            )
 52            self.add_config_element(
 53                config_element=textSearchConfig,
 54            )
 55
 56        # Select the ConfigElement named textList
 57        textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList"))
 58
 59        # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates.
 60        #   (cannot redact "generic" with "*" and also redact "generic" with "@")
 61        for textItem in textList.subelements.copy():
 62            for switch in textItem.switches:
 63                if switch.name == "text" and switch.value.lower() == text.lower():
 64                    # Remove textItem from tetList.subelements using the builtin list .remove method
 65                    textList.subelements.remove(textItem)
 66                    break
 67
 68        # Add textItem to end of textList.subelements
 69        textList.subelements.append(
 70            ConfigElement(
 71                name="textItem",
 72                switches=[
 73                    Switch(name="text", value=text),
 74                    Switch(name="textSetting", attributes={"replacementChar": replacementChar}, value=textSetting)
 75                ],
 76            )
 77        )
 78
 79        # Don't sort textList: this preserves top-down order for redaction settings.
 80
 81    def remove_textItem(self, text: str):
 82        """ Removes a textItem from the textSearchConfig textList subelements. """
 83        textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None)
 84        if not textSearchConfig:
 85            raise glasswall.content_management.errors.config_elements.ConfigElementNotFound("textSearchConfig")
 86
 87        # Select the ConfigElement named textList
 88        textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList"))
 89
 90        all_textItem_texts = [
 91            switch.value.lower()
 92            for textItem in textList.subelements
 93            for switch in textItem.switches
 94            if switch.name == "text"
 95        ]
 96        if text not in all_textItem_texts:
 97            raise glasswall.content_management.errors.switches.SwitchNotFound(f"No switch found with name: 'text' and value: '{text}'")
 98
 99        # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates.
100        #   (cannot redact "generic" with "*" and also redact "GeNeRiC" with "@")
101        for textItem in textList.subelements.copy():
102            for switch in textItem.switches:
103                if switch.name == "text" and switch.value.lower() == text.lower():
104                    textList.subelements.remove(textItem)
105                    break

A content management policy for Word Search.

WordSearch(default="allow", config={ "textSearchConfig": { "@libVersion": "core2", "textList": [ {"name": "textItem", "switches": [ {"name": "text", "value": "generic"}, {"name": "textSetting", "@replacementChar": "*", "value": "redact"}, ]}, ] } })

WordSearch(default: str = 'allow', config: dict = {})
26    def __init__(self, default: str = "allow", config: dict = {}):
27        self.default = default
28        self.default_config_elements = [
29            ConfigElement(
30                name="sysConfig",
31                switches=[Switch(name="interchange_type", value="xml")],
32            ),
33        ]
34        self.config = config
35
36        super().__init__(
37            default=self.default,
38            default_config_elements=self.default_config_elements,
39            config=self.config,
40        )
default
default_config_elements
config
def add_textItem( self, text: str, replacementChar: str, textSetting: str = 'redact', **kwargs):
42    def add_textItem(self, text: str, replacementChar: str, textSetting: str = "redact", **kwargs):
43        """ Adds a textItem to the textSearchConfig textList subelements. """
44        textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None)
45        if not textSearchConfig:
46            # Add textSearchConfig to ConfigElements
47            textSearchConfig = ConfigElement(
48                name="textSearchConfig",
49                attributes={"libVersion": kwargs.get("libVersion", "core2")},
50                subelements=[ConfigElement(name="textList")]
51            )
52            self.add_config_element(
53                config_element=textSearchConfig,
54            )
55
56        # Select the ConfigElement named textList
57        textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList"))
58
59        # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates.
60        #   (cannot redact "generic" with "*" and also redact "generic" with "@")
61        for textItem in textList.subelements.copy():
62            for switch in textItem.switches:
63                if switch.name == "text" and switch.value.lower() == text.lower():
64                    # Remove textItem from tetList.subelements using the builtin list .remove method
65                    textList.subelements.remove(textItem)
66                    break
67
68        # Add textItem to end of textList.subelements
69        textList.subelements.append(
70            ConfigElement(
71                name="textItem",
72                switches=[
73                    Switch(name="text", value=text),
74                    Switch(name="textSetting", attributes={"replacementChar": replacementChar}, value=textSetting)
75                ],
76            )
77        )
78
79        # Don't sort textList: this preserves top-down order for redaction settings.

Adds a textItem to the textSearchConfig textList subelements.

def remove_textItem(self, text: str):
 81    def remove_textItem(self, text: str):
 82        """ Removes a textItem from the textSearchConfig textList subelements. """
 83        textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None)
 84        if not textSearchConfig:
 85            raise glasswall.content_management.errors.config_elements.ConfigElementNotFound("textSearchConfig")
 86
 87        # Select the ConfigElement named textList
 88        textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList"))
 89
 90        all_textItem_texts = [
 91            switch.value.lower()
 92            for textItem in textList.subelements
 93            for switch in textItem.switches
 94            if switch.name == "text"
 95        ]
 96        if text not in all_textItem_texts:
 97            raise glasswall.content_management.errors.switches.SwitchNotFound(f"No switch found with name: 'text' and value: '{text}'")
 98
 99        # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates.
100        #   (cannot redact "generic" with "*" and also redact "GeNeRiC" with "@")
101        for textItem in textList.subelements.copy():
102            for switch in textItem.switches:
103                if switch.name == "text" and switch.value.lower() == text.lower():
104                    textList.subelements.remove(textItem)
105                    break

Removes a textItem from the textSearchConfig textList subelements.