Skip to content

Commit

Permalink
pw_software_update: Handle pw update inspect-root-metadata
Browse files Browse the repository at this point in the history
Add handling for visualization of contents for a given root metadata
protobuf file. The contents are printed to the terminal as defined
by the RootMetadata message structure in "tuf.proto" and also prints
the number of identified signatures.

Change-Id: If2e33c89023ad7de734ae657eea9e375ea4bc0f9
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/117010
Reviewed-by: Ali Zhang <alizhang@google.com>
Commit-Queue: Eashan Singh <eashansingh@google.com>
  • Loading branch information
Eashan Singh authored and CQ Bot Account committed Nov 4, 2022
1 parent 47f897d commit 1cad75b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
44 changes: 43 additions & 1 deletion pw_software_update/py/pw_software_update/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,50 @@
import sys
from pathlib import Path
from pw_software_update import dev_sign, keys, root_metadata
from pw_software_update.tuf_pb2 import SignedRootMetadata
from pw_software_update.tuf_pb2 import (RootMetadata, SignedRootMetadata)


def inspect_root_metadata_handler(arg) -> None:
"""Prints root metadata contents as defined by "RootMetadata" message
structure in tuf.proto as well as the number of identified signatures.
"""

try:
signed_root_metadata = SignedRootMetadata.FromString(
arg.pathname.read_bytes())

deserialized_root_metadata = RootMetadata.FromString(
signed_root_metadata.serialized_root_metadata)
print(deserialized_root_metadata)

print('Number of signatures found:',
len(signed_root_metadata.signatures))

except IOError as error:
print(error)


def _add_inspect_root_metadata_parser(subparsers) -> None:
"""Parser to handle inspect-root-metadata subcommand"""

formatter_class = lambda prog: argparse.HelpFormatter(
prog, max_help_position=100, width=200)
inspect_root_metadata_parser = subparsers.add_parser(
'inspect-root-metadata',
description='Outputs contents of root metadata',
formatter_class=formatter_class,
help="")

inspect_root_metadata_parser.set_defaults(
func=inspect_root_metadata_handler)
inspect_root_metadata_parser.add_argument('pathname',
type=Path,
help='Path to root metadata')


def sign_root_metadata_handler(arg) -> None:
"""Handler for signing of root metadata"""

try:
signed_root_metadata = dev_sign.sign_root_metadata(
SignedRootMetadata.FromString(arg.root_metadata.read_bytes()),
Expand Down Expand Up @@ -66,6 +105,7 @@ def _add_sign_root_metadata_parser(subparsers) -> None:

def create_root_metadata_handler(arg) -> None:
"""Handler function for creation of root metadata."""

try:
root_metadata.main(arg.out, arg.append_root_key,
arg.append_targets_key, arg.version)
Expand Down Expand Up @@ -153,8 +193,10 @@ def _parse_args() -> argparse.Namespace:

subparsers = parser_root.add_subparsers()
_add_generate_key_parser(subparsers)

_add_create_root_metadata_parser(subparsers)
_add_sign_root_metadata_parser(subparsers)
_add_inspect_root_metadata_parser(subparsers)

return parser_root.parse_args()

Expand Down
1 change: 1 addition & 0 deletions pw_software_update/py/pw_software_update/root_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def main(out: Path, root_key: Iterable[Path], targets_key: Iterable[Path],
TargetsKeys([k.read_bytes() for k in targets_key]), version)
signed = SignedRootMetadata(
serialized_root_metadata=root_metadata.SerializeToString())

out.write_bytes(signed.SerializeToString())


Expand Down

0 comments on commit 1cad75b

Please sign in to comment.