def string_test(): #==================== test upload string ========================= tx_json = {} # tx_size = 128*1024 # tx_data = TypesUtil.string_to_hex(os.urandom(tx_size)) kv_mode = 1 if (kv_mode == 0): # ----------------- 1) value -------------- tx_data = "This is samuel test message!" else: # ----------------- 2) key:value -------------- json_value = {} json_value['id'] = '1' json_value['name'] = "samuel_xu" json_value['age'] = 28 tx_data = TypesUtil.json_to_string(json_value) tx_json['data'] = tx_data post_ret = RPC_Curl.upload_string(tx_json) print(post_ret) #==================== test download string ========================= query_json = {} # use string hash # swarm_hash = '4c9293963f4c1e9b7cd3e9e3a45d41ec8a961278be2701ce071317d4832d3bca' swarm_hash = post_ret['results'] query_ret = RPC_Curl.download_string(swarm_hash) print(query_ret)
def test_ABACRules(): #test Api path_db = 'ABAC.db' # new Userdata table ABACRuleManager.create_table(path_db) #ABACRuleManager.remove_table(path_db) #test insert user data rule_arg = {} rule_arg['Name'] = "rule2" rule_arg['AttrUser'] = "******" rule_arg['AttrAction'] = "GET" rule_arg['AttrResource'] = "/test/api/v1.0/dt" #define environmental attribute env_time = {} env_time['type'] = 'Timespan' env_time['value'] = {} env_time['value']['start'] = '14:12:32' env_time['value']['end'] = '23:12:32' rule_arg['AttrEnvironment'] = TypesUtil.json_to_string(env_time) ABACRuleManager.insert_entry(path_db, rule_arg) #search test rules_list = ABACRuleManager.select_Allentry(path_db) print(rules_list) print(TypesUtil.string_to_json(rules_list[0]['AttrEnvironment'])['type']) rules_entry = ABACRuleManager.select_ByName(path_db, 'rule1') print(rules_entry) #build up fields condition field_data = {} #field_data['Name'] = "rule1" field_data['AttrUser'] = "******" field_data['AttrAction'] = "GET" field_data['AttrResource'] = "/test/api/v1.0/dt/project" #define environmental attribute env_time = {} env_time['type'] = 'Timespan' env_time['value'] = {} env_time['value']['start'] = '8:12:32' env_time['value']['end'] = '14:12:32' #field_data['AttrEnvironment'] = TypesUtil.json_to_string(env_time) #rules_entry = ABACRuleManager.select_ByFieldname(path_db, field_data) #print(rules_entry) #update test update_arg = rule_arg update_arg['AttrUser'] = "******" update_arg['AttrResource'] = "/test/api/v1.0/dt/project" env_update = TypesUtil.string_to_json(update_arg['AttrEnvironment']) env_update['value']['start'] = "13:12:32" env_update['value']['end'] = "22:12:32"
if(test_fun == 0): #==================== test upload data ========================= if(kv_mode==0): # ----------------- 1) value -------------- # tx_size = 128 # tx_data = TypesUtil.string_to_hex(os.urandom(tx_size)) tx_data = "This is samuel test message!" else: # ----------------- 2) key:value -------------- json_value={} json_value['id']='1' json_value['name']="samuel_xu" json_value['age']=28 tx_data = TypesUtil.json_to_string(json_value) tx_json['data']=tx_data post_ret = test_upload_data(target_address, tx_json) print(post_ret) #==================== test download data ========================= # swarm_hash = '4c9293963f4c1e9b7cd3e9e3a45d41ec8a961278be2701ce071317d4832d3bca' swarm_hash = post_ret['data'] query_ret = test_download_data(target_address,swarm_hash) print(query_ret) elif(test_fun == 1): #==================== test upload file ========================= if(op_status==0): file_name = "test_data.txt" else:
def is_valid_access_request(req_args): #Get account address addr_client = req_args.json['host_address'] url_rule = req_args.json['url_rule'] #Define ls_time_exec to save executing time to log ls_time_exec = [] # define branch control flag query_src = 0 # smart contract:0, local cache:1 is_cachetoken = 0 # cache data:1, not cache data:0 #get token data start_time = time.time() if (query_src == 0): # ----------a) query token from smart contract ------------ token_data = CapPolicy.get_token(addr_client) #print(token_data) if (is_cachetoken == 1): # 2) Save token data to local token.dat FileUtil.AddLine('ACtoken.dat', TypesUtil.json_to_string(token_data)) else: # ----------b) read authToken from local cached file ------------ read_token = FileUtil.ReadLines('ACtoken.dat') token_data = TypesUtil.string_to_json(read_token[0]) #print(token_data) exec_time = time.time() - start_time ls_time_exec.append(format(exec_time * 1000, '.3f')) print("Execution time of get_token is:%2.6f" % (exec_time)) #extract access action from request access_data = {} access_data['url_rule'] = url_rule access_data['method'] = req_args.method #print(access_data) start_time = time.time() if (not CapPolicy.is_token_valid(token_data)): print('token valid fail') return False exec_time = time.time() - start_time ls_time_exec.append(format(exec_time * 1000, '.3f')) print("Execution time of is_token_valid is:%2.6f" % (exec_time)) start_time = time.time() if (not CapPolicy.is_access_valid(token_data, access_data)): print('access valid fail') return False exec_time = time.time() - start_time ls_time_exec.append(format(exec_time * 1000, '.3f')) print("Execution time of is_access_valid is:%2.6f" % (exec_time)) #transfer list to string str_time_exec = " ".join(ls_time_exec) #print(str_time_exec) FileUtil.AddLine('capac_exec_time_server.log', str_time_exec) return True
def verify_AuthToken(req_args): # extract client address from req_args #addr_client = req_args['host_address'] addr_client = req_args.json['host_address'] #print(addr_client) # Define ls_time_exec to save executing time to log ls_time_exec = [] # define branch control flag query_src = 0 # smart contract:0, local cache:1 is_cachetoken = 0 # cache data:1, not cache data:0 # mark the start time start_time = time.time() if (query_src == 0): # ----------a) query token from smart contract ------------ # 1) get host Vnode data in contract accounts = myAuthToken.getAccounts() json_VNode_host = AuthPolicy.get_VNodeInfo(accounts[0]) #2) get client Vnode in contract json_VNode_client = AuthPolicy.get_VNodeInfo(addr_client) #print(json_VNode_host) #print(json_VNode_client) if (is_cachetoken == 1): json_authToken = {} json_authToken['host'] = json_VNode_host json_authToken['client'] = json_VNode_client #print(json_authToken) # 2) Save token data to local token.dat FileUtil.AddLine('authToken.dat', TypesUtil.json_to_string(json_authToken)) else: # ----------b) read authToken from local cached file ------------ # 3) read token from local data, low overload read_token = FileUtil.ReadLines('authToken.dat') token_data = TypesUtil.string_to_json(read_token[0]) json_VNode_host = token_data['host'] json_VNode_client = token_data['client'] print("localhost: %s | client: %s" % (json_VNode_host, json_VNode_client)) #3) authicate identity based on token # compare ret_indexAuth = ( json_VNode_host['VZoneID'] == json_VNode_client['VZoneID']) # calculate computational cost exec_time = time.time() - start_time ls_time_exec.append(format(exec_time * 1000, '.3f')) print("Execution time of %s authentication is:%2.6f" % (addr_client, exec_time)) #transfer list to string str_time_exec = " ".join(ls_time_exec) #print(str_time_exec) FileUtil.AddLine('auth_exec_time_server.log', str_time_exec) #return index authentication result return ret_indexAuth
print(json_data['resource']) print(json_data['action']) print(json_data['conditions'])''' #Send transact #mytoken.initAttributeToken(accountAddr); #mytoken.setAttributeToken_isValid(accountAddr, True) #set issue date and expired date nowtime = datetime.datetime.now() #calculate issue_time and expire_time issue_time = DatetimeUtil.datetime_timestamp(nowtime) duration = DatetimeUtil.datetime_duration(0, 1, 0, 0) expire_time = DatetimeUtil.datetime_timestamp(nowtime + duration) #mytoken.setAttributeToken_expireddate(accountAddr, issue_time, expire_time) #set delegation right #mytoken.setAttributeToken_delegateDepth(accountAddr, 4) #mytoken.setAttributeToken_delegatee(accountAddr, '0x9c2da23272c8fec791c54febd0507fb519730cee') #mytoken.setAttributeToken_revokeDelegate(accountAddr, '0x9c2da23272c8fec791c54febd0507fb519730cee') #set attribute attribute = generateAttribute() str_attribute = TypesUtil.json_to_string(attribute) #mytoken.setAttributeToken_Attribute(accountAddr, str_attribute) #read attribute '''read_attr=TypesUtil.string_to_json(token_data[2][1]) print(read_attr['attrEnvironment'][1]['value'])''' pass