Skip to content
This repository has been archived by the owner on Sep 12, 2020. It is now read-only.

Add fail2ban stub page for sysadmins #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions netsocadmin/login_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,24 @@ def protected_page(view_func: typing.Callable[..., None]) -> typing.Callable[...
@functools.wraps(view_func)
def protected_view_func(*args, **kwargs):
if config.LOGGED_IN_KEY not in flask.session or not flask.session[config.LOGGED_IN_KEY]:
return flask.redirect("?asdf=lol")
return flask.render_template("index.html", error_message="")
return flask.redirect("/?asdf=lol")
return view_func(*args, **kwargs)
return protected_view_func


def admin_page(view_func: typing.Callable[..., None]) -> typing.Callable[..., None]:
"""
admin_page is a route function decorator which will check that a user
is logged in and is an admin before allowing the decorated view function to be shown. If the
user is not logged in, it will redirect them to the index page. If the user
is logged in but not an admin, it will rediret them to the tools page.
"""
@functools.wraps(view_func)
def protected_view_func(*args, **kwargs):
if config.LOGGED_IN_KEY not in flask.session or not flask.session[config.LOGGED_IN_KEY]:
return flask.redirect("/?asdf=lol")
if "admin" not in flask.session or not flask.session["admin"]:
return flask.redirect("/tools")
return view_func(*args, **kwargs)
return protected_view_func

Expand All @@ -67,6 +83,13 @@ def is_logged_in():
return config.LOGGED_IN_KEY in flask.session and flask.session[config.LOGGED_IN_KEY]


def is_admin():
"""
Returns True if the user is currently logged in and is an admin.
"""
return is_logged_in() and "admin" in flask.session and flask.session["admin"]


def is_correct_password(user: LoginUser) -> bool:
"""
is_correct_password tells you whether or not a given username + password
Expand Down
1 change: 1 addition & 0 deletions netsocadmin/netsoc_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def internal_error(e):
app.add_url_rule('/tools/mysql', view_func=routes.MySQLView.as_view('mysql'))
app.add_url_rule('/tools/shells', view_func=routes.ShellsView.as_view('shells'))
app.add_url_rule('/tools/backups', view_func=routes.BackupsView.as_view('backups'))
app.add_url_rule('/fail2ban', view_func=routes.Fail2BanView.as_view('fail2ban'))


if __name__ == '__main__':
Expand Down
2 changes: 2 additions & 0 deletions netsocadmin/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .tools.shells import ChangeShell, ShellsView
from .tools.sudo import CompleteSudo, Sudo
from .tools.wordpress import WordpressInstall, WordpressView
from .tools.fail2ban import Fail2BanView
from .tutorials import Tutorials
from .view import TemplateView

Expand Down Expand Up @@ -41,6 +42,7 @@
"HelpView",
"WordpressInstall",
"WordpressView",
"Fail2BanView",
# Tutorials
"Tutorials",
]
28 changes: 28 additions & 0 deletions netsocadmin/routes/tools/fail2ban.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# stdlib
import logging

# lib
import flask

# local
import help_post

from .index import AdminToolView


class Fail2BanView(AdminToolView):
"""
Route: fail2ban
"""
template_file = "fail2ban.html"

page_title = "Fail2Ban"

active = "fail2ban"
# Logger instance
logger = logging.getLogger("netsocadmin.fail2ban")

def dispatch_request(self) -> str:
server = flask.request.args.get("server", "leela")

return self.render(server=server)
22 changes: 22 additions & 0 deletions netsocadmin/routes/tools/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ class ProtectedToolView(TemplateView):
methods = ["GET"]


class AdminView(View):
"""
Super class for all of the admin routes that dont render a template
"""
# Decorate all subclasses with the following decorators
decorators = [login_tools.admin_page]
# Specify the default method(s) that are allowed to be used to access the route
# This can be overriden on a per view basis
methods = ["GET"]


class AdminToolView(TemplateView):
"""
Super class for all of the admin routes that render the tools template
"""
# Decorate all subclasses with the following decorators
decorators = [login_tools.admin_page]
# Specify the default method(s) that are allowed to be used to access the route
# This can be overriden on a per view basis
methods = ["GET"]


class ToolIndex(ProtectedToolView):
"""
Route: tools
Expand Down
2 changes: 2 additions & 0 deletions netsocadmin/routes/tools/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import config

import login_tools

from .index import ProtectedToolView, ProtectedView


Expand Down
1 change: 1 addition & 0 deletions netsocadmin/routes/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def render(self, **data: Union[str, bool]) -> str:
return flask.render_template(
self.template_file,
is_logged_in=login_tools.is_logged_in(),
is_admin=login_tools.is_admin(),
username=flask.session["username"] if "username" in flask.session else None,
page_title=self.page_title,
active=self.active,
Expand Down
24 changes: 24 additions & 0 deletions netsocadmin/templates/fail2ban.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "page-skeleton.html" %}
{% block head %}
{{ super() }}
{% endblock %}

{% block body %}
{{ super() }}
<div class='card'>
<div class='card-content'>
{% if help_error %}
<p class='red-text'>{{ help_error }}</p>
{% endif %}
<form action='/fail2ban' method='get'>
<label for='server'>Server:</label>
<select id='server' name='server' onchange="this.form.submit()">
<option {% if server=="leela" %}selected{% endif %} value='leela'>Leela</option>
<option {% if server=="bigbertha" %}selected{% endif %} value='bigbertha'>BigBertha</option>
<option {% if server=="boole" %}selected{% endif %} value='boole'>Boole</option>
<option {% if server=="lovelace" %}selected{% endif %} value='lovelace'>Lovelace</option>
</select>
</form>
</div>
</div>
{% endblock %}
Loading