sign_json 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 json
  18. import sys
  19. from json import JSONDecodeError
  20. import yaml
  21. from signedjson.key import read_signing_keys
  22. from signedjson.sign import sign_json
  23. from synapse.util import json_encoder
  24. def main():
  25. parser = argparse.ArgumentParser(
  26. description="""Adds a signature to a JSON object.
  27. Example usage:
  28. $ scripts-dev/sign_json.py -N test -k localhost.signing.key "{}"
  29. {"signatures":{"test":{"ed25519:a_ZnZh":"LmPnml6iM0iR..."}}}
  30. """,
  31. formatter_class=argparse.RawDescriptionHelpFormatter,
  32. )
  33. parser.add_argument(
  34. "-N",
  35. "--server-name",
  36. help="Name to give as the local homeserver. If unspecified, will be "
  37. "read from the config file.",
  38. )
  39. parser.add_argument(
  40. "-k",
  41. "--signing-key-path",
  42. help="Path to the file containing the private ed25519 key to sign the "
  43. "request with.",
  44. )
  45. parser.add_argument(
  46. "-K",
  47. "--signing-key",
  48. help="The private ed25519 key to sign the request with.",
  49. )
  50. parser.add_argument(
  51. "-c",
  52. "--config",
  53. default="homeserver.yaml",
  54. help=(
  55. "Path to synapse config file, from which the server name and/or signing "
  56. "key path will be read. Ignored if --server-name and --signing-key(-path) "
  57. "are both given."
  58. ),
  59. )
  60. input_args = parser.add_mutually_exclusive_group()
  61. input_args.add_argument("input_data", nargs="?", help="Raw JSON to be signed.")
  62. input_args.add_argument(
  63. "-i",
  64. "--input",
  65. type=argparse.FileType("r"),
  66. default=sys.stdin,
  67. help=(
  68. "A file from which to read the JSON to be signed. If neither --input nor "
  69. "input_data are given, JSON will be read from stdin."
  70. ),
  71. )
  72. parser.add_argument(
  73. "-o",
  74. "--output",
  75. type=argparse.FileType("w"),
  76. default=sys.stdout,
  77. help="Where to write the signed JSON. Defaults to stdout.",
  78. )
  79. args = parser.parse_args()
  80. if not args.server_name or not (args.signing_key_path or args.signing_key):
  81. read_args_from_config(args)
  82. if args.signing_key:
  83. keys = read_signing_keys([args.signing_key])
  84. else:
  85. with open(args.signing_key_path) as f:
  86. keys = read_signing_keys(f)
  87. json_to_sign = args.input_data
  88. if json_to_sign is None:
  89. json_to_sign = args.input.read()
  90. try:
  91. obj = json.loads(json_to_sign)
  92. except JSONDecodeError as e:
  93. print("Unable to parse input as JSON: %s" % e, file=sys.stderr)
  94. sys.exit(1)
  95. if not isinstance(obj, dict):
  96. print("Input json was not an object", file=sys.stderr)
  97. sys.exit(1)
  98. sign_json(obj, args.server_name, keys[0])
  99. for c in json_encoder.iterencode(obj):
  100. args.output.write(c)
  101. args.output.write("\n")
  102. def read_args_from_config(args: argparse.Namespace) -> None:
  103. with open(args.config, "r") as fh:
  104. config = yaml.safe_load(fh)
  105. if not args.server_name:
  106. args.server_name = config["server_name"]
  107. if not args.signing_key_path and not args.signing_key:
  108. if "signing_key" in config:
  109. args.signing_key = config["signing_key"]
  110. elif "signing_key_path" in config:
  111. args.signing_key_path = config["signing_key_path"]
  112. else:
  113. print(
  114. "A signing key must be given on the commandline or in the config file.",
  115. file=sys.stderr,
  116. )
  117. sys.exit(1)
  118. if __name__ == "__main__":
  119. main()