Ejemplo n.º 1
0
 def test_sshkey_user_and_key_unique_together(self):
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_user()
     key = SSHKey(key=key_string, user=user)
     key.save()
     key2 = SSHKey(key=key_string, user=user)
     self.assertRaises(
         ValidationError, key2.full_clean)
Ejemplo n.º 2
0
 def test_sshkey_same_key_can_be_used_by_different_users(self):
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_user()
     key = SSHKey(key=key_string, user=user)
     key.save()
     user2 = factory.make_user()
     key2 = SSHKey(key=key_string, user=user2)
     key2.full_clean()
Ejemplo n.º 3
0
 def test_sshkey_same_key_can_be_used_by_different_users(self):
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_user()
     key = SSHKey(key=key_string, user=user)
     key.save()
     user2 = factory.make_user()
     key2 = SSHKey(key=key_string, user=user2)
     key2.full_clean()
Ejemplo n.º 4
0
 def test_sshkey_user_and_key_unique_together(self):
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_user()
     key = SSHKey(key=key_string, user=user)
     key.save()
     key2 = SSHKey(key=key_string, user=user)
     self.assertRaises(
         ValidationError, key2.full_clean)
Ejemplo n.º 5
0
    def test_key_can_be_added_if_same_key_already_setup_for_other_user(self):
        key_string = get_data('data/test_rsa0.pub')
        key = SSHKey(user=factory.make_user(), key=key_string)
        key.save()
        response = self.client.post(
            reverse('prefs-add-sshkey'), {'key': key_string})
        new_key = SSHKey.objects.get(key=key_string, user=self.logged_in_user)

        self.assertEqual(httplib.FOUND, response.status_code)
        self.assertItemsEqual(
            [key, new_key], SSHKey.objects.filter(key=key_string))
Ejemplo n.º 6
0
    def test_key_can_be_added_if_same_key_already_setup_for_other_user(self):
        key_string = get_data('data/test_rsa0.pub')
        key = SSHKey(user=factory.make_user(), key=key_string)
        key.save()
        response = self.client.post(reverse('prefs-add-sshkey'),
                                    {'key': key_string})
        new_key = SSHKey.objects.get(key=key_string, user=self.logged_in_user)

        self.assertEqual(httplib.FOUND, response.status_code)
        self.assertItemsEqual([key, new_key],
                              SSHKey.objects.filter(key=key_string))
Ejemplo n.º 7
0
 def test_sshkey_user_and_key_unique_together(self):
     protocol = random.choice(
         [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH])
     auth_id = factory.make_name('auth_id')
     keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id)
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_User()
     key = SSHKey(key=key_string, user=user, keysource=keysource)
     key.save()
     key2 = SSHKey(key=key_string, user=user, keysource=keysource)
     self.assertRaises(ValidationError, key2.full_clean)
Ejemplo n.º 8
0
    def test_add_key_POST_fails_if_key_already_exists_for_the_user(self):
        key_string = get_data('data/test_rsa0.pub')
        key = SSHKey(user=self.logged_in_user, key=key_string)
        key.save()
        response = self.client.post(reverse('prefs-add-sshkey'),
                                    {'key': key_string})

        self.assertEqual(httplib.OK, response.status_code)
        self.assertIn("This key has already been added for this user.",
                      response.content)
        self.assertItemsEqual([key], SSHKey.objects.filter(key=key_string))
Ejemplo n.º 9
0
def copy_ssh_keys(user_from, user_dest):
    """Copies SSH keys from one user to another.

    This is idempotent, and does not clobber the destination user's existing
    keys.
    """
    user_from_keys = get_ssh_keys(user_from)
    user_dest_keys = get_ssh_keys(user_dest)
    for key in set(user_from_keys).difference(user_dest_keys):
        ssh_key = SSHKey(user=user_dest, key=key)
        ssh_key.save()
Ejemplo n.º 10
0
    def test_add_key_POST_fails_if_key_already_exists_for_the_user(self):
        key_string = get_data('data/test_rsa0.pub')
        key = SSHKey(user=self.logged_in_user, key=key_string)
        key.save()
        response = self.client.post(
            reverse('prefs-add-sshkey'), {'key': key_string})

        self.assertEqual(httplib.OK, response.status_code)
        self.assertIn(
            "This key has already been added for this user.",
            response.content)
        self.assertItemsEqual([key], SSHKey.objects.filter(key=key_string))
Ejemplo n.º 11
0
 def test_sshkey_same_key_can_be_used_by_different_sources(self):
     auth_id = factory.make_name('auth_id')
     keysource1 = factory.make_KeySource(protocol=KEYS_PROTOCOL_TYPE.LP,
                                         auth_id=auth_id)
     keysource2 = factory.make_KeySource(protocol=KEYS_PROTOCOL_TYPE.GH,
                                         auth_id=auth_id)
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_User()
     key1 = SSHKey(key=key_string, user=user, keysource=keysource1)
     key1.save()
     key2 = SSHKey(key=key_string, user=user, keysource=keysource2)
     key2.save()
     self.assertIsNone(key2.full_clean())
