-
Notifications
You must be signed in to change notification settings - Fork 1k
Add admin interface to view and enable checks #7134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,40 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import datetime | ||
|
||
import factory | ||
import factory.fuzzy | ||
|
||
from warehouse.malware.models import MalwareCheck, MalwareCheckState, MalwareCheckType | ||
|
||
from .base import WarehouseFactory | ||
|
||
|
||
class MalwareCheckFactory(WarehouseFactory): | ||
class Meta: | ||
model = MalwareCheck | ||
|
||
name = factory.fuzzy.FuzzyText(length=12) | ||
version = 1 | ||
short_description = factory.fuzzy.FuzzyText(length=80) | ||
long_description = factory.fuzzy.FuzzyText(length=300) | ||
check_type = factory.fuzzy.FuzzyChoice([e for e in MalwareCheckType]) | ||
hook_name = ( | ||
"project:release:file:upload" | ||
if check_type == MalwareCheckType.event_hook | ||
else None | ||
) | ||
state = factory.fuzzy.FuzzyChoice([e for e in MalwareCheckState]) | ||
created = factory.fuzzy.FuzzyNaiveDateTime( | ||
datetime.datetime.utcnow() - datetime.timedelta(days=7) | ||
) |
This file contains hidden or 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 hidden or 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,122 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import uuid | ||
|
||
import pretend | ||
import pytest | ||
|
||
from pyramid.httpexceptions import HTTPNotFound | ||
|
||
from warehouse.admin.views import checks as views | ||
from warehouse.malware.models import MalwareCheckState | ||
|
||
from ....common.db.malware import MalwareCheckFactory | ||
|
||
|
||
class TestListChecks: | ||
def test_get_checks_none(self, db_request): | ||
assert views.get_checks(db_request) == {"checks": []} | ||
|
||
def test_get_checks(self, db_request): | ||
checks = [MalwareCheckFactory.create() for _ in range(10)] | ||
assert views.get_checks(db_request) == {"checks": checks} | ||
|
||
def test_get_checks_different_versions(self, db_request): | ||
checks = [MalwareCheckFactory.create() for _ in range(5)] | ||
checks_same = [ | ||
MalwareCheckFactory.create(name="MyCheck", version=i) for i in range(1, 6) | ||
] | ||
checks.append(checks_same[-1]) | ||
assert views.get_checks(db_request) == {"checks": checks} | ||
|
||
|
||
class TestGetCheck: | ||
def test_get_check(self, db_request): | ||
check = MalwareCheckFactory.create() | ||
db_request.matchdict["check_name"] = check.name | ||
assert views.get_check(db_request) == { | ||
"check": check, | ||
"checks": [check], | ||
"states": MalwareCheckState, | ||
} | ||
|
||
def test_get_check_many_versions(self, db_request): | ||
check1 = MalwareCheckFactory.create(name="MyCheck", version="1") | ||
check2 = MalwareCheckFactory.create(name="MyCheck", version="2") | ||
db_request.matchdict["check_name"] = check1.name | ||
assert views.get_check(db_request) == { | ||
"check": check2, | ||
"checks": [check2, check1], | ||
"states": MalwareCheckState, | ||
} | ||
|
||
def test_get_check_not_found(self, db_request): | ||
db_request.matchdict["check_name"] = "DoesNotExist" | ||
with pytest.raises(HTTPNotFound): | ||
views.get_check(db_request) | ||
|
||
|
||
class TestChangeCheckState: | ||
def test_change_to_enabled(self, db_request): | ||
check = MalwareCheckFactory.create( | ||
name="MyCheck", state=MalwareCheckState.disabled | ||
) | ||
|
||
db_request.POST = {"id": check.id, "check_state": "enabled"} | ||
db_request.matchdict["check_name"] = check.name | ||
|
||
db_request.session = pretend.stub( | ||
flash=pretend.call_recorder(lambda *a, **kw: None) | ||
) | ||
db_request.route_path = pretend.call_recorder( | ||
lambda *a, **kw: "/admin/checks/MyCheck/change_state" | ||
) | ||
|
||
views.change_check_state(db_request) | ||
|
||
assert db_request.session.flash.calls == [ | ||
pretend.call("Changed 'MyCheck' check to 'enabled'!", queue="success") | ||
] | ||
assert check.state == MalwareCheckState.enabled | ||
|
||
def test_change_to_invalid_state(self, db_request): | ||
check = MalwareCheckFactory.create(name="MyCheck") | ||
initial_state = check.state | ||
invalid_check_state = "cancelled" | ||
db_request.POST = {"id": check.id, "check_state": invalid_check_state} | ||
db_request.matchdict["check_name"] = check.name | ||
|
||
db_request.session = pretend.stub( | ||
flash=pretend.call_recorder(lambda *a, **kw: None) | ||
) | ||
db_request.route_path = pretend.call_recorder( | ||
lambda *a, **kw: "/admin/checks/MyCheck/change_state" | ||
) | ||
|
||
views.change_check_state(db_request) | ||
|
||
assert db_request.session.flash.calls == [ | ||
pretend.call("Invalid check state provided.", queue="error") | ||
] | ||
assert check.state == initial_state | ||
|
||
def test_check_not_found(self, db_request): | ||
db_request.POST = {"id": uuid.uuid4(), "check_state": "enabled"} | ||
db_request.matchdict["check_name"] = "DoesNotExist" | ||
|
||
db_request.route_path = pretend.call_recorder( | ||
lambda *a, **kw: "/admin/checks/DoesNotExist/change_state" | ||
) | ||
|
||
with pytest.raises(HTTPNotFound): | ||
views.change_check_state(db_request) |
This file contains hidden or 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 hidden or 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
70 changes: 70 additions & 0 deletions
70
warehouse/admin/templates/admin/malware/checks/detail.html
This file contains hidden or 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,70 @@ | ||
{# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-#} | ||
{% extends "admin/base.html" %} | ||
|
||
{% block title %}{{ check.name }}{% endblock %} | ||
|
||
{% block breadcrumb %} | ||
<li><a href="{{ request.route_path('admin.checks.list') }}">Checks</a></li> | ||
<li class="active">{{ check.name }}</li> | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<div class="box box-primary"> | ||
<div class="box-body box-profile"> | ||
<p>{{ check.long_description }}</p> | ||
<h4>Revision History</h4> | ||
<div class="box-body box-attributes"> | ||
<table class="table table-hover"> | ||
<tr> | ||
<th>Version</th> | ||
<th>State</th> | ||
<th>Created</th> | ||
</tr> | ||
{% for c in checks %} | ||
<tr> | ||
<td>{{ c.version }}</td> | ||
<td>{{ c.state.value }}</td> | ||
<td>{{ c.created }}</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="box box-primary"> | ||
<div class="box-header with-border"> | ||
<h3 class="box-title">Change State</h3> | ||
</div> | ||
<form method="POST" action="{{ request.route_path('admin.checks.change_state', check_name=check.name) }}"> | ||
<input type="hidden" name="id" value="{{ check.id }}"> | ||
<input name="csrf_token" type="hidden" value="{{ request.session.get_csrf_token() }}"> | ||
<div class="box-body"> | ||
<div class="form-group col-sm-4"> | ||
<select name="check_state" id="check_state"> | ||
{% for state in states %} | ||
<option value="{{ state.value }}" {{'disabled selected' if check.state == state else ''}}> | ||
{{ state.value }} | ||
</option> | ||
{% endfor %} | ||
</select> | ||
</div> | ||
<div class="pull-right col-sm-4"> | ||
<button type="submit" class="btn btn-primary pull-right">Save</button> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains hidden or 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,57 @@ | ||
{# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-#} | ||
{% extends "admin/base.html" %} | ||
|
||
{% block title %}Malware Checks{% endblock %} | ||
|
||
{% block breadcrumb %} | ||
<li class="active">Checks</li> | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<div class="box box-primary"> | ||
<div class="box-body table-responsive no-padding"> | ||
<table class="table table-hover"> | ||
<tr> | ||
<th>Check Name</th> | ||
<th>State</th> | ||
<th>Revisions</th> | ||
<th>Last Modified</th> | ||
<th>Description</th> | ||
</tr> | ||
{% for check in checks %} | ||
<tr> | ||
<td> | ||
<a href="{{ request.route_path('admin.checks.detail', check_name=check.name) }}"> | ||
{{ check.name }} | ||
</a> | ||
</td> | ||
<td>{{ check.state.value }}</td> | ||
<td>{{ check.version }}</td> | ||
<td>{{ check.created }}</td> | ||
<td>{{ check.short_description }}</td> | ||
</tr> | ||
{% else %} | ||
<tr> | ||
<td colspan="5"> | ||
<center> | ||
<i>No checks!</i> | ||
</center> | ||
</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
</div> | ||
</div> | ||
{% endblock content %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.