root: fix migrations missing using db_alias
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
		@ -7,12 +7,13 @@ from django.db.backends.base.schema import BaseDatabaseSchemaEditor
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def backport_is_backchannel(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
					def backport_is_backchannel(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
				
			||||||
 | 
					    db_alias = schema_editor.connection.alias
 | 
				
			||||||
    from authentik.providers.ldap.models import LDAPProvider
 | 
					    from authentik.providers.ldap.models import LDAPProvider
 | 
				
			||||||
    from authentik.providers.scim.models import SCIMProvider
 | 
					    from authentik.providers.scim.models import SCIMProvider
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for model in [LDAPProvider, SCIMProvider]:
 | 
					    for model in [LDAPProvider, SCIMProvider]:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            for obj in model.objects.only("is_backchannel"):
 | 
					            for obj in model.objects.using(db_alias).only("is_backchannel"):
 | 
				
			||||||
                obj.is_backchannel = True
 | 
					                obj.is_backchannel = True
 | 
				
			||||||
                obj.save()
 | 
					                obj.save()
 | 
				
			||||||
        except (DatabaseError, InternalError, ProgrammingError):
 | 
					        except (DatabaseError, InternalError, ProgrammingError):
 | 
				
			||||||
 | 
				
			|||||||
@ -21,7 +21,7 @@ def set_oobe_flow_authentication(apps: Apps, schema_editor: BaseDatabaseSchemaEd
 | 
				
			|||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if users.exists():
 | 
					    if users.exists():
 | 
				
			||||||
        Flow.objects.filter(slug="initial-setup").update(authentication="require_superuser")
 | 
					        Flow.objects.using(db_alias).filter(slug="initial-setup").update(authentication="require_superuser")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Migration(migrations.Migration):
 | 
					class Migration(migrations.Migration):
 | 
				
			||||||
 | 
				
			|||||||
@ -13,16 +13,17 @@ import authentik.outposts.models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def fix_missing_token_identifier(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
					def fix_missing_token_identifier(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
				
			||||||
 | 
					    db_alias = schema_editor.connection.alias
 | 
				
			||||||
    User = apps.get_model("authentik_core", "User")
 | 
					    User = apps.get_model("authentik_core", "User")
 | 
				
			||||||
    Token = apps.get_model("authentik_core", "Token")
 | 
					    Token = apps.get_model("authentik_core", "Token")
 | 
				
			||||||
    from authentik.outposts.models import Outpost
 | 
					    from authentik.outposts.models import Outpost
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for outpost in Outpost.objects.using(schema_editor.connection.alias).all().only("pk"):
 | 
					    for outpost in Outpost.objects.using(db_alias).all().only("pk"):
 | 
				
			||||||
        user_identifier = outpost.user_identifier
 | 
					        user_identifier = outpost.user_identifier
 | 
				
			||||||
        users = User.objects.filter(username=user_identifier)
 | 
					        users = User.objects.using(db_alias).filter(username=user_identifier)
 | 
				
			||||||
        if not users.exists():
 | 
					        if not users.exists():
 | 
				
			||||||
            continue
 | 
					            continue
 | 
				
			||||||
        tokens = Token.objects.filter(user=users.first())
 | 
					        tokens = Token.objects.using(db_alias).filter(user=users.first())
 | 
				
			||||||
        for token in tokens:
 | 
					        for token in tokens:
 | 
				
			||||||
            if token.identifier != outpost.token_identifier:
 | 
					            if token.identifier != outpost.token_identifier:
 | 
				
			||||||
                token.identifier = outpost.token_identifier
 | 
					                token.identifier = outpost.token_identifier
 | 
				
			||||||
@ -37,8 +38,8 @@ def migrate_to_service_connection(apps: Apps, schema_editor: BaseDatabaseSchemaE
 | 
				
			|||||||
        "authentik_outposts", "KubernetesServiceConnection"
 | 
					        "authentik_outposts", "KubernetesServiceConnection"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    docker = DockerServiceConnection.objects.filter(local=True).first()
 | 
					    docker = DockerServiceConnection.objects.using(db_alias).filter(local=True).first()
 | 
				
			||||||
    k8s = KubernetesServiceConnection.objects.filter(local=True).first()
 | 
					    k8s = KubernetesServiceConnection.objects.using(db_alias).filter(local=True).first()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        for outpost in Outpost.objects.using(db_alias).all().exclude(deployment_type="custom"):
 | 
					        for outpost in Outpost.objects.using(db_alias).all().exclude(deployment_type="custom"):
 | 
				
			||||||
@ -54,21 +55,21 @@ def migrate_to_service_connection(apps: Apps, schema_editor: BaseDatabaseSchemaE
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def remove_pb_prefix_users(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
					def remove_pb_prefix_users(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
				
			||||||
    alias = schema_editor.connection.alias
 | 
					    db_alias = schema_editor.connection.alias
 | 
				
			||||||
    User = apps.get_model("authentik_core", "User")
 | 
					    User = apps.get_model("authentik_core", "User")
 | 
				
			||||||
    Outpost = apps.get_model("authentik_outposts", "Outpost")
 | 
					    Outpost = apps.get_model("authentik_outposts", "Outpost")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for outpost in Outpost.objects.using(alias).all():
 | 
					    for outpost in Outpost.objects.using(db_alias).all():
 | 
				
			||||||
        matching = User.objects.using(alias).filter(username=f"pb-outpost-{outpost.uuid.hex}")
 | 
					        matching = User.objects.using(db_alias).filter(username=f"pb-outpost-{outpost.uuid.hex}")
 | 
				
			||||||
        if matching.exists():
 | 
					        if matching.exists():
 | 
				
			||||||
            matching.delete()
 | 
					            matching.delete()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_config_prefix(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
					def update_config_prefix(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
				
			||||||
    alias = schema_editor.connection.alias
 | 
					    db_alias = schema_editor.connection.alias
 | 
				
			||||||
    Outpost = apps.get_model("authentik_outposts", "Outpost")
 | 
					    Outpost = apps.get_model("authentik_outposts", "Outpost")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for outpost in Outpost.objects.using(alias).all():
 | 
					    for outpost in Outpost.objects.using(db_alias).all():
 | 
				
			||||||
        config = outpost._config
 | 
					        config = outpost._config
 | 
				
			||||||
        for key in list(config):
 | 
					        for key in list(config):
 | 
				
			||||||
            if "passbook" in key:
 | 
					            if "passbook" in key:
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,8 @@ from authentik.sources.saml.processors import constants
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_algorithms(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
					def update_algorithms(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
				
			||||||
 | 
					    db_alias = schema_editor.connection.alias
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    SAMLSource = apps.get_model("authentik_sources_saml", "SAMLSource")
 | 
					    SAMLSource = apps.get_model("authentik_sources_saml", "SAMLSource")
 | 
				
			||||||
    signature_translation_map = {
 | 
					    signature_translation_map = {
 | 
				
			||||||
        "rsa-sha1": constants.RSA_SHA1,
 | 
					        "rsa-sha1": constants.RSA_SHA1,
 | 
				
			||||||
@ -22,7 +24,7 @@ def update_algorithms(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
				
			|||||||
        "sha256": constants.SHA256,
 | 
					        "sha256": constants.SHA256,
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for source in SAMLSource.objects.all():
 | 
					    for source in SAMLSource.objects.using(db_alias).all():
 | 
				
			||||||
        source.signature_algorithm = signature_translation_map.get(
 | 
					        source.signature_algorithm = signature_translation_map.get(
 | 
				
			||||||
            source.signature_algorithm, constants.RSA_SHA256
 | 
					            source.signature_algorithm, constants.RSA_SHA256
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
				
			|||||||
@ -13,8 +13,8 @@ def assign_sources(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 | 
				
			|||||||
    IdentificationStage = apps.get_model("authentik_stages_identification", "identificationstage")
 | 
					    IdentificationStage = apps.get_model("authentik_stages_identification", "identificationstage")
 | 
				
			||||||
    Source = apps.get_model("authentik_core", "source")
 | 
					    Source = apps.get_model("authentik_core", "source")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sources = Source.objects.all()
 | 
					    sources = Source.objects.using(db_alias).all()
 | 
				
			||||||
    for stage in IdentificationStage.objects.all().using(db_alias):
 | 
					    for stage in IdentificationStage.objects.using(db_alias).all():
 | 
				
			||||||
        stage.sources.set(sources)
 | 
					        stage.sources.set(sources)
 | 
				
			||||||
        stage.save()
 | 
					        stage.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user