modify_permissions_allowlist.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2022 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """A tool for modifying privileged permission allowlists."""
  18. from __future__ import print_function
  19. import argparse
  20. import sys
  21. from xml.dom import minidom
  22. class InvalidRootNodeException(Exception):
  23. pass
  24. class InvalidNumberOfPrivappPermissionChildren(Exception):
  25. pass
  26. def modify_allowlist(allowlist_dom, package_name):
  27. if allowlist_dom.documentElement.tagName != 'permissions':
  28. raise InvalidRootNodeException
  29. nodes = allowlist_dom.getElementsByTagName('privapp-permissions')
  30. if nodes.length != 1:
  31. raise InvalidNumberOfPrivappPermissionChildren
  32. privapp_permissions = nodes[0]
  33. privapp_permissions.setAttribute('package', package_name)
  34. def parse_args():
  35. """Parse commandline arguments."""
  36. parser = argparse.ArgumentParser()
  37. parser.add_argument('input', help='input allowlist template file')
  38. parser.add_argument(
  39. 'package_name', help='package name to use in the allowlist'
  40. )
  41. parser.add_argument('output', help='output allowlist file')
  42. return parser.parse_args()
  43. def main():
  44. try:
  45. args = parse_args()
  46. doc = minidom.parse(args.input)
  47. modify_allowlist(doc, args.package_name)
  48. with open(args.output, 'w') as output_file:
  49. doc.writexml(output_file, encoding='utf-8')
  50. except Exception as err:
  51. print('error: ' + str(err), file=sys.stderr)
  52. sys.exit(-1)
  53. if __name__ == '__main__':
  54. main()