-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from freedomofpress/cli-args
Added CLI args and set inject home directory
- Loading branch information
Showing
13 changed files
with
424 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
while [ -n "$1" ]; do | ||
param="$1" | ||
value="$2" | ||
case $param in | ||
--sdc-home) | ||
SDC_HOME="$value" | ||
shift | ||
;; | ||
*) | ||
break | ||
esac | ||
shift | ||
done | ||
|
||
SDC_HOME=${SDC_HOME:-$(mktemp -d)} | ||
|
||
echo "Running app with home directory: $SDC_HOME" | ||
|
||
# create the database for local testing | ||
|
||
python - << EOF | ||
from securedrop_client.models import Base, make_engine | ||
Base.metadata.create_all(make_engine("$SDC_HOME")) | ||
EOF | ||
|
||
exec python -m securedrop_client --sdc-home "$SDC_HOME" $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .app import run | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
|
||
|
||
def safe_mkdir(sdc_home: str, relative_path: str=None) -> None: | ||
''' | ||
Safely create directories while checking permissions along the way. | ||
''' | ||
check_dir_permissions(sdc_home) | ||
|
||
if not relative_path: | ||
return | ||
|
||
full_path = os.path.join(sdc_home, relative_path) | ||
if not full_path == os.path.abspath(full_path): | ||
raise ValueError('Path is not absolute: {}'.format(full_path)) | ||
|
||
path_components = split_path(relative_path) | ||
|
||
path_so_far = sdc_home | ||
for component in path_components: | ||
path_so_far = os.path.join(path_so_far, component) | ||
check_dir_permissions(path_so_far) | ||
os.makedirs(path_so_far, 0o0700, exist_ok=True) | ||
|
||
|
||
def check_dir_permissions(dir_path: str) -> None: | ||
''' | ||
Check that a directory has ``700`` as the final 3 bytes. Raises a | ||
``RuntimeError`` otherwise. | ||
''' | ||
if os.path.exists(dir_path): | ||
stat_res = os.stat(dir_path).st_mode | ||
masked = stat_res & 0o777 | ||
if masked & 0o077: | ||
raise RuntimeError('Unsafe permissions ({}) on {}' | ||
.format(oct(stat_res), dir_path)) | ||
|
||
|
||
def split_path(path: str) -> list: | ||
out = [] | ||
|
||
while path: | ||
path, tail = os.path.split(path) | ||
out.append(tail) | ||
|
||
out.reverse() | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
def safe_tmpdir(tmpdir): | ||
os.chmod(str(tmpdir), 0o0700) | ||
return tmpdir |
Oops, something went wrong.