From 9c51bbe5016495c714789f5df01806a6d366e11a Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 6 Oct 2018 23:56:03 +0200 Subject: Rename kpov_random_helpers to kpov_util --- kpov_judge/tasks/openvpn_simple_smb/task.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'kpov_judge/tasks/openvpn_simple_smb/task.py') diff --git a/kpov_judge/tasks/openvpn_simple_smb/task.py b/kpov_judge/tasks/openvpn_simple_smb/task.py index 2db4337..435b40f 100644 --- a/kpov_judge/tasks/openvpn_simple_smb/task.py +++ b/kpov_judge/tasks/openvpn_simple_smb/task.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# kpov_random_helpers should be imported by add_assignment.py +# kpov_util should be imported by add_assignment.py instructions = { 'si':""" @@ -181,9 +181,9 @@ def gen_params(user_id, params_meta): #(HASH bo naključno generiran niz iz user_id s katerim se bo preverjalo plagiatorstvo) import random r = random.Random(user_id) - net = kpov_random_helpers.IPv4_subnet_gen(r, '10.168.0.0/16', 24) - params['IP_VPNClient1'], params['IP_SimpleArbiterVPN'] = kpov_random_helpers.IPv4_addr_gen(r, net, 2) - params['DIRNAME'] = kpov_random_helpers.fname_gen(r, extension=False) + net = kpov_util.IPv4_subnet_gen(r, '10.168.0.0/16', 24) + params['IP_VPNClient1'], params['IP_SimpleArbiterVPN'] = kpov_util.IPv4_addr_gen(r, net, 2) + params['DIRNAME'] = kpov_util.fname_gen(r, extension=False) params['secret_random_seed']=str(r.random()) return params @@ -246,12 +246,12 @@ def task_check(results, params): s = "\n".join([ "".join([r.choice("0123456789abcdef") for i in range(32)]) for i in range(16)]) - keyfile = kpov_random_helpers.fname_gen(r, extension=False) + keyfile = kpov_util.fname_gen(r, extension=False) # now check the filenames fnames_ok = True for i in range(3): - fname = kpov_random_helpers.fname_gen(r, False) - foo = kpov_random_helpers.fortune(r, 4096) + fname = kpov_util.fname_gen(r, False) + foo = kpov_util.fortune(r, 4096) pos = results['SimpleArbiter_ls'].find(fname + '.txt') fnames_ok = fnames_ok and pos >= 0 #if pos < 0: @@ -308,7 +308,7 @@ def prepare_disks(templates, task_params, global_params): {} -----END OpenVPN Static key V1----- """.format(s) - keyfile = kpov_random_helpers.fname_gen(r, extension=False) + ".key" + keyfile = kpov_util.fname_gen(r, extension=False) + ".key" templates['simpleArbiterDhcpGWVPN'].write("/etc/openvpn/secret.key", s) netaddr_s = """auto tap0 iface tap0 inet static @@ -319,10 +319,10 @@ iface tap0 inet static """.format(task_params['IP_SimpleArbiterVPN']) templates['simpleArbiterDhcpGWVPN'].write_append("/etc/network/interfaces", netaddr_s) for i in range(3): - fname = kpov_random_helpers.fname_gen(r, False) + fname = kpov_util.fname_gen(r, False) templates['simpleArbiterDhcpGWVPN'].write( "/srv/smb/" + fname + '.txt', - kpov_random_helpers.fortune(r, 4096)) + kpov_util.fortune(r, 4096)) write_default_config(templates['simpleArbiterDhcpGWVPN'], global_params) templates['student-VPNClient1'].write("/home/student/" + keyfile, s) # uid, gid (student = ) -- cgit v1.2.1 From b5b35faea7f4205f353f57178ddc795b7dce5043 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:02:26 +0200 Subject: Refactor SSH testing code out of individual tasks Catch SSH errors and report failures as hints. Also some cleanups and 2to3 fixes. --- kpov_judge/tasks/openvpn_simple_smb/task.py | 110 +++++++--------------------- 1 file changed, 28 insertions(+), 82 deletions(-) (limited to 'kpov_judge/tasks/openvpn_simple_smb/task.py') diff --git a/kpov_judge/tasks/openvpn_simple_smb/task.py b/kpov_judge/tasks/openvpn_simple_smb/task.py index 435b40f..fda7a4a 100644 --- a/kpov_judge/tasks/openvpn_simple_smb/task.py +++ b/kpov_judge/tasks/openvpn_simple_smb/task.py @@ -102,60 +102,43 @@ params_meta = { def task(IP_SimpleArbiterVPN, IP_VPNClient1, IP_LANClient1, DIRNAME): - + import collections from pexpect import pxssh # Used to set up an SSH connection to a remote machine import pexpect # Allows the script to spawn a child application and control it as if a human were typing commands - - + # The necessary things we need to check if the task was performed correctly - results = dict() + results = collections.defaultdict(str) - # The login params (these must be used on the machines!) - peer_user = 'student' - peer_passwd = 'vaje' - - ### - # Sets up the SSH connections to the machines - ### # VPNClient1 - sC1 = pxssh.pxssh() - - # Logs in to the machines with the default login params - sC1.login( - IP_LANClient1, - peer_user, - peer_passwd - ) + sC1 = pxssh.pxssh(encoding='utf-8') + sC1.login(IP_LANClient1, 'student', 'vaje') - ###### # sA - ###### - results['SimpleArbiter_ifconfig'] = pexpect.run( - '/sbin/ifconfig -a') - + 'ifconfig -a', encoding='utf-8', env={'PATH': '/bin:/sbin'}) results['SimpleArbiter_route'] = pexpect.run( - '/sbin/route -n') + 'route -n', encoding='utf-8', env={'PATH': '/bin:/sbin'}) # Pings each of the clients # 10.8.0.6 and 10.8.0.10 are the first two default addresses distributed by OpenVPN # Will output everything ping outputs (set to ping 3 times) results['SimpleArbiter_ping_C1'] = pexpect.run( - 'ping -c 3 {}'.format(IP_VPNClient1)) + 'ping -c 3 {}'.format(IP_VPNClient1), encoding='utf-8') results['SimpleArbiter_traceroute'] = pexpect.run( - '/usr/bin/traceroute {}'.format(IP_VPNClient1)) + 'traceroute {}'.format(IP_VPNClient1), encoding='utf-8') sC1.sendline('cat /etc/exports') sC1.prompt() output = sC1.before results['VPNClient1_nfs_access_control_list'] = output results['SimpleArbiter_mount'] = pexpect.run( - 'sudo mount {}:/home/test/{} /mnt'.format(IP_VPNClient1, DIRNAME)) + 'sudo mount {}:/home/test/{} /mnt'.format(IP_VPNClient1, DIRNAME), encoding='utf-8') results['SimpleArbiter_mount_result'] = pexpect.run( - 'sudo mount') + 'sudo mount', encoding='utf-8') results['SimpleArbiter_ls'] = pexpect.run( - 'ls /mnt') + 'ls /mnt', encoding='utf-8') pexpect.run( - 'sudo umount /mnt') + 'sudo umount /mnt', encoding='utf-8') + # Ping the VPN server sC1.sendline('ping -c 3 {0}'.format( IP_SimpleArbiterVPN )) sC1.prompt() @@ -169,10 +152,8 @@ def task(IP_SimpleArbiterVPN, IP_VPNClient1, IP_LANClient1, DIRNAME): sC1.prompt() results['VPNClient1_ps'] = sC1.before sC1.logout() - - - return results + return results def gen_params(user_id, params_meta): params = dict() @@ -189,64 +170,46 @@ def gen_params(user_id, params_meta): def task_check(results, params): - import re score = 0 hints = [] - # zal si se nisem prišla na jasno s pingi + IP_SA = params['IP_SimpleArbiterVPN'].replace('.', '\.') IP_C1 = params['IP_VPNClient1'].replace('.', '\.') - rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format( - IP_SA) - # print rs, re.match(rs, results['SimpleArbiter_ifconfig']) + rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format(IP_SA) if re.search(rs, results['SimpleArbiter_ifconfig']): score += 1 - # print "ifconfig OK" else: hints.append("ifconfig on SimpleArbiter not OK") - pass - # print ('SA_ifconfig', results['SimpleArbiter_ifconfig']) - # results['SimpleArbiter_route'] = pexpect.run( if re.search( - "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format( - IP_C1), + "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format(IP_C1), results['SimpleArbiter_ping_C1']): - # print "Server ping OK" score += 1 else: hints.append("ping from server not OK") - pass - # print ("Server ping", results['SimpleArbiter_ping_C1']) - # ignore this - # print results['SimpleArbiter_mount'] - # print results['SimpleArbiter_traceroute'] rs = "1 +{0} \({0}\)".format(IP_C1) - if re.search(rs, - results['SimpleArbiter_traceroute']): + if re.search(rs, results['SimpleArbiter_traceroute']): score += 1 else: hints.append("traceroute not OK") - pass - # print ("fail!", rs, results['SimpleArbiter_traceroute']) if results['VPNClient1_nfs_access_control_list'].find( '/home/test/' + params['DIRNAME'] + ' ') >= 0: score += 1 if results['SimpleArbiter_mount_result'].find( - '{}:/home/test/{} on /mnt type nfs'.format( - params['IP_VPNClient1'], - params['DIRNAME'])): - # print "mount OK" + '{}:/home/test/{} on /mnt type nfs'.format( + params['IP_VPNClient1'], params['DIRNAME'])): score += 1 else: hints.append("mount not OK") + # get r into the correct state r = random.Random(params['secret_random_seed']) - s = "\n".join([ - "".join([r.choice("0123456789abcdef") for i in range(32)]) + s = "\n".join(["".join([r.choice("0123456789abcdef") for i in range(32)]) for i in range(16)]) keyfile = kpov_util.fname_gen(r, extension=False) + # now check the filenames fnames_ok = True for i in range(3): @@ -254,34 +217,24 @@ def task_check(results, params): foo = kpov_util.fortune(r, 4096) pos = results['SimpleArbiter_ls'].find(fname + '.txt') fnames_ok = fnames_ok and pos >= 0 - #if pos < 0: - # hints.append("missing file:" + fname) if fnames_ok: score += 2 else: hints.append("shared filenames not OK:") + # Ping the VPN server if re.search( - "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format( - IP_SA), + "PING.*\r\n64 bytes from {}: icmp_seq=[0-9]+ ttl=64 time=[0-9.]* ms".format(IP_SA), results['VPNClient1_ping_VPN_server']): - # print "ping OK" score += 1 else: hints.append("ping from client not OK") - pass - # print "Client ping", results['VPNClient1_ping_VPN_server'] - rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format( - IP_C1) - if re.search(rs, - results['VPNClient1_ifconfig']): + rs = r"tap0: flags=.* mtu 1500\r\n +inet {}".format(IP_C1) + if re.search(rs, results['VPNClient1_ifconfig']): score += 1 - # print "ifconfig OK" else: hints.append("ifconfig on VPNClient1 not OK") - pass - # print ('VPNClient1_ifconfig', results['VPNClient1_ifconfig']) if results['VPNClient1_ps'].find('openvpn') > 0: score += 1 @@ -289,11 +242,7 @@ def task_check(results, params): hints.append("openvpn not found running on VPNClient") return score, hints - def prepare_disks(templates, task_params, global_params): - - #d = templates['simpleArbiterDhcp'] - #guestmount -a d -m /dev/VG/LV -m /dev/sda1:/boot --ro /mnt #asistent je pocasnela :) import random @@ -327,6 +276,3 @@ iface tap0 inet static templates['student-VPNClient1'].write("/home/student/" + keyfile, s) # uid, gid (student = ) templates['student-VPNClient1'].chown(1000, 1000, "/home/student/" + keyfile) - - - -- cgit v1.2.1 From 3091fc2877600f7926ed6a596c316183499125f8 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:11:49 +0200 Subject: Remove coding: lines Python 3 source is utf-8 by default. --- kpov_judge/tasks/openvpn_simple_smb/task.py | 1 - 1 file changed, 1 deletion(-) (limited to 'kpov_judge/tasks/openvpn_simple_smb/task.py') diff --git a/kpov_judge/tasks/openvpn_simple_smb/task.py b/kpov_judge/tasks/openvpn_simple_smb/task.py index fda7a4a..2e66efb 100644 --- a/kpov_judge/tasks/openvpn_simple_smb/task.py +++ b/kpov_judge/tasks/openvpn_simple_smb/task.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # kpov_util should be imported by add_assignment.py -- cgit v1.2.1 From dbf585c2a59dbcb7f6d803d5756826b562cd7f43 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:38:40 +0200 Subject: Fix or remove hashbangs --- kpov_judge/tasks/openvpn_simple_smb/task.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'kpov_judge/tasks/openvpn_simple_smb/task.py') diff --git a/kpov_judge/tasks/openvpn_simple_smb/task.py b/kpov_judge/tasks/openvpn_simple_smb/task.py index 2e66efb..d4bb510 100644 --- a/kpov_judge/tasks/openvpn_simple_smb/task.py +++ b/kpov_judge/tasks/openvpn_simple_smb/task.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # kpov_util should be imported by add_assignment.py instructions = { @@ -44,12 +42,7 @@ computers = { 'disks': [ { 'name': 'simpleArbiterDhcpGWVPN', - # attempt automount }, - #{ 'name': 'CDROM', - # 'options': {'readonly': True}, - # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], - #}, ], 'network_interfaces': [ { @@ -66,10 +59,6 @@ computers = { 'disks': [ { 'name': 'student-VPNClient1', }, - #{ 'name': 'CDROM', - # 'options':{'readonly': True}, - # 'parts': [],# no parts, no mounting. - #} ], 'network_interfaces': [ { @@ -90,6 +79,7 @@ networks = { 'public': False } } + #Tukaj sem generiral tri parametre, prosil bi če se upoštevajo pri Tasku. params_meta = { 'IP_SimpleArbiterVPN': {'descriptions':{'si':'IP za SimpleArbiter na VPN'}, 'w': False, 'public': True, 'type': 'IP', 'generated': True}, @@ -99,7 +89,6 @@ params_meta = { 'secret_random_seed': {'descriptions':{'si':'Seme za skrivnost'}, 'w': False, 'public': False, 'type': None, 'generated': True}, } - def task(IP_SimpleArbiterVPN, IP_VPNClient1, IP_LANClient1, DIRNAME): import collections from pexpect import pxssh # Used to set up an SSH connection to a remote machine -- cgit v1.2.1