1 # command line interface
7 from geniclient import *
9 long_opts = ["keyfile=", "help", "outfile=", "credfile=", "username=", "email="]
11 # default command line options
28 server_url = "https://localhost:12345/"
31 parts = hrn.split(".")
\r
35 print "syntax: cli <options> command <args>"
37 print " --username ... username (or hrn) of user making call"
38 print " --outfile ... save response to a file"
39 print " --credfile ... credential of user making call (or 'None')"
40 print " --keyfile ... private key file of user making call"
41 print " --email ... email address"
43 print " resolve <hrn>"
44 print " dumpCredential"
45 print " getCredential <type> <hrn>"
47 print " createKey <filename>"
48 print " createGid <hrn> <uuid|None> <pubkey_fn>"
49 print " register <type> <hrn> <gid_filename>"
50 print " remove <type> <hrn>"
52 def process_options():
56 global cert_file, cred_file
57 global key_file, out_file
58 global uuid, pkey_fn, gid_fn, email, gid_pkey_fn
60 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
68 elif name == "--username":
70 elif name == "--outfile":
72 elif name == "--credfile":
74 elif name == "--certfile":
76 elif name == "--keyfile":
78 elif name == "--email":
82 print "no operation specified"
87 if opname == "resolve":
89 print "syntax: resolve <hrn>"
93 elif opname == "getCredential":
95 print "syntax: getcredential <type> <hrn>"
100 elif opname == "createGid":
102 print "syntax: createGid <hrn> <uuid|None> <pubkey_fn>"
108 gid_pkey_fn = args[3]
110 elif opname == "register":
112 print "syntax: register <type> <hrn> <gid_filename>"
117 elif opname == "remove":
119 print "syntax: remove <type> <hrn>"
123 leaf_name = get_leaf(username)
125 if cert_file == None:
126 cert_file = leaf_name + ".cert"
129 key_file = leaf_name + ".pkey"
131 if cred_file == None:
132 cred_file = leaf_name + ".cred"
135 print " username:", username
136 print "cert_file:", cert_file
137 print " key_file:", key_file
138 print "cred_file:", cred_file
139 print "operation:", opname
142 print " out_file:", out_file
144 def get_authority(x):
146 return ".".join(parts[:3])
148 def dumpCredential():
151 # creates a self-signed certificate and private key
153 k = Keypair(create=True)
160 ik = Keypair(create=True)
163 print "writing private key to", key_file
164 k.save_to_file(key_file)
166 #cert = Certificate(subject=username)
168 #cert.set_issuer(ik, iname)
170 #print "writing self-signed cert to", cert_file
171 #cert.save_to_file(cert_file)
173 def load_publickey_string(fn):
175 key_string = f.read()
177 # if the filename is a private key file, then extract the public key
178 if "PRIVATE KEY" in key_string:
179 outfn = tempfile.mktemp()
180 cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
183 key_string = f.read()
194 # if the operation is not a local operation, then create a geniclient to
196 if (opname != "dumpCredential") and (opname != "help") and (opname != "createKey"):
197 if not os.path.exists(key_file):
198 print "key file", key_file, "does not exist"
200 if not os.path.exists(cert_file):
201 k = Keypair(filename = key_file)
202 cert = Certificate(subject=username)
204 cert.set_issuer(k, username)
206 print "writing self-signed cert to", cert_file
207 cert.save_to_file(cert_file)
208 client = GeniClient(server_url, key_file, cert_file)
210 # if a cred_file was specified, then load the credential
211 if (cred_file=="None") or (opname == "help") or (opname == "createKey"):
214 cred = Credential(filename = cred_file)
216 if opname == "dumpCredential":
219 elif opname == "help":
222 elif opname == "createKey":
225 elif (opname == "resolve"):
226 result = client.resolve(cred, hrn)
228 for record in result:
234 elif (opname == "getCredential"):
235 result = client.get_credential(cred, type, hrn)
240 file(out_file, "w").write(result.save_to_string(save_parents=True))
244 elif (opname == "list"):
245 result = client.list(cred)
247 for record in result:
253 elif (opname == "createGid"):
254 # try loading it from a private or a public key file
255 pkey_string = load_publickey_string(gid_pkey_fn)
257 gid = client.create_gid(cred, hrn, uuid, pkey_string)
262 file(out_file,"w").write(gid.save_to_string(save_parents=True))
266 elif (opname == "register"):
270 print "ERROR: must specify --email <addr> when registering users"
271 geni_info['email'] = email
272 gid = GID(filename=gid_fn)
273 record = GeniRecord(name=hrn, gid=gid, type=type, pointer=-1)
274 record.set_geni_info(geni_info)
276 result = client.register(cred, record)
278 elif (opname == "remove"):
279 record_list = client.resolve(cred, hrn)
281 print "no records match hrn"
283 matching_records = []
284 for record in record_list:
285 if record.get_type() == type:
286 matching_records.append(record)
288 if not matching_records:
289 print "records match hrn, but no records match type"
291 for record in matching_records:
292 client.remove(cred,record)
295 print "unknown operation: " + opname
297 if __name__=="__main__":