Skip to content

Commit b3d43f8

Browse files
authored
Merge pull request #158 from ComputerScienceHouse/willnilges/remove-ssh
Remove SSH functionality from Proxstar
2 parents 2c17d69 + 125d7fa commit b3d43f8

File tree

9 files changed

+34
-68
lines changed

9 files changed

+34
-68
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ COPY start_worker.sh start_scheduler.sh .
77
COPY .git ./.git
88
COPY *.py .
99
COPY proxstar ./proxstar
10-
RUN touch proxmox_ssh_key targets && chmod a+w proxmox_ssh_key targets # This is some OKD shit.
10+
RUN touch targets && chmod a+w targets # This is some OKD shit.
1111
ENTRYPOINT ddtrace-run gunicorn proxstar:app --bind=0.0.0.0:8080

HACKING/.env.template

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ PROXSTAR_PROXMOX_USER=api@pve
2626
PROXSTAR_PROXMOX_PASS= # Password for proxstar user
2727
PROXSTAR_PROXMOX_ISO_STORAGE=nfs-iso
2828
PROXSTAR_PROXMOX_VM_STORAGE=ceph
29-
PROXSTAR_PROXMOX_SSH_USER=root
30-
PROXSTAR_PROXMOX_SSH_KEY="" # Ask an RTP. This is gonna look like a certificate.
31-
PROXSTAR_PROXMOX_SSH_KEY_PASS= # Password for above certificate
3229

3330
# STARRS
3431
PROXSTAR_STARRS_DB_HOST=proxstar-postgres

HACKING/build_env.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
podman build . --tag=proxstar

config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
PROXMOX_PASS = environ.get('PROXSTAR_PROXMOX_PASS', '')
3838
PROXMOX_ISO_STORAGE = environ.get('PROXSTAR_PROXMOX_ISO_STORAGE', 'nfs-iso')
3939
PROXMOX_VM_STORAGE = environ.get('PROXSTAR_PROXMOX_VM_STORAGE', 'ceph')
40-
PROXMOX_SSH_USER = environ.get('PROXSTAR_PROXMOX_SSH_USER', '')
41-
PROXMOX_SSH_KEY = environ.get('PROXSTAR_PROXMOX_SSH_KEY', '')
42-
PROXMOX_SSH_KEY_PASS = environ.get('PROXSTAR_PROXMOX_SSH_KEY_PASS', '')
43-
4440
# STARRS
4541
STARRS_DB_HOST = environ.get('PROXSTAR_STARRS_DB_HOST', '')
4642
STARRS_DB_NAME = environ.get('PROXSTAR_DB_NAME', 'starrs')

proxstar/__init__.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@
7777
environment=app.config['SENTRY_ENV'],
7878
)
7979

80-
if not os.path.exists('proxmox_ssh_key'):
81-
with open('proxmox_ssh_key', 'w') as ssh_key_file:
82-
ssh_key_file.write(app.config['PROXMOX_SSH_KEY'])
83-
84-
ssh_tunnels = []
85-
8680
auth = get_auth(app)
8781

8882
redis_conn = Redis(app.config['REDIS_HOST'], app.config['REDIS_PORT'])
@@ -258,6 +252,9 @@ def vm_power(vmid, action):
258252
connect_proxmox()
259253
if user.rtp or int(vmid) in user.allowed_vms:
260254
vm = VM(vmid)
255+
vnc_token_key = f'vnc_token|{vmid}'
256+
# For deleting the token from redis later
257+
vnc_token = redis_conn.get(vnc_token_key).decode('utf-8')
261258
if action == 'start':
262259
vmconfig = vm.config
263260
usage_check = user.check_usage(vmconfig['cores'], vmconfig['memory'], 0)
@@ -266,16 +263,18 @@ def vm_power(vmid, action):
266263
vm.start()
267264
elif action == 'stop':
268265
vm.stop()
269-
# TODO (willnilges): Replace with remove target function or something
270-
# send_stop_ssh_tunnel(vmid)
266+
delete_vnc_target(token=vnc_token)
267+
redis_conn.delete(vnc_token_key)
271268
elif action == 'shutdown':
272269
vm.shutdown()
273-
# send_stop_ssh_tunnel(vmid)
270+
delete_vnc_target(token=vnc_token)
271+
redis_conn.delete(vnc_token_key)
274272
elif action == 'reset':
275273
vm.reset()
276274
elif action == 'suspend':
277275
vm.suspend()
278-
# send_stop_ssh_tunnel(vmid)
276+
delete_vnc_target(token=vnc_token)
277+
redis_conn.delete(vnc_token_key)
279278
elif action == 'resume':
280279
vm.resume()
281280
return '', 200
@@ -296,6 +295,7 @@ def vm_console(vmid):
296295
)
297296
node = f'{vm.node}.csh.rit.edu'
298297
token = add_vnc_target(node, vnc_port)
298+
redis_conn.set(f'vnc_token|{vmid}', str(token)) # Store the VNC token in Redis.
299299
return {
300300
'host': app.config['VNC_HOST'],
301301
'port': app.config['VNC_PORT'],
@@ -630,11 +630,6 @@ def health():
630630

631631
def exit_handler():
632632
stop_websockify()
633-
for tunnel in ssh_tunnels:
634-
try:
635-
tunnel.stop()
636-
except:
637-
pass
638633

639634

640635
atexit.register(exit_handler)

proxstar/proxmox.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ def connect_proxmox():
2323
raise
2424

2525

26-
def connect_proxmox_ssh():
27-
for host in app.config['PROXMOX_HOSTS']:
28-
try:
29-
proxmox = ProxmoxAPI(
30-
host,
31-
user=app.config['PROXMOX_SSH_USER'],
32-
private_key_file='proxmox_ssh_key',
33-
password=app.config['PROXMOX_SSH_KEY_PASS'],
34-
backend='ssh_paramiko',
35-
)
36-
proxmox.version.get()
37-
return proxmox
38-
except:
39-
if app.config['PROXMOX_HOSTS'].index(host) == (len(app.config['PROXMOX_HOSTS']) - 1):
40-
logging.error('unable to connect to any of the given Proxmox servers')
41-
raise
42-
43-
4426
def get_node_least_mem(proxmox):
4527
nodes = proxmox.nodes.get()
4628
sorted_nodes = sorted(nodes, key=lambda x: ('mem' not in x, x.get('mem', None)))

proxstar/tasks.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77
from flask import Flask
88
from rq import get_current_job
9+
from redis import Redis
910
from sqlalchemy import create_engine
1011
from sqlalchemy.orm import sessionmaker
1112

@@ -22,6 +23,7 @@
2223
from proxstar.starrs import get_next_ip, register_starrs, delete_starrs
2324
from proxstar.user import User, get_vms_for_rtp
2425
from proxstar.vm import VM, clone_vm, create_vm
26+
from proxstar.vnc import delete_vnc_target
2527

2628
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
2729

@@ -150,7 +152,16 @@ def process_expiring_vms_task():
150152
vm.name, vm.id
151153
)
152154
)
153-
# send_stop_ssh_tunnel(vm.id) # TODO (willnilges): Remove target from targets file
155+
try:
156+
redis_conn = Redis(app.config['REDIS_HOST'], app.config['REDIS_PORT'])
157+
vmid = vm['vmid']
158+
vnc_token_key = f'vnc_token|{vmid}'
159+
vnc_token = redis_conn.get(vnc_token_key).decode('utf-8')
160+
delete_vnc_target(token=vnc_token)
161+
redis_conn.delete(vnc_token_key)
162+
except Exception as e: # pylint: disable=W0703
163+
print(f'ERROR: Could not delete target from targets file: {e}')
164+
154165
delete_vm_task(vm.id)
155166
if expiring_vms:
156167
send_vm_expire_email(pool, expiring_vms)

