@@ -108,47 +108,47 @@ async def stream_logs(request: web.Request) -> web.StreamResponse:
108108async def operate_logs_json (request : web .Request , authenticated_sender : str ) -> web .StreamResponse :
109109 """Logs of a VM (not streaming) as json"""
110110 vm_hash = get_itemhash_or_400 (request .match_info )
111+ with set_vm_for_logging (vm_hash = vm_hash ):
112+ # This endpoint allow logs for past executions, so we look into the database if any execution by that hash
113+ # occurred, which we can then use to look for rights. We still check in the pool first, it is faster
114+ pool : VmPool = request .app ["vm_pool" ]
115+ execution = pool .executions .get (vm_hash )
116+ if execution :
117+ message = execution .message
118+ else :
119+ record = await metrics .get_last_record_for_vm (vm_hash = vm_hash )
120+ if not record :
121+ raise aiohttp .web_exceptions .HTTPNotFound (body = "No execution found for this VM" )
122+ message = get_message_executable_content (json .loads (record .message ))
123+ if not is_sender_authorized (authenticated_sender , message ):
124+ return web .Response (status = 403 , body = "Unauthorized sender" )
111125
112- # This endpoint allow logs for past executions, so we look into the database if any execution by that hash
113- # occurred, which we can then use to look for rights. We still check in the pool first, it is faster
114- pool : VmPool = request .app ["vm_pool" ]
115- execution = pool .executions .get (vm_hash )
116- if execution :
117- message = execution .message
118- else :
119- record = await metrics .get_last_record_for_vm (vm_hash = vm_hash )
120- if not record :
121- raise aiohttp .web_exceptions .HTTPNotFound (body = "No execution found for this VM" )
122- message = get_message_executable_content (json .loads (record .message ))
123- if not is_sender_authorized (authenticated_sender , message ):
124- return web .Response (status = 403 , body = "Unauthorized sender" )
125-
126- _journal_stdout_name = f"vm-{ vm_hash } -stdout"
127- _journal_stderr_name = f"vm-{ vm_hash } -stderr"
128-
129- response = web .StreamResponse ()
130- response .headers ["Transfer-encoding" ] = "chunked"
131- response .headers ["Content-Type" ] = "application/json"
132- await response .prepare (request )
133- await response .write (b"[" )
134-
135- first = True
136- for entry in get_past_vm_logs (_journal_stdout_name , _journal_stderr_name ):
137- if not first :
138- await response .write (b",\n " )
139- first = False
140- log_type = "stdout" if entry ["SYSLOG_IDENTIFIER" ] == _journal_stdout_name else "stderr"
141- msg = {
142- "SYSLOG_IDENTIFIER" : entry ["SYSLOG_IDENTIFIER" ],
143- "MESSAGE" : entry ["MESSAGE" ],
144- "file" : log_type ,
145- "__REALTIME_TIMESTAMP" : entry ["__REALTIME_TIMESTAMP" ],
146- }
147- await response .write (dumps_for_json (msg ).encode ())
148- await response .write (b"]" )
149-
150- await response .write_eof ()
151- return response
126+ _journal_stdout_name = f"vm-{ vm_hash } -stdout"
127+ _journal_stderr_name = f"vm-{ vm_hash } -stderr"
128+
129+ response = web .StreamResponse ()
130+ response .headers ["Transfer-encoding" ] = "chunked"
131+ response .headers ["Content-Type" ] = "application/json"
132+ await response .prepare (request )
133+ await response .write (b"[" )
134+
135+ first = True
136+ for entry in get_past_vm_logs (_journal_stdout_name , _journal_stderr_name ):
137+ if not first :
138+ await response .write (b",\n " )
139+ first = False
140+ log_type = "stdout" if entry ["SYSLOG_IDENTIFIER" ] == _journal_stdout_name else "stderr"
141+ msg = {
142+ "SYSLOG_IDENTIFIER" : entry ["SYSLOG_IDENTIFIER" ],
143+ "MESSAGE" : entry ["MESSAGE" ],
144+ "file" : log_type ,
145+ "__REALTIME_TIMESTAMP" : entry ["__REALTIME_TIMESTAMP" ],
146+ }
147+ await response .write (dumps_for_json (msg ).encode ())
148+ await response .write (b"]" )
149+
150+ await response .write_eof ()
151+ return response
152152
153153
154154async def authenticate_websocket_for_vm_or_403 (execution : VmExecution , vm_hash : ItemHash , ws : web .WebSocketResponse ):
0 commit comments