Repository: mytardis/mytardis

Issue:
Push to functionality is broken in latest release
Using "Push To" in the latest release results in 500 server error with follow:
```
ValueError: The file 'push-to/angular-resource.min.js' could not be found with <django.contrib.staticfiles.storage.CachedStaticFilesStorage object at 0x7f9aee748e50>
```
Likely related to regression from shift to using NPM to manage JS deps.


Discussion:

Resolving patch:
diff --git a/tardis/apps/push_to/models.py b/tardis/apps/push_to/models.py
--- a/tardis/apps/push_to/models.py
+++ b/tardis/apps/push_to/models.py
@@ -11,6 +11,7 @@
     AutoAddPolicy, PKey, DSSKey, ECDSAKey
 from paramiko.config import SSH_PORT
 from paramiko.message import Message
+from paramiko.py3compat import encodebytes
 
 from .apps import PushToConfig
 from .exceptions import NoSuitableCredential
@@ -49,44 +50,6 @@ def save(self, *args, **kwargs):
 
         super(KeyPair, self).save(*args, **kwargs)
 
-    @staticmethod
-    def _get_key_type_from_public_key(public_key):
-        public_key = StringIO(base64.b64decode(public_key))
-        length = int(public_key.read(4).encode('hex'), 16)
-        return public_key.read(length)
-
-    def __setattr__(self, attrname, val):
-        if attrname == 'public_key':
-            super(KeyPair, self).__setattr__(attrname,
-                                             self._validate_public_key(val))
-        else:
-            super(KeyPair, self).__setattr__(attrname, val)
-
-    def _validate_public_key(self, value):
-        """
-        This method is called when we attempt to assign a value to the
-        public_key field of the KeyPair model.  If we attempt to assign
-        a non-empty string, this method, checks if it has multiple
-        components, e.g. "ssh-rsa AAA...", and if so, saves the key type
-        e.g. "ssh-rsa" in self.key_type, and returns the public key string
-        e.g. "AAA...".  If we are attempting to assing an empty (None)
-        value, we just return that value.
-        """
-        if value:
-            # Check if the public key is in the id_rsa.pub format
-            pub_key_fields = value.split()
-            if len(pub_key_fields) >= 2:
-                public_key = pub_key_fields[1]
-            else:
-                public_key = value
-                # Extract the key type
-            self.key_type = KeyPair._get_key_type_from_public_key(public_key)
-            return public_key
-
-        # It's OK for an empty value (None) to be assigned to the public_key
-        # field whtn the model instance is initialized:
-        return value
-
     @property
     def key(self):
         """
@@ -133,8 +96,12 @@ def key(self, pkey):
         """
         if not isinstance(pkey, PKey):
             raise ValueError('invalid PKey object supplied')
-        self.key_type = pkey.get_name()
-        self.public_key = pkey.get_base64()
+        if pkey.public_blob is not None:
+            self.key_type = pkey.public_blob.key_type
+            self.public_key = encodebytes(pkey.public_blob.key_blob)
+        else:
+            self.key_type = pkey.get_name()
+            self.public_key = pkey.get_base64()
         self.private_key = None
         if pkey.can_sign():
             key_data = StringIO()
diff --git a/tardis/apps/push_to/ssh_authz.py b/tardis/apps/push_to/ssh_authz.py
--- a/tardis/apps/push_to/ssh_authz.py
+++ b/tardis/apps/push_to/ssh_authz.py
@@ -37,9 +37,7 @@ def sign_certificate(credential, token, url):
     private_key = StringIO()
     old_key.write_private_key(private_key)
     private_key.seek(0)
-    cert = RSAKey(
-        data=base64.b64decode(cert_data),
-        file_obj=private_key)
+    cert = RSAKey.from_private_key(private_key)
     cert.load_certificate(Message(base64.b64decode(cert_data)))
     credential.key = cert
     credential.remote_user = remote_user_name