proxstar/vnc.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import time
44
import urllib.parse
55

6-
from deprecated import deprecated
76
import requests
87
from flask import current_app as app
9-
from sshtunnel import SSHTunnelForwarder
108

119
from proxstar import logging
1210
from proxstar.util import gen_password
@@ -45,7 +43,6 @@ def get_vnc_targets():
4543

4644
def add_vnc_target(node, port):
4745
# TODO (willnilges): This doesn't throw an error if the target file is wrong.
48-
# TODO (willnilges): This will duplicate targets
4946
targets = get_vnc_targets()
5047
target = next((target for target in targets if target['host'] == f'{node}:{port}'), None)
5148
if target:
@@ -59,15 +56,22 @@ def add_vnc_target(node, port):
5956
return token
6057

6158

62-
def delete_vnc_target(node, port):
59+
def delete_vnc_target(node=None, port=None, token=None):
6360
targets = get_vnc_targets()
64-
target = next((target for target in targets if target['host'] == f'{node}:{port}'), None)
61+
if node is not None and port is not None:
62+
target = next((target for target in targets if target['host'] == f'{node}:{port}'), None)
63+
elif token is not None:
64+
target = next((target for target in targets if target['token'] == f'{token}'), None)
65+
else:
66+
raise ValueError('Need either a node and port, or a token.')
6567
if target:
6668
targets.remove(target)
6769
target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w')
6870
for target in targets:
6971
target_file.write(f"{target['token']}: {target['host']}\n")
7072
target_file.close()
73+
else:
74+
raise LookupError('Target does not exist')
7175

7276

7377
def open_vnc_session(vmid, node, proxmox_user, proxmox_pass):
@@ -103,22 +107,3 @@ def open_vnc_session(vmid, node, proxmox_user, proxmox_pass):
103107
).json()['data']
104108

105109
return urllib.parse.quote_plus(vncproxy_response_data['ticket']), vncproxy_response_data['port']
106-
107-
108-
@deprecated('No longer in use')
109-
def start_ssh_tunnel(node, port):
110-
"""Forwards a port on a node
111-
to the proxstar container
112-
"""
113-
port = int(port)
114-
115-
server = SSHTunnelForwarder(
116-
node,
117-
ssh_username=app.config['PROXMOX_SSH_USER'],
118-
ssh_pkey='proxmox_ssh_key',
119-
ssh_private_key_password=app.config['PROXMOX_SSH_KEY_PASS'],
120-
remote_bind_address=('127.0.0.1', port),
121-
local_bind_address=('127.0.0.1', port),
122-
)
123-
server.start()
124-
return server

requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ black~=21.9b0
22
csh-ldap==2.4.0
33
click~=7.1.2
44
ddtrace~=1.2.1
5-
deprecated==1.2.13
65
flask==1.1.4
76
jinja2==2.11.3
87
flask-pyoidc==1.3.0
@@ -19,7 +18,6 @@ rq==1.10.1
1918
rq-dashboard==0.6.1
2019
rq-scheduler==0.10.0
2120
sqlalchemy==1.3.22
22-
sshtunnel==0.2.2
2321
tenacity==5.0.2
2422
websockify==0.9.0
2523
pylint==2.13.9

0 commit comments

Comments
 (0)