BATS fell down pushing a process into the background, so I switched to shunit2 /...
[certmaster.git] / scripts / certmaster-ca
index 7370ef3..75bfad3 100755 (executable)
@@ -1,30 +1,33 @@
 #!/usr/bin/python -tt
 # sign/list keys
+# --ca ca sign/list certs for the 'ca' 
 # --sign hostname hostname hostname
 # --list # lists all csrs needing to be signed
-# --list-all ?
+# --list-all ca list all certs for a given ca
 # --clean? not sure what it will do
 
 import sys
 import glob
+import optparse
 import os
 
 import certmaster
 import certmaster.certs
 import certmaster.certmaster
 
-
-
-from optparse import OptionParser
-
 def errorprint(stuff):
     print >> sys.stderr, stuff
 
+class CertmasterCAOptionParser(optparse.OptionParser):
+    def get_version(self):
+        return file("/etc/certmaster/version").read().strip()
 
 def parseargs(args):
     usage = 'certmaster-ca <option> [args]'
-    parser = OptionParser(usage=usage)
-    
+    parser = CertmasterCAOptionParser(usage=usage,version=True)
+
+    parser.add_option("", '--ca', default='', action="store", dest="ca", metavar="CA",
+          help="certificate authority used to sign/list certs")
     parser.add_option('-l', '--list', default=False, action="store_true",
           help='list signing requests remaining')
     parser.add_option('-s', '--sign', default=False, action="store_true",
@@ -56,11 +59,17 @@ def main(args):
     
     (opts, args) = parseargs(args)
 
+    ## Check that the ca option matches a configured ca
+    try:
+        certauth = cm.cfg.ca[opts.ca]
+    except:
+        errorprint("Unknown ca %s: check /etc/certmaster.cfg" % opts.ca)
+        return 1
         
     if opts.list:
-        hns = cm.get_csrs_waiting()
+        hns = cm.get_csrs_waiting(certauth)
         if hns:
-            for hn in cm.get_csrs_waiting():
+            for hn in sorted(hns):
                 print hn
         else:
            print 'No certificates to sign'
@@ -73,14 +82,14 @@ def main(args):
             return 1
             
         for hn in args:
-            csrglob = '%s/%s.csr' % (cm.cfg.csrroot, hn)
+            csrglob = '%s/%s.csr' % (certauth.csrroot, hn)
             csrs = glob.glob(csrglob)
             if not csrs:
                 errorprint('No match for %s to sign' % hn)
                 return 1
             
             for fn in csrs:
-                certfile = cm.sign_this_csr(fn)
+                certfile = cm.sign_this_csr(fn, certauth)
                 print '%s signed - cert located at %s' % (fn, certfile)
         return 0
     
@@ -90,7 +99,7 @@ def main(args):
             return 1
         
         for hn in args:
-            cm.remove_this_cert(hn)
+            cm.remove_this_cert(hn, certauth)
         
         return 0
 
@@ -99,9 +108,9 @@ def main(args):
         if args:
             hostglobs = args
 
-        signed_certs = cm.get_signed_certs(args)
+        signed_certs = cm.get_signed_certs(certauth, args)
 
-        for i in signed_certs:
+        for i in sorted(signed_certs):
             print i
             
         return 0
@@ -111,9 +120,9 @@ def main(args):
         if args:
             hostglobs = args
             
-        cert_hashes = cm.get_cert_hashes(hostglobs)
+        cert_hashes = cm.get_cert_hashes(certauth, hostglobs)
 
-        for i in cert_hashes:
+        for i in sorted(cert_hashes):
             print i
             
         return 0