blob: 4a0ca8fe130c3fb55df767433d73c9565da0daa5 [file] [log] [blame]
Andrei Onea580f7b02022-08-17 16:50:48 +00001#!/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
Andrei Onea580f7b02022-08-17 16:50:48 +000019import argparse
20import sys
21from xml.dom import minidom
22
23
24class InvalidRootNodeException(Exception):
25 pass
26
27
28class InvalidNumberOfPrivappPermissionChildren(Exception):
29 pass
30
31
32def modify_allowlist(allowlist_dom, package_name):
33 if allowlist_dom.documentElement.tagName != 'permissions':
34 raise InvalidRootNodeException
35 nodes = allowlist_dom.getElementsByTagName('privapp-permissions')
36 if nodes.length != 1:
37 raise InvalidNumberOfPrivappPermissionChildren
38 privapp_permissions = nodes[0]
39 privapp_permissions.setAttribute('package', package_name)
40
41
42def parse_args():
43 """Parse commandline arguments."""
44
45 parser = argparse.ArgumentParser()
46 parser.add_argument('input', help='input allowlist template file')
47 parser.add_argument(
48 'package_name', help='package name to use in the allowlist'
49 )
50 parser.add_argument('output', help='output allowlist file')
51
52 return parser.parse_args()
53
54
55def main():
56 try:
57 args = parse_args()
58 doc = minidom.parse(args.input)
59 modify_allowlist(doc, args.package_name)
60 with open(args.output, 'w') as output_file:
61 doc.writexml(output_file, encoding='utf-8')
62 except Exception as err:
63 print('error: ' + str(err), file=sys.stderr)
64 sys.exit(-1)
65
66
67if __name__ == '__main__':
68 main()