コード例 #1
0
def start() -> None:
    """
    Set up a new proxy object with an error handler, configuration that we read
    from  argv[1], and the original user request from STDIN.
    """
    try:
        configure_logging()

        logging.debug("Starting SecureDrop Proxy {}".format(version))

        # path to config file must be at argv[1]
        if len(sys.argv) != 2:
            raise ValueError(
                "sd-proxy script not called with path to configuration file")

        # read config. `read_conf` will call `p.err_on_done` if there is a config
        # problem, and will return a Conf object on success.
        conf_path = sys.argv[1]
        # a fresh, new proxy object
        p = proxy.Proxy(conf_path=conf_path)

        # read user request from STDIN
        incoming_lines = []
        for line in sys.stdin:
            incoming_lines.append(line)
        incoming = "\n".join(incoming_lines)

        main.__main__(incoming, p)
    except Exception as e:
        response = {
            "status": http.HTTPStatus.INTERNAL_SERVER_ERROR,
            "body": json.dumps({"error": str(e)}),
        }
        print(json.dumps(response))
        sys.exit(1)
コード例 #2
0
    def on_save(self, fh: IO[bytes], res: Response) -> None:
        fn = str(uuid.uuid4())

        try:
            with tempfile.TemporaryDirectory() as tmpdir:
                tmpfile = os.path.join(os.path.abspath(tmpdir), fn)
                subprocess.run(["cp", fh.name, tmpfile])
                if self.conf.dev is not True:
                    subprocess.run(
                        ["qvm-move-to-vm", self.conf.target_vm, tmpfile])
        except Exception:
            res.status = 500
            res.headers["Content-Type"] = "application/json"
            res.headers["X-Origin-Content-Type"] = res.headers["Content-Type"]
            res.body = json.dumps({
                "error":
                "Unhandled error while handling non-JSON content, sorry"
            })
            return

        res.headers["Content-Type"] = "application/json"
        res.headers["X-Origin-Content-Type"] = res.headers["Content-Type"]
        res.body = json.dumps({"filename": fn})
コード例 #3
0
 def err_on_done(self):
     print(json.dumps(self.res.__dict__))
     sys.exit(1)
コード例 #4
0
 def on_done(self) -> None:
     print(json.dumps(self.res.__dict__))
コード例 #5
0
    def simple_error(self, status: int, err: str) -> None:
        res = Response(status)
        res.body = json.dumps({"error": err})
        res.headers = {"Content-Type": "application/json"}

        self.res = res
コード例 #6
0
 def test_dumps(self):
     """Simple check since this is a passthrough to stdlib json"""
     self.assertEqual(json.dumps({
         "foo": "bar",
         "baz": ["one"]
     }), '{"foo": "bar", "baz": ["one"]}')