316 lines
15 KiB
Python
316 lines
15 KiB
Python
from fastapi import FastAPI,Depends
|
|
import mysql.connector
|
|
from datetime import datetime
|
|
import Classes.main_classes as CL ## Importiert Classes
|
|
import uvicorn
|
|
import logging
|
|
import auth
|
|
from fastapi.security.api_key import APIKey
|
|
|
|
logging.basicConfig(level=logging.DEBUG) # add this line
|
|
logger = logging.getLogger("foo")
|
|
|
|
## uvicorn.exe main:app --reload Startet den API-Server
|
|
|
|
def database(query,name,user):
|
|
|
|
mydb = mysql.connector.connect(
|
|
host="172.17.1.21",
|
|
port=3306,
|
|
user="root",
|
|
password="N53yBCswuawzBzS445VNAhWVMs3N59Gb9szEsrzXRBzarDqpdETpQeyt5v5CGe",
|
|
database="" + name,
|
|
auth_plugin='mysql_native_password',
|
|
)
|
|
mydb.connect()
|
|
cursor = mydb.cursor()
|
|
if "SELECT" in query:
|
|
if user:
|
|
cursor.execute(query, user)
|
|
return cursor.fetchone()
|
|
else:
|
|
cursor.execute(query, user)
|
|
return cursor.fetchall()
|
|
if "SHOW" in query:
|
|
cursor.execute(query)
|
|
return cursor.fetchall()
|
|
if "INSERT" in query:
|
|
cursor.execute(query,user)
|
|
mydb.commit()
|
|
if "UPDATE" in query:
|
|
cursor.execute(query,user)
|
|
mydb.commit()
|
|
mydb.close()
|
|
|
|
app = FastAPI()
|
|
|
|
@app.get("/")
|
|
async def notify(api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database(f"SELECT * FROM Notifications", "Stines-GmbH","")
|
|
|
|
@app.get("/gettime")
|
|
async def gettime(api_key: APIKey = Depends(auth.get_api_key)):
|
|
return f"Current Time from API-Server http://api.stines.de is {datetime.now()}"
|
|
|
|
@app.post("/ping")
|
|
async def ping(ping: CL.CL_Ping_Set,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `Ping-Server` VALUES ('{datetime.now()}','{ping.name}','{ping.ipaddress}')","Stines-GmbH","")
|
|
return f"ADD Ping from {ping.name} to Server"
|
|
|
|
## Get Database from Server
|
|
@app.post("/dbget")
|
|
async def database_get(get: CL.CL_Database_get,api_key: APIKey = Depends(auth.get_api_key)):
|
|
get_database = database("SHOW DATABASES","","")
|
|
if get.name in f"({get_database},)":
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
@app.post("/host/get/netvolume")
|
|
async def getvolume(get: CL.CL_GET_VOLUMES,api_key: APIKey = Depends(auth.get_api_key)):
|
|
## Anpassung zum neune Schema erstellen
|
|
result = database(f"SELECT `{get.dbname}`.Netvolume.Volume_Path, `{get.dbname}`.Netvolume.Volume_Key, `{get.dbname}`.Netvolume.Volume_Name FROM `{get.dbname}`.`Active-Directory-User` INNER JOIN `{get.dbname}`.GruppenToADUser ON `{get.dbname}`.GruppenToADUser.ADUserID = `{get.dbname}`.`Active-Directory-User`.ID INNER JOIN `{get.dbname}`.Gruppen ON `{get.dbname}`.GruppenToADUser.GruppenID = `{get.dbname}`.Gruppen.id INNER JOIN `{get.dbname}`.GruppenToNetvolumes ON `{get.dbname}`.GruppenToNetvolumes.GruppenID = `{get.dbname}`.Gruppen.id INNER JOIN `{get.dbname}`.Netvolume ON `{get.dbname}`.GruppenToNetvolumes.NetvolumeID = `{get.dbname}`.Netvolume.ID WHERE `{get.dbname}`.`Active-Directory-User`.SamAccountName = '{get.user}' ORDER BY `{get.dbname}`.`Active-Directory-User`.SamAccountName DESC","","")
|
|
volume = []
|
|
if not result:
|
|
return False
|
|
else:
|
|
for i in result:
|
|
volume.append(i)
|
|
return volume
|
|
|
|
@app.post("/info/getuser")
|
|
async def getuser(get: CL.CL_GET_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT `disabled` FROM `Active-Directory-RDS-User` where `SamAccountName` = '{get.user}'",f"{get.database}","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/host/get")
|
|
async def gethost(get: CL.CL_GET_HOST,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT id FROM `hardware` where `hostname` = '{get.hostname}'",f"{get.dbname}","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/host/get/cpu")
|
|
async def getcpu(get: CL.CL_GET_CPU,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(
|
|
f"SELECT id FROM `{get.dbname}`.cpulist WHERE cpuname = '{get.cpuname} AND cputyp = {get.cputype}')",
|
|
"", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/host/get/database")
|
|
async def getdatabase(get: CL.CL_GET_DATABASE,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT Datenbank FROM `Kunden` where `Subnetz` = '{get.ip}'","Stines-GmbH","")
|
|
print(result[0][0])
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/host/set")
|
|
async def sethost(get: CL.CL_SET_HOST,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `{get.dbname}`.hardware (hostname,ipadresse,ram,hddused,hddfree,hddtotal) VALUES ('{get.hostname}','{get.ipadresse}','{get.ram}','{get.hddused}','{get.hddfree}','{get.hddtotal}')","","")
|
|
result = database(
|
|
f"SELECT id FROM `{get.dbname}`.hardware WHERE hostname = '{get.hostname}')",
|
|
"", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/host/cpu")
|
|
async def setcpu(get: CL.CL_SET_CPU,api_key: APIKey = Depends(auth.get_api_key)):
|
|
check = database(f"SELECT id FROM `{get.dbname}`.cpulist WHERE cpuname = '{get.cpuname}' AND ghz")
|
|
database(f"INSERT INTO `{get.dbname}`.cpulist (cpuname,ghz,cputyp) VALUES ('{get.cpuname}','{get.ghz}','{get.cputyp}')","","")
|
|
result = database(
|
|
f"SELECT id FROM `{get.dbname}`.cpulist WHERE cpuname = '{get.cpuname} AND cputyp = {get.cputyp}')",
|
|
"", "")
|
|
database(
|
|
f"INSERT INTO `{get.dbname}`.cpu_to_client (hardwareID,cpulistID) VALUES ('{get.hardwareID}','{result[0][0]}',)",
|
|
"", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/host/update")
|
|
async def updatehost(get: CL.CL_UPDATE_HOST,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"UPDATE `{get.dbname}`.hardware SET ipadresse = '{get.ipadresse}', ram = '{get.ram}', hddused = '{get.hddused}' ,hddfree = '{get.hddfree}',hddtotal = '{get.hddtotal}' WHERE hostname = '{get.hostname}'","","")
|
|
result = database(f"SELECT id FROM `{get.dbname}`.hardware WHERE hostname = '{get.hostname}'","", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/user/set")
|
|
async def set_user(get: CL.CL_SET_HOST_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `{get.dbname}`.user (user) VALUES ('{get.user}')","","")
|
|
result = database(f"SELECT id FROM `{get.dbname}`.user WHERE user = '{get.user}'","","")
|
|
database(f"INSERT INTO `{get.dbname}`.hardware_to_user (userID,hardwareID) VALUES ('{result[0][0]}',{get.hardwareID})", "", "")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result
|
|
|
|
@app.post("/user/get")
|
|
async def get_user(get: CL.CL_GET_HOST_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT id FROM `{get.dbname}`.`Active-Directory-User` WHERE SamAccountName = '{get.user}' ORDER BY ID DESC LIMIT 1","","")
|
|
return result[0][0]
|
|
|
|
@app.post("/user/set/lastlogon")
|
|
async def set_lastlogon_user(get: CL.CL_SET_LASTLOGON_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `{get.dbname}`.LastLogonToADUser (aduserid,lastlogon) VALUES ('{get.userID}','{get.lastlogon}')", "", "")
|
|
|
|
|
|
@app.post("/info/getclient")
|
|
async def getclient(get: CL.CL_GET_CLIENT,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT Firma FROM `Kunden-Clients` where `Client-Name` = '{get.name}'","Stines-GmbH","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/info/addclient")
|
|
async def addclient(addclient: CL.CL_ADD_CLIENT,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(addclient.query)
|
|
database(addclient.query,addclient.database,"")
|
|
return f"Client wurde zur Datenbank hinzugefügt"
|
|
|
|
|
|
@app.post("/info/getdatabase")
|
|
async def getdatabase(get: CL.CL_GET_DATABASE,api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database(f"SELECT name FROM `Kunden-Server` where `IP-Adresse` = '{get.ip}'","Stines-GmbH","")[0][0]
|
|
|
|
@app.post("/dbset")
|
|
async def dbset(get: CL.CL_Database_set,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(get.query,get.dbname,"")
|
|
return f"Created {get.dbname}"
|
|
|
|
@app.post("/cpu_info")
|
|
async def cpu_info(get: CL.CL_CPUinfo,api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database(f"SELECT `Prozessor-Anzahl` FROM `CPU-Liste` WHERE `CPU-Name`='{get.name}'","Stines-GmbH","")
|
|
|
|
@app.post("/systeminfo_set")
|
|
async def systeminfo_set(get: CL.CL_Systeminfo_set,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"UPDATE `Kunden-Server` SET RAM={get.ram}, Prozessor={get.prozessor}, CPU='{get.cpu}',single_free_hdd='{get.single_free_hdd}',single_used_hdd='{get.single_used_hdd}',single_total_hdd='{get.single_total_hdd}',total_free_hdd='{get.total_free_hdd}',total_used_hdd='{get.total_used_hdd}',total_total_hdd='{get.total_total_hdd}' WHERE `IP-Adresse`='{get.ipaddress}'","Stines-GmbH","")
|
|
return f"Server was UPDATE RAM={get.ram}, Prozessor={get.prozessor}, CPU={get.cpu}, IP-Adresse={get.ipaddress}"
|
|
|
|
@app.post("/systeminfo_get")
|
|
async def systeminfo_set(get: CL.CL_Systeminfo_get,api_key: APIKey = Depends(auth.get_api_key)):
|
|
return database("SELECT * FROM `Stines-GmbH`.`Kunden-Server` WHERE `IP-Adresse` = ""'{}'""".format(get.ipaddress),"","")
|
|
|
|
@app.post("/ad/getuserid")
|
|
async def getuser(get: CL.CL_GET_AD_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(get.query,get.dbname,"")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/ad/getuser")
|
|
async def getuser(get: CL.CL_GET_AD_GROUP_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(get.query,get.dbname,"")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/ad/adduser")
|
|
async def add_ad_user(get: CL.CL_ADD_AD_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,get.entry))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/ad/reportadduser")
|
|
async def add_report_user(get: CL.CL_ADD_REPORT_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,""))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/ad/user/disabled")
|
|
async def disabled_user(get: CL.CL_CHECK_DISABLED_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT `{get.dbname}`.`Gruppen`.`Name` ,`{get.dbname}`.`GruppenToADUser`.`Deaktiviert` from `{get.dbname}`.`Active-Directory-User` INNER join `{get.dbname}`.`GruppenToADUser` on `{get.dbname}`.`Active-Directory-User`.`ID` = `{get.dbname}`.`GruppenToADUser`.`ADUserID` INNER join`{get.dbname}`.`Gruppen` on `{get.dbname}`.`Gruppen`.`ID` = `{get.dbname}`.`GruppenToADUser`.`GruppenID` WHERE `{get.dbname}`.`Active-Directory-User`.`SamAccountName` = '{get.user}'",f"{get.dbname}","")
|
|
return result
|
|
|
|
@app.post("/report/getgroup")
|
|
async def get_report_group(get: CL.CL_GET_REPORT_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT ID FROM `{get.dbname}`.`Gruppen` WHERE Name = '{get.group_name}'","","")
|
|
print(result)
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/report/getuserid")
|
|
async def get_user_id(get: CL.CL_GET_USER_ID,api_key: APIKey = Depends(auth.get_api_key)):
|
|
result = database(f"SELECT ID FROM `{get.dbname}`.`Active-Directory-User` WHERE SamAccountName = '{get.user}'","","")
|
|
if not result:
|
|
return False
|
|
else:
|
|
return result[0][0]
|
|
|
|
@app.post("/report/addusertogroup")
|
|
async def get_addusertogroup(get: CL.CL_ADD_USER_TO_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
userid = database(f"SELECT ID FROM `{get.dbname}`.`Active-Directory-User` WHERE SamAccountName = '{get.member}' ORDER BY ID DESC LIMIT 1","","")[0][0]
|
|
groupid = database(f"SELECT ID FROM `{get.dbname}`.Gruppen WHERE Name = '{get.groupname}'","","")
|
|
if not groupid:
|
|
database(f"INSERT INTO `{get.dbname}`.Gruppen (Name) VALUES ('{get.groupname}')","","")
|
|
groupid = database(f"SELECT ID FROM `{get.dbname}`.Gruppen WHERE Name = '{get.groupname}'", "", "")[0][0]
|
|
check = database(f"SELECT * FROM `{get.dbname}`.GruppenToADUser WHERE ADUserID = {userid} AND GruppenID = {groupid[0][0]}","","")
|
|
if not check:
|
|
database(f"INSERT INTO `{get.dbname}`.GruppenToADUser (ADUserID,GruppenID) VALUES ({userid},{groupid[0][0]})","","")
|
|
else:
|
|
newid = database(f"SELECT db1.ID FROM `{get.dbname}`.`Active-Directory-User` as db1 WHERE db1.SamAccountName = '{get.member}' ORDER BY ID DESC LIMIT 1","","")
|
|
print(newid)
|
|
oldid = database(f"SELECT gtu.ADUserID FROM `{get.dbname}`.GruppenToADUser as gtu INNER JOIN `{get.dbname}`.`Active-Directory-User` as db2 ON gtu.ADUserID = db2.id WHERE db2.SamAccountName = '{get.member}' LIMIT 1","","")
|
|
print(oldid)
|
|
database(f"UPDATE `{get.dbname}`.GruppenToADUser as gtu SET gtu.ADUserID = '{newid[0][0]}' WHERE gtu.ADUserID = '{oldid[0][0]}'","","")
|
|
return check
|
|
|
|
@app.post("/report/addgroup")
|
|
async def get_addreport_group(get: CL.CL_ADD_REPORT_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,""))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/ad/addgroup")
|
|
async def add_ad_user(get: CL.CL_ADD_AD_GROUP,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,get.entry))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/ex/adduser")
|
|
async def add_ad_user(get: CL.CL_ADD_EX_USER,api_key: APIKey = Depends(auth.get_api_key)):
|
|
print(database(get.query,get.dbname,get.entry))
|
|
return f"Create {get.query} - {get.dbname}"
|
|
|
|
@app.post("/add/group/volume")
|
|
async def send_group_volume(get: CL.CL_ADD_GROUP_VOLUME,api_key: APIKey = Depends(auth.get_api_key)):
|
|
volume_id = database(f"SELECT ID FROM Netvolume WHERE Volume_Path = '{get.path}' AND Volume_Name = '{get.label}'", f"{get.dbname}", "")
|
|
if not volume_id:
|
|
database(f"INSERT INTO `{get.dbname}`.Netvolume (Volume_Key,Volume_Path,Volume_Name) VALUES ('{get.letter}','{get.path}','{get.label}')", "",
|
|
"")
|
|
group_id = database(f"SELECT GruppenID FROM GruppenToNetvolumes WHERE NetvolumeID = '{volume_id[0][0]}'", f"{get.dbname}", "")
|
|
if not group_id:
|
|
group_id = database(f"SELECT ID FROM `{get.dbname}`.Gruppen WHERE Name = '{get.group}'", "","")
|
|
if not group_id:
|
|
group_id = database(f"SELECT ID FROM `{get.dbname}`.`Active-Directory-User` WHERE SamAccountName = '{get.group}' ORDER BY ID DESC LIMIT 1", "", "")
|
|
database(f"INSERT INTO `{get.dbname}`.GruppenToNetvolumes (UserID,NetvolumeID) VALUES ('{group_id[0][0]}','{volume_id[0][0]}')", "","")
|
|
else:
|
|
database(
|
|
f"INSERT INTO `{get.dbname}`.GruppenToNetvolumes (GruppenID,NetvolumeID) VALUES ('{group_id[0][0]}','{volume_id[0][0]}')",
|
|
"", "")
|
|
# database(get.query,get.dbname,get.entry)
|
|
return f"Create {get.label} - {get.path}"
|
|
|
|
|
|
@app.post("/volume_mount")
|
|
async def volume_mount(get: CL.CL_GET_VOLUME,api_key: APIKey = Depends(auth.get_api_key)):
|
|
database(f"INSERT INTO `Volume-Mount` (date,user) VALUES ('{get.date}','{get.user}')","Stines-GmbH","")
|
|
return f"User {get.user} ADD"
|
|
|
|
## Startet den API-Server
|
|
if __name__ == '__main__':
|
|
uvicorn.run("main:app", host='0.0.0.0', port=8001, reload=True, log_level="debug") |