Compare commits
No commits in common. "02fad76482e5e18032a22680133028f8b1ebc264" and "942b6581602cba72a6374b7a78c75380c41126dc" have entirely different histories.
02fad76482
...
942b658160
7 changed files with 9 additions and 100 deletions
|
|
@ -1,54 +0,0 @@
|
|||
from django.test import TestCase
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.exceptions import ValidationError
|
||||
from webui.models import BorgRepository
|
||||
|
||||
|
||||
class BorgRepositoryTestCase(TestCase):
|
||||
def test_ssh_key_regex(self):
|
||||
user = User.objects.create(username="foo")
|
||||
|
||||
# good repo
|
||||
repo = BorgRepository.objects.create(
|
||||
name="test1",
|
||||
key="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOYOkeGl40Bss9LMmreCtzq+uXw4IQ/E5SKsBRcKAfF3 jo@hubris",
|
||||
user=user,
|
||||
)
|
||||
|
||||
repo.full_clean()
|
||||
|
||||
# good repo, key without comment
|
||||
repo = BorgRepository.objects.create(
|
||||
name="test2",
|
||||
key="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOYOkeGl40Bss9LMmreCtzq+uXw4IQ/E5SKsBRcKAfF3",
|
||||
user=user,
|
||||
)
|
||||
repo.full_clean()
|
||||
|
||||
# bad repo (wrong key)
|
||||
repo = BorgRepository.objects.create(name="test3", key="Foo", user=user)
|
||||
self.assertRaises(ValidationError, repo.full_clean)
|
||||
|
||||
# bad repo (multiple valid keys )
|
||||
repo = BorgRepository.objects.create(
|
||||
name="test4",
|
||||
key="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOYOkeGl40Bss9LMmreCtzq+uXw4IQ/E5SKsBRcKAfF3 jo@hubris\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOYOkeGl40Bss9LMmreCtzq+uXw4IQ/E5SKsBRcKAfF3 jo@hubris",
|
||||
user=user,
|
||||
)
|
||||
self.assertRaises(ValidationError, repo.full_clean)
|
||||
|
||||
# bad repo, leading whitespace
|
||||
repo = BorgRepository.objects.create(
|
||||
name="test5",
|
||||
key=" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOYOkeGl40Bss9LMmreCtzq+uXw4IQ/E5SKsBRcKAfF3 jo@hubris",
|
||||
user=user,
|
||||
)
|
||||
self.assertRaises(ValidationError, repo.full_clean)
|
||||
|
||||
# good repo, with whitespace in commment
|
||||
repo = BorgRepository.objects.create(
|
||||
name="test6",
|
||||
key="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOYOkeGl40Bss9LMmreCtzq+uXw4IQ/E5SKsBRcKAfF3 jo@hubris whitespace in comment",
|
||||
user=user,
|
||||
)
|
||||
repo.full_clean()
|
||||
|
|
@ -8,6 +8,8 @@ from django.conf import settings
|
|||
|
||||
logger = getLogger("deployments")
|
||||
|
||||
# sync users
|
||||
|
||||
|
||||
def sync_repos(dry_run=False):
|
||||
"""Synchronize the repos"""
|
||||
|
|
@ -17,7 +19,7 @@ def sync_repos(dry_run=False):
|
|||
|
||||
repos_by_key = defaultdict(list)
|
||||
for repo in repos:
|
||||
repos_by_key[f"{repo.key_type} {repo.key_value}"].append(repo)
|
||||
repos_by_key[repo.key].append(repo)
|
||||
|
||||
# create .ssh directory
|
||||
ssh_dir = settings.BACKUP_AUTHORIZED_KEYS.parent
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ class BorgRepositoryForm(forms.ModelForm):
|
|||
class Meta:
|
||||
fields = ["name", "key", "user"]
|
||||
model = BorgRepository
|
||||
widgets = {"user": forms.HiddenInput(), "key": forms.TextInput()}
|
||||
widgets = {"user": forms.HiddenInput()}
|
||||
|
||||
|
||||
class RegisterUserForm(UserCreationForm):
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class BorgRepository(models.Model):
|
|||
key = models.TextField(
|
||||
validators=[
|
||||
RegexValidator(
|
||||
r"^(ssh\-rsa|ecdsa\-sha2\-nistp256|ssh\-ed25519) ([a-zA-Z0-9\+/=]+)( ([\S ]*))?$",
|
||||
r"(ssh\-rsa|ecdsa\-sha2\-nistp256|ssh\-ed25519) ([a-zA-Z0-9\+/=]+) (\S*)",
|
||||
message="not a valid SSH public key.",
|
||||
),
|
||||
]
|
||||
|
|
@ -46,9 +46,9 @@ class BorgRepository(models.Model):
|
|||
def repo_url(self) -> str:
|
||||
return f"{settings.BACKUP_USER}@{settings.BACKUP_REPO_HOST}:{self.user.pk}/{self.pk}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
def save(self):
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
super().save()
|
||||
update_user.enqueue()
|
||||
|
||||
def delete(self):
|
||||
|
|
@ -56,45 +56,6 @@ class BorgRepository(models.Model):
|
|||
super().delete()
|
||||
update_user.enqueue()
|
||||
|
||||
@property
|
||||
def key_type(self) -> str:
|
||||
return self.key.split(" ", 1)[0].strip()
|
||||
|
||||
@property
|
||||
def key_value(self) -> str:
|
||||
return self.key.split(" ", 2)[1].strip()
|
||||
|
||||
@property
|
||||
def key_comment(self) -> str | None:
|
||||
splits = self.key.split(" ", 2)
|
||||
if len(splits) == 3:
|
||||
return splits[2].strip()
|
||||
return None
|
||||
|
||||
def truncated_key(self, length: int = 60):
|
||||
"""
|
||||
Returns a truncated version of the key for display purposes.
|
||||
The Key type and the comment are retained as much as possible.
|
||||
"""
|
||||
|
||||
# check how long the truncated part of the key may be at most.
|
||||
max_len = length - len(self.key_type) - 1
|
||||
if self.key_comment:
|
||||
max_len = max_len - len(self.key_comment) - 1
|
||||
|
||||
if len(self.key_value) > max_len:
|
||||
max_len -= 1 # remove one character if we add an ellipsis
|
||||
trunc_key = self.key_value[:max_len] + "…"
|
||||
else:
|
||||
trunc_key = self.key_value
|
||||
|
||||
output = f"{self.key_type} {trunc_key}"
|
||||
|
||||
if self.key_comment:
|
||||
output += f" {self.key_comment}"
|
||||
|
||||
return output
|
||||
|
||||
|
||||
class Voucher(models.Model):
|
||||
used = models.BooleanField(default=False)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
{% for repo in object_list %}
|
||||
<tr>
|
||||
<td>{{ repo.name }}</td>
|
||||
<td><code>{{ repo.truncated_key }}</code></td>
|
||||
<td><code>{{ repo.key | truncatechars:40 }}</code></td>
|
||||
<td><code>{{ repo.repo_url}}</code></td>
|
||||
<td><a href="{% url 'borg_update' pk=repo.pk %}" class="btn btn-warning btn-sm">Edit</a>
|
||||
<a href="{% url 'borg_delete' pk=repo.pk %}" class="btn btn-danger btn-sm">Delete</a></td>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
</row>
|
||||
|
||||
<row>
|
||||
<p>This is a service offering space for backups free of charge. This is a hobby project. It comes with no guarantees for anything, especially availability of the service. This service is in a testing phase at the moment. It might be unavailable at times and features might change quickly.</p>
|
||||
<p>This is a service offering space for backups free of charge. This is a hobby project. It comes with no garantees for anything, especially availability of the service. This service is in a testing phase at the moment. It might be unavailable at times and features might change quickly.</p>
|
||||
<p>Currently this service offers only backups via <a href="https://www.borgbackup.org/">Borg Backup</a>.</p>
|
||||
<p>To access this service you need an account. To register for an account you need a voucher. Vouchers are required to control the amount of users, so that there is enough space available for everyone.</p>
|
||||
</row>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue