permission_controller.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from fate_flow.db.fate_casbin import CB
  2. from fate_flow.utils.log_utils import getLogger
  3. from fate_flow.entity.permission_parameters import PermissionParameters, DataSet, CheckReturn
  4. from fate_flow.entity.types import PermissionType
  5. from fate_flow.hook.common.parameters import PermissionReturn
  6. from fate_flow.settings import DATASET_PERMISSION, COMPONENT_PERMISSION, CASBIN_MODEL_CONF
  7. logger = getLogger("permission")
  8. class PermissionController:
  9. def __init__(self, src_party_id):
  10. self.src_party_id = str(src_party_id)
  11. self.casbin_controller = CB
  12. if not self.casbin_controller:
  13. raise Exception("No permission controller is found, please check whether the switch of permission control"
  14. " is turned on")
  15. def check(self, permission_type, value):
  16. logger.info(f"check source party id {self.src_party_id} {permission_type} {value}")
  17. result = self.casbin_controller.enforce(self.src_party_id, permission_type, value)
  18. logger.info(f"result: {result}")
  19. return result
  20. def grant_or_delete(self, permission_parameters: PermissionParameters):
  21. logger.info(f"{'grant' if not permission_parameters.is_delete else 'delete'} parameters:"
  22. f" {permission_parameters.to_dict()}")
  23. self.check_parameters(permission_parameters)
  24. for permission_type in PermissionType.values():
  25. permission_value = getattr(permission_parameters, permission_type)
  26. if permission_value:
  27. if permission_value != "*":
  28. if permission_type in [PermissionType.COMPONENT.value]:
  29. value_list = [value.strip() for value in permission_value.split(self.value_delimiter)]
  30. elif permission_type in [PermissionType.DATASET.value]:
  31. if isinstance(permission_value, list):
  32. value_list = [DataSet(**value).casbin_value for value in permission_value]
  33. else:
  34. value_list = [DataSet(**permission_value).casbin_value]
  35. else:
  36. raise ValueError(f"permission type {permission_type} is not supported")
  37. for value in value_list:
  38. if not permission_parameters.is_delete:
  39. self.casbin_controller.grant(self.src_party_id, permission_type, value)
  40. else:
  41. self.casbin_controller.delete(self.src_party_id, permission_type, value)
  42. else:
  43. if not permission_parameters.is_delete:
  44. for value in self.all_value(permission_type):
  45. self.casbin_controller.grant(self.src_party_id, permission_type, value)
  46. else:
  47. self.casbin_controller.delete_all(self.src_party_id, permission_type)
  48. def query(self):
  49. result = {PermissionType.DATASET.value: [], PermissionType.COMPONENT.value: []}
  50. for casbin_result in self.casbin_controller.query(self.src_party_id):
  51. if casbin_result[1] == PermissionType.DATASET.value:
  52. casbin_result[2] = DataSet.load_casbin_value(casbin_result[2])
  53. result[casbin_result[1]].append(casbin_result[2])
  54. return result
  55. def check_parameters(self, permission_parameters):
  56. for permission_type in PermissionType.values():
  57. permission_value = getattr(permission_parameters, permission_type)
  58. if permission_value:
  59. if permission_type == PermissionType.COMPONENT.value and not COMPONENT_PERMISSION:
  60. raise ValueError(f"component permission switch is {COMPONENT_PERMISSION}")
  61. if permission_type == PermissionType.DATASET.value and not DATASET_PERMISSION:
  62. raise ValueError(f"dataset permission switch is {DATASET_PERMISSION}")
  63. if permission_type in [PermissionType.COMPONENT.value]:
  64. if permission_value != "*":
  65. value_list = [value.strip() for value in permission_value.split(self.value_delimiter)]
  66. self.check_values(permission_type, value_list)
  67. if permission_type in [PermissionType.DATASET.value]:
  68. if isinstance(permission_value, list):
  69. for dataset in permission_value:
  70. DataSet(**dataset).check()
  71. elif isinstance(permission_value, dict):
  72. DataSet(**permission_value).check()
  73. elif permission_value == "*":
  74. pass
  75. else:
  76. raise ValueError(f"permission type {permission_type} value {permission_value} error")
  77. def check_values(self, permission_type, values):
  78. error_value = []
  79. value_list = self.all_value(permission_type)
  80. for value in values:
  81. if value not in value_list:
  82. error_value.append(value)
  83. if error_value:
  84. raise ValueError(f"permission type {permission_type} value {error_value} error")
  85. def all_value(self, permission_type):
  86. if permission_type == PermissionType.COMPONENT.value:
  87. value_list = self.all_component
  88. else:
  89. raise Exception(f"permission type {permission_type} not support grant all")
  90. return value_list
  91. @property
  92. def all_component(self):
  93. from fate_flow.db.db_models import ComponentInfo
  94. component_list = []
  95. for component in ComponentInfo.select():
  96. component_list.append(component.f_component_name.lower())
  97. return component_list
  98. @property
  99. def value_delimiter(self):
  100. return ","
  101. class PermissionCheck(object):
  102. def __init__(self, src_party_id, component_list, dataset_list, **kwargs):
  103. self.component_list = component_list
  104. self.dataset_list = dataset_list
  105. self.controller = PermissionController(src_party_id)
  106. def check_component(self) -> PermissionReturn:
  107. for component_name in self.component_list:
  108. if not self.controller.check(PermissionType.COMPONENT.value, component_name):
  109. return PermissionReturn(CheckReturn.NO_COMPONENT_PERMISSION, f"check component permission failed: {component_name}")
  110. return PermissionReturn()
  111. def check_dataset(self) -> PermissionReturn:
  112. for dataset in self.dataset_list:
  113. if not self.controller.check(PermissionType.DATASET.value, dataset.casbin_value):
  114. return PermissionReturn(CheckReturn.NO_DATASET_PERMISSION, f"check dataset permission failed: {dataset.value}")
  115. return PermissionReturn()