Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Fixes #57 runs black & isort for code formatting check #61

Merged
merged 3 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
787844dc339e090aa0e9ac2241895365522c4119
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ safety: ## Runs `safety check` to check python dependencies for vulnerabilities
done

.PHONY: lint
lint: ## Run flake8
lint: isort black ## Run isort, black and flake8
@flake8 securedrop_proxy tests

.PHONY: mypy
mypy: ## Run mypy static type checker
@mypy --ignore-missing-imports securedrop_proxy

.PHONY: black
black: ## Run black for file formatting
@black --config ./blackconfig/pyproject.toml --check securedrop_proxy tests

.PHONY: isort
isort: ## Run isort for file formatting
@isort -c -w 100 securedrop_proxy/*.py tests/*.py --diff

.PHONY: update-pip-requirements
update-pip-requirements: ## Updates all Python requirements files via pip-compile.
Expand Down
2 changes: 2 additions & 0 deletions blackconfig/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 100
8 changes: 5 additions & 3 deletions dev-requirements.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
black==19.10b0
coverage==5.0
flake8==3.6.0
isort==4.3.21
mccabe==0.6.1
multidict==4.4.2
mypy==0.701
mypy-extensions==0.4.1
pip-tools==3.1.0
mypy==0.761
mypy-extensions==0.4.3
pip-tools==4.3.0
pycodestyle==2.4.0
pyflakes==2.0.0
six==1.11.0
Expand Down
205 changes: 143 additions & 62 deletions dev-requirements.txt

Large diffs are not rendered by default.

21 changes: 7 additions & 14 deletions securedrop_proxy/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@
import json
import logging
import os
import sys
import platform
import sys
from logging.handlers import SysLogHandler, TimedRotatingFileHandler

from logging.handlers import TimedRotatingFileHandler, SysLogHandler

from securedrop_proxy import main
from securedrop_proxy import proxy
from securedrop_proxy import main, proxy
from securedrop_proxy.version import version


DEFAULT_HOME = os.path.join(os.path.expanduser("~"), ".securedrop_proxy")
LOGLEVEL = os.environ.get("LOGLEVEL", "info").upper()


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.
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()
Expand All @@ -36,9 +33,7 @@ def start() -> None:

# 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"
)
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.
Expand Down Expand Up @@ -74,9 +69,7 @@ def configure_logging() -> None:
log_file = os.path.join(home, "logs", "proxy.log")

# set logging format
log_fmt = (
"%(asctime)s - %(name)s:%(lineno)d(%(funcName)s) %(levelname)s: %(message)s"
)
log_fmt = "%(asctime)s - %(name)s:%(lineno)d(%(funcName)s) %(levelname)s: %(message)s"
formatter = logging.Formatter(log_fmt)

# define log handlers such as for rotating log files
Expand Down
4 changes: 1 addition & 3 deletions securedrop_proxy/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json
import logging
from typing import Dict, Any
from typing import Any, Dict

from securedrop_proxy import proxy


from securedrop_proxy.proxy import Proxy

logger = logging.getLogger(__name__)
Expand Down
45 changes: 14 additions & 31 deletions securedrop_proxy/proxy.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import furl
import http
import json
import logging
import requests
import tempfile
import werkzeug

import os
import subprocess
import sys
import tempfile
import uuid
import yaml
from tempfile import _TemporaryFileWrapper # type: ignore
from typing import Dict, Optional

import securedrop_proxy.version as version
import furl
import requests
import werkzeug
import yaml

from tempfile import _TemporaryFileWrapper # type: ignore
import securedrop_proxy.version as version

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,10 +44,7 @@ def __init__(self, status: int) -> None:


class Proxy:
def __init__(
self, conf_path: str, req: Req = Req(), timeout: float = 120.0
) -> None:
# The configuration path for Proxy is a must.
def __init__(self, conf_path: str, req: Req = Req(), timeout: float = 10.0) -> None:
self.read_conf(conf_path)

self.req = req
Expand All @@ -75,28 +71,20 @@ def err_on_done(self):
def read_conf(self, conf_path: str) -> None:

if not os.path.isfile(conf_path):
self.simple_error(
500, "Configuration file does not exist at {}".format(conf_path)
)
self.simple_error(500, "Configuration file does not exist at {}".format(conf_path))
self.err_on_done()

try:
with open(conf_path) as fh:
conf_in = yaml.safe_load(fh)
except yaml.YAMLError:
self.simple_error(
500,
"YAML syntax error while reading configuration file {}".format(
conf_path
),
500, "YAML syntax error while reading configuration file {}".format(conf_path),
)
self.err_on_done()
except Exception:
self.simple_error(
500,
"Error while opening or reading configuration file {}".format(
conf_path
),
500, "Error while opening or reading configuration file {}".format(conf_path),
)
self.err_on_done()

Expand Down Expand Up @@ -266,23 +254,18 @@ def proxy(self) -> None:
requests.exceptions.TooManyRedirects,
) as e:
logger.error(e)
self.simple_error(
http.HTTPStatus.BAD_GATEWAY, "could not connect to server"
)
self.simple_error(http.HTTPStatus.BAD_GATEWAY, "could not connect to server")
except requests.exceptions.HTTPError as e:
logger.error(e)
try:
self.simple_error(
e.response.status_code,
http.HTTPStatus(e.response.status_code).phrase.lower(),
e.response.status_code, http.HTTPStatus(e.response.status_code).phrase.lower(),
)
except ValueError:
# Return a generic error message when the response
# status code is not found in http.HTTPStatus.
self.simple_error(e.response.status_code, "unspecified server error")
except Exception as e:
logger.error(e)
self.simple_error(
http.HTTPStatus.INTERNAL_SERVER_ERROR, "internal proxy error"
)
self.simple_error(http.HTTPStatus.INTERNAL_SERVER_ERROR, "internal proxy error")
self.on_done()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description_content_type="text/markdown",
license="GPLv3+",
install_requires=["requests", "furl", "pyyaml", "werkzeug"],
python_requires=">=3.5",
python_requires=">=3.7",
url="https://github.com/freedomofpress/securedrop-proxy",
packages=setuptools.find_packages(exclude=["docs", "tests"]),
package_data={
Expand Down
7 changes: 3 additions & 4 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest.mock import patch

import vcr

from securedrop_proxy import entrypoint


Expand Down Expand Up @@ -53,7 +54,7 @@ def test_missing_config(self):
@patch("securedrop_proxy.entrypoint.TimedRotatingFileHandler")
def test_configure_logging(self, mock_log_conf, mock_log_conf_sys, mock_logging):
with sdhome() as homedir:
mock_log_file = os.path.join(homedir, 'logs', 'proxy.log')
mock_log_file = os.path.join(homedir, "logs", "proxy.log")
entrypoint.configure_logging()
mock_log_conf.assert_called_once_with(mock_log_file)
# For rsyslog handler
Expand All @@ -71,9 +72,7 @@ def test_unwritable_log_folder(self):
output = None
with sdhome() as home:
os.chmod(home, 0o0444)
with unittest.mock.patch(
"sys.stdout", new_callable=io.StringIO
) as mock_stdout:
with unittest.mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
with self.assertRaises(SystemExit):
entrypoint.start()
output = mock_stdout.getvalue()
Expand Down
7 changes: 3 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import http
from io import StringIO
import json
import subprocess
import sys
import types
import unittest
import uuid
import types
from io import StringIO

import vcr

from securedrop_proxy import main
from securedrop_proxy import proxy
from securedrop_proxy import main, proxy


class TestMain(unittest.TestCase):
Expand Down
13 changes: 5 additions & 8 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import sys
import http
import json
import sys
import tempfile
import types
import unittest
import uuid
import types
from io import StringIO
import tempfile
from unittest.mock import patch

import requests
import vcr

from securedrop_proxy import proxy
from securedrop_proxy import version
from securedrop_proxy import proxy, version


class TestProxyValidConfig(unittest.TestCase):
Expand Down Expand Up @@ -245,9 +244,7 @@ def test_internal_server_error(self):
self.assertEqual(p.res.status, http.HTTPStatus.INTERNAL_SERVER_ERROR)
self.assertIn("application/json", p.res.headers["Content-Type"])
body = json.loads(p.res.body)
self.assertEqual(
body["error"], http.HTTPStatus.INTERNAL_SERVER_ERROR.phrase.lower()
)
self.assertEqual(body["error"], http.HTTPStatus.INTERNAL_SERVER_ERROR.phrase.lower())

@vcr.use_cassette("fixtures/proxy_internal_error.yaml")
def test_internal_error(self):
Expand Down