예제 #1
0
    def _run_interactive_bash(
        self,
        runner: ArmoryInstance,
        check_run=False,
        num_eval_batches=None,
        skip_benign=None,
        skip_attack=None,
        validate_config=None,
    ) -> None:
        user_group_id = self.get_id()
        lines = [
            "Container ready for interactive use.",
            bold(
                "*** In a new terminal, run the following to attach to the container:"
            ),
            bold(
                red(
                    f"    docker exec -it -u {user_group_id} {runner.docker_container.short_id} bash"
                )
            ),
        ]
        if self.config.get("scenario"):
            options = self._build_options(
                check_run=check_run,
                num_eval_batches=num_eval_batches,
                skip_benign=skip_benign,
                skip_attack=skip_attack,
                validate_config=validate_config,
            )
            tmp_dir = os.path.join(self.host_paths.tmp_dir, self.config["eval_id"])
            os.makedirs(tmp_dir)
            self.tmp_config = os.path.join(tmp_dir, "interactive-config.json")
            docker_config_path = os.path.join(
                paths.runtime_paths().tmp_dir,
                self.config["eval_id"],
                "interactive-config.json",
            )
            with open(self.tmp_config, "w") as f:
                f.write(json.dumps(self.config, sort_keys=True, indent=4) + "\n")

            lines.extend(
                [
                    bold("*** To run your scenario in the container:"),
                    bold(
                        red(
                            f"    python -m armory.scenarios.base {docker_config_path}{options} --load-config-from-file"
                        )
                    ),
                    bold("*** To gracefully shut down container, press: Ctrl-C"),
                    "",
                ]
            )
        logger.info("\n".join(lines))
        while True:
            time.sleep(1)
예제 #2
0
파일: evaluator.py 프로젝트: rperi/armory
    def _run_config(
        self,
        runner: ArmoryInstance,
        check_run=False,
        num_eval_batches=None,
        skip_benign=None,
        skip_attack=None,
        validate_config=None,
    ) -> int:
        logger.info(bold(red("Running evaluation script")))

        b64_config = self._b64_encode_config()
        options = self._build_options(
            check_run=check_run,
            num_eval_batches=num_eval_batches,
            skip_benign=skip_benign,
            skip_attack=skip_attack,
            validate_config=validate_config,
        )
        if self.no_docker:
            kwargs = {}
            python = sys.executable
        else:
            kwargs = {"user": self.get_id()}
            python = "python"

        cmd = f"{python} -m armory.scenarios.base {b64_config}{options}"
        return runner.exec_cmd(cmd, **kwargs)
예제 #3
0
 def _run_interactive_bash(self, runner) -> None:
     lines = [
         "Container ready for interactive use.",
         bold(
             "*** In a new terminal, run the following to attach to the container:"
         ),
         bold(
             red(f"    docker exec -itu0 {runner.docker_container.short_id} bash"
                 )),
         bold("*** To run your script in the container:"),
         bold(
             red(f"    python -m {self.config['evaluation']['eval_file']} {self.unix_config_path}"
                 )),
         bold("*** To gracefully shut down container, press: Ctrl-C"),
         "",
     ]
     logger.info("\n".join(lines))
     while True:
         time.sleep(1)
예제 #4
0
    def _run_interactive_bash(self, runner: ArmoryInstance) -> None:
        user_id = os.getuid() if os.name != "nt" else 0
        group_id = os.getgid() if os.name != "nt" else 0
        lines = [
            "Container ready for interactive use.",
            bold(
                "*** In a new terminal, run the following to attach to the container:"
            ),
            bold(
                red(
                    f"    docker exec -it -u {user_id}:{group_id} {runner.docker_container.short_id} bash"
                )
            ),
        ]
        if self.config.get("scenario"):
            tmp_dir = os.path.join(self.host_paths.tmp_dir, self.config["eval_id"])
            os.makedirs(tmp_dir)
            self.tmp_config = os.path.join(tmp_dir, "interactive-config.json")
            docker_config_path = os.path.join(
                paths.runtime_paths().tmp_dir,
                self.config["eval_id"],
                "interactive-config.json",
            )
            with open(self.tmp_config, "w") as f:
                f.write(json.dumps(self.config, sort_keys=True, indent=4) + "\n")

            lines.extend(
                [
                    bold("*** To run your scenario in the container:"),
                    bold(
                        red(
                            f"    python -m armory.scenarios.base {docker_config_path} --load-config-from-file"
                        )
                    ),
                    bold("*** To gracefully shut down container, press: Ctrl-C"),
                    "",
                ]
            )
        logger.info("\n".join(lines))
        while True:
            time.sleep(1)
