diff --git a/proxstar/__init__.py b/proxstar/__init__.py index 8979285..f770025 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -331,17 +331,11 @@ def vm_power(vmid, action): @auth.oidc_auth def vm_console(vmid): user = User(session['userinfo']['preferred_username']) - connect_proxmox() + proxmox = connect_proxmox() if user.rtp or int(vmid) in user.allowed_vms: # import pdb; pdb.set_trace() vm = VM(vmid) - vnc_ticket, vnc_port = open_vnc_session( - vmid, - vm.node, - app.config['PROXMOX_USER'], - app.config['PROXMOX_TOKEN_NAME'], - app.config['PROXMOX_TOKEN_VALUE'], - ) + vnc_ticket, vnc_port = open_vnc_session(vmid, vm.node, proxmox) node = f'{vm.node}.csh.rit.edu' token = add_vnc_target(node, vnc_port) redis_conn.set(f'vnc_token|{vmid}', str(token)) # Store the VNC token in Redis. diff --git a/proxstar/vnc.py b/proxstar/vnc.py index a95c2ea..06df58e 100644 --- a/proxstar/vnc.py +++ b/proxstar/vnc.py @@ -3,7 +3,6 @@ import subprocess import time import urllib.parse -import requests from flask import current_app as app from proxstar import logging @@ -74,25 +73,15 @@ def delete_vnc_target(node=None, port=None, token=None): raise LookupError('Target does not exist') -def open_vnc_session(vmid, node, proxmox_user, proxmox_token_name, proxmox_token_value): +def open_vnc_session(vmid, node, proxmox): """Pings the Proxmox API to request a VNC Proxy connection. Authenticates against the API using a Uname/Token, gets a few tokens back, then uses those tokens to open the VNC Proxy. Use these to connect to the VM's host with websockify proxy. Returns: Ticket to use as the noVNC password, and a port. """ - # Get Proxmox API ticket and CSRF_Prevention_Token - # TODO (willnilges): Use Proxmoxer to get this information # TODO (willnilges): Report errors - proxy_params = {'node': node, 'vmid': str(vmid), 'websocket': '1', 'generate-password': '0'} - vncproxy_response_data = requests.post( - f'https://{node}.csh.rit.edu:8006/api2/json/nodes/{node}/qemu/{vmid}/vncproxy', - verify=False, - timeout=5, - params=proxy_params, - headers={ - 'Authorization': f'PVEAPIToken={proxmox_user}!{proxmox_token_name}={proxmox_token_value}' - }, - ).json()['data'] + params = {'websocket': '1', 'generate-password': '0'} + vncproxy_response_data = proxmox.nodes(node).qemu(str(vmid)).vncproxy.post(**params) return urllib.parse.quote_plus(vncproxy_response_data['ticket']), vncproxy_response_data['port']