+++ /dev/null
-#!/usr/bin/python
-
-import os, sys
-from M2Crypto import SSL
-from sec import *
-from cliexcep import *
-import report
-
-# XXX SMBAKER: changed MAX_RESULT from 3000B to 32KB
-MAX_RESULT = 32768
-
-def verify_callback(preverify_ok, ctx):
- return 1
-
-class GENIClient():
- def __init__(self, hrn, type, id_file, id_key_file, acc_file, cred_file):
- self.hrn = hrn
- self.type = type
-
- #check if the certificate and the private key exists, terminate if not
- if not os.path.exists(id_file):
- report.error("Certificate file " + id_file + " does not exist")
- raise NonexistingFile(id_file)
-
- if not os.path.exists(id_key_file):
- report.error("Key file: " + id_key_file + " does not exist")
- raise NonexistingFile(key_file)
-
- report.trace("cert: " + id_file + ", key_file: " + id_key_file)
-
- #check the acc and cred files
- if not os.path.exists(acc_file) or not is_valid_chain(acc_file):
- report.trace("replacing acc_file: " + acc_file + " with anonymous acc")
- open(acc_file, 'w').write('ANONYM')
-
- if not os.path.exists(cred_file) or not is_valid_chain(cred_file):
- report.trace("replacing cred_file: " + cred_file + " with no_cred")
- open(cred_file, 'w').write('NO_CRED')
-
- #initialize the security system
- self.sec = Sec('client', id_file, id_key_file, acc_file, cred_file)
- #ssl parameters
- self.ctx = SSL.Context()
- self.ctx.load_cert(self.sec.id_file, self.sec.id_key_file)
- self.ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, depth=9, callback=verify_callback)
-
- def connect(self, host, port):
- #if the acc and cred needs renewal then do call to authority
- if self.type == 'user' or self.type == 'slice' or self.type == 'SA':
- reg_type = 'slice'
- else:
- reg_type ='component'
-
- auth_host = host
- auth_port = port
-
- report.trace("renewing accounting")
- renew_res1 = renew_cert('accounting', '.', reg_type, self.hrn, None, None, (auth_host, auth_port), self.sec)
- if renew_res1 == None:
- report.error("There is no certificate in the directory .")
- raise NoCertInDirectory(".")
-
- report.trace("renewing credential")
- renew_res2 = renew_cert('credential', '.', reg_type, self.hrn, None, None, (auth_host, auth_port), self.sec)
- # XXX check result of renew_res2 ?
-
- #connect to server
- server = SSL.Connection(self.ctx)
-
- report.trace("connecting")
- server.connect((host,port))
-
- report.trace("authenticating")
- peer = self.sec.auth_protocol(server)
- if peer:
- report.trace("Authentication successful")
- return server
- else:
- report.error("Authentication failed")
- raise AuthenticationFailed()
-
-def toFileFormat(res_str):
- out_str = ""
- try:
- res_dict = eval(res_str)
- if res_dict['geni'].has_key('pubkey'): # in public key, replace '\n' with ' '
- pubkey = res_dict['geni']['pubkey']
- pubkey = pubkey.split('-----BEGIN RSA PRIVATE KEY-----')[1].split('-----END RSA PRIVATE KEY-----')[0].replace('\n',' ')
- pubkey = '-----BEGIN RSA PRIVATE KEY-----'+pubkey+'-----END RSA PRIVATE KEY-----'
- res_dict['geni']['pubkey'] = pubkey
-
- if res_dict.has_key('message'):
- out_str = res_dict['message']+'\n'
- else:
- out_str = "{'geni':{\n"
- for key in res_dict['geni']:
- val = ''
- if res_dict['geni'][key] == None:
- val = ''
- elif isinstance(res_dict['geni'][key], str):
- val = res_dict['geni'][key]
- else:
- val = str(res_dict['geni'][key])
- out_str = out_str+"'"+key+"':"+val+"\n"
- out_str = out_str + "}\n"
- out_str = out_str + "'pl':{\n"
- for key in res_dict['pl']:
- val = ''
- if res_dict['pl'][key] == None:
- val = ''
- if isinstance(res_dict['pl'][key], str):
- val = res_dict['pl'][key]
- else:
- val = str(res_dict['pl'][key])
- out_str = out_str+"'"+key+"':"+val+"\n"
- out_str = out_str + "}}"
- except:
- out_str = res_str
- return out_str
-
-def evaluate(call_data):
- call_data = eval(call_data)
- #adjust the key format to obey server's storage format
- if call_data['g_params'].has_key('pubkey'): #replace the ' ' with '\n'
- pubkey = call_data['g_params']['pubkey']
- pubkey = pubkey.split('-----BEGIN RSA PRIVATE KEY-----')[1].split('-----END RSA PRIVATE KEY-----')[0].replace(' ','\n')
- pubkey = '-----BEGIN RSA PRIVATE KEY-----'+pubkey+'-----END RSA PRIVATE KEY-----'
- call_data['g_params']['pubkey'] = pubkey
- return call_data
-
-def oldmain():
- try:
- #read the input file
- fp = open('tmp_input.txt', 'r')
- user_data = fp.readline()
- call_data = fp.read()
- print 'Read file.\n'
-
- #client related info
- HRN = user_data.split(' ')[0]
- TYPE = user_data.split(' ')[1].split('\n')[0]
- name = get_leaf(HRN)
- ID_FILE = name+'.cert'
- ID_KEY_FILE = name+'.pkey'
- ACC_FILE = 'acc_file'
- CRED_FILE = 'cred_file'
- my_client = GENIClient(HRN, TYPE, ID_FILE, ID_KEY_FILE, ACC_FILE, CRED_FILE)
- print 'Constructed client.\n'
-
- #operation call
- message = evaluate(call_data)
- server = my_client.connect(SERVER_HOST, SERVER_PORT)
- if server:
- server.write(str(message))
- result = toFileFormat(server.read(MAX_RESULT))
- server.close()
- print 'Performed the call.\n'
- else:
- result = "Error in client data structures.\n"
-
- #write result to output file
- open('tmp_output.txt','w').write(result)
- print 'Written to file.\n'
- except "XXX": # XXX smbaker
- #write result to output file
- open('tmp_output.txt','w').write("An error occurred in client stub.\n")
- print 'Exception occurred.\n'
-
-#if __name__=="__main__":
-# print 'Client started.\n'
-# os.system("echo foo > foo.txt")
-# os.system("mv tmp_input.3 tmp_input.4")
-# os.system("mv tmp_input.2 tmp_input.3")
-# os.system("mv tmp_input.1 tmp_input.2")
-# os.system("cp tmp_input.txt tmp_input.1")
-# main()
+++ /dev/null
-import unittest
-import xmlrpclib
-from gackshandle import *
-
-class TestGacksHandle(unittest.TestCase):
- def setUp(self):
- pass
-
- def testCreate(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- self.assertEqual(h.id, "cpu")
- self.assertEqual(h.unitStart, 10)
- self.assertEqual(h.unitStop, 15)
- self.assertEqual(h.timeStart, 20)
- self.assertEqual(h.timeStop, 25)
-
- def testAsString(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- s = h.as_string()
- self.assertEqual(s, "cpu#10-15#20-25")
-
- def testLoadFromString(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- s = h.as_string()
- h2 = GacksHandle(string=s)
- self.assertEqual(h.id, h2.id)
- self.assertEqual(h.unitStart, h2.unitStart)
- self.assertEqual(h.unitStop, h2.unitStop)
- self.assertEqual(h.timeStart, h2.timeStart)
- self.assertEqual(h.timeStop, h2.timeStop)
-
- def testGetQuantity(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- self.assertEqual(h.get_quantity(), 5)
-
- def testGetDuration(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- self.assertEqual(h.get_duration(), 5)
-
- def testClone(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- h2 = h.clone()
- self.assertEqual(h.id, h2.id)
- self.assertEqual(h.unitStart, h2.unitStart)
- self.assertEqual(h.unitStop, h2.unitStop)
- self.assertEqual(h.timeStart, h2.timeStart)
- self.assertEqual(h.timeStop, h2.timeStop)
-
- def testSplitUnit(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- parts = h.split_unit(12)
- h1 = parts[0]
- h2 = parts[1]
-
- self.assertEqual(h1.id, "cpu")
- self.assertEqual(h1.unitStart, 10)
- self.assertEqual(h1.unitStop, 12)
- self.assertEqual(h1.timeStart, 20)
- self.assertEqual(h1.timeStop, 25)
-
- self.assertEqual(h2.id, "cpu")
- self.assertEqual(h2.unitStart, 12)
- self.assertEqual(h2.unitStop, 15)
- self.assertEqual(h2.timeStart, 20)
- self.assertEqual(h2.timeStop, 25)
-
- def testSplitTime(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- parts = h.split_time(23)
- h1 = parts[0]
- h2 = parts[1]
-
- self.assertEqual(h1.id, "cpu")
- self.assertEqual(h1.unitStart, 10)
- self.assertEqual(h1.unitStop, 15)
- self.assertEqual(h1.timeStart, 20)
- self.assertEqual(h1.timeStop, 23)
-
- self.assertEqual(h2.id, "cpu")
- self.assertEqual(h2.unitStart, 10)
- self.assertEqual(h2.unitStop, 15)
- self.assertEqual(h2.timeStart, 23)
- self.assertEqual(h2.timeStop, 25)
-
- def testSplitSubset(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
-
- # split out a subset right in the middle
- parts = h.clone().split_subset(12, 13, 22, 23)
-
- self.assertEqual(len(parts), 5)
- self.assert_(find_handle_in_list(parts, 10, 12, 20, 25)) # h1
- self.assert_(find_handle_in_list(parts, 12, 13, 20, 22)) # h2
- self.assert_(find_handle_in_list(parts, 12, 13, 23, 25)) # h3
- self.assert_(find_handle_in_list(parts, 13, 15, 20, 25)) # h4
- self.assert_(find_handle_in_list(parts, 12, 13, 22, 23)) # s
-
- # split out a subset in the top left corner
- parts = h.clone().split_subset(10, 13, 20, 23)
-
- self.assertEqual(len(parts), 3)
- self.assert_(find_handle_in_list(parts, 10, 13, 23, 25)) # h3
- self.assert_(find_handle_in_list(parts, 13, 15, 20, 25)) # h4
- self.assert_(find_handle_in_list(parts, 10, 13, 20, 23)) # s
-
- # split out a subset in the bottom right corner
- parts = h.clone().split_subset(12, 15, 22, 25)
-
- self.assertEqual(len(parts), 3)
- self.assert_(find_handle_in_list(parts, 10, 12, 20, 25)) # h1
- self.assert_(find_handle_in_list(parts, 12, 15, 20, 22)) # h2
- self.assert_(find_handle_in_list(parts, 12, 15, 22, 25)) # s
-
- def testDict(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- d = h.as_dict()
-
- h2 = GacksHandle(dict = d)
-
- self.assertEqual(h.id, h2.id)
- self.assertEqual(h.unitStart, h2.unitStart)
- self.assertEqual(h.unitStop, h2.unitStop)
- self.assertEqual(h.timeStart, h2.timeStart)
- self.assertEqual(h.timeStop, h2.timeStop)
-
-class TestGacksRecord(unittest.TestCase):
- def setUp(self):
- pass
-
- def testCreate(self):
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- self.assertEqual(r.id, "cpu")
- self.assertEqual(r.unitStart, 10)
- self.assertEqual(r.unitStop, 15)
- self.assertEqual(r.timeStart, 20)
- self.assertEqual(r.timeStop, 25)
- self.assertEqual(r.allocatorHRNs, ["foo", "bar"])
- self.assertEqual(r.consumerHRN, "slice1")
-
- def testClone(self):
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- r2 = r.clone()
-
- self.assertEqual(r.id, r2.id)
- self.assertEqual(r.unitStart, r2.unitStart)
- self.assertEqual(r.unitStop, r2.unitStop)
- self.assertEqual(r.timeStart, r2.timeStart)
- self.assertEqual(r.timeStop, r2.timeStop)
- self.assertEqual(r.allocatorHRNs, r2.allocatorHRNs)
- self.assertEqual(r.consumerHRN, r2.consumerHRN)
-
- def testSetAllocator(self):
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- r.set_allocator("bar", "bob", -1, 1)
- self.assertEqual(r.allocatorHRNs, ["foo", "bar", "bob"])
-
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- r.set_allocator("bar", "bob", -1, 0)
- self.assertEqual(r.allocatorHRNs, ["foo", "bob"])
-
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar","foo","bar"], "slice1")
- r.set_allocator("bar", "bob", 0, 0)
- self.assertEqual(r.allocatorHRNs, ["foo", "bob"])
-
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar","foo","bar"], "slice1")
- r.set_allocator("bar", "bob", 1, 0)
- self.assertEqual(r.allocatorHRNs, ["foo", "bar", "foo", "bob"])
-
- def testGetAllocator(self):
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- self.assertEqual(r.get_allocators(), ["foo", "bar"])
-
- def testSetConsumer(self):
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- r.set_consumer("slice2")
- self.assertEqual(r.get_consumer(), "slice2")
-
- def testGetConsumer(self):
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- self.assertEqual(r.get_consumer(), "slice1")
-
- def testTestDict(self):
- r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
- d = r.as_dict()
-
- r2 = GacksRecord(dict=d)
-
- self.assertEqual(r.id, r2.id)
- self.assertEqual(r.unitStart, r2.unitStart)
- self.assertEqual(r.unitStop, r2.unitStop)
- self.assertEqual(r.timeStart, r2.timeStart)
- self.assertEqual(r.timeStop, r2.timeStop)
- self.assertEqual(r.allocatorHRNs, r2.allocatorHRNs)
- self.assertEqual(r.consumerHRN, r2.consumerHRN)
-
-
-
-if __name__ == "__main__":
- unittest.main()
+++ /dev/null
-import unittest
-import xmlrpclib
-from gacksreceipt import *
-
-class TestGacksReceipt(unittest.TestCase):
- def setUp(self):
- pass
-
- def testCreate(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- r = GacksReceipt(subject="foo1", handle = h, action="foo")
- self.assertEqual(r.handle, h)
- self.assertEqual(r.action, "foo")
-
- def testSetHandle(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- r = GacksReceipt(subject="foo1", handle = h)
- self.assertEqual(r.handle, h)
-
- h2 = GacksHandle("cpu", 10, 15, 20, 25)
- r.SetHandle(h2);
- self.assertEqual(r.handle, h2)
-
- def testSetAction(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- r = GacksReceipt(subject="foo1", handle = h, action="foo")
- self.assertEqual(r.action, "foo")
-
- r.SetAction("bar");
- self.assertEqual(r.action, "bar")
-
- def testEncodeDecode(self):
- h = GacksHandle("cpu", 10, 15, 20, 25)
- rec1 = GacksRecord("cpu", 10, 13, 20, 25, ["aaa","bbb"], "slice1")
- rec2 = GacksRecord("cpu", 13, 15, 20, 25, ["aaa","bbb"], "slice1")
- r = GacksReceipt(subject="foo1", handle = h, action="foo", reclist=[rec1,rec2])
-
- r.encode()
-
- str = r.save_to_string()
-
- r2 = GacksReceipt(string = str)
- #r2.decode()
- #r2.dump()
-
- h2 = r2.GetHandle()
- self.assertEqual(h2.id, "cpu")
- self.assertEqual(h2.unitStart, 10)
- self.assertEqual(h2.unitStop, 15)
- self.assertEqual(h2.timeStart, 20)
- self.assertEqual(h2.timeStop, 25)
-
- self.assertEqual(r2.GetAction(), "foo")
-
- reclist = r2.GetRecords()
- r1=reclist[0]
- r2=reclist[1]
- self.assertEqual(r1.id, "cpu")
- self.assertEqual(r1.unitStart, 10)
- self.assertEqual(r1.unitStop, 13)
- self.assertEqual(r1.timeStart, 20)
- self.assertEqual(r1.timeStop, 25)
- self.assertEqual(r2.id, "cpu")
- self.assertEqual(r2.unitStart, 13)
- self.assertEqual(r2.unitStop, 15)
- self.assertEqual(r2.timeStart, 20)
- self.assertEqual(r2.timeStop, 25)
-
-if __name__ == "__main__":
- unittest.main()