def upgrade(): # 1. Read the push_registration_url and ttl from the Firebase Config fb_gateways = get_smsgateway(gwtype=GWTYPE) print(fb_gateways) # 2. Check which policy contains this Firebase Config P = PolicyClass() pols = P.list_policies(scope=SCOPE.ENROLL, action="{0!s}".format(PUSH_ACTION.FIREBASE_CONFIG)) # iterate through all enrollment policies for pol in pols: # Check for all firebase gateways, if this policy needs to be modified for fbgw in fb_gateways: if pol.get("action").get( PUSH_ACTION.FIREBASE_CONFIG) == fbgw.identifier: print("Modifying policy {0!s}".format(pol.get("name"))) # This is an enrollment policy, that references this very firebase config # 3. Add the push_registration_url and ttl to this policy registration_url = fbgw.option_dict.get("registration URL") ttl = fbgw.option_dict.get("time to live") # We can leave most of the parameters None, since it will update the policy. # We still need to pass the original "active" and "check_all_resolvers" params # and we need to update the action action = pol.get("action") # Only add registration_url and ttl to the policy, if these values actually exist, # to avoid deleting (setting an empty value) in the policy. if registration_url: action[PUSH_ACTION.REGISTRATION_URL] = registration_url if ttl: action[PUSH_ACTION.TTL] = ttl r = set_policy( name=pol.get("name"), scope=SCOPE.ENROLL, active=pol.get("active"), check_all_resolvers=pol.get("check_all_resolvers"), action=action) print("+- Updated policy {0!s}: {1!s}".format( pol.get("name"), r)) # 4. Delete push_registration_url and ttl from the Firebase Config # Note: If we had a firebase config, that would not be used in a policy, # the url and ttl would not be deleted from the firebase config. But this # does not matter. I like to keep it in this for-loop to avoid side unknown side effects. print("Deleting URL and TTL from the Firebase Gateway config.") if registration_url: delete_smsgateway_option(fbgw.id, "registration URL") if ttl: delete_smsgateway_option(fbgw.id, "time to live")
def test_02_create_modify_delete_smsgateway_configuration(self): identifier = "myGW" provider_module = "privacyidea.lib.smsprovider.HTTPSmsPrpvoder" id = set_smsgateway(identifier, provider_module, description="test", options={ "HTTP_METHOD": "POST", "URL": "example.com" }) self.assertTrue(id > 0) gw = get_smsgateway(id=id) self.assertEqual(gw[0].description, "test") # update the description set_smsgateway(identifier, provider_module, description="This is a sensible description") gw = get_smsgateway(id=id) self.assertEqual(gw[0].description, "This is a sensible description") # update some options set_smsgateway(identifier, provider_module, options={ "HTTP_METHOD": "POST", "URL": "example.com", "new key": "value" }) gw = get_smsgateway(id=id) self.assertEqual(len(gw[0].option_dict), 3) self.assertEqual(gw[0].option_dict.get("HTTP_METHOD"), "POST") self.assertEqual(gw[0].option_dict.get("URL"), "example.com") self.assertEqual(gw[0].option_dict.get("new key"), "value") # delete a single option r = delete_smsgateway_option(id, "URL") gw = get_smsgateway(id=id) self.assertEqual(len(gw[0].option_dict), 2) self.assertEqual(gw[0].option_dict.get("HTTP_METHOD"), "POST") self.assertEqual(gw[0].option_dict.get("URL"), None) self.assertEqual(gw[0].option_dict.get("new key"), "value") # finally delete the gateway definition r = delete_smsgateway(identifier) self.assertEqual(r, id) # delete successful? gw = get_smsgateway() self.assertEqual(len(gw), 0)
def delete_gateway_option(gwid=None, option=None): """ this function deletes an option of a gateway definition :param gwid: The id of the sms gateway definition :return: json with success or fail """ if option.startswith("option."): option = option[7:] res = delete_smsgateway_option(gwid, option) g.audit_object.log({"success": res, "info": "{0!s}/{1!s}".format(gwid, option)}) return send_result(res)
def delete_gateway_option(gwid=None, option=None): """ this function deletes an option of a gateway definition :param gwid: The id of the sms gateway definition :return: json with success or fail """ if option.startswith("option."): option = option[7:] res = delete_smsgateway_option(gwid, option) g.audit_object.log({ "success": res, "info": u"{0!s}/{1!s}".format(gwid, option) }) return send_result(res)
def test_02_create_modify_delete_smsgateway_configuration(self): identifier = "myGW" provider_module = "privacyidea.lib.smsprovider.HTTPSmsPrpvoder" id = set_smsgateway(identifier, provider_module, description="test", options={"HTTP_METHOD": "POST", "URL": "example.com"}) self.assertTrue(id > 0) gw = get_smsgateway(id=id) self.assertEqual(gw[0].description, "test") # update the description set_smsgateway(identifier, provider_module, description="This is a sensible description") gw = get_smsgateway(id=id) self.assertEqual(gw[0].description, "This is a sensible description") # update some options set_smsgateway(identifier, provider_module, options={"HTTP_METHOD": "POST", "URL": "example.com", "new key": "value"}) gw = get_smsgateway(id=id) self.assertEqual(len(gw[0].option_dict), 3) self.assertEqual(gw[0].option_dict.get("HTTP_METHOD"), "POST") self.assertEqual(gw[0].option_dict.get("URL"), "example.com") self.assertEqual(gw[0].option_dict.get("new key"), "value") # delete a single option r = delete_smsgateway_option(id, "URL") gw = get_smsgateway(id=id) self.assertEqual(len(gw[0].option_dict), 2) self.assertEqual(gw[0].option_dict.get("HTTP_METHOD"), "POST") self.assertEqual(gw[0].option_dict.get("URL"), None) self.assertEqual(gw[0].option_dict.get("new key"), "value") # finally delete the gateway definition r = delete_smsgateway(identifier) self.assertEqual(r, id) # delete successful? gw = get_smsgateway() self.assertEqual(len(gw), 0)
def test_02_create_modify_delete_smsgateway_configuration(self): identifier = "myGW" provider_module = "privacyidea.lib.smsprovider.HttpSMSProvider.HttpSMSProvider" id = set_smsgateway(identifier, provider_module, description="test", options={ "HTTP_METHOD": "POST", "URL": "example.com" }, headers={ "Authorization": "QWERTZ", "BANANA": "will be eaten" }) self.assertTrue(id > 0) gw = get_smsgateway(id=id) self.assertEqual(gw[0].description, "test") # update the description set_smsgateway(identifier, provider_module, description="This is a sensible description") gw = get_smsgateway(id=id) self.assertEqual(gw[0].description, "This is a sensible description") # update some options set_smsgateway(identifier, provider_module, options={ "HTTP_METHOD": "POST", "URL": "example.com", "IDENTICAL_KEY": "new option" }, headers={ "Authorization": "ValueChanged", "IDENTICAL_KEY": "new header", "URL": "URL_in_headers" }) gw = get_smsgateway(id=id) self.assertEqual(len(gw[0].option_dict), 3) self.assertEqual(gw[0].option_dict.get("HTTP_METHOD"), "POST") self.assertEqual(gw[0].option_dict.get("URL"), "example.com") self.assertEqual(gw[0].option_dict.get("IDENTICAL_KEY"), "new option") self.assertEqual(gw[0].header_dict.get("Authorization"), "ValueChanged") self.assertEqual(gw[0].header_dict.get("BANANA"), None) self.assertEqual(gw[0].header_dict.get("IDENTICAL_KEY"), "new header") self.assertEqual(gw[0].header_dict.get("URL"), "URL_in_headers") # delete a single option r = delete_smsgateway_option(id, "URL") gw = get_smsgateway(id=id) self.assertEqual(len(gw[0].option_dict), 2) self.assertEqual(gw[0].option_dict.get("HTTP_METHOD"), "POST") self.assertEqual(gw[0].option_dict.get("URL"), None) self.assertEqual(gw[0].option_dict.get("IDENTICAL_KEY"), "new option") # delete a single header r = delete_smsgateway_header(id, "IDENTICAL_KEY") gw = get_smsgateway(id=id) self.assertEqual(gw[0].header_dict.get("IDENTICAL_KEY"), None) # delete a single header via generic function r = delete_smsgateway_key_generic(id, "URL", Type="header") gw = get_smsgateway(id=id) self.assertEqual(gw[0].header_dict.get("URL"), None) # finally delete the gateway definition r = delete_smsgateway(identifier) self.assertEqual(r, id) # delete successful? gw = get_smsgateway() self.assertEqual(len(gw), 0)