Skip to content

Commit

Permalink
Add auth data to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Sep 18, 2023
1 parent dc4b4ef commit 9ef9e8f
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions IM/InfrastructureList.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_inf_ids(auth=None):
if auth:
# In this case only loads the auth data to improve performance
inf_ids = []
for inf_id in InfrastructureList._get_inf_ids_from_db():
for inf_id in InfrastructureList._get_inf_ids_from_db(auth):
inf = None
# Check if the infrastructure is in memory
if (inf_id in InfrastructureList.infrastructure_list and
Expand Down Expand Up @@ -153,10 +153,11 @@ def init_table():
InfrastructureList.logger.debug("Creating the IM database!.")
if db.db_type == DataBase.MYSQL:
db.execute("CREATE TABLE inf_list(rowid INTEGER NOT NULL AUTO_INCREMENT UNIQUE,"
" id VARCHAR(255) PRIMARY KEY, deleted INTEGER, date TIMESTAMP, data LONGBLOB)")
" id VARCHAR(255) PRIMARY KEY, deleted INTEGER, date TIMESTAMP, data LONGBLOB,"
" auth LONGBLOB)")
elif db.db_type == DataBase.SQLITE:
db.execute("CREATE TABLE inf_list(id VARCHAR(255) PRIMARY KEY, deleted INTEGER,"
" date TIMESTAMP, data LONGBLOB)")
" date TIMESTAMP, data LONGBLOB, auth LONGBLOB)")
db.close()
return True
else:
Expand All @@ -175,25 +176,30 @@ def _get_data_from_db(db_url, inf_id=None, auth=None):
db = DataBase(db_url)
if db.connect():
inf_list = {}
data_field = "data"
if auth:
data_field = "auth"
if inf_id:
if db.db_type == DataBase.MONGO:
res = db.find("inf_list", {"id": inf_id}, {"data": True})
res = db.find("inf_list", {"id": inf_id}, {data_field: True, "deleted": True})
else:
res = db.select("select data from inf_list where id = %s", (inf_id,))
res = db.select("select " + data_field + ",deleted from inf_list where id = %s", (inf_id,))
else:
if db.db_type == DataBase.MONGO:
res = db.find("inf_list", {"deleted": 0}, {"data": True}, [('_id', -1)])
res = db.find("inf_list", {"deleted": 0}, {data_field: True, "deleted": True}, [('_id', -1)])
else:
res = db.select("select data from inf_list where deleted = 0 order by rowid desc")
res = db.select("select " + data_field + ",deleted from inf_list where deleted = 0 order by rowid desc")
if len(res) > 0:
for elem in res:
if db.db_type == DataBase.MONGO:
data = elem['data']
data = elem[data_field]
deleted = elem["deleted"]
else:
data = elem[0]
deleted = elem[1]
try:
if auth:
inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize_auth(data)
inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize_auth(inf_id, deleted, data)
else:
inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize(data)
inf_list[inf.id] = inf
Expand Down Expand Up @@ -239,15 +245,51 @@ def _save_data_to_db(db_url, inf_list, inf_id=None):
return None

@staticmethod
def _get_inf_ids_from_db():
def _gen_where_from_auth(auth, get_all):
like = ""
if auth:
for elem in auth.getAuthInfo('InfrastructureManager'):
if elem.get("username"):
if like:
like += " and "
like += "auth like '%%" + elem.get("username") + "%%'"

if like:
return "where deleted = 0 and auth is null or (" + like + ")"
elif get_all:
return ""
else:
return "where deleted = 0"

@staticmethod
def _gen_filter_from_auth(auth, get_all):
like = ""
if auth:
for elem in auth.getAuthInfo('InfrastructureManager'):
if elem.get("username"):
if like:
like += "|"
like += elem.get("username")

if like:
return {"deleted": 0, "auth": {"$regex": like}}
elif get_all:
return {}
else:
return {"deleted": 0}

@staticmethod
def _get_inf_ids_from_db(auth=None, get_all=False):
try:
db = DataBase(Config.DATA_DB)
if db.connect():
inf_list = []
if db.db_type == DataBase.MONGO:
res = db.find("inf_list", {"deleted": 0}, {"id": True}, [('id', -1)])
filter = InfrastructureList._gen_filter_from_auth(auth, get_all)
res = db.find("inf_list", filter, {"id": True}, [('id', -1)])
else:
res = db.select("select id from inf_list where deleted = 0 order by rowid desc")
where = InfrastructureList._gen_where_from_auth(auth, get_all)
res = db.select("select id from inf_list %s order by rowid desc" % where)
for elem in res:
if db.db_type == DataBase.MONGO:
inf_list.append(elem['id'])
Expand Down

0 comments on commit 9ef9e8f

Please sign in to comment.