ia64/xen-unstable

changeset 11661:3dea280880e2

[XM] Report proper ACMError(s) instead of silently exiting.

Remove try except blocks so that ACMError(s) are properly reported to
the user rather than silently fail.

Signed-off-by: Alastair Tse <atse@xensource.com>
author atse@norwich.uk.xensource.com
date Thu Sep 28 12:46:09 2006 +0100 (2006-09-28)
parents 98c369711006
children 8cffe84d8f51
files tools/python/xen/xm/addlabel.py tools/python/xen/xm/dry-run.py tools/python/xen/xm/dumppolicy.py tools/python/xen/xm/rmlabel.py
line diff
     1.1 --- a/tools/python/xen/xm/addlabel.py	Thu Sep 28 12:29:52 2006 +0100
     1.2 +++ b/tools/python/xen/xm/addlabel.py	Thu Sep 28 12:46:09 2006 +0100
     1.3 @@ -115,43 +115,45 @@ def add_domain_label(label, configfile, 
     1.4      config_fd.close()
     1.5  
     1.6  
     1.7 -def main (argv):
     1.8 -    try:
     1.9 -        policyref = None
    1.10 -        if len(argv) not in (4, 5):
    1.11 -            raise OptionError('Needs either 2 or 3 arguments')
    1.12 -
    1.13 -        label = argv[1]
    1.14 -
    1.15 -        if len(argv) == 5:
    1.16 -            policyref = argv[4]
    1.17 -        elif security.on():
    1.18 -            policyref = security.active_policy
    1.19 -        else:
    1.20 -            security.err("No active policy. Policy must be specified in command line.")
    1.21 +def main(argv):
    1.22 +    policyref = None
    1.23 +    if len(argv) not in (4, 5):
    1.24 +        raise OptionError('Needs either 2 or 3 arguments')
    1.25 +    
    1.26 +    label = argv[1]
    1.27 +    
    1.28 +    if len(argv) == 5:
    1.29 +        policyref = argv[4]
    1.30 +    elif security.on():
    1.31 +        policyref = security.active_policy
    1.32 +    else:
    1.33 +        raise OptionError("No active policy. Must specify policy on the "
    1.34 +                          "command line.")
    1.35  
    1.36 -        if argv[2].lower() == "dom":
    1.37 -            configfile = argv[3]
    1.38 -            if configfile[0] != '/':
    1.39 -                for prefix in [".", "/etc/xen"]:
    1.40 -                    configfile = prefix + "/" + configfile
    1.41 -                    if os.path.isfile(configfile):
    1.42 -                        break
    1.43 -            if not validate_config_file(configfile):
    1.44 -                raise OptionError('Invalid config file')
    1.45 -            else:
    1.46 -                add_domain_label(label, configfile, policyref)
    1.47 -        elif argv[2].lower() == "res":
    1.48 -            resource = argv[3]
    1.49 -            add_resource_label(label, resource, policyref)
    1.50 +    if argv[2].lower() == "dom":
    1.51 +        configfile = argv[3]
    1.52 +        if configfile[0] != '/':
    1.53 +            for prefix in [".", "/etc/xen"]:
    1.54 +                configfile = prefix + "/" + configfile
    1.55 +                if os.path.isfile(configfile):
    1.56 +                    break
    1.57 +        if not validate_config_file(configfile):
    1.58 +            raise OptionError('Invalid config file')
    1.59          else:
    1.60 -            raise OptionError('Need to specify either "dom" or "res" as object to add label to.')
    1.61 +            add_domain_label(label, configfile, policyref)
    1.62 +    elif argv[2].lower() == "res":
    1.63 +        resource = argv[3]
    1.64 +        add_resource_label(label, resource, policyref)
    1.65 +    else:
    1.66 +        raise OptionError('Need to specify either "dom" or "res" as '
    1.67 +                          'object to add label to.')
    1.68              
    1.69 -    except security.ACMError:
    1.70 +if __name__ == '__main__':
    1.71 +    try:
    1.72 +        main(sys.argv)
    1.73 +    except Exception, e:
    1.74 +        sys.stderr.write('Error: %s\n' % str(e))
    1.75          sys.exit(-1)
    1.76 -
    1.77 -if __name__ == '__main__':
    1.78 -    main(sys.argv)
    1.79      
    1.80  
    1.81  
     2.1 --- a/tools/python/xen/xm/dry-run.py	Thu Sep 28 12:29:52 2006 +0100
     2.2 +++ b/tools/python/xen/xm/dry-run.py	Thu Sep 28 12:46:09 2006 +0100
     2.3 @@ -32,27 +32,26 @@ def help():
     2.4      individually along with the final security decision."""
     2.5  
     2.6  def main (argv):
     2.7 -    try:
     2.8 -        if len(argv) != 2:
     2.9 -            raise OptionError('Invalid number of arguments')
    2.10 -
    2.11 -        passed = 0
    2.12 -        (opts, config) = create.parseCommandLine(argv)
    2.13 -        if create.check_domain_label(config, verbose=1):
    2.14 -            if create.config_security_check(config, verbose=1):
    2.15 -                passed = 1
    2.16 -        else:
    2.17 -            print "Checking resources: (skipped)"
    2.18 -                
    2.19 -        if passed:
    2.20 -            print "Dry Run: PASSED"
    2.21 -        else:
    2.22 -            print "Dry Run: FAILED"
    2.23 -            sys.exit(-1)
    2.24 -
    2.25 -    except security.ACMError:
    2.26 +    if len(argv) != 2:
    2.27 +        raise OptionError('Invalid number of arguments')
    2.28 +    
    2.29 +    passed = 0
    2.30 +    (opts, config) = create.parseCommandLine(argv)
    2.31 +    if create.check_domain_label(config, verbose=1):
    2.32 +        if create.config_security_check(config, verbose=1):
    2.33 +            passed = 1
    2.34 +    else:
    2.35 +        print "Checking resources: (skipped)"
    2.36 +        
    2.37 +    if passed:
    2.38 +        print "Dry Run: PASSED"
    2.39 +    else:
    2.40 +        print "Dry Run: FAILED"
    2.41          sys.exit(-1)
    2.42  
    2.43 -
    2.44  if __name__ == '__main__':
    2.45 -    main(sys.argv)
    2.46 +    try:
    2.47 +        main(sys.argv)
    2.48 +    except Exception, e:
    2.49 +        sys.stderr.write('Error: %s\n' % str(e))
    2.50 +        sys.exit(-1)
     3.1 --- a/tools/python/xen/xm/dumppolicy.py	Thu Sep 28 12:29:52 2006 +0100
     3.2 +++ b/tools/python/xen/xm/dumppolicy.py	Thu Sep 28 12:46:09 2006 +0100
     3.3 @@ -19,7 +19,7 @@
     3.4  """
     3.5  import sys
     3.6  from xen.util.security import ACMError, err, dump_policy
     3.7 -
     3.8 +from xen.xm.opts import OptionError
     3.9  
    3.10  def help():
    3.11      return """
    3.12 @@ -27,16 +27,16 @@ def help():
    3.13      (low-level)."""
    3.14  
    3.15  def main(argv):
    3.16 +    if len(argv) != 1:
    3.17 +        raise OptionError("No arguments expected.")
    3.18 +
    3.19 +    dump_policy()
    3.20 +
    3.21 +if __name__ == '__main__':
    3.22      try:
    3.23 -        if len(argv) != 1:
    3.24 -            usage()
    3.25 -
    3.26 -        dump_policy()
    3.27 -    except ACMError:
    3.28 +        main(sys.argv)
    3.29 +    except Exception, e:
    3.30 +        sys.stderr.write('Error: %s\n' % str(e))    
    3.31          sys.exit(-1)
    3.32  
    3.33  
    3.34 -if __name__ == '__main__':
    3.35 -    main(sys.argv)
    3.36 -
    3.37 -
     4.1 --- a/tools/python/xen/xm/rmlabel.py	Thu Sep 28 12:29:52 2006 +0100
     4.2 +++ b/tools/python/xen/xm/rmlabel.py	Thu Sep 28 12:46:09 2006 +0100
     4.3 @@ -42,14 +42,14 @@ def rm_resource_label(resource):
     4.4      try:
     4.5          access_control = dictio.dict_read("resources", file)
     4.6      except:
     4.7 -        security.err("Resource file not found, cannot remove label!")
     4.8 +        raise security.ACMError("Resource file not found, cannot remove label!")
     4.9  
    4.10      # remove the entry and update file
    4.11      if access_control.has_key(resource):
    4.12          del access_control[resource]
    4.13          dictio.dict_write(access_control, "resources", file)
    4.14      else:
    4.15 -        security.err("Resource not labeled.")
    4.16 +        raise security.ACMError("Resource not labeled")
    4.17  
    4.18  
    4.19  def rm_domain_label(configfile):
    4.20 @@ -65,8 +65,8 @@ def rm_domain_label(configfile):
    4.21                  fd = open(file, "rb")
    4.22                  break
    4.23      if not fd:
    4.24 -        security.err("Configuration file '"+configfile+"' not found.")
    4.25 -
    4.26 +        raise OptionError("Configuration file '%s' not found." % configfile)
    4.27 +        
    4.28      # read in the domain config file, removing label
    4.29      ac_entry_re = re.compile("^access_control\s*=.*", re.IGNORECASE)
    4.30      ac_exit_re = re.compile(".*'\].*")
    4.31 @@ -86,7 +86,7 @@ def rm_domain_label(configfile):
    4.32  
    4.33      # send error message if we didn't find anything to remove
    4.34      if not removed:
    4.35 -        security.err("Domain not labeled.")
    4.36 +        raise security.ACMError('Domain not labeled')
    4.37  
    4.38      # write the data back out to the file
    4.39      fd = open(file, "wb")
    4.40 @@ -102,17 +102,18 @@ def main (argv):
    4.41      if argv[1].lower() not in ('dom', 'res'):
    4.42          raise OptionError('Unrecognised type argument: %s' % argv[1])
    4.43  
    4.44 -    try:
    4.45 -        if argv[1].lower() == "dom":
    4.46 -            configfile = argv[2]
    4.47 -            rm_domain_label(configfile)
    4.48 -        elif argv[1].lower() == "res":
    4.49 -            resource = argv[2]
    4.50 -            rm_resource_label(resource)
    4.51 -    except security.ACMError:
    4.52 -        sys.exit(-1)
    4.53 +    if argv[1].lower() == "dom":
    4.54 +        configfile = argv[2]
    4.55 +        rm_domain_label(configfile)
    4.56 +    elif argv[1].lower() == "res":
    4.57 +        resource = argv[2]
    4.58 +        rm_resource_label(resource)
    4.59  
    4.60  if __name__ == '__main__':
    4.61 -    main(sys.argv)
    4.62 +    try:
    4.63 +        main(sys.argv)
    4.64 +    except Exception, e:
    4.65 +        sys.stderr.write('Error: %s\n' % str(e))
    4.66 +        sys.exit(-1)    
    4.67  
    4.68