Exemple #1
0
 def test_decrypt_and_verify_failed(self):
     """Test decrypt and verify. """
     apikey = "pWEzB4yMM1518346407"
     client_cert_path = os.path.join(self.cur_path, "../cryption/ecc/certs")
     with self.assertRaises(Exception):
         result = decrypt_and_verify(self.cipher, apikey, client_cert_path)
         self.assertTrue(result)
 def test_decrypt_and_verify(self):
     """Test decrypt and verify. """
     run_cmd_decrypt_and_verify = mock.Mock(return_value=self.cipher_body)
     apikey = "pWEzB4yMM1518346407"
     client_cert_path = os.path.join(self.cur_path, "../cryption/ecc/certs")
     with mock.patch('cryption.crypto.run_cmd', run_cmd_decrypt_and_verify):
         result = decrypt_and_verify(self.cipher, apikey, client_cert_path)
         self.assertEqual(0, result["ErrCode"])
Exemple #3
0
    def require_ok(self, resp):
        """Validate response.
    
        :param resp: response
        :Returns: plain response body. If failing to decrypt
        the json, then will put client error message and error
        code into "ErrMsg" field, and put client error code(
        like 100XX) into "ErrCode" field
        """
        result = RESP_DICT
        if resp.status_code != STATUS_CODE_OK:
            logging.error("Status code: {}, Client Error, body: {}".format(
                    resp.status_code,
                    resp.text))
    
        if len(resp.text) <= 0:
            logging.error("Respond error: Body empty")
            result["ErrCode"] = CODE_RESP_BODY_EMPTY
            result["ErrMsg"] = MSG_RESP_BODY_EMPTY
            logging.error("*****result: {}****".format(result))

            return result
    
        # Decrypt and verify
        if self.__enable_crypto:
            try:
                plain_body = ""
                plain_body = decrypt_and_verify(
                        resp.text,
                        self.__apikey,
                        self.__cert_path
                        )
                result.update(json.loads(plain_body))
            except Exception:
                logging.error(
                        "cannot decrypt_and_verify response body: %s",
                        resp.text
                        )
                result["ErrCode"] = CODE_DECRYPT_FAILED
                result["ErrMsg"] = MSG_DECRYPT_FAILED
            finally:
                return result
    
        result.update(json.loads(resp.text))
        return result
Exemple #4
0
def require_ok(resp, apikey, cert_path):
    """Validate response.

    :param resp: response
    :param apikey: the api key authorized by the server
    :param cert_path: path of private key file and cert file
    :Returns: plain response body
    """
    if resp.status_code != requests.codes.ok:
        logging.error("Status code: %s, Client Error, body: %s" %
                      (resp.status_code, resp.text))

    if len(resp.text) <= 0:
        logging.error("Respond error: Body empty")
        return None

    ## Decrypt and verify
    plain_body = decrypt_and_verify(resp.text, apikey, cert_path)
    return json.loads(plain_body)