gen_provenance_metadata.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2022 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import argparse
  17. import hashlib
  18. import os.path
  19. import sys
  20. import google.protobuf.text_format as text_format
  21. import provenance_metadata_pb2
  22. def Log(*info):
  23. if args.verbose:
  24. for i in info:
  25. print(i)
  26. def ParseArgs(argv):
  27. parser = argparse.ArgumentParser(description='Create provenance metadata for a prebuilt artifact')
  28. parser.add_argument('-v', '--verbose', action='store_true', help='Print more information in execution')
  29. parser.add_argument('--module_name', help='Module name', required=True)
  30. parser.add_argument('--artifact_path', help='Relative path of the prebuilt artifact in source tree', required=True)
  31. parser.add_argument('--install_path', help='Absolute path of the artifact in the filesystem images', required=True)
  32. parser.add_argument('--metadata_path', help='Path of the provenance metadata file created for the artifact', required=True)
  33. return parser.parse_args(argv)
  34. def main(argv):
  35. global args
  36. args = ParseArgs(argv)
  37. Log("Args:", vars(args))
  38. provenance_metadata = provenance_metadata_pb2.ProvenanceMetadata()
  39. provenance_metadata.module_name = args.module_name
  40. provenance_metadata.artifact_path = args.artifact_path
  41. provenance_metadata.artifact_install_path = args.install_path
  42. Log("Generating SHA256 hash")
  43. h = hashlib.sha256()
  44. with open(args.artifact_path, "rb") as artifact_file:
  45. h.update(artifact_file.read())
  46. provenance_metadata.artifact_sha256 = h.hexdigest()
  47. Log("Check if there is attestation for the artifact")
  48. attestation_file_name = args.artifact_path + ".intoto.jsonl"
  49. if os.path.isfile(attestation_file_name):
  50. provenance_metadata.attestation_path = attestation_file_name
  51. text_proto = [
  52. "# proto-file: build/soong/provenance/proto/provenance_metadata.proto",
  53. "# proto-message: ProvenanceMetaData",
  54. "",
  55. text_format.MessageToString(provenance_metadata)
  56. ]
  57. with open(args.metadata_path, "wt") as metadata_file:
  58. file_content = "\n".join(text_proto)
  59. Log("Writing provenance metadata in textproto:", file_content)
  60. metadata_file.write(file_content)
  61. if __name__ == '__main__':
  62. main(sys.argv[1:])