Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 57501d9

Browse files
authored
Update sign_json to support inline key config (#11139)
It's been possible to configure a key inline in the homeserver.yaml since 13bc1e0. Update `sign_json` to work with this.
1 parent 62db603 commit 57501d9

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

changelog.d/11139.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update the `sign_json` script to support inline configuration of the signing key.

scripts-dev/sign_json

+25-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,19 @@ Example usage:
5151
"request with.",
5252
)
5353

54+
parser.add_argument(
55+
"-K",
56+
"--signing-key",
57+
help="The private ed25519 key to sign the request with.",
58+
)
59+
5460
parser.add_argument(
5561
"-c",
5662
"--config",
5763
default="homeserver.yaml",
5864
help=(
5965
"Path to synapse config file, from which the server name and/or signing "
60-
"key path will be read. Ignored if --server-name and --signing-key-path "
66+
"key path will be read. Ignored if --server-name and --signing-key(-path) "
6167
"are both given."
6268
),
6369
)
@@ -87,11 +93,14 @@ Example usage:
8793

8894
args = parser.parse_args()
8995

90-
if not args.server_name or not args.signing_key_path:
96+
if not args.server_name or not (args.signing_key_path or args.signing_key):
9197
read_args_from_config(args)
9298

93-
with open(args.signing_key_path) as f:
94-
key = read_signing_keys(f)[0]
99+
if args.signing_key:
100+
keys = read_signing_keys([args.signing_key])
101+
else:
102+
with open(args.signing_key_path) as f:
103+
keys = read_signing_keys(f)
95104

96105
json_to_sign = args.input_data
97106
if json_to_sign is None:
@@ -107,7 +116,7 @@ Example usage:
107116
print("Input json was not an object", file=sys.stderr)
108117
sys.exit(1)
109118

110-
sign_json(obj, args.server_name, key)
119+
sign_json(obj, args.server_name, keys[0])
111120
for c in json_encoder.iterencode(obj):
112121
args.output.write(c)
113122
args.output.write("\n")
@@ -118,8 +127,17 @@ def read_args_from_config(args: argparse.Namespace) -> None:
118127
config = yaml.safe_load(fh)
119128
if not args.server_name:
120129
args.server_name = config["server_name"]
121-
if not args.signing_key_path:
122-
args.signing_key_path = config["signing_key_path"]
130+
if not args.signing_key_path and not args.signing_key:
131+
if "signing_key" in config:
132+
args.signing_key = config["signing_key"]
133+
elif "signing_key_path" in config:
134+
args.signing_key_path = config["signing_key_path"]
135+
else:
136+
print(
137+
"A signing key must be given on the commandline or in the config file.",
138+
file=sys.stderr,
139+
)
140+
sys.exit(1)
123141

124142

125143
if __name__ == "__main__":

0 commit comments

Comments
 (0)