Example #1
0
    def test_03_remove_authorized_keys(self):
        # 0 keys left in a empty authorized_keys
        self.assertEquals(0, git.remove_authorized_keys([]))        

        # adding one key
        self.assertEquals(1, git.add_authorized_keys([ "ssh-rsa test ema@mars" ]))

        # we should now have exactly 1 authorized key
        self.assertEquals(1, len(git.get_authorized_keys()))

        # adding another key
        self.assertEquals(1, git.add_authorized_keys([ "ssh-rsa test2 ema@mars" ]))

        # we should now have exactly 2 authorized keys
        self.assertEquals(2, len(git.get_authorized_keys()))

        # adding another key
        self.assertEquals(1, git.add_authorized_keys([ "ssh-rsa test3 ema@mars" ]))

        # we should now have exactly 3 authorized keys
        self.assertEquals(3, len(git.get_authorized_keys()))

        # removing a single key should leave us with 2 keys
        self.assertEquals(2, git.remove_authorized_keys([ "ssh-rsa test2 ema@mars" ]))

        # we should now have exactly 2 authorized keys
        self.assertEquals(2, len(git.get_authorized_keys()))

        # removing the two remaining keys
        self.assertEquals(0, git.remove_authorized_keys(
                [ "ssh-rsa test ema@mars", "ssh-rsa test3 ema@mars" ]
        ))

        # we should now have no authorized_key left
        self.assertEquals(0, len(git.get_authorized_keys()))
Example #2
0
    def test_02_add_authorized_keys(self):
        new_keys = [ "ssh-rsa test123 ema@uranus" ]

        # add_authorized_keys should return 1 on successful insertion
        self.assertEquals(1, git.add_authorized_keys(new_keys))

        # add_authorized_keys should return 0 if the key is already present
        self.assertEquals(0, git.add_authorized_keys(new_keys))

        # we should now have exactly 1 authorized key
        self.assertEquals(1, len(git.get_authorized_keys()))

        # adding a new one
        self.assertEquals(1, git.add_authorized_keys([ "ssh-rsa test ema@mars" ]))

        # we should now have exactly 2 authorized keys
        self.assertEquals(2, len(git.get_authorized_keys()))
Example #3
0
    def upload_authorized_key(self, kwargs):
        exp_params = [('key', is_uploaded_file)]
        try:
            key = check_arguments(exp_params, kwargs)
        except Exception as ex:
            return HttpErrorResponse("%s" % ex)

        key_lines = key.file.readlines()
        num_added = git.add_authorized_keys(key_lines)

        return HttpJsonResponse({'outcome': "%s keys added to authorized_keys" % num_added})
Example #4
0
  def upload_authorized_key(self, kwargs):
    if 'key' not in kwargs:
      return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_MISSING, 'key').message)

    key = kwargs.pop('key')

    if len(kwargs) != 0:
      return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_UNEXPECTED, kwargs.keys()).message)
    if not isinstance(key, FileUploadField):
      return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_INVALID, detail='key should be a file').message)

    key_lines = key.file.readlines()
    num_added = git.add_authorized_keys(key_lines)
    
    return HttpJsonResponse({'outcome': "%s keys added to authorized_keys" % num_added })
Example #5
0
    def upload_authorized_key(self, kwargs):
        if 'key' not in kwargs:
            return HttpErrorResponse(
                ManagerException(ManagerException.E_ARGS_MISSING,
                                 'key').message)

        key = kwargs.pop('key')

        if len(kwargs) != 0:
            return HttpErrorResponse(
                ManagerException(ManagerException.E_ARGS_UNEXPECTED,
                                 kwargs.keys()).message)
        if not isinstance(key, FileUploadField):
            return HttpErrorResponse(
                ManagerException(ManagerException.E_ARGS_INVALID,
                                 detail='key should be a file').message)

        key_lines = key.file.readlines()
        num_added = git.add_authorized_keys(key_lines)

        return HttpJsonResponse(
            {'outcome': "%s keys added to authorized_keys" % num_added})