From 30d681469fc4eb24357dd2e51559722cb29312ed Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Mon, 23 Sep 2024 12:43:52 -0700 Subject: [PATCH] Add new `build_policy_templates` action Currently, group policy is only generated during `create_dist`. This allows us to quickly create `policy_templates.zip` and the Brave specific version `brave_policy_templates.zip`. Fixes https://github.com/brave/brave-browser/issues/41217 --- BUILD.gn | 9 +++ components/policy/BUILD.gn | 15 ++++- components/policy/pack_policy_templates.py | 64 ++++++++++++---------- package.json | 3 +- 4 files changed, 61 insertions(+), 30 deletions(-) mode change 100644 => 100755 components/policy/pack_policy_templates.py diff --git a/BUILD.gn b/BUILD.gn index 31c6a0a3bd4d..808c542d84e1 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -247,6 +247,15 @@ if (!is_ios) { } } +if (!is_android && !is_ios) { + group("brave_group_policy_templates") { + # Currently, group policy resources are only generated on Windows. + if (is_win) { + deps = [ "//brave/components/policy:pack_policy_templates" ] + } + } +} + if (!is_ios) { brave_paks("packed_resources") { if (is_mac) { diff --git a/components/policy/BUILD.gn b/components/policy/BUILD.gn index 96863041c2d0..58aef060bf83 100644 --- a/components/policy/BUILD.gn +++ b/components/policy/BUILD.gn @@ -1,7 +1,20 @@ +# Copyright (c) 2024 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + +# See `components/policy/BUILD.gn` for more info about how these files +# are generated. Also see: +# `chromium_src/components/policy/tools/generate_policy_source.py` +# for Brave specific group policy definitions. + if (is_win) { action("pack_policy_templates") { chrome_pack_policy_templates = "//components/policy:pack_policy_templates" - deps = [ chrome_pack_policy_templates ] + deps = [ + "//components/policy:policy_templates", + chrome_pack_policy_templates, + ] script = "pack_policy_templates.py" chrome_policy_templates_zip = get_label_info(chrome_pack_policy_templates, "root_out_dir") + diff --git a/components/policy/pack_policy_templates.py b/components/policy/pack_policy_templates.py old mode 100644 new mode 100755 index 245e94b5962c..4903b88d0b8b --- a/components/policy/pack_policy_templates.py +++ b/components/policy/pack_policy_templates.py @@ -4,6 +4,13 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at https://mozilla.org/MPL/2.0/. +# Script that prepares a Brave-specific version of the policy_templates.zip +# file that folks expect for administering Brave via group policy. +# +# For more info, see: +# https://support.brave.com/hc/en-us/articles/360039248271-Group-Policy +# and +# https://github.com/brave/brave-browser/issues/26502 """ Create a Zip file of Windows Group Policy templates similar to Chrome's. """ @@ -16,37 +23,38 @@ from zipfile import ZipFile, ZIP_DEFLATED def main(): - chrome_policy_zip, dest_zip = _get_args() - _pack_policy_templates(chrome_policy_zip, dest_zip) + chrome_policy_zip, dest_zip = _get_args() + _pack_policy_templates(chrome_policy_zip, dest_zip) def _get_args(): - parser = argparse.ArgumentParser() - parser.add_argument('chrome_policy_zip', - help="Path to Chrome's policy_templates.zip") - parser.add_argument('dest_zip', - help="Path to the Zip file to be created") - args = parser.parse_args() - return args.chrome_policy_zip, args.dest_zip + parser = argparse.ArgumentParser() + parser.add_argument('chrome_policy_zip', + help="Path to Chrome's policy_templates.zip") + parser.add_argument('dest_zip', help="Path to the Zip file to be created") + args = parser.parse_args() + return args.chrome_policy_zip, args.dest_zip def _pack_policy_templates(chrome_policy_zip, dest_zip): - with TemporaryDirectory() as tmp_dir: - with ZipFile(chrome_policy_zip) as src_zip: - src_zip.extract('VERSION', tmp_dir) - namelist = src_zip.namelist() - for dir_ in ('windows/adm/', 'windows/admx/', 'windows/examples/'): - src_zip.extractall(tmp_dir, (n for n in namelist if n.startswith(dir_))) - - # Some sanity checks: - assert exists(join(tmp_dir, 'windows/adm/en-US/chrome.adm')) - assert exists(join(tmp_dir, 'windows/admx/chrome.admx')) - assert exists(join(tmp_dir, 'windows/admx/en-US/chrome.adml')) - - with ZipFile(dest_zip, 'w', ZIP_DEFLATED) as dest_zipfile: - for dirpath, _, filenames in os.walk(tmp_dir): - for filename in filenames: - filepath = join(dirpath, filename) - arcname = relpath(filepath, tmp_dir).replace('chrome', 'brave') - dest_zipfile.write(filepath, arcname=arcname) + with TemporaryDirectory() as tmp_dir: + with ZipFile(chrome_policy_zip) as src_zip: + src_zip.extract('VERSION', tmp_dir) + namelist = src_zip.namelist() + for dir_ in ('windows/adm/', 'windows/admx/', 'windows/examples/'): + src_zip.extractall(tmp_dir, + (n for n in namelist if n.startswith(dir_))) + + # Some sanity checks: + assert exists(join(tmp_dir, 'windows/adm/en-US/chrome.adm')) + assert exists(join(tmp_dir, 'windows/admx/chrome.admx')) + assert exists(join(tmp_dir, 'windows/admx/en-US/chrome.adml')) + + with ZipFile(dest_zip, 'w', ZIP_DEFLATED) as dest_zipfile: + for dirpath, _, filenames in os.walk(tmp_dir): + for filename in filenames: + filepath = join(dirpath, filename) + arcname = relpath(filepath, + tmp_dir).replace('chrome', 'brave') + dest_zipfile.write(filepath, arcname=arcname) if __name__ == '__main__': - main() + main() diff --git a/package.json b/package.json index 238af823a122..9d2ea0f61bc1 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "gen_env": "node ./build/commands/scripts/genEnv.js", "gen_gradle": "node ./build/commands/scripts/commands.js gen_gradle", "ios_pack_js": "webpack --config ios/brave-ios/webpack.config.js", - "ios_bootstrap": "node ./build/commands/scripts/iosCommands.js ios_bootstrap" + "ios_bootstrap": "node ./build/commands/scripts/iosCommands.js ios_bootstrap", + "build_policy_templates": "node ./build/commands/scripts/commands.js build --target=brave_group_policy_templates" }, "repository": { "type": "git",