Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
raise OMPSUploadedFileError when zip file is encrypted
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Cmiel <acmiel@redhat.com>
  • Loading branch information
chmeliik authored and MartinBasti committed Mar 7, 2019
1 parent 1d89b70 commit 530e2e3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
8 changes: 7 additions & 1 deletion omps/api/v1/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ def extract_zip_file(
uncompressed_size, max_uncompressed_size
))

bad_file = archive.testzip()
try:
bad_file = archive.testzip()
except RuntimeError as e:
# trying to open an encrypted zip file without a password
raise OMPSUploadedFileError(str(e))

if bad_file is not None:
raise OMPSUploadedFileError(
"CRC check failed for file {} in archive".format(bad_file)
)

archive.extractall(target_dir)
archive.close()

Expand Down
25 changes: 25 additions & 0 deletions tests/api/v1/test_api_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ def test_push_zipfile_unauthorized(client, endpoint_push_zipfile):
assert rv_json['error'] == 'OMPSAuthorizationHeaderRequired'


@pytest.mark.usefixtures("mocked_quay_io")
def test_push_zipfile_encrypted(
client, encrypted_zip_archive,
endpoint_push_zipfile, auth_header):
"""Test if proper error is returned when the attached zip file
is encrypted
"""
with open(encrypted_zip_archive, 'rb') as f:
data = {
'file': (f, f.name),
}
rv = client.post(
endpoint_push_zipfile.url_path,
headers=auth_header,
data=data,
content_type='multipart/form-data',
)

assert rv.status_code == 400, rv.get_json()
rv_json = rv.get_json()
assert rv_json['status'] == 400
assert rv_json['error'] == 'OMPSUploadedFileError'
assert 'is encrypted' in rv_json['message']


def test_push_koji_nvr(client):
"""Test REST API for pushing operators form koji by NVR"""
rv = client.post('/v1/organization-X/repo-Y/koji/nvr-Z')
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def valid_manifests_archive(datadir, tmpdir):
return path


@pytest.fixture
def encrypted_zip_archive(datadir):
"""Path to the encrypted zip archive created in advance"""
return os.path.join(datadir, 'encrypted.zip')


@pytest.fixture
def mocked_quay_io():
"""Mocking quay.io answers"""
Expand Down
Binary file added tests/data/encrypted.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/integration/push_archive/push_archive_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def test_upload_password_protected_zip(omps):
response = omps.upload(organization=TEST_NAMESPACE,
repo=TEST_PACKAGE, archive=archive)

assert response.status_code == requests.codes.internal_server_error
assert response.json()['error'] == 'InternalServerError'
assert response.status_code == requests.codes.bad_request
assert response.json()['error'] == 'OMPSUploadedFileError'
assert 'is encrypted' in response.json()['message']


Expand Down
5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ max-line-length: 90
ignore =
# E731 do not assign a lambda expression, use a def
E731
exclude =
# flake8 defaults
.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg,
# added
venv

0 comments on commit 530e2e3

Please sign in to comment.