/
users.py
356 lines (342 loc) · 10.8 KB
/
users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2018, 2019, 2020, 2021, 2022 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Reana-Server User Endpoints."""
import logging
import traceback
from bravado.exception import HTTPError
from flask import Blueprint, jsonify
from reana_db.models import AuditLogAction
from reana_commons.config import (
REANA_COMPONENT_PREFIX,
REANA_INFRASTRUCTURE_KUBERNETES_NAMESPACE,
)
from reana_commons.email import send_email, REANA_EMAIL_RECEIVER
from reana_commons.errors import REANAEmailNotificationError
from reana_server import __version__
from reana_server.config import REANA_HOSTNAME
from reana_server.decorators import signin_required
from reana_server.utils import JinjaEnv
blueprint = Blueprint("users", __name__)
@blueprint.route("/you", methods=["GET"])
@signin_required(token_required=False)
def get_you(user):
r"""Endpoint to get user information.
---
get:
summary: Gets information about authenticated user.
description: >-
This resource provides basic information about an authenticated
user based on the session cookie presence.
operationId: get_you
produces:
- application/json
parameters:
- name: access_token
in: query
description: API access_token of user.
required: false
type: string
responses:
200:
description: >-
User information correspoding to the session cookie sent
in the request.
schema:
type: object
properties:
email:
type: string
reana_server_version:
type: string
reana_token:
type: object
properties:
value:
type: string
status:
type: string
requested_at:
type: string
quota:
type: object
properties:
disk:
type: object
properties:
usage:
type: object
properties:
raw:
type: number
human_readable:
type: string
limit:
type: object
properties:
raw:
type: number
human_readable:
type: string
health:
type: string
cpu:
type: object
properties:
usage:
type: object
properties:
raw:
type: number
human_readable:
type: string
limit:
type: object
properties:
raw:
type: number
human_readable:
type: string
health:
type: string
examples:
application/json:
{
"email": "user@reana.info",
"reana_server_version": "0.8.1",
"reana_token": {
"value": "Drmhze6EPcv0fN_81Bj-nA",
"status": "active",
"requested_at": "Mon, 25 May 2020 10:39:57 GMT",
},
"full_name": "John Doe",
"username": "jdoe",
"quota": {
"cpu": {
"limit": {
"raw": 200000,
"human_readable": "3m 20s"
},
"usage": {
"raw": 70536,
"human_readable": "1m 10s"
},
"health": "healthy"
},
"disk": {
"limit": {
"raw": 52430000,
"human_readable": "50 MB"
},
"usage": {
"raw": 784384,
"human_readable": "766 KB"
},
"health": "healthy"
}
}
}
401:
description: >-
Error message indicating that the uses is not authenticated.
schema:
type: object
properties:
message:
type: string
examples:
application/json:
{
"message": "User not logged in"
}
403:
description: >-
Request failed. User token not valid.
schema:
type: object
properties:
message:
type: string
examples:
application/json:
{
"message": "Token is not valid."
}
500:
description: >-
Request failed. Internal server error.
schema:
type: object
properties:
message:
type: string
examples:
application/json:
{
"message": "Internal server error."
}
"""
try:
if user:
return (
jsonify(
{
"email": user.email,
"reana_server_version": __version__,
"reana_token": {
"value": user.access_token,
"status": user.access_token_status,
"requested_at": (
user.latest_access_token.created
if user.latest_access_token
else None
),
},
"full_name": user.full_name,
"username": user.username,
"quota": user.get_quota_usage(),
}
),
200,
)
return jsonify(message="User not logged in"), 401
except HTTPError as e:
logging.error(traceback.format_exc())
return jsonify(e.response.json()), e.response.status_code
except ValueError as e:
logging.error(traceback.format_exc())
return jsonify({"message": str(e)}), 403
except Exception as e:
logging.error(traceback.format_exc())
return jsonify({"message": str(e)}), 500
@blueprint.route("/token", methods=["PUT"])
@signin_required(token_required=False)
def request_token(user):
r"""Endpoint to request user access token.
---
put:
summary: Requests a new access token for the authenticated user.
description: >-
This resource allows the user to create an empty REANA access token
and mark it as requested.
operationId: request_token
produces:
- application/json
parameters:
- name: access_token
in: query
description: API access_token of user.
required: false
type: string
responses:
200:
description: >-
User information correspoding to the session cookie sent
in the request.
schema:
type: object
properties:
reana_token:
type: object
properties:
status:
type: string
requested_at:
type: string
examples:
application/json:
{
"reana_token": {
"status": "requested",
"requested_at": "Mon, 25 May 2020 10:45:15 GMT"
}
}
401:
description: >-
Error message indicating that the uses is not authenticated.
schema:
type: object
properties:
message:
type: string
examples:
application/json:
{
"message": "User not logged in"
}
403:
description: >-
Request failed. User token not valid.
schema:
type: object
properties:
message:
type: string
examples:
application/json:
{
"message": "Token is not valid."
}
500:
description: >-
Request failed. Internal server error.
schema:
type: object
properties:
message:
type: string
examples:
application/json:
{
"message": "Internal server error."
}
"""
try:
user.request_access_token()
user.log_action(AuditLogAction.request_token)
email_subject = f"[{REANA_HOSTNAME}] Token request ({user.email})"
fields = [
"id_",
"email",
"full_name",
"username",
"access_token",
"access_token_status",
]
user_data = "\n".join([f"{f}: {getattr(user, f, None)}" for f in fields])
email_body = JinjaEnv.render_template(
"emails/token_request.txt",
user_data=user_data,
user_email=user.email,
reana_hostname=REANA_HOSTNAME,
namespace=REANA_INFRASTRUCTURE_KUBERNETES_NAMESPACE,
component_prefix=REANA_COMPONENT_PREFIX,
)
try:
send_email(REANA_EMAIL_RECEIVER, email_subject, email_body)
except REANAEmailNotificationError:
logging.error(traceback.format_exc())
return (
jsonify(
{
"reana_token": {
"status": user.access_token_status,
"requested_at": user.latest_access_token.created,
}
}
),
200,
)
except HTTPError as e:
logging.error(traceback.format_exc())
return jsonify(e.response.json()), e.response.status_code
except ValueError as e:
logging.error(traceback.format_exc())
return jsonify({"message": str(e)}), 403
except Exception as e:
logging.error(traceback.format_exc())
return jsonify({"message": str(e)}), 500