1
0

convert_server_keys.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import hashlib
  2. import json
  3. import sys
  4. import time
  5. import psycopg2
  6. import yaml
  7. from canonicaljson import encode_canonical_json
  8. from signedjson.key import read_signing_keys
  9. from signedjson.sign import sign_json
  10. from unpaddedbase64 import encode_base64
  11. db_binary_type = memoryview
  12. def select_v1_keys(connection):
  13. cursor = connection.cursor()
  14. cursor.execute("SELECT server_name, key_id, verify_key FROM server_signature_keys")
  15. rows = cursor.fetchall()
  16. cursor.close()
  17. results = {}
  18. for server_name, key_id, verify_key in rows:
  19. results.setdefault(server_name, {})[key_id] = encode_base64(verify_key)
  20. return results
  21. def select_v1_certs(connection):
  22. cursor = connection.cursor()
  23. cursor.execute("SELECT server_name, tls_certificate FROM server_tls_certificates")
  24. rows = cursor.fetchall()
  25. cursor.close()
  26. results = {}
  27. for server_name, tls_certificate in rows:
  28. results[server_name] = tls_certificate
  29. return results
  30. def select_v2_json(connection):
  31. cursor = connection.cursor()
  32. cursor.execute("SELECT server_name, key_id, key_json FROM server_keys_json")
  33. rows = cursor.fetchall()
  34. cursor.close()
  35. results = {}
  36. for server_name, key_id, key_json in rows:
  37. results.setdefault(server_name, {})[key_id] = json.loads(
  38. str(key_json).decode("utf-8")
  39. )
  40. return results
  41. def convert_v1_to_v2(server_name, valid_until, keys, certificate):
  42. return {
  43. "old_verify_keys": {},
  44. "server_name": server_name,
  45. "verify_keys": {key_id: {"key": key} for key_id, key in keys.items()},
  46. "valid_until_ts": valid_until,
  47. "tls_fingerprints": [fingerprint(certificate)],
  48. }
  49. def fingerprint(certificate):
  50. finger = hashlib.sha256(certificate)
  51. return {"sha256": encode_base64(finger.digest())}
  52. def rows_v2(server, json):
  53. valid_until = json["valid_until_ts"]
  54. key_json = encode_canonical_json(json)
  55. for key_id in json["verify_keys"]:
  56. yield (server, key_id, "-", valid_until, valid_until, db_binary_type(key_json))
  57. def main():
  58. config = yaml.safe_load(open(sys.argv[1]))
  59. valid_until = int(time.time() / (3600 * 24)) * 1000 * 3600 * 24
  60. server_name = config["server_name"]
  61. signing_key = read_signing_keys(open(config["signing_key_path"]))[0]
  62. database = config["database"]
  63. assert database["name"] == "psycopg2", "Can only convert for postgresql"
  64. args = database["args"]
  65. args.pop("cp_max")
  66. args.pop("cp_min")
  67. connection = psycopg2.connect(**args)
  68. keys = select_v1_keys(connection)
  69. certificates = select_v1_certs(connection)
  70. json = select_v2_json(connection)
  71. result = {}
  72. for server in keys:
  73. if server not in json:
  74. v2_json = convert_v1_to_v2(
  75. server, valid_until, keys[server], certificates[server]
  76. )
  77. v2_json = sign_json(v2_json, server_name, signing_key)
  78. result[server] = v2_json
  79. yaml.safe_dump(result, sys.stdout, default_flow_style=False)
  80. rows = [row for server, json in result.items() for row in rows_v2(server, json)]
  81. cursor = connection.cursor()
  82. cursor.executemany(
  83. "INSERT INTO server_keys_json ("
  84. " server_name, key_id, from_server,"
  85. " ts_added_ms, ts_valid_until_ms, key_json"
  86. ") VALUES (%s, %s, %s, %s, %s, %s)",
  87. rows,
  88. )
  89. connection.commit()
  90. if __name__ == "__main__":
  91. main()