예제 #5
0
    def _run_config(self, runner: ArmoryInstance, check_run=False) -> None:
        logger.info(bold(red("Running evaluation script")))

        b64_config = self._b64_encode_config()
        options = ""
        if self.no_docker:
            options += " --no-docker"
        if check_run:
            options += " --check"
        if logger.level == logging.DEBUG:
            options += " --debug"

        runner.exec_cmd(f"python -m armory.scenarios.base {b64_config}{options}")
예제 #6
0
 def _run_interactive_bash(self, runner) -> None:
     lines = [
         "Container ready for interactive use.",
         bold(
             "*** In a new terminal, run the following to attach to the container:"
         ),
         bold(
             red(f"    docker exec -it -u {self.user_id}:{self.group_id} {runner.docker_container.short_id} bash"
                 )),
     ]
     if self.config.get("scenario"):
         lines.extend([
             bold("*** To run your scenario in the container:"),
             bold(
                 red(f"    python -m armory.scenarios.base {self.docker_config_path}"
                     )),
             bold("*** To gracefully shut down container, press: Ctrl-C"),
             "",
         ])
     logger.info("\n".join(lines))
     while True:
         time.sleep(1)
예제 #7
0
 def _run_jupyter(self, runner, host_port=8888) -> None:
     lines = [
         "About to launch jupyter.",
         bold(
             "*** To connect to jupyter, please open the following in a browser:"
         ),
         bold(red(f"    http://127.0.0.1:{host_port}")),
         bold(
             "*** To connect on the command line as well, in a new terminal, run:"
         ),
         bold(
             f"    docker exec -it -u {self.user_id}:{self.group_id} {runner.docker_container.short_id} bash"
         ),
         bold("*** To gracefully shut down container, press: Ctrl-C"),
         "",
         "Jupyter notebook log:",
     ]
     logger.info("\n".join(lines))
     runner.exec_cmd(
         "jupyter lab --ip=0.0.0.0 --no-browser --allow-root --NotebookApp.token=''",
         user="******",
     )
예제 #8
0
    def _run_jupyter(self, runner: ArmoryInstance, ports: dict) -> None:
        if not self.root:
            logger.warning("Running Jupyter Lab as root inside the container.")

        user_group_id = self.get_id()
        port = list(ports.keys())[0]
        lines = [
            "About to launch jupyter.",
            bold("*** To connect on the command line as well, in a new terminal, run:"),
            bold(
                red(
                    f"    docker exec -it -u {user_group_id} {runner.docker_container.short_id} bash"
                )
            ),
            bold("*** To gracefully shut down container, press: Ctrl-C"),
            "",
            "Jupyter notebook log:",
        ]
        logger.info("\n".join(lines))
        runner.exec_cmd(
            f"jupyter lab --ip=0.0.0.0 --port {port} --no-browser --allow-root",
            user="******",
        )
예제 #9
0
 def _run_command(self, runner: ArmoryInstance, command: str) -> int:
     logger.info(bold(red(f"Running bash command: {command}")))
     return runner.exec_cmd(command,
                            user=self.get_id(),
                            expect_sentinel=False)
예제 #10
0
 def _run_command(self, runner: ArmoryInstance, command: str) -> None:
     logger.info(bold(red(f"Running bash command: {command}")))
     runner.exec_cmd(command)
예제 #11
0
 def _run_command(self, runner, command) -> None:
     logger.info(bold(red(f"Running bash command: {command}")))
     runner.exec_cmd(command)
예제 #12
0
 def _run_config(self, runner) -> None:
     logger.info(bold(red("Running evaluation script")))
     runner.exec_cmd(
         f"python -m armory.scenarios.base {self.docker_config_path}")
예제 #13
0
 def _run_config(self, runner) -> None:
     logger.info(bold(red("Running evaluation script")))
     runner.exec_cmd(
         f"python -m {self.config['evaluation']['eval_file']} {self.unix_config_path}"
     )