Automate PrivilegeRegistry to code
This commit attempts to automate the creation of our privileges
structures from the redfish privilege registry. It accomplishes this by
updating parse_registries.py to also pull down the privilege registry
from DMTF.
The script then generates privilege_registry.hpp, which include const
defines for all the privilege registry entries in the same format that
the Privileges struct accepts. This allows new clients to simply
reference the variable to these privilege structures, instead of having
to manually (ie error pronely) put the privileges in themselves.
This commit updates all the routes.
For the moment, override and OEM schemas are not considered. Today we
don't have any OEM-specific Redfish routes, so the existing ones inherit
their parents schema. Overrides have other issues, and are already
incorrect as Redfish defines them.
Binary size remains unchanged after this patchset.
Tested:
Ran redfish service validator
Ran test case from f9a6708c4c6490257e2eb6a8c04458f500902476 to ensure
that the new privileges constructor didn't cause us to regress the brace
construction initializer.
Checked binary size with:
gzip -c
$BBPATH/tmp/work/s7106-openbmc-linux-gnueabi/obmc-phosphor-image/1.0-r0/rootfs/usr/bin/bmcweb
| wc -c
1244048
(tested on previous patchset)
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Ideede3d5b39d50bffe7fe78a0848bdbc22ac387f
diff --git a/scripts/parse_registries.py b/scripts/parse_registries.py
index 05fc62d..a94fc04 100755
--- a/scripts/parse_registries.py
+++ b/scripts/parse_registries.py
@@ -63,6 +63,10 @@
return (path, json_file, type_name, url)
+def clang_format(filename):
+ subprocess.check_call(["clang-format-11", "-i", filename])
+
+
files = []
files.append(make_getter('Base.1.10.0.json',
'base_message_registry.hpp', 'base'))
@@ -115,4 +119,73 @@
registry.write("\"{}\",".format(message["Resolution"]))
registry.write("}},")
registry.write("};}\n")
- subprocess.check_call(["clang-format-11", "-i", file])
+ clang_format(file)
+
+
+def get_privilege_string_from_list(privilege_list):
+ privilege_string = "{{\n"
+ for privilege_json in privilege_list:
+ privileges = privilege_json["Privilege"]
+ privilege_string += " {"
+ for privilege in privileges:
+ if privilege == "NoAuth":
+ continue
+ privilege_string += "\""
+ privilege_string += privilege
+ privilege_string += "\",\n"
+ if privilege != "NoAuth":
+ privilege_string = privilege_string[:-2]
+ privilege_string += "}"
+ privilege_string += ",\n"
+ privilege_string = privilege_string[:-2]
+ privilege_string += "\n}}"
+ return privilege_string
+
+def get_variable_name_for_privilege_set(privilege_list):
+ names = []
+ for privilege_json in privilege_list:
+ privileges = privilege_json["Privilege"]
+ names.append("And".join(privileges))
+ return "Or".join(names)
+
+def make_privilege_registry():
+ path, json_file, type_name, url = make_getter('Redfish_1.1.0_PrivilegeRegistry.json',
+ 'privilege_registry.hpp', 'privilege')
+ with open(path, 'w') as registry:
+ registry.write("#pragma once\n")
+ registry.write("//{} is generated. Do not edit directly\n".format(os.path.basename(path)))
+
+ registry.write("#include <privileges.hpp>\n\n")
+ registry.write("namespace redfish::privileges{\n")
+
+ privilege_dict = {}
+ for mapping in json_file["Mappings"]:
+ # first pass, identify all the unique privilege sets
+ for operation, privilege_list in mapping["OperationMap"].items():
+ privilege_dict[get_privilege_string_from_list(
+ privilege_list)] = (privilege_list, )
+ registry.write("// clang-format off\n")
+ for index, key in enumerate(privilege_dict):
+ (privilege_list, ) = privilege_dict[key]
+ name = get_variable_name_for_privilege_set(privilege_list)
+ registry.write("const std::array<Privileges, {}> privilegeSet{} = {};\n".format(
+ len(privilege_list), name, key))
+ privilege_dict[key] = (privilege_list, name)
+ registry.write("// clang-format on\n\n")
+
+ for mapping in json_file["Mappings"]:
+ entity = mapping["Entity"]
+ registry.write("//{}\n".format(entity))
+ for operation, privilege_list in mapping["OperationMap"].items():
+ privilege_string = get_privilege_string_from_list(
+ privilege_list)
+ operation = operation.lower()
+
+ registry.write("const auto& {}{} = privilegeSet{};\n".format(
+ operation, entity , privilege_dict[privilege_string][1]))
+ registry.write("\n")
+ registry.write("} // namespace redfish::privileges\n")
+ clang_format(path)
+
+
+make_privilege_registry()