Skip to content
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

General improvements to SSH management tab #118

Open
wants to merge 5 commits into
base: main
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
5 changes: 4 additions & 1 deletion zezere/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from rules.contrib.admin import ObjectPermissionsModelAdmin

from zezere.models import Device, RunRequest
from zezere.models import Device, RunRequest, SSHKey


class RunRequestAdmin(ObjectPermissionsModelAdmin):
Expand All @@ -12,6 +12,9 @@ class RunRequestAdmin(ObjectPermissionsModelAdmin):
class DeviceAdmin(ObjectPermissionsModelAdmin):
pass

class SSHKeyAdmin(ObjectPermissionsModelAdmin):
pass

admin.site.register(RunRequest, RunRequestAdmin)
admin.site.register(Device, DeviceAdmin)
admin.site.register(SSHKey, SSHKeyAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 4.0.4 on 2022-04-14 00:49

import django.core.validators
from django.db import migrations, models
import django.utils.timezone
import zezere.models


class Migration(migrations.Migration):

dependencies = [
('zezere', '0012_auto_20200323_1130'),
]

operations = [
migrations.AddField(
model_name='sshkey',
name='date',
field=models.DateField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='sshkey',
name='title',
field=models.CharField(blank=True, max_length=10, verbose_name='SSH title'),
),
migrations.AlterField(
model_name='device',
name='mac_address',
field=models.CharField(max_length=20, unique=True, validators=[django.core.validators.RegexValidator('^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$'), zezere.models.validator_disallow_blacklisted_mac], verbose_name='Device MAC Address'),
),
]
2 changes: 2 additions & 0 deletions zezere/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ class Meta:
owner: models.ForeignKey = models.ForeignKey(
User, on_delete=models.PROTECT, related_name="sshkeys", default=None
)
title: models.CharField = models.CharField("SSH title",max_length=10, blank=True)
key: models.CharField = models.CharField("SSH Key", max_length=1024)
date: models.DateTimeField = models.DateField(auto_now_add=True)


def validator_disallow_blacklisted_mac(value):
Expand Down
73 changes: 46 additions & 27 deletions zezere/templates/portal/sshkeys.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,52 @@
{% block title %}SSH Keys{% endblock %}

{% block content %}
Your SSH Keys:
<table border="1">
<div class="container-fluid">
<div class="row">
<h1>Saved SSH Keys</h1>
<hr/>
{% if not sshkeys %}
<h3> No SSH key saved.</h3>
{% else %}
{% for sshkey in sshkeys %}
<div class="panel panel-default container-fluid">
<div class="panel-body row">
<div class="col-md-2 col-sm-2 text-muted">
<h3>&#128477;</h3>
</div>
<div class="col-md-8 col-sm-8">
<strong>{{ sshkey.title }} </strong><br/>
<small> {{ sshkey.key }} </small><br/>
<small class="text-muted">
<em> Added on {{ sshkey.date }}</em></small>
</div>
<div class="col-md-2 col-sm-2">
<form method="POST" action="/portal/sshkeys/delete/">
{% csrf_token %}
<input type="hidden" name="sshkey_id" value="{{ sshkey.id }}">
<input class ="btn btn-danger" type="submit" value="Delete key">
</form>
</div>

<tr>
<th>Key</th>
<th>Actions</th>
</tr>
{% for sshkey in sshkeys %}
<tr>
<td>{{ sshkey.key }}</td>
<td>
<form method="POST" action="/portal/sshkeys/delete/">
{% csrf_token %}
<input type="hidden" name="sshkey_id" value="{{ sshkey.id }}">
<input type="submit" value="Delete key">
</form>
</td>
</tr>
{% endfor %}
</table>

Add a new key:
<form method="POST" action="/portal/sshkeys/add/">
{% csrf_token %}
Key contents:
<textarea name="sshkey"></textarea>
<input type="submit" value="Add key">
</form>
</div>
</div>

{% endfor %}
{% endif %}
</div>
<div class="row">
<h1>Add new key</h1>
<hr/>
<form method="POST" action="/portal/sshkeys/add/">
{% csrf_token %}
Title: <br/>
<input type="text" name="sshtitle" placeholder="Add title"><br/>
<br/>
Key contents:<br/>
<textarea name="sshkey"></textarea><br/>
<br/>
<input class ="btn btn-success" type="submit" value="Add key">
</form>
</div>
</div>
{% endblock %}
7 changes: 3 additions & 4 deletions zezere/views_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ def remove_ssh_key(request):
@require_POST
def add_ssh_key(request):
keyval = request.POST["sshkey"].strip()
if not keyval:
titleval = request.POST["sshtitle"]
if not (keyval and titleval):
return redirect("portal_sshkeys")

key = SSHKey(owner=request.user, key=keyval)
key.full_clean()
key.save()
SSHKey.objects.create(owner=request.user, key=keyval,title=titleval)
return redirect("portal_sshkeys")