def edit_host(user_id, host_id, hostname, servs): """Edits the given host.""" user_id = str(user_id) host_id = str(host_id) if host_id in red.smembers(_KEY_USER_HOSTS.format(user_id)): pip = red.pipeline() pip.hset(_KEY_HOST.format(host_id), _ATTR_HOST_NAME, hostname) saved_servs = red.smembers(_KEY_HOST_SET_SERVS.format(host_id)) # Remove all pip.delete(_KEY_HOST_SET_SERVS.format(host_id)) for serv in saved_servs: if red.scard(_KEY_SERVNAME_HOSTS.format(serv)) == 1: pip.delete(_KEY_SERVNAME_HOSTS.format(serv)) pip.srem(_KEY_SERVS, serv) else: pip.srem(_KEY_SERVNAME_HOSTS.format(serv), host_id) # create new associated servs for serv in servs: pip.sadd(_KEY_HOST_SET_SERVS.format(host_id), serv) pip.sadd(_KEY_SERVNAME_HOSTS.format(serv), host_id) pip.sadd(_KEY_SERVS, serv) pip.execute() return (True, 'Everything went fine!') else: return (False, 'Unknown host for the given user.')
def _is_entry_consistent(user_id, typ, mac, nets): """Check whether the new entry rule is consistent with the existing ones, aka not black list and white list at the same time and not repeated.""" # Get the entries from the opposite type, check the mac if typ == 'B': sames = red.smembers(_KEY_ENTRY_BLACK_USER.format(user_id)) else: sames = red.smembers(_KEY_ENTRY_WHITE_USER.format(user_id)) if len(sames) > 0: for same in sames: entry = get_entry(same) if entry['mac'] == mac: for netid, _ in entry['nets']: if netid in nets: return False if typ == 'B': opposites = red.smembers(_KEY_ENTRY_WHITE_USER.format(user_id)) else: opposites = red.smembers(_KEY_ENTRY_BLACK_USER.format(user_id)) if len(opposites) > 0: for opp in opposites: entry = get_entry(opp) if entry['mac'] == mac: for netid, _ in entry['nets']: if netid in nets: return False return True
def get_entries(typ, user_id): """Gets all the entries of the specific type (B, W) for the given user.""" ents = None ret = [] if typ == 'B': ents = red.smembers(_KEY_ENTRY_BLACK_USER.format(user_id)) elif typ == 'W': ents = red.smembers(_KEY_ENTRY_WHITE_USER.format(user_id)) if ents is not None: for ent in ents: tmp = get_entry(ent) if tmp is not None: ret.append(tmp) return ret
def get_user_hosts(user_id): """Returns the hosts of a user""" hosts = red.smembers(_KEY_USER_HOSTS.format(str(user_id))) ret = [] for host in hosts: ret.append(get_host(host)) return ret
def delete_network(user_id, net_id): """Deletes a network.""" pip = red.pipeline() user_id = str(user_id) net_id = str(net_id) pip.delete(_KEY_NET.format(net_id)) pip.srem(_KEY_NETS_USER.format(user_id), net_id) # Delete associated events events = get_user_events_network(user_id, net_id) for event in events: pip.lrem(_KEY_EVENTS_USER_DATE.format(user_id, event['date']), 0, event['id']) pip.delete(_KEY_EVENT_USER.format(event['id'], user_id)) pip.delete(_KEY_EVENTS_USER_NET.format(user_id, net_id)) # user-network # Delete associated entries on wb lists for entry in get_entries_network(net_id): # delete network from the entry, if it has one network delete entry if red.scard(_KEY_ENTRY_SET_NETS.format(entry['id'])) == 1: pip.delete(_KEY_ENTRY.format(entry['id'])) # delete from w or b list of entries if entry['type'] == 'B': pip.srem(_KEY_ENTRY_BLACK_USER.format(user_id), entry['id']) else: pip.srem(_KEY_ENTRY_WHITE_USER.format(user_id), entry['id']) pip.srem(_KEY_ENTRY_SET_NETS.format(entry['id']), 0, net_id) pip.execute() # Search for associated api keys to this network apik_ids = red.smembers(_KEY_APIKS_USER.format(user_id)) for apik in apik_ids: tmp = red.hgetall(_KEY_APIK.format(apik)) if tmp and tmp['network'] == net_id: _delete_api_key(user_id, apik)
def delete_entry(user_id, entry_id): """Deletes an entry.""" user_id = str(user_id) entry_id = str(entry_id) auth = entry_id in red.smembers(_KEY_ENTRY_BLACK_USER.format(user_id)) if not auth: auth = entry_id in red.smembers(_KEY_ENTRY_WHITE_USER.format(user_id)) if auth: temp_ent = get_entry(entry_id) if temp_ent['type'] == 'B': red.srem(_KEY_ENTRY_BLACK_USER.format(user_id), entry_id) else: red.srem(_KEY_ENTRY_WHITE_USER.format(user_id), entry_id) red.delete(_KEY_ENTRY.format(entry_id)) for net in red.smembers(_KEY_ENTRY_SET_NETS.format(entry_id)): red.srem(_KEY_NET_SET_ENTRIES.format(net), entry_id) red.delete(_KEY_ENTRY_SET_NETS.format(entry_id))
def get_user_networks(user_id): """Returns all the user's networks.""" keys = red.smembers(_KEY_NETS_USER.format(str(user_id))) nets = [] for key in keys: tmp = get_network(key) if tmp is not None: nets.append(tmp) return nets
def get_host(host_id): """Returs a host.""" host_id = str(host_id) temp = red.hgetall(_KEY_HOST.format(host_id)) if temp is not None: temp['id'] = host_id servs = red.smembers(_KEY_HOST_SET_SERVS.format(host_id)) temp['services'] = servs return temp
def get_entry(entry_id): """Gets an entry.""" entry_id = str(entry_id) temp = red.hgetall(_KEY_ENTRY.format(entry_id)) if temp is not None: temp['id'] = entry_id nets = red.smembers(_KEY_ENTRY_SET_NETS.format(entry_id)) tempnets = [] if nets is not None: for net in nets: tempnet = get_network(net) tempnets.append((net, tempnet['name'])) temp['nets'] = tempnets return temp
def delete_host(user_id, host_id): """Deletes a host""" user_id = str(user_id) host_id = str(host_id) pip = red.pipeline() pip.srem(_KEY_USER_HOSTS.format(user_id), host_id) pip.delete(_KEY_HOST.format(host_id)) servs = red.smembers(_KEY_HOST_SET_SERVS.format(host_id)) for serv in servs: if red.scard(_KEY_SERVNAME_HOSTS.format(serv)) == 1: pip.delete(_KEY_SERVNAME_HOSTS.format(serv)) pip.srem(_KEY_SERVS, serv) else: pip.srem(_KEY_SERVNAME_HOSTS.format(serv), host_id) red.delete(_KEY_HOST_SET_SERVS.format(host_id)) pip.execute()
def get_user_network_ids(user_id): """Returns the netids for the networks of the given user.""" return red.smembers(_KEY_NETS_USER.format(str(user_id)))
def get_user_api_keys(user_id): """Returns the ids of the keys the user has.""" return red.smembers(_KEY_APIKS_USER.format(str(user_id)))
def get_services(): """Returns the set of known services.""" return red.smembers(_KEY_SERVS)
def get_entries_network(net_id): """Gets the entries in the given network.""" net_id = str(net_id) entries = red.smembers(_KEY_NET_SET_ENTRIES.format(net_id)) ret = [get_entry(x) for x in entries] return ret