diff --git a/code/auth-service/app.py b/code/auth-service/app.py index d30ff3539dd91df3c0829381daacf2d24ebc2f64..0a8130200e9a89fe4d65b37a0c7ee82cc9a58331 100644 --- a/code/auth-service/app.py +++ b/code/auth-service/app.py @@ -155,22 +155,26 @@ def cleanupBlacklist(): return Response("Deletion unsuccessful", status=409, mimetype='application/json') -# @app.route("/list") -# def listUser(): -# token = "" -# authorizationHeader = request.headers.get('authorization') -# if authorizationHeader is not None: -# token = authorizationHeader.replace("Bearer ", "") -# if authModel.verify(token): -# if not authModel.isAdmin(token): -# return Response('List unsuccessful', status=403, mimetype='application/json') -# else: -# return Response("List unsuccessful", status=401, mimetype='application/json') - -# if authModel.list_users(): -# return Response("") -# else: -# return Response("") +@app.route("/list", methods=["POST"]) +def listUser(): + token = "" + + authorizationHeader = request.headers.get('authorization') + if authorizationHeader is not None: + token = authorizationHeader.replace("Bearer ", "") + if authModel.verify(token): + if not authModel.isAdmin(token): + return Response("Listing unsuccessful. Not authorized.", status=403, mimetype='application/json') + else: + return Response("Listing unsuccessful. Not authorized.", status=401, mimetype='application/json') + + roles = request.form.get("roles") + createResponse = authModel.list_users(roles) + + if createResponse != "": + return Response("list: " + str(createResponse), status=200, mimetype='application/json') + else: + return Response("Listing unsuccessful. Please adjust your credentials", status=409, mimetype='application/json') @app.route('/health') diff --git a/code/auth-service/authModel.py b/code/auth-service/authModel.py index 89664aa617c5720171087da762c3034830978142..6ac127737eecc3e7326c8b8d6732844063f6924c 100644 --- a/code/auth-service/authModel.py +++ b/code/auth-service/authModel.py @@ -142,7 +142,7 @@ def create(username, password, is_admin, roles_str): for role in roles: cur.execute("INSERT INTO roles (user_id, role) VALUES(%s, %s);", (user_id, role)) conn.commit() - return True + return roles except (Exception, psycopg2.DatabaseError) as error: app.logger.error(error) if conn is not None: @@ -247,16 +247,42 @@ def cleanBlacklist(): conn.close() -def list_users(): - conn = None +def list_users(roles_str): + role = str(roles_str) + role = role.strip() try: conn, cur = db_connect() - cur.execute("SELECT * FROM users") - return True - - except: - pass - + user_list = "" + if role != "null": + query = """ + SELECT u.username, r.role + FROM users u + JOIN roles r ON u.id = r.user_id + WHERE r.role = %s; + """ + cur.execute(query, (role,)) + rows = cur.fetchall() + user_list = rows + user_list = [(row[0], row[1]) for row in rows] + else: + query = """ + SELECT u.username, r.role + FROM users u + JOIN roles r ON u.id = r.user_id; + """ + cur.execute(query) + rows = cur.fetchall() + user_list = str(rows) + " | Erfolg" + user_list = [row[0] for row in rows] + return user_list + except (Exception, psycopg2.DatabaseError) as error: + app.logger.error(error) + traceback.print_exc() + return [] + finally: + if conn is not None: + cur.close() + conn.close() def db_setup(): conn, cur = db_connect() diff --git a/helm/helm-msa/templates/deployment/auth-service-deployment.yaml b/helm/helm-msa/templates/deployment/auth-service-deployment.yaml index e3cbbbf8309b51842abe476f3d1fc510f5f73588..97925b941cb104758f13907ac5c7da71ad42310b 100644 --- a/helm/helm-msa/templates/deployment/auth-service-deployment.yaml +++ b/helm/helm-msa/templates/deployment/auth-service-deployment.yaml @@ -16,7 +16,7 @@ spec: spec: containers: - name: auth-service - image: ginyanote/auth-service:1.0.8 + image: dominicdaniel/auth-service:1.1 imagePullPolicy: Always ports: - containerPort: 5000 diff --git a/helm/helm-msa/templates/ingress_route/auth-service-ingress-route.yaml b/helm/helm-msa/templates/ingress_route/auth-service-ingress-route.yaml index 993b15cc056d22aa578e700f66bfe62afd6f0dce..cd72eea835b65d782830d0262a0eccc821b57b9d 100644 --- a/helm/helm-msa/templates/ingress_route/auth-service-ingress-route.yaml +++ b/helm/helm-msa/templates/ingress_route/auth-service-ingress-route.yaml @@ -32,6 +32,16 @@ spec: name: auth-service-service namespace: default port: 5000 + - kind: Rule + match: PathPrefix(`/list`) + middlewares: + - name: fw-auth-mw + namespace: default + services: + - kind: Service + name: auth-service-service + namespace: default + port: 5000 - kind: Rule match: PathPrefix(`/blacklist/cleanup`) middlewares: