Skip to content

Commit 1246ab2

Browse files
committed
Add disallow new upload AdminFlag
1 parent 1858eaa commit 1246ab2

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

tests/unit/forklift/test_legacy.py

+19
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,25 @@ def test_is_duplicate_false(self, pyramid_config, db_request):
753753

754754

755755
class TestFileUpload:
756+
def test_fails_disallow_new_upload(self, pyramid_config, pyramid_request):
757+
pyramid_config.testing_securitypolicy(userid=1)
758+
pyramid_request.flags = pretend.stub(
759+
enabled=lambda value: value == AdminFlagValue.DISALLOW_NEW_UPLOAD
760+
)
761+
pyramid_request.help_url = pretend.call_recorder(lambda **kw: "/the/help/url/")
762+
pyramid_request.user = pretend.stub(primary_email=pretend.stub(verified=True))
763+
764+
with pytest.raises(HTTPForbidden) as excinfo:
765+
legacy.file_upload(pyramid_request)
766+
767+
resp = excinfo.value
768+
769+
assert resp.status_code == 403
770+
assert resp.status == (
771+
"403 New uploads are temporarily disabled. "
772+
"See /the/help/url/ for details"
773+
)
774+
756775
@pytest.mark.parametrize("version", ["2", "3", "-1", "0", "dog", "cat"])
757776
def test_fails_invalid_version(self, pyramid_config, pyramid_request, version):
758777
pyramid_config.testing_securitypolicy(userid=1)

warehouse/admin/flags.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
class AdminFlagValue:
1919
DISALLOW_DELETION = "disallow-deletion"
2020
DISALLOW_NEW_PROJECT_REGISTRATION = "disallow-new-project-registration"
21+
DISALLOW_NEW_UPLOAD = "disallow-new-upload"
2122
DISALLOW_NEW_USER_REGISTRATION = "disallow-new-user-registration"
2223
READ_ONLY = "read-only"
2324

warehouse/forklift/legacy.py

+9
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,15 @@ def file_upload(request):
734734
HTTPForbidden, "Read-only mode: Uploads are temporarily disabled"
735735
)
736736

737+
if request.flags.enabled(AdminFlagValue.DISALLOW_NEW_UPLOAD):
738+
raise _exc_with_message(
739+
HTTPForbidden,
740+
"New uploads are temporarily disabled. "
741+
"See {projecthelp} for details".format(
742+
projecthelp=request.help_url(_anchor="admin-intervention")
743+
),
744+
)
745+
737746
# Log an attempt to upload
738747
metrics = request.find_service(IMetricsService, context=None)
739748
metrics.increment("warehouse.upload.attempt")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
"""
13+
Add disallow-new-upload AdminFlag
14+
15+
Revision ID: ee4c59b2ef3a
16+
Revises: 8650482fb903
17+
Create Date: 2019-08-23 22:34:29.180163
18+
"""
19+
20+
from alembic import op
21+
22+
revision = "ee4c59b2ef3a"
23+
down_revision = "8650482fb903"
24+
25+
26+
def upgrade():
27+
op.execute(
28+
"""
29+
INSERT INTO admin_flags(id, description, enabled, notify)
30+
VALUES (
31+
'disallow-new-upload',
32+
'Disallow ALL new uploads',
33+
FALSE,
34+
FALSE
35+
)
36+
"""
37+
)
38+
39+
40+
def downgrade():
41+
op.execute("DELETE FROM admin_flags WHERE id = 'disallow-new-upload'")

0 commit comments

Comments
 (0)