Пример #1
0
    async def synchronize(self, request, body, auth_user, **kwargs):
        file = open("result_test_file_size", "a")
        time_start = time.time()
        username = auth_user["username"]
        mappings = body['mapping']
        for mapping in mappings:
            existed_mapping = elasticsearch.get_index_name(
                username, mapping['identifier'])
            if existed_mapping != {}:
                return fail("Existed mapping")

        ids_ = elasticsearch.create_mapping_blockchain_mapping_record(
            username, mappings)
        try:
            for mapping in mappings:
                ipfs.mkdir(mapping['identifier'] + "_" + username)
            language_processor.processor(mappings, auth_user)
        except Exception as e:
            elasticsearch.delete_mapping_blockchain_mapping_record(ids_)
            for mapping in mappings:
                ipfs.rm(mapping['identifier'] + "_" + username)
            return fail(str(e))
        save_to_blockchain.delay()
        string = {
            "time": time.time() - time_start,
            "index": mappings[0]['identifier'],
            "size": const.BLOCK_IPFS_SIZE,
            "type": "save"
        }
        file.writelines(str(string) + ", \n")
        return json_response(swagger.parse_swagger())
Пример #2
0
def create_account(body):
    password = body['password']
    username = body['username']
    user = elasticsearch.get_account_data(username)
    if user != {}:
        return fail("User existed")
    aes_key = crypto.fit_length(password)
    password = crypto.hash_password(password).hex()
    user_record = {
        "password": password,
        "username": username,
        "account_detail": {}
    }
    eth_address = ""
    if "eth_endpoint" in body:
        try:
            account = create_eth_account(body['eth_endpoint'])
        except Exception as e:
            return fail(e)
        private_key = account['private_key']
        eth_address = account['address']
        address = account['address']
        private_key = crypto.encrypt_private_key(aes_key, private_key).hex()
        user_record["account_detail"]["etherum"] = {
            "public_key": address,
            "private_key": private_key,
            "endpoint_url": body['eth_endpoint']
        }
    if "sawtooth_endpoint" in body:
        try:
            account = create_sawtooth_account(body['sawtooth_endpoint'])
        except Exception as e:
            logging.warning(type(e))
            return fail(message=str(e))
        private_key = account['private_key']
        address = account['address']
        private_key = crypto.encrypt_private_key(
            aes_key, bytes.fromhex(private_key)).hex()
        user_record["account_detail"]["sawtooth"] = {
            "public_key": address,
            "private_key": private_key,
            "endpoint_url": body['sawtooth_endpoint']
        }

    elasticsearch.create_user(user_record)
    return success("User created", address=eth_address)
Пример #3
0
 async def get_data(self, request, body, auth_user, **kwargs):
     file = open("result_test_file_size", "a")
     time_start = time.time()
     index_name = body.get("identifier")
     filter_condition = body.get("filter")
     mapping = elasticsearch.get_index_name(auth_user['username'],
                                            index_name)
     if mapping == {}:
         return fail("Wrong identifier")
     else:
         blockchain_type = mapping["_source"]["blockchain"]
     endpoint_url = auth_user["account_detail"][blockchain_type][
         "endpoint_url"]
     public_key = auth_user["account_detail"][blockchain_type]["public_key"]
     password = auth_user['password']
     transaction_id = mapping['_source']['transaction_id']
     condition = []
     for i in range(0, len(filter_condition)):
         condition.append(filter_condition[i].split("->"))
         attribute_array = condition[i][0].split(".")
         str_replace = "data"
         for attribute in attribute_array:
             str_replace += "['" + attribute + "']"
         condition[i][1] = condition[i][1].replace(condition[i][0],
                                                   str_replace)
         if condition[i][0] not in condition[i][1]:
             return fail(
                 "Field {field} is not referred in filter in declaration {declaration}"
                 .format(field=condition[i][0],
                         declaration=filter_condition[i]))
     try:
         data_return = get_data(transaction_id, blockchain_type,
                                endpoint_url, condition, public_key,
                                password)
     except Exception as e:
         return fail(str(e))
     string = {
         "time": time.time() - time_start,
         "index": index_name,
         "size": const.BLOCK_IPFS_SIZE,
         "type": "get"
     }
     file.writelines(str(string) + ", \n")
     return success(data_return)
Пример #4
0
def validate_types(schema, body):
    try:
        validate(instance=body, schema=schema)
    except ValidationError as e:
        string_array_error = str(e).split("\n")
        array = {"On instance", "[", "]", "'", ":", " "}
        for a in array:
            string_array_error[5] = string_array_error[5].replace(a, "")
        message = string_array_error[0] + " on field '" + string_array_error[5] + "'"

        return fail(message)
Пример #5
0
 async def fun(*args, **kwargs):
     if args[1].headers["Content-Type"].lower() == "application/json":
         try:
             body = await args[1].json()
         except:
             return fail("Improper json format")
     else:
         data = await args[1].post()
         body = data.get("mapping").file.read()
         body = json.loads(body.decode())
     kwargs['body'] = body
     return await func(*args, **kwargs)
Пример #6
0
    async def fun(*args, **kwargs):
        token = args[1].headers.get('AUTHORIZATION')
        if token is None:
            return fail('No auth token provided')
        token_prefixes = ('Bearer', 'Token')
        for prefix in token_prefixes:
            if prefix in token:
                token = token.partition(prefix)[2].strip()
        try:
            token_dict = token_handler.deserialize_auth_token(
                const.SECRET_KEY, token)
        except BadSignature:
            return fail('Invalid auth token')
        username = token_dict.get('username')
        password = token_dict.get('password')

        user = elasticsearch.get_account_data(username)
        if len(user) == 0:
            return fail('Token is not associated with an user')
        user['password'] = password
        kwargs['auth_user'] = user
        return await func(*args, **kwargs)
Пример #7
0
def validate_fields(required_fields, body):
    for field in required_fields:
        if body.get(field) is None:
            return fail(
                "'{}' parameter is required".format(field))