Skip to content

Commit 0b21c65

Browse files
authored
Merge pull request #178 from jabbate19/vnc_proxmoxer
Use Proxmoxer for VNC Port and Ticket
2 parents 05fe1be + 29aaf85 commit 0b21c65

File tree

3 files changed

+6
-29
lines changed

3 files changed

+6
-29
lines changed

gunicorn.conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import subprocess
33

44
from flask import Flask
5+
56
app = Flask(__name__)
67
if os.path.exists(os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config_local.py")):
78
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config_local.py")

proxstar/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,11 @@ def vm_power(vmid, action):
331331
@auth.oidc_auth
332332
def vm_console(vmid):
333333
user = User(session['userinfo']['preferred_username'])
334-
connect_proxmox()
334+
proxmox = connect_proxmox()
335335
if user.rtp or int(vmid) in user.allowed_vms:
336336
# import pdb; pdb.set_trace()
337337
vm = VM(vmid)
338-
vnc_ticket, vnc_port = open_vnc_session(
339-
vmid, vm.node, app.config['PROXMOX_USER'], app.config['PROXMOX_PASS']
340-
)
338+
vnc_ticket, vnc_port = open_vnc_session(vmid, vm.node, proxmox)
341339
node = f'{vm.node}.csh.rit.edu'
342340
token = add_vnc_target(node, vnc_port)
343341
redis_conn.set(f'vnc_token|{vmid}', str(token)) # Store the VNC token in Redis.

proxstar/vnc.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import time
44
import urllib.parse
55

6-
import requests
76
from flask import current_app as app
87

98
from proxstar import logging
@@ -74,36 +73,15 @@ def delete_vnc_target(node=None, port=None, token=None):
7473
raise LookupError('Target does not exist')
7574

7675

77-
def open_vnc_session(vmid, node, proxmox_user, proxmox_pass):
76+
def open_vnc_session(vmid, node, proxmox):
7877
"""Pings the Proxmox API to request a VNC Proxy connection. Authenticates
7978
against the API using a Uname/Pass, gets a few tokens back, then uses those
8079
tokens to open the VNC Proxy. Use these to connect to the VM's host with
8180
websockify proxy.
8281
Returns: Ticket to use as the noVNC password, and a port.
8382
"""
84-
# Get Proxmox API ticket and CSRF_Prevention_Token
85-
# TODO (willnilges): Use Proxmoxer to get this information
8683
# TODO (willnilges): Report errors
87-
data = {'username': proxmox_user, 'password': proxmox_pass}
88-
response_data = requests.post(
89-
f'https://{node}.csh.rit.edu:8006/api2/json/access/ticket',
90-
verify=False,
91-
data=data,
92-
).json()['data']
93-
if response_data is None:
94-
raise requests.AuthenticationError(
95-
'Could not authenticate against `ticket` endpoint! Check uname/password'
96-
)
97-
csrf_prevention_token = response_data['CSRFPreventionToken']
98-
ticket = response_data['ticket']
99-
proxy_params = {'node': node, 'vmid': str(vmid), 'websocket': '1', 'generate-password': '0'}
100-
vncproxy_response_data = requests.post(
101-
f'https://{node}.csh.rit.edu:8006/api2/json/nodes/{node}/qemu/{vmid}/vncproxy',
102-
verify=False,
103-
timeout=5,
104-
params=proxy_params,
105-
headers={'CSRFPreventionToken': csrf_prevention_token},
106-
cookies={'PVEAuthCookie': ticket},
107-
).json()['data']
84+
params = {'websocket': '1', 'generate-password': '0'}
85+
vncproxy_response_data = proxmox.nodes(node).qemu(str(vmid)).vncproxy.post(**params)
10886

10987
return urllib.parse.quote_plus(vncproxy_response_data['ticket']), vncproxy_response_data['port']

0 commit comments

Comments
 (0)