Test patch:
diff --git a/tardis/apps/push_to/tests/test_models.py b/tardis/apps/push_to/tests/test_models.py
--- a/tardis/apps/push_to/tests/test_models.py
+++ b/tardis/apps/push_to/tests/test_models.py
@@ -45,43 +45,6 @@ def test_pkey_to_credential(self):
         self.assertEqual(key.get_fingerprint(),
                          credential_copy.key.get_fingerprint())
 
-    def test_key_type_detection(self):
-        rsa_key_string = (
-            'AAAAB3NzaC1yc2EAAAADAQABAAABAQC55qMy9V9u+kXvyQfeztFjzf0Mz7ieRit1'
-            'lEaQLV9zn5AGGzametc8JGWIwWju3iBW0WIndkZltNmk3pdbAxl9S4gP3B9ga+5w'
-            'J1fADaYOl8OmKOu/ovg1Jtll3wRhkI4L3cHAuoPjwOc1Erbj1VTnyZi0FrmsNBUJ'
-            'yQdrf0Qge+lBXHUbccerObYX1JX+nAGlJkYMBKPJWfjdnj4ff5nNr0ZxutiB7FyG'
-            'v+VRrGndbBRTCq19WF+NaHOSoNgwKnQYjAmDYXZ2O2q036y9tFYJ6BUma4qVV1hf'
-            'gPcMFU9vZA/cXDE8WJxxZAzscztoGl97QQGKFnu7e7odat+/czRl')
-        dsa_key_string = (
-            'AAAAB3NzaC1kc3MAAACBANpiUkTHu88d1ldKC1p6NkWqMMLZ3y2Gk/jnwPEQVylw'
-            'NmZoqpBU86TyPm5kNiuAEF9Jn3K2B8HfemaeSO6ZFsyIgEWyvs6nnVGccWhrVbc+'
-            '74z8vayj7MjhisqA7xBFASGWjHoAumZXwicr9XIpTC6LYL01VU7dfMP49w4SByXZ'
-            'AAAAFQDhxd00faq4j7wUP6tDFCQt/jWLNQAAAIEAm5ZhxzjP0nYRU8HqcqZP8TKd'
-            'TWj+A6cInuL0eB1u4jIUeDFFGGz3H/bHaP86OkMVsXsHTwnAlBeAvfAaE5pux58k'
-            'tgAdmaFdosn0RSlycAFTs3z2zeSBa9xC+8xs/RMIZu66Km+ut5OGgX5gu+z033BY'
-            'OPuj08t0s4EIZmakFEAAAACAQi2LborZAIaWXX5SesRwYH8yNlB5wT4VNNt4Xmt0'
-            'jC/KkM2Xb/WCesu+n+NxFFQfAnEYScZtFj7iIjX5LbqV+gyJi/+cU6IofMoBO7vS'
-            'taP78YcfLGgYOGA3M7rofeBpj9eGlr6uKqqL3h+e3gxAtzjA6pRlmnUKRafUEEuD'
-            'EsI=')
-
-        credential = Credential.objects.create(user=self.user1,
-                                               remote_user='remote_user')
-        credential.public_key = rsa_key_string
-        self.assertEqual(credential.key.get_name(), 'ssh-rsa')
-
-        credential.key_type = None
-        credential.public_key = 'ssh-rsa ' + rsa_key_string
-        self.assertEqual(credential.key.get_name(), 'ssh-rsa')
-
-        credential.key_type = None
-        credential.public_key = dsa_key_string
-        self.assertEqual(credential.key.get_name(), 'ssh-dss')
-
-        credential.key_type = None
-        credential.public_key = 'ssh-dss ' + dsa_key_string
-        self.assertEqual(credential.key.get_name(), 'ssh-dss')
-
     def test_keys_from_private_key_only(self):
         rsa_private_key = '''-----BEGIN RSA PRIVATE KEY-----
 MIIEogIBAAKCAQEAsCx+Qxl+fDkygxeSVHdDgS0MBoh0vtisGjiM3WlIAyQ4a/oK