(bugfix) Add SSH private key file upload
Allow SSH credentials to be created from an uploaded private key file as an alternative to pasting the key into a textarea. Use multipart form handling in the credential views so server-side keys can be imported without copy/paste wrapping or formatting damage. Cover the upload path with a view test while keeping existing pasted key validation behavior intact.
This commit is contained in:
@@ -6,6 +6,7 @@ from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
@@ -117,6 +118,29 @@ class ViewTests(TestCase):
|
||||
self.assertEqual(credential.private_key, "PRIVATE KEY\n")
|
||||
self.assertEqual(credential.public_key, "DERIVED PUBLIC KEY")
|
||||
|
||||
def test_ssh_credentials_view_creates_key_from_uploaded_file(self) -> None:
|
||||
self.client.force_login(self.staff_user)
|
||||
uploaded_key = SimpleUploadedFile("id_ed25519", b"UPLOADED PRIVATE KEY\n", content_type="text/plain")
|
||||
|
||||
with patch("pobsync_backend.forms.validate_ssh_private_key", return_value="DERIVED PUBLIC KEY"):
|
||||
response = self.client.post(
|
||||
reverse("create_ssh_credential"),
|
||||
{
|
||||
"name": "backup-key",
|
||||
"private_key_file": uploaded_key,
|
||||
"private_key": "",
|
||||
"public_key": "",
|
||||
"known_hosts": "",
|
||||
"notes": "uploaded",
|
||||
},
|
||||
follow=True,
|
||||
)
|
||||
|
||||
self.assertRedirects(response, reverse("ssh_credentials"))
|
||||
credential = SshCredential.objects.get(name="backup-key")
|
||||
self.assertEqual(credential.private_key, "UPLOADED PRIVATE KEY\n")
|
||||
self.assertEqual(credential.public_key, "DERIVED PUBLIC KEY")
|
||||
|
||||
def test_ssh_credentials_view_rejects_invalid_key(self) -> None:
|
||||
self.client.force_login(self.staff_user)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user