Skip to content

Commit

Permalink
Add new build_policy_templates action
Browse files Browse the repository at this point in the history
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 brave/brave-browser#41217
  • Loading branch information
bsclifton committed Sep 24, 2024
1 parent 203bf41 commit 30d6814
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 30 deletions.
9 changes: 9 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 14 additions & 1 deletion components/policy/BUILD.gn
Original file line number Diff line number Diff line change
@@ -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") +
Expand Down
64 changes: 36 additions & 28 deletions components/policy/pack_policy_templates.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 30d6814

Please sign in to comment.