mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(accounts): an SSH key is a whole key, and added once
The profile took an SSH key whose blob only named its type
("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5") or was cut short, and offered it
for a VM, where it lets no one in. A key is now parsed whole with
cryptography (which paramiko already brings), security keys
(sk-ssh-ed25519) included.
The same key with another comment was added a second time, and
deleting one left the other to be installed. Keys are now compared by
their type and key, without the comment.
This commit is contained in:
parent
2ab1bac0c7
commit
f18be7c33e
4 changed files with 47 additions and 35 deletions
|
|
@ -1,9 +1,13 @@
|
|||
"""A user's SSH keys go with the user (validate_ssh_key: test_utils)."""
|
||||
"""A user's SSH keys go with the user, and a key is added once
|
||||
(validate_ssh_key: test_utils)."""
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
|
||||
from accounts.forms import UserSSHKeyForm
|
||||
from accounts.models import UserSSHKey
|
||||
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
||||
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
|
||||
|
||||
|
||||
class SshKeyTestCase(TestCase):
|
||||
|
|
@ -20,3 +24,10 @@ class SshKeyTestCase(TestCase):
|
|||
user.delete()
|
||||
self.assertFalse(UserSSHKey.objects.filter(id=key.id).exists())
|
||||
|
||||
|
||||
def test_the_same_key_with_another_comment_is_refused(self):
|
||||
user = get_user_model().objects.create_user(username="dup_key_user", password="password")
|
||||
key = Ed25519PrivateKey.generate().public_key().public_bytes(Encoding.OpenSSH, PublicFormat.OpenSSH).decode()
|
||||
UserSSHKey.objects.create(user=user, keyname="laptop", keypublic=f"{key} laptop")
|
||||
form = UserSSHKeyForm({"keyname": "desktop", "keypublic": f"{key} other comment"}, user=user)
|
||||
self.assertEqual(form.errors["keypublic"], ["Public key already exist"])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue