Ejemplo n.º 1
0
    def status(self) -> str:
        """
        this function returns the current status of the acceptance process.
        this is useful for calls of external software.
        when the status is waited the file content is read and the final acceptance status is
        determined here. the temporary file is also deleted in this function.

        possible status values:
        - ready
        - disabled
        - started
        - waiting
        - waited
        - accepted
        - not accepted
        """

        if self.mystatus == "waited":
            content = file_get_contents(self.filename)
            content = content.replace("\n", "")
            content = content.replace("\t", "")

            if content.lower() == "true":
                info("acceptance result: Accepted")
                self.set_status("accepted")
            else:
                info("acceptance result: Not Accepted (file content: {})".
                     format(content))
                self.set_status("not accepted")

            delete_file_if_exists(self.filename)

        return self.mystatus
Ejemplo n.º 2
0
    def diff_new_current(self, ruletype: str) -> bool:
        """
        TODO: Doku

        True = There are differences between new and current
        False = There are no differences between new and current
        """
        state = False

        new = file_get_contents("{}/new/{}".format(self.rulesfolder, ruletype))
        current = file_get_contents("{}/current/{}".format(
            self.rulesfolder, ruletype))
        if new != current:
            return True

        return state
Ejemplo n.º 3
0
    def wait(self) -> None:
        """
        this function executes a sleep for the configured duration.
        the sleep is only executed when the start function was triggered before
        and not if the status is disabled.
        """
        if self.mystatus == "started":
            seconds = deepcopy(self.duration)
            info("acceptance wait process started. waiting for {} seconds".
                 format(seconds))
            self.set_status("waiting")

            while seconds > 0:
                content = file_get_contents(self.filename)
                content = content.replace("\n", "")
                content = content.replace("\t", "")
                content = content.replace(" ", "")
                if content.lower() == "true":
                    break
                sleep(2)
                seconds = seconds - 2

            self.set_status("waited")

        info("acceptance wait process finished")
Ejemplo n.º 4
0
 def get_acceptance_status(self) -> str:
     """
     get the status of the current acceptance
     """
     filepath = ".acceptance_status"
     if file_exists(filepath):
         return file_get_contents(filepath)
     return ""
Ejemplo n.º 5
0
 def test_save_new_rules(self):
     """
     TODO: Doku
     """
     self.rules.save_new_rules("tcp", ["80", "443"])
     self.assertEqual(
         file_get_contents("{}/new/tcp".format(self.rules.rulesfolder)),
         "80\n443")
Ejemplo n.º 6
0
 def copy_rules(self, source: str, dest: str) -> None:
     """
     TODO: Doku
     """
     for ruletype in self.types:
         content = file_get_contents("{}/{}/{}".format(
             self.rulesfolder, source, ruletype))
         write_into_file(
             "{}/{}/{}".format(self.rulesfolder, dest, ruletype), content)
Ejemplo n.º 7
0
 def status(self) -> str:
     """
     the function lists the iptables configuration as string
     this is not machine readable!
     """
     tmpfile = ".iptables_list"
     execute_os_command("{} -L > {}".format(self.iptables_bin, tmpfile))
     content = file_get_contents(tmpfile)
     delete_file_if_exists(tmpfile)
     return content
Ejemplo n.º 8
0
 def get_default_payload(self, title, css="easywall"):
     """the function creates a object of information that are needed on every page"""
     payload = DefaultPayload()
     payload.year = datetime.today().year
     payload.title = title
     payload.customcss = css
     payload.machine = self.get_machine_infos()
     payload.latest_version = self.cfg.get_value("VERSION", "version")
     payload.current_version = file_get_contents("{}/../.version".format(get_abs_path_of_filepath(__file__)))
     payload.commit_sha = self.cfg.get_value("VERSION", "sha")
     payload.commit_date = self.get_commit_date(self.cfg.get_value("VERSION", "date"))
     return payload
Ejemplo n.º 9
0
    def test_backup_current_rules(self) -> None:
        """
        TODO: Doku
        """
        write_into_file("{}/current/tcp".format(self.rules.rulesfolder), """80
443
""")
        write_into_file("{}/backup/tcp".format(self.rules.rulesfolder), "")
        self.rules.backup_current_rules()
        self.assertEqual(file_get_contents("{}/backup/tcp".format(self.rules.rulesfolder)), """80
443
""")
Ejemplo n.º 10
0
 def test_file(self):
     assert not file_exists("testfile")
     create_file_if_not_exists("testfile")
     assert file_exists("testfile")
     write_into_file("testfile", "testcontent")
     assert file_get_contents("testfile") == "testcontent"
     assert len(get_abs_path_of_filepath("testfile")) > 0
     rename_file("testfile", "testfilenew")
     assert not file_exists("testfile")
     assert file_exists("testfilenew")
     delete_file_if_exists("testfilenew")
     assert not file_exists("testfile")
     assert not file_exists("testfilenew")
Ejemplo n.º 11
0
 def test_file(self) -> None:
     """TODO: Doku."""
     self.assertFalse(file_exists("testfile"))
     create_file_if_not_exists("testfile")
     self.assertTrue(file_exists("testfile"))
     write_into_file("testfile", "testcontent")
     self.assertEqual(file_get_contents("testfile"), "testcontent")
     self.assertGreater(len(get_abs_path_of_filepath("testfile")), 0)
     rename_file("testfile", "testfilenew")
     self.assertFalse(file_exists("testfile"))
     self.assertTrue(file_exists("testfilenew"))
     delete_file_if_exists("testfilenew")
     self.assertFalse(file_exists("testfile"))
     self.assertFalse(file_exists("testfilenew"))
Ejemplo n.º 12
0
 def get_default_payload(self,
                         title: str,
                         css: str = "easywall") -> DefaultPayload:
     """the function creates a object of information that are needed on every page"""
     payload = DefaultPayload()
     payload.year = datetime.today().year
     payload.title = title
     payload.customcss = css
     payload.machine = self.get_machine_infos()
     payload.latest_version = str(self.cfg.get_value("VERSION", "version"))
     payload.current_version = file_get_contents(".version")
     payload.commit_sha = str(self.cfg.get_value("VERSION", "sha"))
     payload.commit_date = self.get_commit_date(
         str(self.cfg.get_value("VERSION", "date")))
     payload.config_mismatch = self.get_config_version_mismatch()
     return payload
Ejemplo n.º 13
0
 def test_file(self):
     """
     TODO: Doku
     """
     assert not file_exists("testfile")
     create_file_if_not_exists("testfile")
     assert file_exists("testfile")
     write_into_file("testfile", "testcontent")
     assert file_get_contents("testfile") == "testcontent"
     self.assertGreater(len(get_abs_path_of_filepath("testfile")), 0)
     rename_file("testfile", "testfilenew")
     assert not file_exists("testfile")
     assert file_exists("testfilenew")
     delete_file_if_exists("testfilenew")
     assert not file_exists("testfile")
     assert not file_exists("testfilenew")
Ejemplo n.º 14
0
 def get_new_rules(self, ruletype: str) -> list:
     """
     TODO: Doku
     """
     return file_get_contents("{}/new/{}".format(self.rulesfolder,
                                                 ruletype)).splitlines()
Ejemplo n.º 15
0
 def get_current_rules(self, ruletype: str) -> List[str]:
     """
     TODO: Doku
     """
     return file_get_contents("{}/current/{}".format(
         self.rulesfolder, ruletype)).splitlines()
Ejemplo n.º 16
0
 def load(self) -> dict:
     """TODO: Doku."""
     content = file_get_contents(self.filepath)
     return dict(load(content, Loader=Loader))