Skip to content

Commit 8775ce3

Browse files
authored
Merge pull request #6 from crane-cloud/fx-authentication
fx: fix authentication and prome error
2 parents 399473e + e37e781 commit 8775ce3

6 files changed

Lines changed: 60 additions & 15 deletions

File tree

app/controllers/app.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
from app.helpers.utils import get_app_data
99

10+
1011
class AppMemoryUsageView(Resource):
1112
@jwt_required
1213
def post(self, project_id, app_id):
@@ -31,7 +32,14 @@ def post(self, project_id, app_id):
3132
except Exception as error:
3233
return dict(status='fail', message=str(error)), 500
3334

34-
new_data = json.loads(prom_memory_data)
35+
if not prom_memory_data:
36+
return dict(status='fail', message="Failed to connect to prometheus"), 500
37+
38+
try:
39+
new_data = json.loads(prom_memory_data)
40+
except Exception as error:
41+
return dict(status='fail', message=str(prom_memory_data)), 500
42+
3543
final_data_list = []
3644

3745
try:
@@ -70,7 +78,14 @@ def post(self, project_id, app_id):
7078
except Exception as error:
7179
return dict(status='fail', message=str(error)), 500
7280

73-
new_data = json.loads(prom_cpu_data)
81+
if not prom_cpu_data:
82+
return dict(status='fail', message="Failed to connect to prometheus"), 500
83+
84+
try:
85+
new_data = json.loads(prom_cpu_data)
86+
except Exception as error:
87+
return dict(status='fail', message=str(prom_cpu_data)), 500
88+
7489
final_data_list = []
7590

7691
try:
@@ -109,7 +124,14 @@ def post(self, project_id, app_id):
109124
except Exception as error:
110125
return dict(status='fail', message=str(error)), 500
111126

112-
new_data = json.loads(prom_net_data)
127+
if not prom_net_data:
128+
return dict(status='fail', message="Failed to connect to prometheus"), 500
129+
130+
try:
131+
new_data = json.loads(prom_net_data)
132+
except Exception as error:
133+
return dict(status='fail', message=str(prom_net_data)), 500
134+
113135
final_data_list = []
114136

115137
try:

app/controllers/project.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ def post(self, project_id):
3131
except Exception as error:
3232
return dict(status='fail', message=str(error)), 500
3333

34-
new_data = json.loads(prom_memory_data)
34+
if not prom_memory_data:
35+
return dict(status='fail', message="Failed to connect to prometheus"), 500
36+
37+
try:
38+
new_data = json.loads(prom_memory_data)
39+
except Exception as error:
40+
return dict(status='fail', message=str(prom_memory_data)), 500
41+
3542
final_data_list = []
3643

3744
try:
@@ -70,9 +77,15 @@ def post(self, project_id):
7077
except Exception as error:
7178
return dict(status='fail', message=str(error)), 500
7279

73-
new_data = json.loads(prom_cpu_data)
74-
final_data_list = []
80+
if not prom_cpu_data:
81+
return dict(status='fail', message="Failed to connect to prometheus"), 500
7582

83+
try:
84+
new_data = json.loads(prom_cpu_data)
85+
except Exception as error:
86+
return dict(status='fail', message=str(prom_cpu_data)), 500
87+
88+
final_data_list = []
7689
try:
7790
for value in new_data["data"]["result"][0]["values"]:
7891
mem_case = {'timestamp': float(
@@ -109,7 +122,14 @@ def post(self, project_id):
109122
except Exception as error:
110123
return dict(status='fail', message=str(error)), 500
111124

112-
new_data = json.loads(prom_ntw_data)
125+
if not prom_ntw_data:
126+
return dict(status='fail', message="Failed to connect to prometheus"), 500
127+
128+
try:
129+
new_data = json.loads(prom_ntw_data)
130+
except Exception as error:
131+
return dict(status='fail', message=str(prom_ntw_data)), 500
132+
113133
final_data_list = []
114134

115135
try:

app/helpers/authenticate.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from flask_jwt_extended import (
2-
jwt_required as jwt,
3-
verify_jwt_in_request
4-
)
1+
# from flask_jwt_extended import (
2+
# jwt_required as jwt,
3+
# verify_jwt_in_request
4+
# )
55
from functools import wraps
6-
import jwt
6+
from jose import jwt
77
from flask import request, jsonify
88
import os
99

@@ -18,7 +18,6 @@ def wrapper(*args, **kwargs):
1818

1919
if access_token is None:
2020
return dict(message='Access token was not supplied'), 401
21-
2221
try:
2322
token = access_token.split(' ')[1]
2423
if (access_token.split(' ')[0] != "Bearer"):
@@ -27,7 +26,8 @@ def wrapper(*args, **kwargs):
2726
payload = jwt.decode(token, os.getenv(
2827
'JWT_SALT'), algorithms=['HS256'])
2928

30-
except jwt.exceptions.DecodeError:
29+
except Exception as e:
30+
print(e)
3131
return dict(message="Access token is not valid or key"), 401
3232

3333
return fn(*args, **kwargs)

app/helpers/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def get_project_data(project_id, request):
3737
return SimpleNamespace(status='failed', message="Failed to fetch project for current user", status_code=400)
3838

3939
project_response = project_response.json()
40+
# print(project_response)
4041

4142
project_data = project_response['data']['project']
4243

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ services:
3030
FLASK_APP: server.py
3131
PRODUCT_BASE_URL: ${PRODUCT_BASE_URL:-http://127.0.0.1:5000}
3232
ports:
33-
- "${FLASK_PORT:-4000}:4000"
33+
- "${FLASK_PORT:-4000}:5000"
3434
volumes:
3535
- .:/app
3636
depends_on:

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ six==1.16.0
3030
urllib3==2.2.1
3131
Werkzeug==3.0.2
3232
marshmallow==3.21.1
33+
python-jose==3.3.0
34+

0 commit comments

Comments
 (0)