1 # command line interface
5 from clientstub import *
7 long_opts = ["username=", "usertype=", "help", "outfile=", "credfile="]
9 # default command line options
10 username = "planetlab.scott.pl.smbaker"
21 server_host = "127.0.0.1"
26 print "syntax: cli <options> command <args>"
28 print " --username ... hrn of user performing op"
29 print " --usertype ... kind of user performing op (user, slice, ...)"
30 print " --outfile ... write reply to file"
31 print " --credfile ... credential to pass"
33 print " lookup <type> <hrn>"
34 print " dumpCredential"
35 print " getCredential <cred_name>"
38 def process_options():
39 global username, usertype, opname
40 global type, hrn, cred_name
42 global id_file, cred_file
43 global acc_file, key_file, out_file
45 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
50 if name == "--username":
52 elif name == "--usertype":
54 elif name == "--help":
57 elif name == "--outfile":
59 elif name == "--credfile":
63 report.error("no operation specified")
68 if opname == "lookup":
70 report.error("syntax: lookup <type> <hrn>")
75 elif opname == "getCredential":
77 report.error("syntax: getcredential <cred_name>")
81 elif opname == "start":
83 report.error("syntax: start <hrn>")
88 leaf_name = get_leaf(username)
91 id_file = leaf_name + ".cert"
94 key_file = leaf_name + ".pkey"
100 cred_file = "cred_file"
103 print " username:", username
104 print " leaf:", leaf_name
105 print " usertype:", usertype
106 print " id_file:", id_file
107 print " key_file:", key_file
108 print " acc_file:", acc_file
109 print "cred_file:", cred_file
110 print "operation:", opname
113 print "cred_name:", cred_name
114 print " out_file:", out_file
116 def get_authority(x):
118 return ".".join(parts[:3])
120 def compose_message():
123 dict = {"opname": opname}
125 if opname == "lookup":
126 g_params["hrn"] = hrn
127 g_params["type"] = type
129 elif opname == "getCredential":
130 g_params["cred_name"] = cred_name
132 parts = cred_name.split(":")
134 report.error("bad format for getCredential (slice:hrn.of.slice, ...)")
136 # XXX smbaker: this looks redundant
137 if parts[0] == "slice":
138 g_params["hrn"] = get_authority(parts[1])
139 g_params["type"] = "slice"
141 elif opname == "start":
142 g_params["hrn"] = hrn
143 g_params["type"] = "slice"
145 dict["g_params"] = g_params
146 dict["p_params"] = p_params
151 message = compose_message()
153 client = GENIClient(username, usertype, id_file, key_file, acc_file, cred_file)
155 server = client.connect(server_host, server_port)
157 report.error("failed to connect to server")
160 report.trace("message:" + str(message))
162 server.write(str(message))
164 reply = server.read(MAX_RESULT)
166 report.error("No reply")
170 open(out_file, "w").write(reply)
172 print "////// RESULT: //////"
175 def dumpCredential():
176 cred_str = open(cred_file).read()
177 c_pem = X509.load_cert_string(cred_str)
178 subjectAltName = c_pem.get_ext("subjectAltName").get_value()
179 info_cert = get_cred_info(subjectAltName)
181 print "subject:", c_pem.get_subject().CN
182 print "issuer:", c_pem.get_issuer().CN
184 print " ", subjectAltName
186 op_set = info_cert['operation_set']
187 for item in op_set.keys():
188 rights = op_set[item]
189 print " ", item, ", ".join(rights)
192 interfaces = info_cert['on_interfaces']
193 for item in interfaces:
194 print " ", item['lbl'], item['type'], item['name']
200 if opname == "dumpCredential":
204 elif opname == "help":
208 elif (opname == "lookup") or \
209 (opname == "getCredential") or \
214 report.error("unknown operation: " + opname)
216 if __name__=="__main__":