Skip to content

Commit

Permalink
Merge pull request #55 from freedomofpress/more_typing
Browse files Browse the repository at this point in the history
Restructures the code base with more object methods
  • Loading branch information
rmol authored Jan 10, 2020
2 parents e12b896 + c17bac9 commit 21c2963
Show file tree
Hide file tree
Showing 13 changed files with 552 additions and 582 deletions.
19 changes: 0 additions & 19 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ common-steps:
version: 2
jobs:
build-stretch:
docker:
- image: circleci/python:3.5-stretch
steps:
- checkout
- *install_packaging_dependencies
- *verify_requirements
- *make_source_tarball
- *build_debian_package

build-buster:
docker:
- image: circleci/python:3.7-buster
Expand All @@ -67,13 +57,6 @@ jobs:
- *make_source_tarball
- *build_debian_package

test-stretch:
docker:
- image: circleci/python:3.5-stretch
steps:
- checkout
- *run_tests

test-buster:
docker:
- image: circleci/python:3.7-buster
Expand All @@ -85,7 +68,5 @@ workflows:
version: 2
securedrop_proxy_ci:
jobs:
- test-stretch
- test-buster
- build-stretch
- build-buster
44 changes: 0 additions & 44 deletions securedrop_proxy/callbacks.py

This file was deleted.

58 changes: 0 additions & 58 deletions securedrop_proxy/config.py

This file was deleted.

52 changes: 22 additions & 30 deletions securedrop_proxy/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,68 @@

from logging.handlers import TimedRotatingFileHandler

from securedrop_proxy import callbacks
from securedrop_proxy import config
from securedrop_proxy import main
from securedrop_proxy import proxy
from securedrop_proxy.version import version


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


def start():
'''
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))

# a fresh, new proxy object
p = proxy.Proxy()

# set up an error handler early, so we can use it during
# configuration, etc
original_on_done = p.on_done
p.on_done = callbacks.err_on_done
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")
raise ValueError(
"sd-proxy script not called with path to configuration file"
)

# read config. `read_conf` will call `p.on_done` if there is a config
# 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]
p.conf = config.read_conf(conf_path, p)
# a fresh, new proxy object
p = proxy.Proxy(conf_path=conf_path)

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

p.on_done = original_on_done
main.__main__(incoming, p)
except Exception as e:
response = {
"status": http.HTTPStatus.INTERNAL_SERVER_ERROR,
"body": json.dumps({
"error": str(e),
})
"body": json.dumps({"error": str(e)}),
}
print(json.dumps(response))
sys.exit(1)


def configure_logging() -> None:
'''
"""
All logging related settings are set up by this function.
'''
"""
home = os.getenv("SECUREDROP_HOME", DEFAULT_HOME)
log_folder = os.path.join(home, 'logs')
log_folder = os.path.join(home, "logs")
if not os.path.exists(log_folder):
os.makedirs(log_folder)

log_file = os.path.join(home, 'logs', 'proxy.log')
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
33 changes: 17 additions & 16 deletions securedrop_proxy/main.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import json
import logging
from typing import Dict, Any

from securedrop_proxy import callbacks
from securedrop_proxy import proxy


from securedrop_proxy.proxy import Proxy

logger = logging.getLogger(__name__)


def __main__(incoming, p):
'''
def __main__(incoming: str, p: Proxy) -> None:
"""
Deserialize incoming request in order to build and send a proxy request.
'''
logging.debug('Creating request to be sent by proxy')
"""
logging.debug("Creating request to be sent by proxy")

client_req = None
client_req: Dict[str, Any] = {}
try:
client_req = json.loads(incoming)
except json.decoder.JSONDecodeError as e:
logging.error(e)
p.simple_error(400, 'Invalid JSON in request')
p.on_done(p.res)
p.simple_error(400, "Invalid JSON in request")
p.on_done()
return

req = proxy.Req()
try:
req.method = client_req['method']
req.path_query = client_req['path_query']
req.method = client_req["method"]
req.path_query = client_req["path_query"]
except KeyError as e:
logging.error(e)
p.simple_error(400, 'Missing keys in request')
p.on_done(p.res)
p.simple_error(400, "Missing keys in request")
p.on_done()

if "headers" in client_req:
req.headers = client_req['headers']
req.headers = client_req["headers"]

if "body" in client_req:
req.body = client_req['body']
req.body = client_req["body"]

p.req = req
if not p.on_save:
p.on_save = callbacks.on_save

p.proxy()
Loading

0 comments on commit 21c2963

Please sign in to comment.