Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions utils/autotailor
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class Tailoring:
self.id = "xccdf_auto_tailoring_default"
self.version = 1
self.original_ds_filename = ""
self.use_relative_path = False

self.profiles = []

Expand All @@ -319,8 +320,19 @@ class Tailoring:
root.set("id", self.id)

benchmark = ET.SubElement(root, "{%s}benchmark" % NS)
datastream_uri = pathlib.Path(
self.original_ds_filename).absolute().as_uri()
if self.use_relative_path:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The to_xml method starts getting long. I suggest extracting the code that determines the URI to a separate method eg. _get_datastream_uri()

# Preserve relative paths as-is, convert absolute paths to basename
ds_path = pathlib.Path(self.original_ds_filename)
if ds_path.is_absolute():
# Convert absolute path to basename for compatibility with layered products
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option name is -relative-path but if this branch is executed there will be a basename which doesn't have to be a relative path. Consider some better name for the command line option.

datastream_uri = ds_path.name
else:
# Keep relative paths as provided by the user
datastream_uri = self.original_ds_filename
else:
# Use absolute URI (default behavior for backward compatibility)
datastream_uri = pathlib.Path(
self.original_ds_filename).absolute().as_uri()
benchmark.set("href", datastream_uri)

version = ET.SubElement(root, "{%s}version" % NS)
Expand Down Expand Up @@ -433,6 +445,10 @@ def get_parser():
"-o", "--output", default="-",
help="Where to save the tailoring file. If not supplied, write to "
"standard output.")
parser.add_argument(
"--relative-path", action="store_true",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the new option in the man page.

help="Use relative path (basename only) for the benchmark href instead of "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test that covers this feature.

"absolute file:// URI.")
return parser


Expand All @@ -447,6 +463,7 @@ if __name__ == "__main__":
t = Tailoring()
t.original_ds_filename = args.datastream
t.reverse_dns = args.id_namespace
t.use_relative_path = args.relative_path

if args.json_tailoring:
t.import_json_tailoring(args.json_tailoring)
Expand Down
Loading