conf_utils.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. from filelock import FileLock
  18. from importlib import import_module
  19. from fate_arch.common import file_utils
  20. SERVICE_CONF = "service_conf.yaml"
  21. TRANSFER_CONF = "transfer_conf.yaml"
  22. def conf_realpath(conf_name):
  23. conf_path = f"conf/{conf_name}"
  24. return os.path.join(file_utils.get_project_base_directory(), conf_path)
  25. def get_base_config(key, default=None, conf_name=SERVICE_CONF) -> dict:
  26. local_config = {}
  27. local_path = conf_realpath(f'local.{conf_name}')
  28. if os.path.exists(local_path):
  29. local_config = file_utils.load_yaml_conf(local_path)
  30. if not isinstance(local_config, dict):
  31. raise ValueError(f'Invalid config file: "{local_path}".')
  32. if key is not None and key in local_config:
  33. return local_config[key]
  34. config_path = conf_realpath(conf_name)
  35. config = file_utils.load_yaml_conf(config_path)
  36. if not isinstance(config, dict):
  37. raise ValueError(f'Invalid config file: "{config_path}".')
  38. config.update(local_config)
  39. return config.get(key, default) if key is not None else config
  40. def decrypt_database_password(password):
  41. encrypt_password = get_base_config("encrypt_password", False)
  42. encrypt_module = get_base_config("encrypt_module", False)
  43. private_key = get_base_config("private_key", None)
  44. if not password or not encrypt_password:
  45. return password
  46. if not private_key:
  47. raise ValueError("No private key")
  48. module_fun = encrypt_module.split("#")
  49. pwdecrypt_fun = getattr(import_module(module_fun[0]), module_fun[1])
  50. return pwdecrypt_fun(private_key, password)
  51. def decrypt_database_config(database=None, passwd_key="passwd"):
  52. if not database:
  53. database = get_base_config("database", {})
  54. database[passwd_key] = decrypt_database_password(database[passwd_key])
  55. return database
  56. def update_config(key, value, conf_name=SERVICE_CONF):
  57. conf_path = conf_realpath(conf_name=conf_name)
  58. if not os.path.isabs(conf_path):
  59. conf_path = os.path.join(file_utils.get_project_base_directory(), conf_path)
  60. with FileLock(os.path.join(os.path.dirname(conf_path), ".lock")):
  61. config = file_utils.load_yaml_conf(conf_path=conf_path) or {}
  62. config[key] = value
  63. file_utils.rewrite_yaml_conf(conf_path=conf_path, config=config)