generate_policy_source.py 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. '''python3 %(prog)s [options]
  6. Pass at least:
  7. --chrome-version-file <path to src/chrome/VERSION> or --all-chrome-versions
  8. --target-platform <which platform the target code will be generated for and can
  9. be one of (win, mac, linux, chromeos, ios, fuchsia)>
  10. --policy-templates-file <path to the policy_templates.json input file>.'''
  11. from argparse import ArgumentParser
  12. from collections import defaultdict
  13. from collections import namedtuple
  14. from collections import OrderedDict
  15. from functools import cmp_to_key
  16. from functools import partial
  17. import ast
  18. import codecs
  19. import json
  20. import os
  21. import re
  22. import sys
  23. import textwrap
  24. sys.path.insert(
  25. 0,
  26. os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir,
  27. 'third_party', 'six', 'src'))
  28. import six
  29. from xml.sax.saxutils import escape as xml_escape
  30. if sys.version_info.major == 2:
  31. string_type = basestring
  32. else:
  33. string_type = str
  34. CHROME_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'
  35. CHROMIUM_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Chromium'
  36. PLATFORM_STRINGS = {
  37. 'chrome_frame': ['win'],
  38. 'chrome_os': ['chrome_os'],
  39. 'android': ['android'],
  40. 'webview_android': ['android'],
  41. 'ios': ['ios'],
  42. 'fuchsia': ['fuchsia'],
  43. 'chrome.win': ['win'],
  44. 'chrome.linux': ['linux'],
  45. 'chrome.mac': ['mac'],
  46. 'chrome.*': ['win', 'mac', 'linux'],
  47. 'chrome.win7': ['win'],
  48. }
  49. class PolicyDetails:
  50. """Parses a policy template and caches all its details."""
  51. # Maps policy types to a tuple with 4 other types:
  52. # - the equivalent base::Value::Type or 'TYPE_EXTERNAL' if the policy
  53. # references external data
  54. # - the equivalent Protobuf field type
  55. # - the name of one of the protobufs for shared policy types
  56. # - the equivalent type in Android's App Restriction Schema
  57. TYPE_MAP = {
  58. 'dict': ('Type::DICTIONARY', 'string', 'String', 'string'),
  59. 'external': ('TYPE_EXTERNAL', 'string', 'String', 'invalid'),
  60. 'int': ('Type::INTEGER', 'int64', 'Integer', 'integer'),
  61. 'int-enum': ('Type::INTEGER', 'int64', 'Integer', 'choice'),
  62. 'list': ('Type::LIST', 'StringList', 'StringList', 'string'),
  63. 'main': ('Type::BOOLEAN', 'bool', 'Boolean', 'bool'),
  64. 'string': ('Type::STRING', 'string', 'String', 'string'),
  65. 'string-enum': ('Type::STRING', 'string', 'String', 'choice'),
  66. 'string-enum-list': ('Type::LIST', 'StringList', 'StringList',
  67. 'multi-select'),
  68. }
  69. class EnumItem:
  70. def __init__(self, item):
  71. self.caption = PolicyDetails._RemovePlaceholders(item['caption'])
  72. self.value = item['value']
  73. def _ConvertPlatform(self, platform):
  74. '''Converts product platform string in policy_templates.json to platform
  75. string that is defined in build config.'''
  76. if platform not in PLATFORM_STRINGS:
  77. raise RuntimeError('Platform "%s" is not supported' % platform)
  78. return PLATFORM_STRINGS[platform]
  79. def __init__(self, policy, chrome_major_version, deprecation_milestone_buffer,
  80. target_platform, valid_tags):
  81. self.id = policy['id']
  82. self.name = policy['name']
  83. self.tags = policy.get('tags', None)
  84. self._CheckTagsValidity(valid_tags)
  85. features = policy.get('features', {})
  86. self.can_be_recommended = features.get('can_be_recommended', False)
  87. self.can_be_mandatory = features.get('can_be_mandatory', True)
  88. self.internal_only = features.get('internal_only', False)
  89. self.metapolicy_type = features.get('metapolicy_type', '')
  90. self.is_deprecated = policy.get('deprecated', False)
  91. self.is_device_only = policy.get('device_only', False)
  92. self.per_profile = features.get('per_profile', False)
  93. self.supported_chrome_os_management = policy.get(
  94. 'supported_chrome_os_management', ['active_directory', 'google_cloud'])
  95. self.schema = policy['schema']
  96. self.validation_schema = policy.get('validation_schema')
  97. self.has_enterprise_default = 'default_for_enterprise_users' in policy
  98. if self.has_enterprise_default:
  99. self.enterprise_default = policy['default_for_enterprise_users']
  100. if self.has_enterprise_default:
  101. self.default_policy_level = policy.get('default_policy_level', '')
  102. if (self.default_policy_level == 'recommended'
  103. and not self.can_be_recommended):
  104. raise RuntimeError('Policy ' + self.name +
  105. ' has default_policy_level set to ' +
  106. self.default_policy_level + ', '
  107. 'but can_be_recommended feature is not set to True')
  108. self.cloud_only = features.get('cloud_only', False)
  109. self.platforms = set()
  110. self.future_on = set()
  111. for platform, version_range in map(lambda s: s.split(':'),
  112. policy.get('supported_on', [])):
  113. split_result = version_range.split('-')
  114. if len(split_result) != 2:
  115. raise RuntimeError('supported_on must have exactly one dash: "%s"' % p)
  116. (version_min, version_max) = split_result
  117. if version_min == '':
  118. raise RuntimeError('supported_on must define a start version: "%s"' % p)
  119. # Skip if filtering by Chromium version and the current Chromium version
  120. # does not support the policy.
  121. if chrome_major_version:
  122. if (int(version_min) > chrome_major_version
  123. or version_max != '' and int(version_max) <
  124. chrome_major_version - deprecation_milestone_buffer):
  125. continue
  126. self.platforms.update(self._ConvertPlatform(platform))
  127. for platform in policy.get('future_on', []):
  128. self.future_on.update(self._ConvertPlatform(platform))
  129. if self.is_device_only and self.platforms.union(self.future_on) > set(
  130. ['chrome_os']):
  131. raise RuntimeError('device_only is only allowed for Chrome OS: "%s"' %
  132. self.name)
  133. self.is_supported = (target_platform in self.platforms
  134. or target_platform in self.future_on)
  135. self.is_future = target_platform in self.future_on
  136. if policy['type'] not in PolicyDetails.TYPE_MAP:
  137. raise NotImplementedError(
  138. 'Unknown policy type for %s: %s' % (policy['name'], policy['type']))
  139. self.policy_type, self.protobuf_type, self.policy_protobuf_type, \
  140. self.restriction_type = PolicyDetails.TYPE_MAP[policy['type']]
  141. self.desc = '\n'.join(
  142. map(str.strip,
  143. PolicyDetails._RemovePlaceholders(policy['desc']).splitlines()))
  144. self.caption = PolicyDetails._RemovePlaceholders(policy['caption'])
  145. self.max_size = policy.get('max_size', 0)
  146. items = policy.get('items')
  147. if items is None:
  148. self.items = None
  149. else:
  150. self.items = [PolicyDetails.EnumItem(entry) for entry in items]
  151. PH_PATTERN = re.compile('<ph[^>]*>([^<]*|[^<]*<ex>([^<]*)</ex>[^<]*)</ph>')
  152. def _CheckTagsValidity(self, valid_tags):
  153. if self.tags == None:
  154. raise RuntimeError('Policy ' + self.name + ' has to contain a list of '
  155. 'tags!\n An empty list is also valid but means '
  156. 'setting this policy can never harm the user\'s '
  157. 'privacy or security.\n')
  158. for tag in self.tags:
  159. if not tag in valid_tags:
  160. raise RuntimeError('Invalid Tag:' + tag + '!\n'
  161. 'Chose a valid tag from \'risk_tag_definitions\' (a '
  162. 'subproperty of root in policy_templates.json)!')
  163. # Simplistic grit placeholder stripper.
  164. @staticmethod
  165. def _RemovePlaceholders(text):
  166. result = ''
  167. pos = 0
  168. for m in PolicyDetails.PH_PATTERN.finditer(text):
  169. result += text[pos:m.start(0)]
  170. result += m.group(2) or m.group(1)
  171. pos = m.end(0)
  172. result += text[pos:]
  173. return result
  174. class PolicyAtomicGroup:
  175. """Parses a policy atomic group and caches its name and policy names"""
  176. def __init__(self, policy_group, available_policies,
  177. policies_already_in_group):
  178. self.id = policy_group['id']
  179. self.name = policy_group['name']
  180. self.policies = policy_group.get('policies', None)
  181. self._CheckPoliciesValidity(available_policies, policies_already_in_group)
  182. def _CheckPoliciesValidity(self, available_policies,
  183. policies_already_in_group):
  184. if self.policies == None or len(self.policies) <= 0:
  185. raise RuntimeError('Atomic policy group ' + self.name +
  186. ' has to contain a list of '
  187. 'policies!\n')
  188. for policy in self.policies:
  189. if policy in policies_already_in_group:
  190. raise RuntimeError('Policy: ' + policy +
  191. ' cannot be in more than one atomic group '
  192. 'in policy_templates.json)!')
  193. policies_already_in_group.add(policy)
  194. if not policy in available_policies:
  195. raise RuntimeError('Invalid policy: ' + policy + ' in atomic group ' +
  196. self.name + '.\n')
  197. def ParseVersionFile(version_path):
  198. chrome_major_version = None
  199. for line in open(version_path, 'r').readlines():
  200. key, val = line.rstrip('\r\n').split('=', 1)
  201. if key == 'MAJOR':
  202. chrome_major_version = val
  203. break
  204. if chrome_major_version is None:
  205. raise RuntimeError('VERSION file does not contain major version.')
  206. return int(chrome_major_version)
  207. def main():
  208. parser = ArgumentParser(usage=__doc__)
  209. parser.add_argument(
  210. '--pch',
  211. '--policy-constants-header',
  212. dest='header_path',
  213. help='generate header file of policy constants',
  214. metavar='FILE')
  215. parser.add_argument(
  216. '--pcc',
  217. '--policy-constants-source',
  218. dest='source_path',
  219. help='generate source file of policy constants',
  220. metavar='FILE')
  221. parser.add_argument(
  222. '--cpp',
  223. '--cloud-policy-protobuf',
  224. dest='cloud_policy_proto_path',
  225. help='generate cloud policy protobuf file',
  226. metavar='FILE')
  227. parser.add_argument(
  228. '--cpfrp',
  229. '--cloud-policy-full-runtime-protobuf',
  230. dest='cloud_policy_full_runtime_proto_path',
  231. help='generate cloud policy full runtime protobuf',
  232. metavar='FILE')
  233. parser.add_argument(
  234. '--csp',
  235. '--chrome-settings-protobuf',
  236. dest='chrome_settings_proto_path',
  237. help='generate chrome settings protobuf file',
  238. metavar='FILE')
  239. parser.add_argument(
  240. '--policy-common-definitions-protobuf',
  241. dest='policy_common_definitions_proto_path',
  242. help='policy common definitions protobuf file path',
  243. metavar='FILE')
  244. parser.add_argument(
  245. '--policy-common-definitions-full-runtime-protobuf',
  246. dest='policy_common_definitions_full_runtime_proto_path',
  247. help='generate policy common definitions full runtime protobuf file',
  248. metavar='FILE')
  249. parser.add_argument(
  250. '--csfrp',
  251. '--chrome-settings-full-runtime-protobuf',
  252. dest='chrome_settings_full_runtime_proto_path',
  253. help='generate chrome settings full runtime protobuf',
  254. metavar='FILE')
  255. parser.add_argument(
  256. '--ard',
  257. '--app-restrictions-definition',
  258. dest='app_restrictions_path',
  259. help='generate an XML file as specified by '
  260. 'Android\'s App Restriction Schema',
  261. metavar='FILE')
  262. parser.add_argument(
  263. '--rth',
  264. '--risk-tag-header',
  265. dest='risk_header_path',
  266. help='generate header file for policy risk tags',
  267. metavar='FILE')
  268. parser.add_argument(
  269. '--crospch',
  270. '--cros-policy-constants-header',
  271. dest='cros_constants_header_path',
  272. help='generate header file of policy constants for use in '
  273. 'Chrome OS',
  274. metavar='FILE')
  275. parser.add_argument(
  276. '--crospcc',
  277. '--cros-policy-constants-source',
  278. dest='cros_constants_source_path',
  279. help='generate source file of policy constants for use in '
  280. 'Chrome OS',
  281. metavar='FILE')
  282. parser.add_argument(
  283. '--chrome-version-file',
  284. dest='chrome_version_file',
  285. help='path to src/chrome/VERSION',
  286. metavar='FILE')
  287. parser.add_argument(
  288. '--all-chrome-versions',
  289. action='store_true',
  290. dest='all_chrome_versions',
  291. default=False,
  292. help='do not restrict generated policies by chrome version')
  293. parser.add_argument(
  294. '--target-platform',
  295. dest='target_platform',
  296. help='the platform the generated code should run on - can be one of'
  297. '(win, mac, linux, chromeos, fuchsia)',
  298. metavar='PLATFORM')
  299. parser.add_argument(
  300. '--policy-templates-file',
  301. dest='policy_templates_file',
  302. help='path to the policy_templates.json input file',
  303. metavar='FILE')
  304. parser.add_argument(
  305. '--deprecation-milestone-buffer',
  306. dest='deprecation_milestone_buffer',
  307. type=int,
  308. help='Number of major versions before a code for a policy stops being '
  309. 'generated',
  310. default=2)
  311. args = parser.parse_args()
  312. has_arg_error = False
  313. if not args.target_platform:
  314. print('Error: Missing --target-platform=<platform>')
  315. has_arg_error = True
  316. if not args.policy_templates_file:
  317. print('Error: Missing'
  318. ' --policy-templates-file=<path to policy_templates.json>')
  319. has_arg_error = True
  320. if not args.chrome_version_file and not args.all_chrome_versions:
  321. print('Error: Missing'
  322. ' --chrome-version-file=<path to src/chrome/VERSION>\n'
  323. ' or --all-chrome-versions')
  324. has_arg_error = True
  325. if has_arg_error:
  326. print('')
  327. parser.print_help()
  328. return 2
  329. version_path = args.chrome_version_file
  330. target_platform = args.target_platform
  331. template_file_name = args.policy_templates_file
  332. deprecation_milestone_buffer = int(args.deprecation_milestone_buffer)
  333. # --target-platform accepts "chromeos" as its input because that's what is
  334. # used within GN. Within policy templates, "chrome_os" is used instead.
  335. if target_platform == 'chromeos':
  336. target_platform = 'chrome_os'
  337. if args.all_chrome_versions:
  338. chrome_major_version = None
  339. else:
  340. chrome_major_version = ParseVersionFile(version_path)
  341. template_file_contents = _LoadJSONFile(template_file_name)
  342. risk_tags = RiskTags(template_file_contents)
  343. policy_details = [
  344. PolicyDetails(policy, chrome_major_version, deprecation_milestone_buffer,
  345. target_platform, risk_tags.GetValidTags())
  346. for policy in template_file_contents['policy_definitions']
  347. if policy['type'] != 'group'
  348. ]
  349. risk_tags.ComputeMaxTags(policy_details)
  350. sorted_policy_details = sorted(policy_details, key=lambda policy: policy.name)
  351. policy_details_set = list(map((lambda x: x.name), policy_details))
  352. policies_already_in_group = set()
  353. policy_atomic_groups = [
  354. PolicyAtomicGroup(group, policy_details_set, policies_already_in_group)
  355. for group in template_file_contents['policy_atomic_group_definitions']
  356. ]
  357. sorted_policy_atomic_groups = sorted(
  358. policy_atomic_groups, key=lambda group: group.name)
  359. def GenerateFile(path, writer, sorted=False, xml=False):
  360. if path:
  361. with codecs.open(path, 'w', encoding='utf-8') as f:
  362. _OutputGeneratedWarningHeader(f, template_file_name, xml)
  363. writer(sorted and sorted_policy_details or policy_details,
  364. sorted and sorted_policy_atomic_groups or policy_atomic_groups,
  365. target_platform, f, risk_tags)
  366. if args.header_path:
  367. GenerateFile(args.header_path, _WritePolicyConstantHeader, sorted=True)
  368. if args.source_path:
  369. GenerateFile(args.source_path, _WritePolicyConstantSource, sorted=True)
  370. if args.risk_header_path:
  371. GenerateFile(args.risk_header_path, _WritePolicyRiskTagHeader)
  372. if args.cloud_policy_proto_path:
  373. GenerateFile(args.cloud_policy_proto_path, _WriteCloudPolicyProtobuf)
  374. if (args.policy_common_definitions_full_runtime_proto_path and
  375. args.policy_common_definitions_proto_path):
  376. GenerateFile(
  377. args.policy_common_definitions_full_runtime_proto_path,
  378. partial(_WritePolicyCommonDefinitionsFullRuntimeProtobuf,
  379. args.policy_common_definitions_proto_path))
  380. if args.cloud_policy_full_runtime_proto_path:
  381. GenerateFile(args.cloud_policy_full_runtime_proto_path,
  382. partial(_WriteCloudPolicyProtobuf, is_full_runtime=True))
  383. if args.chrome_settings_proto_path:
  384. GenerateFile(args.chrome_settings_proto_path, _WriteChromeSettingsProtobuf)
  385. if args.chrome_settings_full_runtime_proto_path:
  386. GenerateFile(args.chrome_settings_full_runtime_proto_path,
  387. partial(_WriteChromeSettingsProtobuf, is_full_runtime=True))
  388. if target_platform == 'android' and args.app_restrictions_path:
  389. GenerateFile(args.app_restrictions_path, _WriteAppRestrictions, xml=True)
  390. # Generated code for Chrome OS (unused in Chromium).
  391. if args.cros_constants_header_path:
  392. GenerateFile(
  393. args.cros_constants_header_path,
  394. _WriteChromeOSPolicyConstantsHeader,
  395. sorted=True)
  396. if args.cros_constants_source_path:
  397. GenerateFile(
  398. args.cros_constants_source_path,
  399. _WriteChromeOSPolicyConstantsSource,
  400. sorted=True)
  401. return 0
  402. #------------------ shared helpers ---------------------------------#
  403. def _OutputGeneratedWarningHeader(f, template_file_path, xml_style):
  404. left_margin = '//'
  405. if xml_style:
  406. left_margin = ' '
  407. f.write('<?xml version="1.0" encoding="utf-8"?>\n' '<!--\n')
  408. else:
  409. f.write('//\n')
  410. f.write(left_margin + ' DO NOT MODIFY THIS FILE DIRECTLY!\n')
  411. f.write(left_margin + ' IT IS GENERATED BY generate_policy_source.py\n')
  412. f.write(left_margin + ' FROM ' + template_file_path + '\n')
  413. if xml_style:
  414. f.write('-->\n\n')
  415. else:
  416. f.write(left_margin + '\n\n')
  417. COMMENT_WRAPPER = textwrap.TextWrapper()
  418. COMMENT_WRAPPER.width = 80
  419. COMMENT_WRAPPER.initial_indent = '// '
  420. COMMENT_WRAPPER.subsequent_indent = '// '
  421. COMMENT_WRAPPER.replace_whitespace = False
  422. # Writes a comment, each line prefixed by // and wrapped to 80 spaces.
  423. def _OutputComment(f, comment):
  424. for line in six.ensure_text(comment).splitlines():
  425. if len(line) == 0:
  426. f.write('//')
  427. else:
  428. f.write(COMMENT_WRAPPER.fill(line))
  429. f.write('\n')
  430. def _LoadJSONFile(json_file):
  431. with codecs.open(json_file, 'r', encoding='utf-8') as f:
  432. return json.load(f)
  433. def _GetSupportedChromeUserPolicies(policies, protobuf_type):
  434. return [
  435. p for p in policies if p.is_supported and not p.is_device_only
  436. and p.policy_protobuf_type == protobuf_type
  437. ]
  438. # Returns the policies supported by at least one platform.
  439. # Ensure only windows supported policies are returned when building for windows.
  440. # Eventually only supported policies on every platforms will be returned.
  441. def _GetSupportedPolicies(policies, target_platform):
  442. # TODO(crbug.com/1348959): Remove this special case once deprecated policies
  443. # have been removed for fuchsia
  444. if target_platform == 'fuchsia':
  445. is_deprecated = lambda policy: len(policy.platforms) + len(
  446. policy.future_on) > 0 and policy.is_deprecated
  447. return [
  448. policy for policy in policies
  449. if (policy.is_supported or is_deprecated(policy))
  450. ]
  451. return [policy for policy in policies if policy.is_supported]
  452. #------------------ policy constants header ------------------------#
  453. # Return a list of all policies of type |metapolicy_type|.
  454. def _GetMetapoliciesOfType(policies, metapolicy_type):
  455. return [
  456. policy for policy in policies if policy.metapolicy_type == metapolicy_type
  457. ]
  458. def _WritePolicyConstantHeader(all_policies, policy_atomic_groups,
  459. target_platform, f, risk_tags):
  460. policies = _GetSupportedPolicies(all_policies, target_platform)
  461. f.write('''#ifndef COMPONENTS_POLICY_POLICY_CONSTANTS_H_
  462. #define COMPONENTS_POLICY_POLICY_CONSTANTS_H_
  463. #include <cstdint>
  464. #include <string>
  465. #include "components/policy/core/common/policy_details.h"
  466. #include "components/policy/core/common/policy_map.h"
  467. namespace enterprise_management {
  468. class BooleanPolicyProto;
  469. class CloudPolicySettings;
  470. class IntegerPolicyProto;
  471. class StringListPolicyProto;
  472. class StringPolicyProto;
  473. }
  474. namespace em = enterprise_management;
  475. namespace policy {
  476. namespace internal {
  477. struct SchemaData;
  478. }
  479. ''')
  480. if target_platform == 'win':
  481. f.write('// The windows registry path where Chrome policy '
  482. 'configuration resides.\n'
  483. 'extern const wchar_t kRegistryChromePolicyKey[];\n')
  484. f.write('''#if BUILDFLAG(IS_CHROMEOS)
  485. // Sets default profile policies values for enterprise users.
  486. void SetEnterpriseUsersProfileDefaults(PolicyMap* policy_map);
  487. // Sets default system-wide policies values for enterprise users.
  488. void SetEnterpriseUsersSystemWideDefaults(PolicyMap* policy_map);
  489. // Sets all default values for enterprise users.
  490. void SetEnterpriseUsersDefaults(PolicyMap* policy_map);
  491. #endif
  492. // Returns the PolicyDetails for |policy| if |policy| is a known
  493. // Chrome policy, otherwise returns nullptr.
  494. const PolicyDetails* GetChromePolicyDetails(
  495. const std::string& policy);
  496. // Returns the schema data of the Chrome policy schema.
  497. const internal::SchemaData* GetChromeSchemaData();
  498. ''')
  499. f.write('// Key names for the policy settings.\n' 'namespace key {\n\n')
  500. for policy in policies:
  501. # TODO(joaodasilva): Include only supported policies in
  502. # configuration_policy_handler.cc and configuration_policy_handler_list.cc
  503. # so that these names can be conditional on 'policy.is_supported'.
  504. # http://crbug.com/223616
  505. f.write('extern const char k' + policy.name + '[];\n')
  506. f.write('\n} // namespace key\n\n')
  507. f.write('// Group names for the policy settings.\n' 'namespace group {\n\n')
  508. for group in policy_atomic_groups:
  509. f.write('extern const char k' + group.name + '[];\n')
  510. f.write('\n} // namespace group\n\n')
  511. f.write('struct AtomicGroup {\n'
  512. ' const short id;\n'
  513. ' const char* policy_group;\n'
  514. ' const char* const* policies;\n'
  515. '};\n\n')
  516. f.write('extern const AtomicGroup kPolicyAtomicGroupMappings[];\n\n')
  517. f.write('extern const size_t kPolicyAtomicGroupMappingsLength;\n\n')
  518. # Declare arrays of metapolicies.
  519. f.write('// Arrays of metapolicies.\n' 'namespace metapolicy {\n\n')
  520. f.write('extern const char* const kMerge[%s];\n' %
  521. len(_GetMetapoliciesOfType(policies, METAPOLICY_TYPE['merge'])))
  522. f.write('extern const char* const kPrecedence[%s];\n\n' %
  523. len(_GetMetapoliciesOfType(policies, METAPOLICY_TYPE['precedence'])))
  524. f.write('} // namespace metapolicy\n\n')
  525. f.write('enum class StringPolicyType {\n'
  526. ' STRING,\n'
  527. ' JSON,\n'
  528. ' EXTERNAL,\n'
  529. '};\n\n')
  530. # User policy proto pointers, one struct for each protobuf type.
  531. protobuf_types = _GetProtobufTypes()
  532. for protobuf_type in protobuf_types:
  533. _WriteChromePolicyAccessHeader(policies, f, protobuf_type)
  534. f.write('constexpr int64_t kDevicePolicyExternalDataResourceCacheSize = %d;\n'
  535. % _ComputeTotalDevicePolicyExternalDataMaxSize(policies))
  536. f.write('\n} // namespace policy\n\n'
  537. '#endif // COMPONENTS_POLICY_POLICY_CONSTANTS_H_\n')
  538. def _WriteChromePolicyAccessHeader(policies, f, protobuf_type):
  539. supported_user_policies = _GetSupportedChromeUserPolicies(
  540. policies, protobuf_type)
  541. f.write('// Read access to the protobufs of all supported %s user policies.\n'
  542. % protobuf_type.lower())
  543. f.write('struct %sPolicyAccess {\n' % protobuf_type)
  544. f.write(' const char* policy_key;\n'
  545. ' bool per_profile;\n'
  546. ' bool (*has_proto)(const em::CloudPolicySettings& policy);\n'
  547. ' const em::%sPolicyProto& (*get_proto)(\n'
  548. ' const em::CloudPolicySettings& policy);\n' % protobuf_type)
  549. if protobuf_type == 'String':
  550. f.write(' const StringPolicyType type;\n')
  551. f.write('};\n')
  552. f.write('extern const std::array<%sPolicyAccess, %d> k%sPolicyAccess;\n\n' %
  553. (protobuf_type, len(supported_user_policies), protobuf_type))
  554. def _ComputeTotalDevicePolicyExternalDataMaxSize(policies):
  555. total_device_policy_external_data_max_size = 0
  556. for policy in policies:
  557. if policy.is_device_only and policy.policy_type == 'TYPE_EXTERNAL':
  558. total_device_policy_external_data_max_size += policy.max_size
  559. return total_device_policy_external_data_max_size
  560. #------------------ policy constants source ------------------------#
  561. SchemaNodeKey = namedtuple('SchemaNodeKey',
  562. 'schema_type extra is_sensitive_value')
  563. SchemaNode = namedtuple(
  564. 'SchemaNode',
  565. 'schema_type extra is_sensitive_value has_sensitive_children comments')
  566. PropertyNode = namedtuple('PropertyNode', 'key schema')
  567. PropertiesNode = namedtuple(
  568. 'PropertiesNode',
  569. 'begin end pattern_end required_begin required_end additional name')
  570. RestrictionNode = namedtuple('RestrictionNode', 'first second')
  571. # A mapping of the simple schema types to base::Value::Types.
  572. SIMPLE_SCHEMA_NAME_MAP = {
  573. 'boolean': 'Type::BOOLEAN',
  574. 'integer': 'Type::INTEGER',
  575. 'null': 'Type::NONE',
  576. 'number': 'Type::DOUBLE',
  577. 'string': 'Type::STRING',
  578. }
  579. METAPOLICY_TYPE = {
  580. 'merge': 'merge',
  581. 'precedence': 'precedence',
  582. }
  583. INVALID_INDEX = -1
  584. MIN_INDEX = -1
  585. MAX_INDEX = (1 << 15) - 1 # signed short in c++
  586. MIN_POLICY_ID = 0
  587. MAX_POLICY_ID = (1 << 16) - 1 # unsigned short
  588. MIN_EXTERNAL_DATA_SIZE = 0
  589. MAX_EXTERNAL_DATA_SIZE = (1 << 32) - 1 # unsigned int32
  590. class SchemaNodesGenerator:
  591. """Builds the internal structs to represent a JSON schema."""
  592. def __init__(self, shared_strings):
  593. """Creates a new generator.
  594. |shared_strings| is a map of strings to a C expression that evaluates to
  595. that string at runtime. This mapping can be used to reuse existing string
  596. constants."""
  597. self.shared_strings = shared_strings
  598. self.key_index_map = {} # |SchemaNodeKey| -> index in |schema_nodes|
  599. self.schema_nodes = [] # List of |SchemaNode|s
  600. self.property_nodes = [] # List of |PropertyNode|s
  601. self.properties_nodes = [] # List of |PropertiesNode|s
  602. self.restriction_nodes = [] # List of |RestrictionNode|s
  603. self.required_properties = []
  604. self.int_enums = []
  605. self.string_enums = []
  606. self.ranges = {}
  607. self.id_map = {}
  608. def GetString(self, s):
  609. if s in self.shared_strings:
  610. return self.shared_strings[s]
  611. # Generate JSON escaped string, which is slightly different from desired
  612. # C/C++ escaped string. Known differences includes unicode escaping format.
  613. return json.dumps(s)
  614. def AppendSchema(self, schema_type, extra, is_sensitive_value, comment=''):
  615. # Find existing schema node with same structure.
  616. key_node = SchemaNodeKey(schema_type, extra, is_sensitive_value)
  617. if key_node in self.key_index_map:
  618. index = self.key_index_map[key_node]
  619. if comment:
  620. self.schema_nodes[index].comments.add(comment)
  621. return index
  622. # Create new schema node.
  623. index = len(self.schema_nodes)
  624. comments = {comment} if comment else set()
  625. schema_node = SchemaNode(schema_type, extra, is_sensitive_value, False,
  626. comments)
  627. self.schema_nodes.append(schema_node)
  628. self.key_index_map[key_node] = index
  629. return index
  630. def AppendRestriction(self, first, second):
  631. r = RestrictionNode(str(first), str(second))
  632. if not r in self.ranges:
  633. self.ranges[r] = len(self.restriction_nodes)
  634. self.restriction_nodes.append(r)
  635. return self.ranges[r]
  636. def GetSimpleType(self, name, is_sensitive_value):
  637. return self.AppendSchema(SIMPLE_SCHEMA_NAME_MAP[name], INVALID_INDEX,
  638. is_sensitive_value, 'simple type: ' + name)
  639. def SchemaHaveRestriction(self, schema):
  640. return any(keyword in schema
  641. for keyword in ['minimum', 'maximum', 'enum', 'pattern'])
  642. def IsConsecutiveInterval(self, seq):
  643. sortedSeq = sorted(seq)
  644. return all(
  645. sortedSeq[i] + 1 == sortedSeq[i + 1] for i in range(len(sortedSeq) - 1))
  646. def GetEnumIntegerType(self, schema, is_sensitive_value, name):
  647. assert all(type(x) == int for x in schema['enum'])
  648. possible_values = schema['enum']
  649. if self.IsConsecutiveInterval(possible_values):
  650. index = self.AppendRestriction(max(possible_values), min(possible_values))
  651. return self.AppendSchema(
  652. 'Type::INTEGER', index, is_sensitive_value,
  653. 'integer with enumeration restriction (use range instead): %s' % name)
  654. offset_begin = len(self.int_enums)
  655. self.int_enums += possible_values
  656. offset_end = len(self.int_enums)
  657. return self.AppendSchema('Type::INTEGER',
  658. self.AppendRestriction(offset_begin, offset_end),
  659. is_sensitive_value,
  660. 'integer with enumeration restriction: %s' % name)
  661. def GetEnumStringType(self, schema, is_sensitive_value, name):
  662. assert all(type(x) == str for x in schema['enum'])
  663. offset_begin = len(self.string_enums)
  664. self.string_enums += schema['enum']
  665. offset_end = len(self.string_enums)
  666. return self.AppendSchema('Type::STRING',
  667. self.AppendRestriction(offset_begin, offset_end),
  668. is_sensitive_value,
  669. 'string with enumeration restriction: %s' % name)
  670. def GetEnumType(self, schema, is_sensitive_value, name):
  671. if len(schema['enum']) == 0:
  672. raise RuntimeError('Empty enumeration in %s' % name)
  673. elif schema['type'] == 'integer':
  674. return self.GetEnumIntegerType(schema, is_sensitive_value, name)
  675. elif schema['type'] == 'string':
  676. return self.GetEnumStringType(schema, is_sensitive_value, name)
  677. else:
  678. raise RuntimeError('Unknown enumeration type in %s' % name)
  679. def GetPatternType(self, schema, is_sensitive_value, name):
  680. if schema['type'] != 'string':
  681. raise RuntimeError('Unknown pattern type in %s' % name)
  682. pattern = schema['pattern']
  683. # Try to compile the pattern to validate it, note that the syntax used
  684. # here might be slightly different from re2.
  685. # TODO(binjin): Add a python wrapper of re2 and use it here.
  686. re.compile(pattern)
  687. index = len(self.string_enums)
  688. self.string_enums.append(pattern)
  689. return self.AppendSchema('Type::STRING', self.AppendRestriction(
  690. index, index), is_sensitive_value,
  691. 'string with pattern restriction: %s' % name)
  692. def GetRangedType(self, schema, is_sensitive_value, name):
  693. if schema['type'] != 'integer':
  694. raise RuntimeError('Unknown ranged type in %s' % name)
  695. min_value_set, max_value_set = False, False
  696. if 'minimum' in schema:
  697. min_value = int(schema['minimum'])
  698. min_value_set = True
  699. if 'maximum' in schema:
  700. max_value = int(schema['maximum'])
  701. max_value_set = True
  702. if min_value_set and max_value_set and min_value > max_value:
  703. raise RuntimeError('Invalid ranged type in %s' % name)
  704. index = self.AppendRestriction(
  705. str(max_value) if max_value_set else 'INT_MAX',
  706. str(min_value) if min_value_set else 'INT_MIN')
  707. return self.AppendSchema('Type::INTEGER', index, is_sensitive_value,
  708. 'integer with ranged restriction: %s' % name)
  709. def Generate(self, schema, name):
  710. """Generates the structs for the given schema.
  711. |schema|: a valid JSON schema in a dictionary.
  712. |name|: the name of the current node, for the generated comments."""
  713. if '$ref' in schema:
  714. if 'id' in schema:
  715. raise RuntimeError("Schemas with a $ref can't have an id")
  716. if not isinstance(schema['$ref'], string_type):
  717. raise RuntimeError("$ref attribute must be a string")
  718. return schema['$ref']
  719. is_sensitive_value = schema.get('sensitiveValue', False)
  720. assert type(is_sensitive_value) is bool
  721. if schema['type'] in SIMPLE_SCHEMA_NAME_MAP:
  722. if not self.SchemaHaveRestriction(schema):
  723. # Simple types use shared nodes.
  724. return self.GetSimpleType(schema['type'], is_sensitive_value)
  725. elif 'enum' in schema:
  726. return self.GetEnumType(schema, is_sensitive_value, name)
  727. elif 'pattern' in schema:
  728. return self.GetPatternType(schema, is_sensitive_value, name)
  729. else:
  730. return self.GetRangedType(schema, is_sensitive_value, name)
  731. if schema['type'] == 'array':
  732. return self.AppendSchema(
  733. 'Type::LIST',
  734. self.GenerateAndCollectID(schema['items'], 'items of ' + name),
  735. is_sensitive_value)
  736. elif schema['type'] == 'object':
  737. # Reserve an index first, so that dictionaries come before their
  738. # properties. This makes sure that the root node is the first in the
  739. # SchemaNodes array.
  740. # This however, prevents de-duplication for object schemas since we could
  741. # only determine duplicates after all child schema nodes are generated as
  742. # well and then we couldn't remove the newly created schema node without
  743. # invalidating all child schema indices.
  744. index = len(self.schema_nodes)
  745. self.schema_nodes.append(
  746. SchemaNode('Type::DICTIONARY', INVALID_INDEX, is_sensitive_value,
  747. False, {name}))
  748. if 'additionalProperties' in schema:
  749. additionalProperties = self.GenerateAndCollectID(
  750. schema['additionalProperties'], 'additionalProperties of ' + name)
  751. else:
  752. additionalProperties = INVALID_INDEX
  753. # Properties must be sorted by name, for the binary search lookup.
  754. # Note that |properties| must be evaluated immediately, so that all the
  755. # recursive calls to Generate() append the necessary child nodes; if
  756. # |properties| were a generator then this wouldn't work.
  757. sorted_properties = sorted(schema.get('properties', {}).items())
  758. properties = [
  759. PropertyNode(
  760. self.GetString(key), self.GenerateAndCollectID(subschema, key))
  761. for key, subschema in sorted_properties
  762. ]
  763. pattern_properties = []
  764. for pattern, subschema in schema.get('patternProperties', {}).items():
  765. pattern_properties.append(
  766. PropertyNode(
  767. self.GetString(pattern),
  768. self.GenerateAndCollectID(subschema, pattern)))
  769. begin = len(self.property_nodes)
  770. self.property_nodes += properties
  771. end = len(self.property_nodes)
  772. self.property_nodes += pattern_properties
  773. pattern_end = len(self.property_nodes)
  774. if index == 0:
  775. self.root_properties_begin = begin
  776. self.root_properties_end = end
  777. required_begin = len(self.required_properties)
  778. required_properties = schema.get('required', [])
  779. assert type(required_properties) is list
  780. assert all(type(x) == str for x in required_properties)
  781. self.required_properties += required_properties
  782. required_end = len(self.required_properties)
  783. # Check that each string in |required_properties| is in |properties|.
  784. properties = schema.get('properties', {})
  785. for name in required_properties:
  786. assert name in properties
  787. extra = len(self.properties_nodes)
  788. self.properties_nodes.append(
  789. PropertiesNode(begin, end, pattern_end, required_begin, required_end,
  790. additionalProperties, name))
  791. # Update index at |extra| now, since that was filled with a dummy value
  792. # when the schema node was created.
  793. self.schema_nodes[index] = self.schema_nodes[index]._replace(extra=extra)
  794. return index
  795. else:
  796. assert False
  797. def GenerateAndCollectID(self, schema, name):
  798. """A wrapper of Generate(), will take the return value, check and add 'id'
  799. attribute to self.id_map. The wrapper needs to be used for every call to
  800. Generate().
  801. """
  802. index = self.Generate(schema, name)
  803. if 'id' not in schema:
  804. return index
  805. id_str = schema['id']
  806. if id_str in self.id_map:
  807. raise RuntimeError('Duplicated id: ' + id_str)
  808. self.id_map[id_str] = index
  809. return index
  810. def Write(self, f):
  811. """Writes the generated structs to the given file.
  812. |f| an open file to write to."""
  813. f.write('const internal::SchemaNode kSchemas[] = {\n'
  814. '// Type' + ' ' * 27 +
  815. 'Extra IsSensitiveValue HasSensitiveChildren\n')
  816. for schema_node in self.schema_nodes:
  817. assert schema_node.extra >= MIN_INDEX and schema_node.extra <= MAX_INDEX
  818. comment = ('\n' + ' ' * 69 + '// ').join(sorted(schema_node.comments))
  819. f.write(' { base::Value::%-19s %4s %-16s %-5s }, // %s\n' %
  820. (schema_node.schema_type + ',', str(schema_node.extra) + ',',
  821. str(schema_node.is_sensitive_value).lower() + ',',
  822. str(schema_node.has_sensitive_children).lower(), comment))
  823. f.write('};\n\n')
  824. if self.property_nodes:
  825. f.write('const internal::PropertyNode kPropertyNodes[] = {\n'
  826. '// Property' + ' ' * 61 + 'Schema\n')
  827. for property_node in self.property_nodes:
  828. f.write(' { %-64s %6d },\n' % (property_node.key + ',',
  829. property_node.schema))
  830. f.write('};\n\n')
  831. if self.properties_nodes:
  832. f.write('const internal::PropertiesNode kProperties[] = {\n'
  833. '// Begin End PatternEnd RequiredBegin RequiredEnd'
  834. ' Additional Properties\n')
  835. for properties_node in self.properties_nodes:
  836. for i in range(0, len(properties_node) - 1):
  837. assert (properties_node[i] >= MIN_INDEX and
  838. properties_node[i] <= MAX_INDEX)
  839. f.write(
  840. ' { %5d, %5d, %5d, %5d, %10d, %5d }, // %s\n' % properties_node)
  841. f.write('};\n\n')
  842. if self.restriction_nodes:
  843. f.write('const internal::RestrictionNode kRestrictionNodes[] = {\n')
  844. f.write('// FIRST, SECOND\n')
  845. for restriction_node in self.restriction_nodes:
  846. f.write(' {{ %-8s %4s}},\n' % (restriction_node.first + ',',
  847. restriction_node.second))
  848. f.write('};\n\n')
  849. if self.required_properties:
  850. f.write('const char* const kRequiredProperties[] = {\n')
  851. for required_property in self.required_properties:
  852. f.write(' %s,\n' % self.GetString(required_property))
  853. f.write('};\n\n')
  854. if self.int_enums:
  855. f.write('const int kIntegerEnumerations[] = {\n')
  856. for possible_values in self.int_enums:
  857. f.write(' %d,\n' % possible_values)
  858. f.write('};\n\n')
  859. if self.string_enums:
  860. f.write('const char* const kStringEnumerations[] = {\n')
  861. for possible_values in self.string_enums:
  862. f.write(' %s,\n' % self.GetString(possible_values))
  863. f.write('};\n\n')
  864. f.write('const internal::SchemaData* GetChromeSchemaData() {\n')
  865. f.write(' static const internal::SchemaData kChromeSchemaData = {\n'
  866. ' kSchemas,\n')
  867. f.write(' kPropertyNodes,\n' if self.property_nodes else ' nullptr,\n')
  868. f.write(' kProperties,\n' if self.properties_nodes else ' nullptr,\n')
  869. f.write(' kRestrictionNodes,\n' if self.
  870. restriction_nodes else ' nullptr,\n')
  871. f.write(' kRequiredProperties,\n' if self.
  872. required_properties else ' nullptr,\n')
  873. f.write(' kIntegerEnumerations,\n' if self.int_enums else ' nullptr,\n')
  874. f.write(
  875. ' kStringEnumerations,\n' if self.string_enums else ' nullptr,\n')
  876. f.write(' %d, // validation_schema root index\n' %
  877. self.validation_schema_root_index)
  878. f.write(' };\n\n')
  879. f.write(' return &kChromeSchemaData;\n' '}\n\n')
  880. def GetByID(self, id_str):
  881. if not isinstance(id_str, string_type):
  882. return id_str
  883. if id_str not in self.id_map:
  884. raise RuntimeError('Invalid $ref: ' + id_str)
  885. return self.id_map[id_str]
  886. def ResolveID(self, index, tuple_type, params):
  887. simple_tuple = params[:index] + (self.GetByID(
  888. params[index]),) + params[index + 1:]
  889. return tuple_type(*simple_tuple)
  890. def ResolveReferences(self):
  891. """Resolve reference mapping, required to be called after Generate()
  892. After calling Generate(), the type of indices used in schema structures
  893. might be either int or string. An int type suggests that it's a resolved
  894. index, but for string type it's unresolved. Resolving a reference is as
  895. simple as looking up for corresponding ID in self.id_map, and replace the
  896. old index with the mapped index.
  897. """
  898. self.schema_nodes = list(
  899. map(partial(self.ResolveID, 1, SchemaNode), self.schema_nodes))
  900. self.property_nodes = list(
  901. map(partial(self.ResolveID, 1, PropertyNode), self.property_nodes))
  902. self.properties_nodes = list(
  903. map(partial(self.ResolveID, 3, PropertiesNode), self.properties_nodes))
  904. def FindSensitiveChildren(self):
  905. """Wrapper function, which calls FindSensitiveChildrenRecursive().
  906. """
  907. if self.schema_nodes:
  908. self.FindSensitiveChildrenRecursive(0, set())
  909. def FindSensitiveChildrenRecursive(self, index, handled_schema_nodes):
  910. """Recursively compute |has_sensitive_children| for the schema node at
  911. |index| and all its child elements. A schema has sensitive children if any
  912. of its children has |is_sensitive_value|==True or has sensitive children
  913. itself.
  914. """
  915. node = self.schema_nodes[index]
  916. if index in handled_schema_nodes:
  917. return node.has_sensitive_children or node.is_sensitive_value
  918. handled_schema_nodes.add(index)
  919. has_sensitive_children = False
  920. if node.schema_type == 'Type::DICTIONARY':
  921. properties_node = self.properties_nodes[node.extra]
  922. # Iterate through properties and patternProperties.
  923. for property_index in range(properties_node.begin,
  924. properties_node.pattern_end - 1):
  925. sub_index = self.property_nodes[property_index].schema
  926. has_sensitive_children |= self.FindSensitiveChildrenRecursive(
  927. sub_index, handled_schema_nodes)
  928. # AdditionalProperties
  929. if properties_node.additional != INVALID_INDEX:
  930. sub_index = properties_node.additional
  931. has_sensitive_children |= self.FindSensitiveChildrenRecursive(
  932. sub_index, handled_schema_nodes)
  933. elif node.schema_type == 'Type::LIST':
  934. sub_index = node.extra
  935. has_sensitive_children |= self.FindSensitiveChildrenRecursive(
  936. sub_index, handled_schema_nodes)
  937. if has_sensitive_children:
  938. self.schema_nodes[index] = self.schema_nodes[index]._replace(
  939. has_sensitive_children=True)
  940. return has_sensitive_children or node.is_sensitive_value
  941. def _GenerateDefaultValue(value):
  942. """Converts a JSON object into a base::Value entry. Returns a tuple, the first
  943. entry being a list of declaration statements to define the variable, the
  944. second entry being a way to access the variable.
  945. If no definition is needed, the first return value will be an empty list. If
  946. any error occurs, the second return value will be None (ie, no way to fetch
  947. the value).
  948. |value|: The deserialized value to convert to base::Value."""
  949. if type(value) == bool or type(value) == int:
  950. return [], 'base::Value(%s)' % json.dumps(value)
  951. elif type(value) == str:
  952. return [], 'base::Value("%s")' % value
  953. elif type(value) == list:
  954. setup = ['base::Value default_value(base::Value::Type::LIST);']
  955. for entry in value:
  956. decl, fetch = _GenerateDefaultValue(entry)
  957. # Nested lists are not supported.
  958. if decl:
  959. return [], None
  960. setup.append('default_value.Append(%s);' % fetch)
  961. return setup, 'std::move(default_value)'
  962. return [], None
  963. def _WritePolicyConstantSource(all_policies, policy_atomic_groups,
  964. target_platform, f, risk_tags):
  965. policies = _GetSupportedPolicies(all_policies, target_platform)
  966. policy_names = [policy.name for policy in policies]
  967. f.write('''#include "components/policy/policy_constants.h"
  968. #include <algorithm>
  969. #include <climits>
  970. #include <iterator>
  971. #include <memory>
  972. #include "base/check_op.h"
  973. #include "base/values.h"
  974. #include "build/branding_buildflags.h"
  975. #include "components/policy/core/common/policy_types.h"
  976. #include "components/policy/core/common/schema_internal.h"
  977. #include "components/policy/proto/cloud_policy.pb.h"
  978. #include "components/policy/risk_tag.h"
  979. namespace policy {
  980. ''')
  981. # Generate the Chrome schema.
  982. chrome_schema = {
  983. 'type': 'object',
  984. 'properties': {},
  985. }
  986. chrome_validation_schema = {
  987. 'type': 'object',
  988. 'properties': {},
  989. }
  990. shared_strings = {}
  991. for policy in policies:
  992. shared_strings[policy.name] = "key::k%s" % policy.name
  993. if policy.is_supported:
  994. chrome_schema['properties'][policy.name] = policy.schema
  995. if policy.validation_schema is not None:
  996. (chrome_validation_schema['properties'][policy.name]
  997. ) = policy.validation_schema
  998. # Note: this list must be kept in sync with the known property list of the
  999. # Chrome schema, so that binary searching in the PropertyNode array gets the
  1000. # right index on this array as well. See the implementation of
  1001. # GetChromePolicyDetails() below.
  1002. # TODO(crbug.com/1074336): kChromePolicyDetails shouldn't be declare if there
  1003. # is no policy.
  1004. f.write('''[[maybe_unused]] const PolicyDetails kChromePolicyDetails[] = {
  1005. // is_deprecated is_future is_device_policy id max_external_data_size, risk tags
  1006. ''')
  1007. for policy in policies:
  1008. if policy.is_supported:
  1009. assert policy.id >= MIN_POLICY_ID and policy.id <= MAX_POLICY_ID
  1010. assert (policy.max_size >= MIN_EXTERNAL_DATA_SIZE and
  1011. policy.max_size <= MAX_EXTERNAL_DATA_SIZE)
  1012. f.write(' // %s\n' % policy.name)
  1013. f.write(' { %-14s%-10s%-17s%4s,%22s, %s },\n' %
  1014. ('true,' if policy.is_deprecated else 'false,',
  1015. 'true,' if policy.is_future else 'false, ',
  1016. 'true,' if policy.is_device_only else 'false,', policy.id,
  1017. policy.max_size, risk_tags.ToInitString(policy.tags)))
  1018. f.write('};\n\n')
  1019. schema_generator = SchemaNodesGenerator(shared_strings)
  1020. schema_generator.GenerateAndCollectID(chrome_schema, 'root node')
  1021. if chrome_validation_schema['properties']:
  1022. schema_generator.validation_schema_root_index = \
  1023. schema_generator.GenerateAndCollectID(chrome_validation_schema,
  1024. 'validation_schema root node')
  1025. else:
  1026. schema_generator.validation_schema_root_index = INVALID_INDEX
  1027. schema_generator.ResolveReferences()
  1028. schema_generator.FindSensitiveChildren()
  1029. schema_generator.Write(f)
  1030. f.write('\n')
  1031. if schema_generator.property_nodes:
  1032. f.write('namespace {\n')
  1033. f.write('bool CompareKeys(const internal::PropertyNode& node,\n'
  1034. ' const std::string& key) {\n'
  1035. ' return node.key < key;\n'
  1036. '}\n\n')
  1037. f.write('} // namespace\n\n')
  1038. if target_platform == 'win':
  1039. f.write('#if BUILDFLAG(GOOGLE_CHROME_BRANDING)\n'
  1040. 'const wchar_t kRegistryChromePolicyKey[] = '
  1041. 'L"' + CHROME_POLICY_KEY + '";\n'
  1042. '#else\n'
  1043. 'const wchar_t kRegistryChromePolicyKey[] = '
  1044. 'L"' + CHROMIUM_POLICY_KEY + '";\n'
  1045. '#endif\n\n')
  1046. # Setting enterprise defaults code generation.
  1047. profile_policy_enterprise_defaults = ""
  1048. system_wide_policy_enterprise_defaults = ""
  1049. for policy in policies:
  1050. if policy.has_enterprise_default and policy.is_supported:
  1051. declare_default_stmts, fetch_default = _GenerateDefaultValue(
  1052. policy.enterprise_default)
  1053. if not fetch_default:
  1054. raise RuntimeError('Type %s of policy %s is not supported at '
  1055. 'enterprise defaults' %
  1056. (policy.policy_type, policy.name))
  1057. # Convert declare_default_stmts to a string with the correct indentation.
  1058. if declare_default_stmts:
  1059. declare_default = ' %s\n' % '\n '.join(declare_default_stmts)
  1060. else:
  1061. declare_default = ''
  1062. policy_level = "POLICY_LEVEL_MANDATORY"
  1063. if policy.default_policy_level == 'recommended':
  1064. policy_level = "POLICY_LEVEL_RECOMMENDED"
  1065. setting_enterprise_default = ''' if (!policy_map->Get(key::k%s)) {
  1066. %s
  1067. policy_map->Set(key::k%s,
  1068. %s,
  1069. POLICY_SCOPE_USER,
  1070. POLICY_SOURCE_ENTERPRISE_DEFAULT,
  1071. %s,
  1072. nullptr);
  1073. }
  1074. ''' % (policy.name, declare_default, policy.name, policy_level, fetch_default)
  1075. if policy.per_profile:
  1076. profile_policy_enterprise_defaults += setting_enterprise_default
  1077. else:
  1078. system_wide_policy_enterprise_defaults += setting_enterprise_default
  1079. f.write('#if BUILDFLAG(IS_CHROMEOS)')
  1080. f.write('''
  1081. void SetEnterpriseUsersProfileDefaults(PolicyMap* policy_map) {
  1082. %s
  1083. }
  1084. ''' % profile_policy_enterprise_defaults)
  1085. f.write('''
  1086. void SetEnterpriseUsersSystemWideDefaults(PolicyMap* policy_map) {
  1087. %s
  1088. }
  1089. ''' % system_wide_policy_enterprise_defaults)
  1090. f.write('''
  1091. void SetEnterpriseUsersDefaults(PolicyMap* policy_map) {
  1092. SetEnterpriseUsersProfileDefaults(policy_map);
  1093. SetEnterpriseUsersSystemWideDefaults(policy_map);
  1094. }
  1095. ''')
  1096. f.write('#endif\n\n')
  1097. f.write('const PolicyDetails* GetChromePolicyDetails('
  1098. 'const std::string& policy) {\n')
  1099. if schema_generator.property_nodes:
  1100. f.write(' // First index in kPropertyNodes of the Chrome policies.\n'
  1101. ' static const int begin_index = %s;\n'
  1102. ' // One-past-the-end of the Chrome policies in kPropertyNodes.\n'
  1103. ' static const int end_index = %s;\n' %
  1104. (schema_generator.root_properties_begin,
  1105. schema_generator.root_properties_end))
  1106. f.write(''' const internal::PropertyNode* begin =
  1107. kPropertyNodes + begin_index;
  1108. const internal::PropertyNode* end = kPropertyNodes + end_index;
  1109. const internal::PropertyNode* it =
  1110. std::lower_bound(begin, end, policy, CompareKeys);
  1111. if (it == end || it->key != policy)
  1112. return nullptr;
  1113. // This relies on kPropertyNodes from begin_index to end_index
  1114. // having exactly the same policies (and in the same order) as
  1115. // kChromePolicyDetails, so that binary searching on the first
  1116. // gets the same results as a binary search on the second would.
  1117. // However, kPropertyNodes has the policy names and
  1118. // kChromePolicyDetails doesn't, so we obtain the index into
  1119. // the second array by searching the first to avoid duplicating
  1120. // the policy name pointers.
  1121. // Offsetting |it| from |begin| here obtains the index we're
  1122. // looking for.
  1123. size_t index = it - begin;
  1124. CHECK_LT(index, std::size(kChromePolicyDetails));
  1125. return kChromePolicyDetails + index;
  1126. ''')
  1127. else:
  1128. f.write('return nullptr;')
  1129. f.write('}\n\n')
  1130. f.write('namespace key {\n\n')
  1131. for policy in policies:
  1132. # TODO(joaodasilva): Include only supported policies in
  1133. # configuration_policy_handler.cc and configuration_policy_handler_list.cc
  1134. # so that these names can be conditional on 'policy.is_supported'.
  1135. # http://crbug.com/223616
  1136. f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name))
  1137. f.write('\n} // namespace key\n\n')
  1138. f.write('namespace group {\n\n')
  1139. for group in policy_atomic_groups:
  1140. f.write('const char k{name}[] = "{name}";\n'.format(name=group.name))
  1141. f.write('\n')
  1142. f.write('namespace {\n\n')
  1143. for group in policy_atomic_groups:
  1144. f.write('const char* const %s[] = {' % (group.name))
  1145. for policy in group.policies:
  1146. if policy in policy_names:
  1147. f.write('key::k%s, ' % (policy))
  1148. f.write('nullptr};\n')
  1149. f.write('\n} // namespace\n')
  1150. f.write('\n} // namespace group\n\n')
  1151. atomic_groups_length = 0
  1152. f.write('const AtomicGroup kPolicyAtomicGroupMappings[] = {\n')
  1153. for group in policy_atomic_groups:
  1154. atomic_groups_length += 1
  1155. f.write(' {')
  1156. f.write(' {id}, group::k{name}, group::{name}'.format(
  1157. id=group.id, name=group.name))
  1158. f.write(' },\n')
  1159. f.write('};\n\n')
  1160. f.write('const size_t kPolicyAtomicGroupMappingsLength = %s;\n\n' %
  1161. (atomic_groups_length))
  1162. f.write('namespace metapolicy {\n\n')
  1163. # Populate merge metapolicy array.
  1164. merge_metapolicies = _GetMetapoliciesOfType(policies,
  1165. METAPOLICY_TYPE['merge'])
  1166. f.write('const char* const kMerge[%s] = {\n' % len(merge_metapolicies))
  1167. for metapolicy in merge_metapolicies:
  1168. f.write(' key::k%s,\n' % metapolicy.name)
  1169. f.write('};\n\n')
  1170. # Populate precedence metapolicy array.
  1171. precedence_metapolicies = _GetMetapoliciesOfType(
  1172. policies, METAPOLICY_TYPE['precedence'])
  1173. f.write('const char* const kPrecedence[%s] = {\n' %
  1174. len(precedence_metapolicies))
  1175. for metapolicy in precedence_metapolicies:
  1176. f.write(' key::k%s,\n' % metapolicy.name)
  1177. f.write('};\n\n')
  1178. f.write('} // namespace metapolicy\n\n')
  1179. protobuf_types = _GetProtobufTypes()
  1180. for protobuf_type in protobuf_types:
  1181. _WriteChromePolicyAccessSource(policies, f, protobuf_type)
  1182. f.write('\n} // namespace policy\n')
  1183. # Return the StringPolicyType enum value for a particular policy type.
  1184. def _GetStringPolicyType(policy_type):
  1185. if policy_type == 'Type::STRING':
  1186. return 'StringPolicyType::STRING'
  1187. elif policy_type == 'Type::DICTIONARY':
  1188. return 'StringPolicyType::JSON'
  1189. elif policy_type == 'TYPE_EXTERNAL':
  1190. return 'StringPolicyType::EXTERNAL'
  1191. raise RuntimeError('Invalid string type: ' + policy_type + '!\n')
  1192. # Writes an array that contains the pointers to the proto field for each policy
  1193. # in |policies| of the given |protobuf_type|.
  1194. def _WriteChromePolicyAccessSource(policies, f, protobuf_type):
  1195. supported_user_policies = _GetSupportedChromeUserPolicies(
  1196. policies, protobuf_type)
  1197. f.write('const std::array<%sPolicyAccess, %d> k%sPolicyAccess {{\n' %
  1198. (protobuf_type, len(supported_user_policies), protobuf_type))
  1199. for policy in supported_user_policies:
  1200. name = policy.name
  1201. lowercase_name = name.lower()
  1202. if protobuf_type == 'String':
  1203. extra_args = ',\n ' + _GetStringPolicyType(policy.policy_type)
  1204. else:
  1205. extra_args = ''
  1206. chunk_number = _ChunkNumber(policy.id)
  1207. if chunk_number == 0:
  1208. has_proto = 'policy.has_%s()' % lowercase_name
  1209. get_proto = 'policy.%s()' % lowercase_name
  1210. else:
  1211. has_subproto = 'policy.has_subproto%d() &&\n' % chunk_number
  1212. has_policy = ' policy.subproto%d().has_%s()' % (
  1213. chunk_number, lowercase_name)
  1214. has_proto = has_subproto + has_policy
  1215. get_proto = 'policy.subproto%d().%s()' % (chunk_number, lowercase_name)
  1216. f.write(' {key::k%s,\n'
  1217. ' %s,\n'
  1218. ' [](const em::CloudPolicySettings& policy) {\n'
  1219. ' return %s;\n'
  1220. ' },\n'
  1221. ' [](const em::CloudPolicySettings& policy)\n'
  1222. ' -> const em::%sPolicyProto& {\n'
  1223. ' return %s;\n'
  1224. ' }%s\n'
  1225. ' },\n' % (name, str(policy.per_profile).lower(), has_proto,
  1226. protobuf_type, get_proto, extra_args))
  1227. f.write('}};\n\n')
  1228. #------------------ policy risk tag header -------------------------#
  1229. class RiskTags(object):
  1230. '''Generates files and strings to translate the parsed risk tags.'''
  1231. # TODO(fhorschig|tnagel): Add, Check & Generate translation descriptions.
  1232. def __init__(self, template_file_contents):
  1233. self.max_tags = None
  1234. self.enum_for_tag = OrderedDict() # Ordered by severity as stated in JSON.
  1235. self._ReadRiskTagMetaData(template_file_contents)
  1236. def GenerateEnum(self):
  1237. values = [' ' + self.enum_for_tag[tag] for tag in self.enum_for_tag]
  1238. values.append(' RISK_TAG_COUNT')
  1239. values.append(' RISK_TAG_NONE')
  1240. enum_text = 'enum RiskTag : uint8_t {\n'
  1241. enum_text += ',\n'.join(values) + '\n};\n'
  1242. return enum_text
  1243. def GetMaxTags(self):
  1244. return str(self.max_tags)
  1245. def GetValidTags(self):
  1246. return [tag for tag in self.enum_for_tag]
  1247. def ToInitString(self, tags):
  1248. all_tags = [self._ToEnum(tag) for tag in tags]
  1249. all_tags += ["RISK_TAG_NONE" for missing in range(len(tags), self.max_tags)]
  1250. str_tags = "{ " + ", ".join(all_tags) + " }"
  1251. return "\n ".join(textwrap.wrap(str_tags, 69))
  1252. def ComputeMaxTags(self, policies):
  1253. self.max_tags = 0
  1254. for policy in policies:
  1255. if not policy.is_supported or policy.tags == None:
  1256. continue
  1257. self.max_tags = max(len(policy.tags), self.max_tags)
  1258. def _ToEnum(self, tag):
  1259. if tag in self.enum_for_tag:
  1260. return self.enum_for_tag[tag]
  1261. raise RuntimeError('Invalid Tag:' + tag + '!\n'
  1262. 'Chose a valid tag from \'risk_tag_definitions\' (a '
  1263. 'subproperty of root in policy_templates.json)!')
  1264. def _ReadRiskTagMetaData(self, template_file_contents):
  1265. for tag in template_file_contents['risk_tag_definitions']:
  1266. if tag.get('name', None) == None:
  1267. raise RuntimeError('Tag in \'risk_tag_definitions\' without '
  1268. 'description found!')
  1269. if tag.get('description', None) == None:
  1270. raise RuntimeError('Tag ' + tag['name'] + ' has no description!')
  1271. if tag.get('user-description', None) == None:
  1272. raise RuntimeError('Tag ' + tag['name'] + ' has no user-description!')
  1273. self.enum_for_tag[tag['name']] = "RISK_TAG_" + tag['name'].replace(
  1274. "-", "_").upper()
  1275. def _WritePolicyRiskTagHeader(policies, policy_atomic_groups, target_platform,
  1276. f, risk_tags):
  1277. f.write('''#ifndef CHROME_COMMON_POLICY_RISK_TAG_H_
  1278. #define CHROME_COMMON_POLICY_RISK_TAG_H_
  1279. #include <stddef.h>
  1280. namespace policy {
  1281. // The tag of a policy indicates which impact a policy can have on
  1282. // a user's privacy and/or security. Ordered descending by
  1283. // impact.
  1284. // The explanation of the single tags is stated in
  1285. // policy_templates.json within the 'risk_tag_definitions' tag.
  1286. ''')
  1287. f.write(risk_tags.GenerateEnum() + '\n')
  1288. f.write('// This constant describes how many risk tags were used by the\n'
  1289. '// policy which uses the most risk tags.\n'
  1290. 'const size_t kMaxRiskTagCount = ' + risk_tags.GetMaxTags() + ';\n'
  1291. '\n'
  1292. '} // namespace policy\n\n'
  1293. '\n'
  1294. '#endif // CHROME_COMMON_POLICY_RISK_TAG_H_')
  1295. #------------------ policy protobufs -------------------------------#
  1296. # This code applies to both Active Directory and Google cloud management.
  1297. CHROME_SETTINGS_PROTO_HEAD = '''
  1298. syntax = "proto2";
  1299. {full_runtime_comment}option optimize_for = LITE_RUNTIME;
  1300. package enterprise_management;
  1301. option go_package="chromium/policy/enterprise_management_proto";
  1302. // For StringList and PolicyOptions.
  1303. import "policy_common_definitions{full_runtime_suffix}.proto";
  1304. '''
  1305. CLOUD_POLICY_PROTO_HEAD = '''
  1306. syntax = "proto2";
  1307. {full_runtime_comment}option optimize_for = LITE_RUNTIME;
  1308. package enterprise_management;
  1309. option go_package="chromium/policy/enterprise_management_proto";
  1310. import "policy_common_definitions{full_runtime_suffix}.proto";
  1311. '''
  1312. # Field IDs [1..RESERVED_IDS] will not be used in the wrapping protobuf.
  1313. RESERVED_IDS = 2
  1314. # All user policies with ID <= |_LAST_TOP_LEVEL_POLICY_ID| are added to the top
  1315. # level of ChromeSettingsProto and CloudPolicySettings, whereas all policies
  1316. # with ID > |_LAST_TOP_LEVEL_POLICY_ID| are nested into sub-protos. See
  1317. # https://crbug.com/1237044 for more details.
  1318. _LAST_TOP_LEVEL_POLICY_ID = 1015
  1319. # The approximate number of policies in one nested chunk for user policies with
  1320. # ID > |_LAST_TOP_LEVEL_POLICY_ID|. See https://crbug.com/1237044 for more
  1321. # details.
  1322. _CHUNK_SIZE = 800
  1323. def _WritePolicyProto(f, policy):
  1324. _OutputComment(f, policy.caption + '\n\n' + policy.desc)
  1325. if policy.items is not None:
  1326. _OutputComment(f, '\nValid values:')
  1327. for item in policy.items:
  1328. _OutputComment(f, ' %s: %s' % (str(item.value), item.caption))
  1329. if policy.policy_type == 'Type::DICTIONARY':
  1330. _OutputComment(
  1331. f, '\nValue schema:\n%s' % json.dumps(
  1332. policy.schema, sort_keys=True, indent=4, separators=(',', ': ')))
  1333. _OutputComment(
  1334. f, '\nSupported on: %s' %
  1335. ', '.join(sorted(list(policy.platforms.union(policy.future_on)))))
  1336. if policy.can_be_recommended and not policy.can_be_mandatory:
  1337. _OutputComment(
  1338. f, '\nNote: this policy must have a RECOMMENDED ' +
  1339. 'PolicyMode set in PolicyOptions.')
  1340. f.write('message %sProto {\n' % policy.name)
  1341. f.write(' optional PolicyOptions policy_options = 1;\n')
  1342. f.write(' optional %s %s = 2;\n' % (policy.protobuf_type, policy.name))
  1343. f.write('}\n\n')
  1344. def _ChunkNumber(policy_id):
  1345. # Compute which chunk the policy should go to. Chunk 0 contains the legacy
  1346. # policies, whereas subsequent chunks contain nested policies.
  1347. if policy_id <= _LAST_TOP_LEVEL_POLICY_ID:
  1348. return 0
  1349. else:
  1350. return (policy_id - _LAST_TOP_LEVEL_POLICY_ID - 1) // _CHUNK_SIZE + 1
  1351. def _FieldNumber(policy_id, chunk_number):
  1352. if chunk_number == 0:
  1353. # For the top-level policies the field number in the proto file is the
  1354. # same as the id assigned to the policy in policy_templates.json,
  1355. # skipping the RESERVED_IDS.
  1356. return policy_id + RESERVED_IDS
  1357. else:
  1358. # For the nested policies, the field numbers should always be in the
  1359. # range [1, _CHUNK_SIZE], therefore we calculate them using this formula.
  1360. return (policy_id - _LAST_TOP_LEVEL_POLICY_ID - 1) % _CHUNK_SIZE + 1
  1361. def _WriteChromeSettingsProtobuf(policies,
  1362. policy_atomic_groups,
  1363. target_platform,
  1364. f,
  1365. risk_tags,
  1366. is_full_runtime=False):
  1367. # For full runtime, disable LITE_RUNTIME switch and import full runtime
  1368. # version of policy_common_definitions.proto.
  1369. full_runtime_comment = '//' if is_full_runtime else ''
  1370. full_runtime_suffix = '_full_runtime' if is_full_runtime else ''
  1371. f.write(
  1372. CHROME_SETTINGS_PROTO_HEAD.format(
  1373. full_runtime_comment=full_runtime_comment,
  1374. full_runtime_suffix=full_runtime_suffix))
  1375. fields = defaultdict(list)
  1376. f.write('// PBs for individual settings.\n\n')
  1377. for policy in policies:
  1378. # Note: This protobuf also gets the unsupported policies, since it's an
  1379. # exhaustive list of all the supported user policies on any platform.
  1380. if not policy.is_device_only:
  1381. # Write the individual policy proto into the file
  1382. _WritePolicyProto(f, policy)
  1383. chunk_number = _ChunkNumber(policy.id)
  1384. field_number = _FieldNumber(policy.id, chunk_number)
  1385. # Add to |fields| in order to eventually add to ChromeSettingsProto.
  1386. fields[chunk_number].append(' optional %sProto %s = %s;\n' %
  1387. (policy.name, policy.name, field_number))
  1388. sorted_chunk_numbers = sorted(fields.keys())
  1389. if len(sorted_chunk_numbers) > 1:
  1390. f.write('// --------------------------------------------------\n'
  1391. '// PBs for policies with ID > %d.\n\n' % _LAST_TOP_LEVEL_POLICY_ID)
  1392. for sorted_chunk_number in sorted_chunk_numbers:
  1393. if sorted_chunk_number == 0:
  1394. continue
  1395. f.write('message ChromeSettingsSubProto%d {\n' % sorted_chunk_number)
  1396. f.write(''.join(fields[sorted_chunk_number]))
  1397. f.write('}\n\n')
  1398. f.write('// --------------------------------------------------\n'
  1399. '// Big wrapper PB containing the above groups.\n\n'
  1400. 'message ChromeSettingsProto {\n')
  1401. for sorted_chunk_number in sorted_chunk_numbers:
  1402. if sorted_chunk_number == 0:
  1403. f.write(''.join(fields[sorted_chunk_number]))
  1404. else:
  1405. f.write(' optional ChromeSettingsSubProto%d subProto%d = %s;\n' %
  1406. (sorted_chunk_number, sorted_chunk_number,
  1407. _LAST_TOP_LEVEL_POLICY_ID + RESERVED_IDS + sorted_chunk_number))
  1408. f.write('}\n')
  1409. def _WriteCloudPolicyProtobuf(policies,
  1410. policy_atomic_groups,
  1411. target_platform,
  1412. f,
  1413. risk_tags,
  1414. is_full_runtime=False):
  1415. # For full runtime, disable LITE_RUNTIME switch and import full runtime
  1416. # version of policy_common_definitions.proto.
  1417. full_runtime_comment = '//' if is_full_runtime else ''
  1418. full_runtime_suffix = '_full_runtime' if is_full_runtime else ''
  1419. f.write(
  1420. CLOUD_POLICY_PROTO_HEAD.format(full_runtime_comment=full_runtime_comment,
  1421. full_runtime_suffix=full_runtime_suffix))
  1422. fields = defaultdict(list)
  1423. for policy in policies:
  1424. if not policy.is_supported or policy.is_device_only:
  1425. continue
  1426. chunk_number = _ChunkNumber(policy.id)
  1427. field_number = _FieldNumber(policy.id, chunk_number)
  1428. # Add to |fields| in order to eventually add to CloudPolicyProto.
  1429. fields[chunk_number].append(
  1430. ' optional %sPolicyProto %s = %s;\n' %
  1431. (policy.policy_protobuf_type, policy.name, field_number))
  1432. sorted_chunk_numbers = sorted(fields.keys())
  1433. if len(sorted_chunk_numbers) > 1:
  1434. for sorted_chunk_number in sorted_chunk_numbers:
  1435. if sorted_chunk_number == 0:
  1436. continue
  1437. f.write('message CloudPolicySubProto%d {\n' % sorted_chunk_number)
  1438. f.write(''.join(fields[sorted_chunk_number]))
  1439. f.write('}\n\n')
  1440. f.write('message CloudPolicySettings {\n')
  1441. for sorted_chunk_number in sorted_chunk_numbers:
  1442. if sorted_chunk_number == 0:
  1443. f.write(''.join(fields[sorted_chunk_number]))
  1444. else:
  1445. f.write(' optional CloudPolicySubProto%d subProto%d = %s;\n' %
  1446. (sorted_chunk_number, sorted_chunk_number,
  1447. _LAST_TOP_LEVEL_POLICY_ID + RESERVED_IDS + sorted_chunk_number))
  1448. f.write('}\n')
  1449. def _WritePolicyCommonDefinitionsFullRuntimeProtobuf(
  1450. policy_common_definitions_proto_path, policies, policy_atomic_groups,
  1451. target_platform, f, risk_tags):
  1452. # For full runtime, disable LITE_RUNTIME switch
  1453. with open(policy_common_definitions_proto_path, 'r') as proto_file:
  1454. policy_common_definitions_proto_code = proto_file.read()
  1455. f.write(
  1456. policy_common_definitions_proto_code.replace(
  1457. "option optimize_for = LITE_RUNTIME;",
  1458. "//option optimize_for = LITE_RUNTIME;"))
  1459. #------------------ Chrome OS policy constants header --------------#
  1460. # This code applies to Active Directory management only.
  1461. # Filter for _GetSupportedChromeOSPolicies().
  1462. def _IsSupportedChromeOSPolicy(type, policy):
  1463. # Filter out unsupported policies.
  1464. if not policy.is_supported:
  1465. return False
  1466. # Filter out device policies if user policies are requested.
  1467. if type == 'user' and policy.is_device_only:
  1468. return False
  1469. # Filter out user policies if device policies are requested.
  1470. if type == 'device' and not policy.is_device_only:
  1471. return False
  1472. # Filter out non-Active-Directory policies.
  1473. if 'active_directory' not in policy.supported_chrome_os_management:
  1474. return False
  1475. return True
  1476. # Returns a list of supported user and/or device policies by filtering
  1477. # |policies|. |type| may be 'user', 'device' or 'both'.
  1478. def _GetSupportedChromeOSPolicies(policies, type):
  1479. if (type not in ['user', 'device', 'both']):
  1480. raise RuntimeError('Unsupported type "%s"' % type)
  1481. return list(filter(partial(_IsSupportedChromeOSPolicy, type), policies))
  1482. # Returns a list of supported user and/or device |policies| additionally
  1483. # filtered by |protobuf_type|, which may be any of |_GetProtobufTypes|.
  1484. def _GetSupportedChromeOSPoliciesForProtobufType(policies, type, protobuf_type):
  1485. supported_policies = _GetSupportedChromeOSPolicies(policies, type)
  1486. return [
  1487. p for p in supported_policies if p.policy_protobuf_type == protobuf_type
  1488. ]
  1489. # Returns the list of all policy.policy_protobuf_type strings from |policies|.
  1490. def _GetProtobufTypes():
  1491. return sorted(['Integer', 'Boolean', 'String', 'StringList'])
  1492. # Writes the definition of an array that contains the pointers to the mutable
  1493. # proto field for each policy in |policies| of the given |protobuf_type|.
  1494. def _WriteChromeOSPolicyAccessHeader(supported_policies, f, protobuf_type):
  1495. f.write('// Access to the mutable protobuf function of all supported '
  1496. '%s user\n// policies.\n' % protobuf_type.lower())
  1497. f.write('struct %sPolicyAccess {\n'
  1498. ' const char* policy_key;\n'
  1499. ' bool per_profile;\n'
  1500. ' enterprise_management::%sPolicyProto* (*mutable_proto_ptr)(\n'
  1501. ' enterprise_management::CloudPolicySettings* policy);\n'
  1502. '};\n' % (protobuf_type, protobuf_type))
  1503. f.write('extern const std::array<%sPolicyAccess, %d> k%sPolicyAccess;\n\n' %
  1504. (protobuf_type, len(supported_policies), protobuf_type))
  1505. # Writes policy_constants.h for use in Chrome OS.
  1506. def _WriteChromeOSPolicyConstantsHeader(policies, policy_atomic_groups,
  1507. target_platform, f, risk_tags):
  1508. f.write('#ifndef __BINDINGS_POLICY_CONSTANTS_H_\n'
  1509. '#define __BINDINGS_POLICY_CONSTANTS_H_\n\n'
  1510. '#include <array>\n\n')
  1511. # Forward declarations.
  1512. supported_user_policies = _GetSupportedChromeOSPolicies(policies, 'user')
  1513. protobuf_types = _GetProtobufTypes()
  1514. f.write('namespace enterprise_management {\n' 'class CloudPolicySettings;\n')
  1515. for protobuf_type in protobuf_types:
  1516. f.write('class %sPolicyProto;\n' % protobuf_type)
  1517. f.write('} // namespace enterprise_management\n\n')
  1518. f.write('namespace policy {\n\n')
  1519. # Policy keys.
  1520. all_supported_policies = _GetSupportedChromeOSPolicies(policies, 'both')
  1521. f.write('// Registry key names for user and device policies.\n'
  1522. 'namespace key {\n\n')
  1523. for policy in all_supported_policies:
  1524. f.write('extern const char k' + policy.name + '[];\n')
  1525. f.write('\n} // namespace key\n\n')
  1526. # Device policy keys.
  1527. f.write('// NULL-terminated list of device policy registry key names.\n')
  1528. f.write('extern const char* kDevicePolicyKeys[];\n\n')
  1529. # User policy proto pointers, one struct for each protobuf type.
  1530. for protobuf_type in protobuf_types:
  1531. supported_user_policies = _GetSupportedChromeOSPoliciesForProtobufType(
  1532. policies, 'user', protobuf_type)
  1533. _WriteChromeOSPolicyAccessHeader(supported_user_policies, f, protobuf_type)
  1534. f.write('} // namespace policy\n\n'
  1535. '#endif // __BINDINGS_POLICY_CONSTANTS_H_\n')
  1536. #------------------ Chrome OS policy constants source --------------#
  1537. # Writes an array that contains the pointers to the mutable proto field for each
  1538. # policy in |policies| of the given |protobuf_type|.
  1539. def _WriteChromeOSPolicyAccessSource(supported_policies, f, protobuf_type):
  1540. f.write('const std::array<%sPolicyAccess, %d> k%sPolicyAccess {{\n' %
  1541. (protobuf_type, len(supported_policies), protobuf_type))
  1542. for policy in supported_policies:
  1543. name = policy.name
  1544. lowercase_name = name.lower()
  1545. chunk_number = _ChunkNumber(policy.id)
  1546. if chunk_number == 0:
  1547. mutable_proto_ptr = 'policy->mutable_%s()' % lowercase_name
  1548. else:
  1549. mutable_proto_ptr = 'policy->mutable_subproto%d()->mutable_%s()' % (
  1550. chunk_number, lowercase_name)
  1551. f.write(' {key::k%s,\n'
  1552. ' %s,\n'
  1553. ' [](em::CloudPolicySettings* policy)\n'
  1554. ' -> em::%sPolicyProto* {\n'
  1555. ' return %s;\n'
  1556. ' }\n'
  1557. ' },\n' % (name, str(
  1558. policy.per_profile).lower(), protobuf_type, mutable_proto_ptr))
  1559. f.write('}};\n\n')
  1560. # Writes policy_constants.cc for use in Chrome OS.
  1561. def _WriteChromeOSPolicyConstantsSource(policies, policy_atomic_groups,
  1562. target_platform, f, risk_tags):
  1563. f.write('#include "bindings/cloud_policy.pb.h"\n'
  1564. '#include "bindings/policy_constants.h"\n\n'
  1565. 'namespace em = enterprise_management;\n\n'
  1566. 'namespace policy {\n\n')
  1567. # Policy keys.
  1568. all_supported_policies = _GetSupportedChromeOSPolicies(policies, 'both')
  1569. f.write('namespace key {\n\n')
  1570. for policy in all_supported_policies:
  1571. f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name))
  1572. f.write('\n} // namespace key\n\n')
  1573. # Device policy keys.
  1574. supported_device_policies = _GetSupportedChromeOSPolicies(policies, 'device')
  1575. f.write('const char* kDevicePolicyKeys[] = {\n\n')
  1576. for policy in supported_device_policies:
  1577. f.write(' key::k%s,\n' % policy.name)
  1578. f.write(' nullptr};\n\n')
  1579. # User policy proto pointers, one struct for each protobuf type.
  1580. protobuf_types = _GetProtobufTypes()
  1581. for protobuf_type in protobuf_types:
  1582. supported_user_policies = _GetSupportedChromeOSPoliciesForProtobufType(
  1583. policies, 'user', protobuf_type)
  1584. _WriteChromeOSPolicyAccessSource(supported_user_policies, f, protobuf_type)
  1585. f.write('} // namespace policy\n')
  1586. #------------------ app restrictions -------------------------------#
  1587. ENROLLMENT_TOKEN_POLICY_NAME = 'CloudManagementEnrollmentToken'
  1588. def _WriteAppRestrictions(policies, policy_atomic_groups, target_platform, f,
  1589. risk_tags):
  1590. def WriteRestrictionCommon(key):
  1591. f.write(' <restriction\n' ' android:key="%s"\n' % key)
  1592. f.write(' android:title="@string/%sTitle"\n' % key)
  1593. f.write(' android:description="@string/%sDesc"\n' % key)
  1594. def WriteItemsDefinition(key):
  1595. f.write(' android:entries="@array/%sEntries"\n' % key)
  1596. f.write(' android:entryValues="@array/%sValues"\n' % key)
  1597. def WriteAppRestriction(policy):
  1598. policy_name = policy.name
  1599. WriteRestrictionCommon(policy_name)
  1600. if policy.items is not None:
  1601. WriteItemsDefinition(policy_name)
  1602. f.write(' android:restrictionType="%s"/>' % policy.restriction_type)
  1603. f.write('\n\n')
  1604. def ShouldWriteAppRestriction(policy):
  1605. return (policy.is_supported and policy.restriction_type != 'invalid'
  1606. and not policy.is_deprecated and not policy.is_future
  1607. and not policy.internal_only and not policy.cloud_only)
  1608. # Compare policies by name, considering that `ENROLLMENT_TOKEN_POLICY_NAME`
  1609. # should come before all other policies in the generate app restrictions file.
  1610. def Compare(policy1, policy2):
  1611. if policy1.name == policy2.name:
  1612. return 0
  1613. if policy1.name == ENROLLMENT_TOKEN_POLICY_NAME:
  1614. return -1
  1615. if policy2.name == ENROLLMENT_TOKEN_POLICY_NAME:
  1616. return 1
  1617. if policy1.name < policy2.name:
  1618. return -1
  1619. return 1
  1620. # _WriteAppRestrictions body
  1621. f.write('<restrictions xmlns:android="'
  1622. 'http://schemas.android.com/apk/res/android">\n\n')
  1623. for policy in sorted(policies, key=cmp_to_key(Compare)):
  1624. if ShouldWriteAppRestriction(policy):
  1625. WriteAppRestriction(policy)
  1626. f.write('</restrictions>')
  1627. if __name__ == '__main__':
  1628. sys.exit(main())