From 0a0cdc2e21238f5e9ef3ac801f1a64629b5f2542 Mon Sep 17 00:00:00 2001 From: chris meyers Date: Thu, 27 Sep 2018 12:18:39 -0400 Subject: [PATCH] at migration time, validate ldap group type params * Previously, we have logic in the API to ensure that ldap group type params, when changed, align with ldap group type Class init expectations. However, we did not have this logic in the migrations. This PR adds the validation check to migrations. --- .../migrations/0006_v331_ldap_group_type.py | 18 +++++++++++ awx/conf/migrations/_ldap_group_type.py | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 awx/conf/migrations/0006_v331_ldap_group_type.py create mode 100644 awx/conf/migrations/_ldap_group_type.py diff --git a/awx/conf/migrations/0006_v331_ldap_group_type.py b/awx/conf/migrations/0006_v331_ldap_group_type.py new file mode 100644 index 0000000000..8bfe3ef0e2 --- /dev/null +++ b/awx/conf/migrations/0006_v331_ldap_group_type.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +# AWX +from awx.conf.migrations._ldap_group_type import fill_ldap_group_type_params + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('conf', '0005_v330_rename_two_session_settings'), + ] + + operations = [ + migrations.RunPython(fill_ldap_group_type_params), + ] diff --git a/awx/conf/migrations/_ldap_group_type.py b/awx/conf/migrations/_ldap_group_type.py new file mode 100644 index 0000000000..479b12cdb7 --- /dev/null +++ b/awx/conf/migrations/_ldap_group_type.py @@ -0,0 +1,30 @@ + +import inspect + +from django.conf import settings +from django.utils.timezone import now + + +def fill_ldap_group_type_params(apps, schema_editor): + group_type = settings.AUTH_LDAP_GROUP_TYPE + Setting = apps.get_model('conf', 'Setting') + + group_type_params = {'name_attr': 'cn', 'member_attr': 'member'} + qs = Setting.objects.filter(key='AUTH_LDAP_GROUP_TYPE_PARAMS') + entry = None + if qs.exists(): + entry = qs[0] + group_type_params = entry.value + else: + entry = Setting(key='AUTH_LDAP_GROUP_TYPE_PARAMS', + value=group_type_params, + created=now(), + modified=now()) + + init_attrs = set(inspect.getargspec(group_type.__init__).args[1:]) + for k in group_type_params.keys(): + if k not in init_attrs: + del group_type_params[k] + + entry.value = group_type_params + entry.save()