mysql_util.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from dataclasses import dataclass
  10. from materialize.mzcompose.composition import Composition
  11. @dataclass
  12. class MySqlSslContext:
  13. ca: str
  14. client_cert: str
  15. client_key: str
  16. def retrieve_ssl_context_for_mysql(c: Composition) -> MySqlSslContext:
  17. # MySQL generates self-signed certificates for SSL connections on startup,
  18. # for both the server and client:
  19. # https://dev.mysql.com/doc/refman/8.3/en/creating-ssl-rsa-files-using-mysql.html
  20. # Grab the correct Server CA and Client Key and Cert from the MySQL container
  21. # (and strip the trailing null byte):
  22. ssl_ca = c.exec("mysql", "cat", "/var/lib/mysql/ca.pem", capture=True).stdout.split(
  23. "\x00", 1
  24. )[0]
  25. ssl_client_cert = c.exec(
  26. "mysql", "cat", "/var/lib/mysql/client-cert.pem", capture=True
  27. ).stdout.split("\x00", 1)[0]
  28. ssl_client_key = c.exec(
  29. "mysql", "cat", "/var/lib/mysql/client-key.pem", capture=True
  30. ).stdout.split("\x00", 1)[0]
  31. return MySqlSslContext(ssl_ca, ssl_client_cert, ssl_client_key)
  32. def retrieve_invalid_ssl_context_for_mysql(c: Composition) -> MySqlSslContext:
  33. # Use the TestCert service to obtain a wrong CA and client cert/key:
  34. c.up({"name": "test-certs", "persistent": True})
  35. ssl_wrong_ca = c.run("test-certs", "cat", "/secrets/ca.crt", capture=True).stdout
  36. ssl_wrong_client_cert = c.run(
  37. "test-certs", "cat", "/secrets/certuser.crt", capture=True
  38. ).stdout
  39. ssl_wrong_client_key = c.run(
  40. "test-certs", "cat", "/secrets/certuser.key", capture=True
  41. ).stdout
  42. return MySqlSslContext(ssl_wrong_ca, ssl_wrong_client_cert, ssl_wrong_client_key)