@@ -0,0 +1,2639 @@
+%22%22%22v3.8: migrate WebAuthn credential algorithm selection%0A%0ARevision ID: 00762b3f7a60%0ARevises: 86f40f535d7c%0ACreate Date: 2022-08-31 11:24:12.226997%0A%0A%22%22%22%0A%0A# revision identifiers, used by Alembic.%0Arevision = '00762b3f7a60'%0Adown_revision = '86f40f535d7c'%0A%0Aimport re%0Afrom alembic import op%0Afrom sqlalchemy import orm, update%0Afrom privacyidea.models import Policy%0A%0Aold_policy_action = 'webauthn_public_key_credential_algorithm_preference'%0Anew_policy_action = 'webauthn_public_key_credential_algorithms'%0Acred_alg_map = %7B%0A    'ecdsa_preferred': 'ecdsa rsassa-pss',%0A    'ecdsa_only': 'ecdsa',%0A    'rsassa-pss_preferred': 'ecdsa rsassa-pss',%0A    'rsassa-pss_only': 'rsassa-pss'%7D%0A%0A%0Adef upgrade():%0A    # we need to change the enrollment policy action from%0A    # %22webauthn_public_key_credential_algorithm_preference%22 to%0A    # %22webauthn_public_key_credential_algorithms%22 and also change the value%0A    # to directly use the (space separated) algorithms.%0A    bind = op.get_bind()%0A    session = orm.Session(bind=bind)%0A%0A    regex = re.compile('%5E%7B0!s%7D'.format(old_policy_action))%0A    try:%0A        for row in session.query(Policy).filter(Policy.action.like('%25%7B0!s%7D%25'.format(old_policy_action))):%0A            # get the current setting%0A            pol_actions = %5Bx.strip() for x in row.action.split(',')%5D%0A            for pol_action in filter(regex.match, pol_actions):%0A                cred_algs = cred_alg_map%5Bpol_action.split('=')%5B1%5D%5D%0A                pol_actions.remove(pol_action)%0A                pol_actions.append('%7B0!s%7D=%7B1!s%7D'.format(new_policy_action, cred_algs))%0A            row.action = ', '.join(pol_actions)%0A        session.commit()%0A    except Exception as e:%0A        session.rollback()%0A        print('Error updating the enrollment policy %7B0!s%7D: %7B1!s%7D'.format(old_policy_action, e))%0A%0A%0Adef downgrade():%0A    # for the downgrade we just use the %22ecdsa_preferred%22 setting%0A    bind = op.get_bind()%0A    session = orm.Session(bind=bind)%0A    regex = re.compile('%5E%7B0!s%7D'.format(new_policy_action))%0A    try:%0A        for row in session.query(Policy).filter(Policy.action.like('%25%7B0!s%7D%25'.format(new_policy_action))):%0A            # get the current setting%0A            pol_actions = %5Bx.strip() for x in row.action.split(',')%5D%0A            for pol_action in filter(regex.match, pol_actions):%0A                pol_actions.remove(pol_action)%0A                pol_actions.append('%7B0!s%7D=%7B1!s%7D'.format(old_policy_action, 'ecdsa_preferred'))%0A            row.action = ', '.join(pol_actions)%0A        session.commit()%0A    except Exception as e:%0A        session.rollback()%0A        print('Error downgrading the enrollment policy %7B0!s%7D: %7B1!s%7D'.format(new_policy_action, e))%0A    pass%0A

Add migration script for webauthn policy changes