Beispiel #1
0
    def test_generate_views(self):
        import base64

        # just test that
        resp = self.client.post(self.keys_url + "/generate")
        self.assertNotEqual(resp, "")

        data = json.loads(resp.content)
        self.assertEqual('private' in data, True)
        self.assertEqual('private' in data, True)

        # public key is base64 encoded
        base64.b64decode(data['public'].replace("ssh-rsa ", ""))

        # remove header/footer
        private = "".join(data['private'].split("\n")[1:-1])

        # private key is base64 encoded
        base64.b64decode(private)

        new_key = PublicKeyPair()
        new_key.content = data['public']
        new_key.name = "new key"
        new_key.user = '******'
        new_key.full_clean()
        new_key.save()
Beispiel #2
0
    def test_generate_views(self):
        import base64

        # just test that
        resp = self.client.post(self.keys_url + "/generate")
        self.assertNotEqual(resp, "")

        data = json.loads(resp.content)
        self.assertEqual('private' in data, True)
        self.assertEqual('private' in data, True)

        # public key is base64 encoded
        base64.b64decode(data['public'].replace("ssh-rsa ", ""))

        # remove header/footer
        private = "".join(data['private'].split("\n")[1:-1])

        # private key is base64 encoded
        base64.b64decode(private)

        new_key = PublicKeyPair()
        new_key.content = data['public']
        new_key.name = "new key"
        new_key.user = '******'
        new_key.full_clean()
        new_key.save()
Beispiel #3
0
def create_new_keypair(request):
    """Generates or imports a keypair.

    Normal response code: 201

    Error response codes: badRequest(400), unauthorized(401), forbidden(403),
                          conflict(409)
    """

    userid = request.credentials.userid
    if PublicKeyPair.user_limit_exceeded(userid):
        return HttpResponseServerError("SSH keys limit exceeded")

    req = utils.get_json_body(request)
    try:
        keypair = req['keypair']
        assert(isinstance(req, dict))
        name = keypair['name']
    except (KeyError, AssertionError):
        raise faults.BadRequest('Malformed request.')

    if re.match(key_name_regex, name) is None:
        raise faults.BadRequest('Invalid name format')

    try:
        # If the key with the same name exists in the database
        # a conflict error will be raised

        util.get_keypair(name, userid)
        # If we get past this point then the key is already present
        # in the database
        raise faults.Conflict('A keypair with that name already exists')
    except faults.ItemNotFound:
        new_keypair = PublicKeyPair(name=name, user=userid)

    gen_keypair = None
    try:
        new_keypair.content = keypair['public_key']
    except KeyError:
        # If the public_key field is omitted, generate a new
        # keypair and return both the private and the public key
        if not SUPPORT_GENERATE_KEYS:
            raise faults.Forbidden(
                "Application does not support ssh keys generation")

        gen_keypair = generate_keypair()
        new_keypair.content = gen_keypair['public']

    new_keypair.save()

    data = keypair_to_dict(new_keypair)
    if gen_keypair is not None:
        data['keypair']['private_key'] = gen_keypair['private']

    return HttpResponse(json.dumps(data), status=201)
Beispiel #4
0
def create_new_keypair(request):
    """Generates or imports a keypair.

    Normal response code: 201

    Error response codes: badRequest(400), unauthorized(401), forbidden(403),
                          conflict(409)
    """

    userid = request.credentials.userid
    if PublicKeyPair.user_limit_exceeded(userid):
        return HttpResponseServerError("SSH keys limit exceeded")

    req = utils.get_json_body(request)
    try:
        keypair = req['keypair']
        assert (isinstance(req, dict))
        name = keypair['name']
    except (KeyError, AssertionError):
        raise faults.BadRequest('Malformed request.')

    if re.match(key_name_regex, name) is None:
        raise faults.BadRequest('Invalid name format')

    try:
        # If the key with the same name exists in the database
        # a conflict error will be raised

        util.get_keypair(name, userid)
        # If we get past this point then the key is already present
        # in the database
        raise faults.Conflict('A keypair with that name already exists')
    except faults.ItemNotFound:
        new_keypair = PublicKeyPair(name=name, user=userid)

    gen_keypair = None
    try:
        new_keypair.content = keypair['public_key']
    except KeyError:
        # If the public_key field is omitted, generate a new
        # keypair and return both the private and the public key
        if not SUPPORT_GENERATE_KEYS:
            raise faults.Forbidden(
                "Application does not support ssh keys generation")

        gen_keypair = generate_keypair()
        new_keypair.content = gen_keypair['public']

    new_keypair.save()

    data = keypair_to_dict(new_keypair)
    if gen_keypair is not None:
        data['keypair']['private_key'] = gen_keypair['private']

    return HttpResponse(json.dumps(data), status=201)