예제 #1
0
def upload_public_key(api_key, public_key, old_public_key=None):
    """Upload agent's public key to Bastio on the account specified by ``api_key``.

    This action will create a new server when ``old_public_key`` is not specified
    and it is the first time we see this ``public_key``. However the public key
    we store that identifies this server will be replaced IFF we already have
    ``old_public_key`` in our records and ``public_key`` does not exist in our
    database.

    :param api_key:
        The API key for the Bastio account.
    :type api_key:
        str
    :param public_key:
        The agent's public key to be uploaded to Bastio's servers.
    :type public_key:
        The string output of :func:`bastio.ssh.crypto.RSAKey.get_public_key`.
    :param old_public_key:
        The agent's old public key to be replaced by a new one.
    :type old_public_key:
        The string output of :func:`bastio.ssh.crypto.RSAKey.get_public_key`.
    :raises:
        :class:`bastio.excepts.BastioAccountError`
    """
    errmsg = "upload public key failed: "

    if not old_public_key:
        old_public_key = ''

    if old_public_key and not RSAKey.validate_public_key(old_public_key):
        raise BastioAccountError(errmsg + "invalid old public key")

    if not RSAKey.validate_public_key(public_key):
        raise BastioAccountError(errmsg + "invalid new public key")

    payload = Json()
    payload.api_key = api_key
    payload.public_key = public_key
    payload.old_public_key = old_public_key

    headers = {'Content-type': 'application/json'}
    response = __send_request('post', url=__upload_key_endpoint, verify=True,
            data=payload.to_json(), headers=headers)
    if response.status_code == requests.codes.bad: # 400
        raise BastioAccountError(errmsg + "missing or invalid field")
    elif response.status_code == requests.codes.forbidden: # 403
        raise BastioAccountError(errmsg + "not authorized or invalid API key")
    elif response.status_code == requests.codes.okay: # 200
        return

    # An unexpected response status code
    raise BastioAccountError(
            errmsg + "unexpected response status code ({})".format(
                response.status_code))
예제 #2
0
    def test_json_mixin(self):
        obj = Json()
        obj.field_str = 'test'
        obj.field_int = 434
        obj.field_float = 4.34
        obj.field_sub = Json()
        obj.field_sub.field_str = 'sub_test'
        obj.field_sub.field_int = 123
        obj.field_sub.field_float = 1.23
        obj_json = Json().from_json(obj.to_json())
        self.assertEqual(obj_json.to_json(), obj.to_json())
        self.assertIn('field_str', obj_json)
        self.assertIsInstance(obj_json.field_str, basestring)
        self.assertIsInstance(obj_json.field_int, int)
        self.assertIsInstance(obj_json.field_float, float)
        self.assertIsInstance(obj_json.field_sub, Json)

        obj_json.field_fail = [Json()]
        with self.assertRaises(TypeError):
            obj_json.to_json()
예제 #3
0
 def _construct_protocol_msg(self):
     obj = Json()
     obj.mid = "234293472786"
     self._msg_parser_raises(obj)
     return obj