authentik.blueprints.migrations.0001_initial

  1# Generated by Django 4.0.6 on 2022-07-31 17:35
  2import uuid
  3from glob import glob
  4from pathlib import Path
  5
  6import django.contrib.postgres.fields
  7from dacite.core import from_dict
  8from django.apps.registry import Apps
  9from django.db import migrations, models
 10from django.db.backends.base.schema import BaseDatabaseSchemaEditor
 11from yaml import load
 12
 13from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_SYSTEM
 14from authentik.lib.config import CONFIG
 15
 16
 17def check_blueprint_v1_file(BlueprintInstance: type, db_alias, path: Path):
 18    """Check if blueprint should be imported"""
 19    from authentik.blueprints.models import BlueprintInstanceStatus
 20    from authentik.blueprints.v1.common import BlueprintLoader, BlueprintMetadata
 21    from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_INSTANTIATE
 22
 23    with open(path, "r", encoding="utf-8") as blueprint_file:
 24        raw_blueprint = load(blueprint_file.read(), BlueprintLoader)
 25        if not raw_blueprint:
 26            return
 27        metadata = raw_blueprint.get("metadata", None)
 28        version = raw_blueprint.get("version", 1)
 29        if version != 1:
 30            return
 31        blueprint_file.seek(0)
 32    instance = BlueprintInstance.objects.using(db_alias).filter(path=path).first()
 33    rel_path = path.relative_to(Path(CONFIG.get("blueprints_dir")))
 34    meta = None
 35    if metadata:
 36        meta = from_dict(BlueprintMetadata, metadata)
 37        if meta.labels.get(LABEL_AUTHENTIK_INSTANTIATE, "").lower() == "false":
 38            return
 39    if not instance:
 40        BlueprintInstance.objects.using(db_alias).create(
 41            name=meta.name if meta else str(rel_path),
 42            path=str(rel_path),
 43            context={},
 44            status=BlueprintInstanceStatus.UNKNOWN,
 45            enabled=True,
 46            managed_models=[],
 47            last_applied_hash="",
 48            metadata=metadata or {},
 49        )
 50
 51
 52def migration_blueprint_import(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 53    BlueprintInstance = apps.get_model("authentik_blueprints", "BlueprintInstance")
 54    Flow = apps.get_model("authentik_flows", "Flow")
 55
 56    db_alias = schema_editor.connection.alias
 57    for file in glob(f"{CONFIG.get('blueprints_dir')}/**/*.yaml", recursive=True):
 58        check_blueprint_v1_file(BlueprintInstance, db_alias, Path(file))
 59
 60    for blueprint in BlueprintInstance.objects.using(db_alias).all():
 61        # If we already have flows (and we should always run before flow migrations)
 62        # then this is an existing install and we want to disable all blueprints
 63        if Flow.objects.using(db_alias).all().exists():
 64            blueprint.enabled = False
 65        # System blueprints are always enabled
 66        if blueprint.metadata.get("labels", {}).get(LABEL_AUTHENTIK_SYSTEM, "").lower() == "true":
 67            blueprint.enabled = True
 68        blueprint.save()
 69
 70
 71class Migration(migrations.Migration):
 72    initial = True
 73
 74    dependencies = [("authentik_flows", "0001_squashed_0007_auto_20200703_2059")]
 75
 76    # migration_blueprint_import below treats "a flow already exists" as "this is an existing
 77    # install". That only holds if we run before any migration that creates a flow, which is
 78    # otherwise up to how Django happens to order the plan.
 79    run_before = [("authentik_core", "0040_provider_invalidation_flow")]
 80
 81    operations = [
 82        migrations.CreateModel(
 83            name="BlueprintInstance",
 84            fields=[
 85                ("created", models.DateTimeField(auto_now_add=True)),
 86                ("last_updated", models.DateTimeField(auto_now=True)),
 87                (
 88                    "managed",
 89                    models.TextField(
 90                        default=None,
 91                        help_text=(
 92                            "Objects which are managed by authentik. These objects are created and"
 93                            " updated automatically. This is flag only indicates that an object can"
 94                            " be overwritten by migrations. You can still modify the objects via"
 95                            " the API, but expect changes to be overwritten in a later update."
 96                        ),
 97                        null=True,
 98                        unique=True,
 99                        verbose_name="Managed by authentik",
100                    ),
101                ),
102                (
103                    "instance_uuid",
104                    models.UUIDField(
105                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
106                    ),
107                ),
108                ("name", models.TextField()),
109                ("metadata", models.JSONField(default=dict)),
110                ("path", models.TextField()),
111                ("context", models.JSONField(default=dict)),
112                ("last_applied", models.DateTimeField(auto_now=True)),
113                ("last_applied_hash", models.TextField()),
114                (
115                    "status",
116                    models.TextField(
117                        choices=[
118                            ("successful", "Successful"),
119                            ("warning", "Warning"),
120                            ("error", "Error"),
121                            ("orphaned", "Orphaned"),
122                            ("unknown", "Unknown"),
123                        ],
124                        default="unknown",
125                    ),
126                ),
127                ("enabled", models.BooleanField(default=True)),
128                (
129                    "managed_models",
130                    django.contrib.postgres.fields.ArrayField(
131                        base_field=models.TextField(), default=list, size=None
132                    ),
133                ),
134            ],
135            options={
136                "verbose_name": "Blueprint Instance",
137                "verbose_name_plural": "Blueprint Instances",
138                "unique_together": {("name", "path")},
139            },
140        ),
141        migrations.RunPython(migration_blueprint_import),
142    ]
def check_blueprint_v1_file(BlueprintInstance: type, db_alias, path: pathlib.Path):
18def check_blueprint_v1_file(BlueprintInstance: type, db_alias, path: Path):
19    """Check if blueprint should be imported"""
20    from authentik.blueprints.models import BlueprintInstanceStatus
21    from authentik.blueprints.v1.common import BlueprintLoader, BlueprintMetadata
22    from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_INSTANTIATE
23
24    with open(path, "r", encoding="utf-8") as blueprint_file:
25        raw_blueprint = load(blueprint_file.read(), BlueprintLoader)
26        if not raw_blueprint:
27            return
28        metadata = raw_blueprint.get("metadata", None)
29        version = raw_blueprint.get("version", 1)
30        if version != 1:
31            return
32        blueprint_file.seek(0)
33    instance = BlueprintInstance.objects.using(db_alias).filter(path=path).first()
34    rel_path = path.relative_to(Path(CONFIG.get("blueprints_dir")))
35    meta = None
36    if metadata:
37        meta = from_dict(BlueprintMetadata, metadata)
38        if meta.labels.get(LABEL_AUTHENTIK_INSTANTIATE, "").lower() == "false":
39            return
40    if not instance:
41        BlueprintInstance.objects.using(db_alias).create(
42            name=meta.name if meta else str(rel_path),
43            path=str(rel_path),
44            context={},
45            status=BlueprintInstanceStatus.UNKNOWN,
46            enabled=True,
47            managed_models=[],
48            last_applied_hash="",
49            metadata=metadata or {},
50        )

Check if blueprint should be imported

def migration_blueprint_import( apps: django.apps.registry.Apps, schema_editor: django.db.backends.base.schema.BaseDatabaseSchemaEditor):
53def migration_blueprint_import(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
54    BlueprintInstance = apps.get_model("authentik_blueprints", "BlueprintInstance")
55    Flow = apps.get_model("authentik_flows", "Flow")
56
57    db_alias = schema_editor.connection.alias
58    for file in glob(f"{CONFIG.get('blueprints_dir')}/**/*.yaml", recursive=True):
59        check_blueprint_v1_file(BlueprintInstance, db_alias, Path(file))
60
61    for blueprint in BlueprintInstance.objects.using(db_alias).all():
62        # If we already have flows (and we should always run before flow migrations)
63        # then this is an existing install and we want to disable all blueprints
64        if Flow.objects.using(db_alias).all().exists():
65            blueprint.enabled = False
66        # System blueprints are always enabled
67        if blueprint.metadata.get("labels", {}).get(LABEL_AUTHENTIK_SYSTEM, "").lower() == "true":
68            blueprint.enabled = True
69        blueprint.save()
class Migration(django.db.migrations.migration.Migration):
 72class Migration(migrations.Migration):
 73    initial = True
 74
 75    dependencies = [("authentik_flows", "0001_squashed_0007_auto_20200703_2059")]
 76
 77    # migration_blueprint_import below treats "a flow already exists" as "this is an existing
 78    # install". That only holds if we run before any migration that creates a flow, which is
 79    # otherwise up to how Django happens to order the plan.
 80    run_before = [("authentik_core", "0040_provider_invalidation_flow")]
 81
 82    operations = [
 83        migrations.CreateModel(
 84            name="BlueprintInstance",
 85            fields=[
 86                ("created", models.DateTimeField(auto_now_add=True)),
 87                ("last_updated", models.DateTimeField(auto_now=True)),
 88                (
 89                    "managed",
 90                    models.TextField(
 91                        default=None,
 92                        help_text=(
 93                            "Objects which are managed by authentik. These objects are created and"
 94                            " updated automatically. This is flag only indicates that an object can"
 95                            " be overwritten by migrations. You can still modify the objects via"
 96                            " the API, but expect changes to be overwritten in a later update."
 97                        ),
 98                        null=True,
 99                        unique=True,
100                        verbose_name="Managed by authentik",
101                    ),
102                ),
103                (
104                    "instance_uuid",
105                    models.UUIDField(
106                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
107                    ),
108                ),
109                ("name", models.TextField()),
110                ("metadata", models.JSONField(default=dict)),
111                ("path", models.TextField()),
112                ("context", models.JSONField(default=dict)),
113                ("last_applied", models.DateTimeField(auto_now=True)),
114                ("last_applied_hash", models.TextField()),
115                (
116                    "status",
117                    models.TextField(
118                        choices=[
119                            ("successful", "Successful"),
120                            ("warning", "Warning"),
121                            ("error", "Error"),
122                            ("orphaned", "Orphaned"),
123                            ("unknown", "Unknown"),
124                        ],
125                        default="unknown",
126                    ),
127                ),
128                ("enabled", models.BooleanField(default=True)),
129                (
130                    "managed_models",
131                    django.contrib.postgres.fields.ArrayField(
132                        base_field=models.TextField(), default=list, size=None
133                    ),
134                ),
135            ],
136            options={
137                "verbose_name": "Blueprint Instance",
138                "verbose_name_plural": "Blueprint Instances",
139                "unique_together": {("name", "path")},
140            },
141        ),
142        migrations.RunPython(migration_blueprint_import),
143    ]

The base class for all migrations.

Migration files will import this from django.db.migrations.Migration and subclass it as a class called Migration. It will have one or more of the following attributes:

  • operations: A list of Operation instances, probably from django.db.migrations.operations
  • dependencies: A list of tuples of (app_path, migration_name)
  • run_before: A list of tuples of (app_path, migration_name)
  • replaces: A list of migration_names

Note that all migrations come out of migrations and into the Loader or Graph as instances, having been initialized with their app label and name.

initial = True
dependencies = [('authentik_flows', '0001_squashed_0007_auto_20200703_2059')]
run_before = [('authentik_core', '0040_provider_invalidation_flow')]
operations = [<CreateModel name='BlueprintInstance', fields=[('created', <django.db.models.fields.DateTimeField>), ('last_updated', <django.db.models.fields.DateTimeField>), ('managed', <django.db.models.fields.TextField>), ('instance_uuid', <django.db.models.fields.UUIDField>), ('name', <django.db.models.fields.TextField>), ('metadata', <django.db.models.fields.json.JSONField>), ('path', <django.db.models.fields.TextField>), ('context', <django.db.models.fields.json.JSONField>), ('last_applied', <django.db.models.fields.DateTimeField>), ('last_applied_hash', <django.db.models.fields.TextField>), ('status', <django.db.models.fields.TextField>), ('enabled', <django.db.models.fields.BooleanField>), ('managed_models', <django.contrib.postgres.fields.array.ArrayField>)], options={'verbose_name': 'Blueprint Instance', 'verbose_name_plural': 'Blueprint Instances', 'unique_together': {('name', 'path')}}>, <RunPython <function migration_blueprint_import>>]