Ejemplo n.º 12
0
 def test_sshkey_user_and_key_unique_together_change_key(self):
     protocol = random.choice(
         [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]
     )
     auth_id = factory.make_name("auth_id")
     keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id)
     key_string1 = get_data("data/test_rsa1.pub")
     key_string2 = get_data("data/test_rsa2.pub")
     user = factory.make_User()
     key1 = SSHKey(key=key_string1, user=user, keysource=keysource)
     key1.save()
     key2 = SSHKey(key=key_string2, user=user, keysource=keysource)
     key2.save()
     key2.key = key1.key
     self.assertRaises(ValidationError, key2.full_clean)
Ejemplo n.º 13
0
 def test_sshkey_user_and_key_unique_together_db_level(self):
     # Even if we hack our way around model-level checks, uniqueness
     # of the user/key combination is enforced at the database level.
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_user()
     existing_key = SSHKey(key=key_string, user=user)
     existing_key.save()
     # The trick to hack around the model-level checks: create a
     # duplicate key for another user, then attach it to the same
     # user as the existing key by updating it directly in the
     # database.
     redundant_key = SSHKey(key=key_string, user=factory.make_user())
     redundant_key.save()
     self.assertRaises(
         IntegrityError,
         SSHKey.objects.filter(id=redundant_key.id).update,
         user=user)
Ejemplo n.º 14
0
 def test_sshkey_user_and_key_unique_together_db_level(self):
     # Even if we hack our way around model-level checks, uniqueness
     # of the user/key combination is enforced at the database level.
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_user()
     existing_key = SSHKey(key=key_string, user=user)
     existing_key.save()
     # The trick to hack around the model-level checks: create a
     # duplicate key for another user, then attach it to the same
     # user as the existing key by updating it directly in the
     # database.
     redundant_key = SSHKey(key=key_string, user=factory.make_user())
     redundant_key.save()
     self.assertRaises(
         IntegrityError,
         SSHKey.objects.filter(id=redundant_key.id).update,
         user=user)
Ejemplo n.º 15
0
    def make_user_with_keys(self, n_keys=2, user=None, **kwargs):
        """Create a user with n `SSHKey`.  If user is not None, use this user
        instead of creating one.

        Additional keyword arguments are passed to `make_user()`.
        """
        if n_keys > MAX_PUBLIC_KEYS:
            raise RuntimeError(
                "Cannot create more than %d public keys.  If you need more: "
                "add more keys in src/maasserver/tests/data/." %
                MAX_PUBLIC_KEYS)
        if user is None:
            user = self.make_user(**kwargs)
        keys = []
        for i in range(n_keys):
            key_string = get_data('data/test_rsa%d.pub' % i)
            key = SSHKey(user=user, key=key_string)
            key.save()
            keys.append(key)
        return user, keys
Ejemplo n.º 16
0
    def make_user_with_keys(self, n_keys=2, user=None, **kwargs):
        """Create a user with n `SSHKey`.  If user is not None, use this user
        instead of creating one.

        Additional keyword arguments are passed to `make_user()`.
        """
        if n_keys > MAX_PUBLIC_KEYS:
            raise RuntimeError(
                "Cannot create more than %d public keys.  If you need more: "
                "add more keys in src/maasserver/tests/data/."
                % MAX_PUBLIC_KEYS)
        if user is None:
            user = self.make_user(**kwargs)
        keys = []
        for i in range(n_keys):
            key_string = get_data('data/test_rsa%d.pub' % i)
            key = SSHKey(user=user, key=key_string)
            key.save()
            keys.append(key)
        return user, keys
Ejemplo n.º 17
0
 def test_sshkey_user_and_key_unique_together_db_level(self):
     # Even if we hack our way around model-level checks, uniqueness
     # of the user/key combination is enforced at the database level.
     protocol = random.choice(
         [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH])
     auth_id = factory.make_name('auth_id')
     keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id)
     key_string = get_data('data/test_rsa0.pub')
     user = factory.make_User()
     existing_key = SSHKey(key=key_string, user=user, keysource=keysource)
     existing_key.save()
     # The trick to hack around the model-level checks: create a
     # duplicate key for another user, then attach it to the same
     # user as the existing key by updating it directly in the
     # database.
     redundant_key = SSHKey(key=key_string,
                            user=factory.make_User(),
                            keysource=keysource)
     redundant_key.save()
     self.assertRaises(IntegrityError,
                       SSHKey.objects.filter(id=redundant_key.id).update,
                       user=user)
Ejemplo n.º 18
0
 def make_sshkey(self, user, key_string=None):
     if key_string is None:
         key_string = get_data('data/test_rsa0.pub')
     key = SSHKey(key=key_string, user=user)
     key.save()
     return key
Ejemplo n.º 19
0
 def make_sshkey(self, user, key_string=None):
     if key_string is None:
         key_string = get_data('data/test_rsa0.pub')
     key = SSHKey(key=key_string, user=user)
     key.save()
     return key