Skip to content

Commit

Permalink
Add authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Graves committed Feb 24, 2016
1 parent 4762741 commit c1aec4b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
5 changes: 3 additions & 2 deletions slingshot/app.py
Expand Up @@ -28,8 +28,9 @@ def copy_dir(directory, destination):
return dest


def submit(archive, url):
r = requests.post(url, files={'file': io.open(archive, 'rb')})
def submit(archive, url, auth=None):
r = requests.post(url, files={'file': io.open(archive, 'rb')},
auth=auth)
r.raise_for_status()


Expand Down
12 changes: 10 additions & 2 deletions slingshot/cli.py
Expand Up @@ -23,7 +23,10 @@ def main():
@click.argument('url')
@click.option('--namespace', default='arrowsmith.mit.edu',
help="Namespace used for generating UUID 5.")
def run(layers, store, url, namespace):
@click.option('--username', help="Username for kepler submission.")
@click.option('--password',
help="Password for kepler submission. Omit for prompt.")
def run(layers, store, url, namespace, username, password):
"""Create and upload bags to the specified endpoint.
This script will create bags from all the layers in the LAYERS
Expand All @@ -40,6 +43,11 @@ def run(layers, store, url, namespace):
The namespace option is used in generating a UUID 5 identifier
for the layer. The default value is arrowsmith.mit.edu.
"""
if username and not password:
password = click.prompt('Password', hide_input=True)
auth = username, password
if not all(auth):
auth = None
data = set(sub_dirs(layers))
uploaded = set(sub_dirs(store))
for directory in data - uploaded:
Expand All @@ -48,7 +56,7 @@ def run(layers, store, url, namespace):
bagit.make_bag(bag)
bag_name = make_uuid(os.path.basename(bag), namespace)
with temp_archive(bag, bag_name) as zf:
submit(zf, url)
submit(zf, url, auth)
except Exception as e:
shutil.rmtree(bag, ignore_errors=True)
raise e
8 changes: 8 additions & 0 deletions tests/test_app.py
Expand Up @@ -48,6 +48,14 @@ def test_submit_raises_error_for_failed_post(zipped_bag):
submit(zipped_bag, 'http://localhost')


def test_submit_uses_authentication(zipped_bag):
with requests_mock.Mocker() as m:
m.post('http://localhost')
submit(zipped_bag, 'http://localhost', ('foo', 'bar'))
assert m.request_history[0].headers['Authorization'] == \
'Basic Zm9vOmJhcg=='


def test_make_uuid_creates_uuid_string():
assert make_uuid('grayscale', 'arrowsmith.mit.edu') == \
'aabfaa4e-15a2-51b5-a684-46c530cb0263'
Expand Down
12 changes: 11 additions & 1 deletion tests/test_cli.py
Expand Up @@ -48,4 +48,14 @@ def test_run_uses_supplied_namespace(runner, layers_dir):
runner.invoke(main, ['run', layers_dir, store, 'http://localhost',
'--namespace', 'foo.bar'])
assert os.path.basename(m.call_args[0][0]) == \
'1de12baa-ec69-5cc2-aa2c-54bd77b3ce40.zip'
'1de12baa-ec69-5cc2-aa2c-54bd77b3ce40.zip'


def test_run_uses_authentication(runner, layers_dir):
with requests_mock.Mocker() as m:
store = tempfile.mkdtemp()
m.post('http://localhost')
runner.invoke(main, ['run', layers_dir, store, 'http://localhost',
'--username', 'foo', '--password', 'bar'])
assert m.request_history[0].headers['Authorization'] == \
'Basic Zm9vOmJhcg=='

0 comments on commit c1aec4b

Please sign in to comment.