def update_dataset(request: UpdateDatasetRequest) -> Dataset: """ Update dataset values: fetcher_active, write_back, and matcher_active. """ config = ThreatExchangeConfig.getx(str(request.privacy_group_id)) config.fetcher_active = request.fetcher_active config.write_back = request.write_back config.matcher_active = request.matcher_active updated_config = hmaconfig.update_config(config).__dict__ updated_config["privacy_group_id"] = updated_config["name"] additional_config = AdditionalMatchSettingsConfig.get( str(request.privacy_group_id)) if request.pdq_match_threshold: if additional_config: additional_config.pdq_match_threshold = int( request.pdq_match_threshold) hmaconfig.update_config(additional_config) else: additional_config = AdditionalMatchSettingsConfig( str(request.privacy_group_id), int(request.pdq_match_threshold)) hmaconfig.create_config(additional_config) elif additional_config: # pdq_match_threshold was set and now should be removed hmaconfig.delete_config(additional_config) return Dataset.from_dict(updated_config)
def update_action_rule( request: ActionRulesRequest, old_name: str, ) -> ActionRulesResponse: """ Update the action rule with name=<oldname>. """ logger.info("old_name: %s", old_name) logger.info("request: %s", request) error_message = "" if ActionRule.exists(request.action_rule.name): try: hmaconfig.update_config(request.action_rule) except Exception as e: error_message = "Unexpected error." handle_unexpected_error(e) elif ActionRule.exists(old_name): try: hmaconfig.create_config(request.action_rule) hmaconfig.delete_config_by_type_and_name("ActionRule", old_name) except Exception as e: error_message = "Unexpected error." handle_unexpected_error(e) else: error_message = f"An action rule named '{request.action_rule.name}' or '{old_name}' does not exist." logger.warning( "An attempt was made to update an action rule named either '%s' or '%s' but neither exist.", request.action_rule.name, old_name, ) response.status = 500 return ActionRulesResponse(error_message)
def edit_config(args): """Update a config of the chosen type""" kwargs = {} for field in fields(args.config_cls): kwargs[field.name] = getattr(args, field.name) config = args.config_cls(**kwargs) hmaconfig.update_config(config) print(config)
def create_or_update_match_settings( request: MatchSettingsUpdateRequest, ) -> MatchSettingsUpdateResponse: """ Create or update a match settings config for a given privacy_group_id """ if config := AdditionalMatchSettingsConfig.get( request.privacy_group_id): config.pdq_match_threshold = request.pdq_match_threshold hmaconfig.update_config(config) return MatchSettingsUpdateResponse( f"match_settings updated for pg_id {request.privacy_group_id} with pdq_match_threshold={request.pdq_match_threshold}" )
def update_action(request: CreateUpdateActionRequest, old_name: str, old_config_sub_stype: str) -> UpdateActionResponse: """ Update an action url and headers """ if old_name != request.name or old_config_sub_stype != request.config_subtype: # The name field can't be updated because it is the primary key # The config sub type can't be updated because it is the config class level param delete_action(old_name) create_action(request) else: config = ActionPerformer._get_subtypes_by_name()[ request.config_subtype].getx(request.name) for key, value in request.fields.items(): setattr(config, key, value) hmaconfig.update_config(config) return UpdateActionResponse(response="The action config is updated.")
def update_dataset(request: UpdateDatasetRequest) -> Dataset: """ Update dataset fetcher_active and write_back """ config = ThreatExchangeConfig.getx(str(request.privacy_group_id)) config.fetcher_active = request.fetcher_active config.write_back = request.write_back updated_config = hmaconfig.update_config(config).__dict__ updated_config["privacy_group_id"] = updated_config["name"] return Dataset.from_dict(updated_config)
def update_privacy_groups_in_use(priavcy_group_id_in_use: set) -> None: collabs = ThreatExchangeConfig.get_all() for collab in collabs: if str(collab.privacy_group_id) not in priavcy_group_id_in_use: collab.in_use = False hmaconfig.update_config(collab)
def update_privacy_group_description(privacy_group_id: str, description: str) -> None: config = ThreatExchangeConfig.getx(privacy_group_id) config.description = description hmaconfig.update_config(config)
def test_subconfigs(self): class MultiConfig(config.HMAConfigWithSubtypes): @staticmethod def get_subtype_classes(): return [ SubtypeOne, SubtypeTwo, SubtypeThree, ] @dataclass class SubtypeOne(MultiConfig): a: int @dataclass class SubtypeAbstractParentClass(MultiConfig): a: bool @dataclass class SubtypeTwo(MultiConfig): b: str @dataclass class SubtypeThree(SubtypeAbstractParentClass): a: t.List[float] # type: ignore one = SubtypeOne("One", 5) two = SubtypeTwo("Two", "five") three = SubtypeThree("Three", [5.0, 0.00001]) # ah ah ah config.create_config(one) config.create_config(two) config.create_config(three) self.assertEqualsAfterDynamodb(one) self.assertEqualsAfterDynamodb(two) self.assertEqualsAfterDynamodb(three) # Getting by the superclass gets you all of them self.assertCountEqual([one, two, three], MultiConfig.get_all()) self.assertEqual(one, MultiConfig.get("One")) self.assertEqual(three, MultiConfig.get("Three")) # Getting by the subclass gets you one of them self.assertEqual(three, SubtypeThree.get("Three")) self.assertIsNone(SubtypeOne.get("Three")) # Renaming behavior stomps on the old one one_replaced = SubtypeTwo("One", "replaces two") config.update_config(one_replaced) self.assertIsNone(SubtypeOne.get("One")) self.assertEqual(one_replaced, MultiConfig.get("One")) self.assertEqual(one_replaced, SubtypeTwo.get("One")) # Writing the superclass gives you an error with self.assertRaisesRegex( ValueError, "Tried to write MultiConfig instead of its subtypes"): config.create_config(MultiConfig("Foo")) # Writing the "abstract" config gives you an error with self.assertRaisesRegex( ValueError, "Tried to write subtype SubtypeAbstractParentClass" " but it's not in get_subtype_classes", ): config.create_config(SubtypeAbstractParentClass("Foo", False))