getpubkeys 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  2. # Read a manifest.yaml file, and for each listed node, execute it with
  3. # the --gen option to generate (or load if they already exist) a
  4. # private/public key pair. It will output the public key to stdout,
  5. # which we capture. At the end, output all of the public keys to a
  6. # pubkeys.yaml file.
  7. # Usage: mkpubkeys [manifestfile.yaml [pubkeyfile.yaml]]
  8. import re
  9. import shlex
  10. import subprocess
  11. import sys
  12. import yaml
  13. # The default input file
  14. MANIFEST = "manifest.yaml"
  15. # The default output file
  16. PUBKEYS = "pubkeys.yaml"
  17. # The TEEMS binary
  18. TEEMS = "./teems"
  19. def getkey(node, manifestdata):
  20. """Get the public key for a particular node (as a 128-character hex
  21. string). The passed manifestdata is the dictionary corresponding to
  22. that node in the manifest."""
  23. cmdline = ''
  24. if 'launchprefix' in manifestdata:
  25. cmdline = manifestdata['launchprefix'] + ' '
  26. cmdline += TEEMS + " --gen %(sprvfile)s %(pubfile)s" % manifestdata
  27. pubkey = None
  28. try:
  29. print("Fetching pubkey for", node)
  30. out = subprocess.run(shlex.split(cmdline), stdout=subprocess.PIPE) \
  31. .stdout.decode('utf-8')
  32. res = re.search('^Pubkey: ([0-9a-f]{128})', out, re.MULTILINE)
  33. if res:
  34. pubkey = res.group(1)
  35. else:
  36. print(res)
  37. except:
  38. # Couldn't find the output
  39. pass
  40. return pubkey
  41. if __name__ == "__main__":
  42. if len(sys.argv) > 1:
  43. MANIFEST = sys.argv[1]
  44. if len(sys.argv) > 2:
  45. PUBKEYS = sys.argv[2]
  46. # A dictionary to store the output pubkeys
  47. pubkeys = {}
  48. # Read the manifest
  49. with open(MANIFEST) as mf:
  50. manifest = yaml.safe_load(mf)
  51. for (node, data) in manifest.items():
  52. key = getkey(node, data)
  53. if key is not None:
  54. pubkeys[node] = key
  55. print('')
  56. print(yaml.dump(pubkeys))
  57. with open(PUBKEYS, 'w') as pf:
  58. yaml.dump(pubkeys, pf)