Skip to content

Commit 7e94f6b

Browse files
jessicamackdjyasin
authored andcommitted
Update LDAP/SAML config dump command (ansible#15106)
* update LDAP config dump * return missing fields if any * update test, remove unused import * return bool and fields. check for missing_fields
1 parent 9c21c49 commit 7e94f6b

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

awx/main/management/commands/dump_auth_config.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import os
33
import sys
44
import re
5-
65
from typing import Any
6+
77
from django.core.management.base import BaseCommand
88
from django.conf import settings
9+
910
from awx.conf import settings_registry
1011

1112

@@ -40,6 +41,15 @@ class Command(BaseCommand):
4041
"USER_SEARCH": False,
4142
}
4243

44+
def is_enabled(self, settings, keys):
45+
missing_fields = []
46+
for key, required in keys.items():
47+
if required and not settings.get(key):
48+
missing_fields.append(key)
49+
if missing_fields:
50+
return False, missing_fields
51+
return True, None
52+
4353
def get_awx_ldap_settings(self) -> dict[str, dict[str, Any]]:
4454
awx_ldap_settings = {}
4555

@@ -64,14 +74,16 @@ def get_awx_ldap_settings(self) -> dict[str, dict[str, Any]]:
6474

6575
if new_key == "SERVER_URI" and value:
6676
value = value.split(", ")
77+
grouped_settings[index][new_key] = value
6778

68-
return grouped_settings
79+
if type(value).__name__ == "LDAPSearch":
80+
data = []
81+
data.append(value.base_dn)
82+
data.append("SCOPE_SUBTREE")
83+
data.append(value.filterstr)
84+
grouped_settings[index][new_key] = data
6985

70-
def is_enabled(self, settings, keys):
71-
for key, required in keys.items():
72-
if required and not settings.get(key):
73-
return False
74-
return True
86+
return grouped_settings
7587

7688
def get_awx_saml_settings(self) -> dict[str, Any]:
7789
awx_saml_settings = {}
@@ -82,7 +94,7 @@ def get_awx_saml_settings(self) -> dict[str, Any]:
8294

8395
def format_config_data(self, enabled, awx_settings, type, keys, name):
8496
config = {
85-
"type": f"awx.authentication.authenticator_plugins.{type}",
97+
"type": f"ansible_base.authentication.authenticator_plugins.{type}",
8698
"name": name,
8799
"enabled": enabled,
88100
"create_objects": True,
@@ -130,7 +142,7 @@ def handle(self, *args, **options):
130142

131143
# dump SAML settings
132144
awx_saml_settings = self.get_awx_saml_settings()
133-
awx_saml_enabled = self.is_enabled(awx_saml_settings, self.DAB_SAML_AUTHENTICATOR_KEYS)
145+
awx_saml_enabled, saml_missing_fields = self.is_enabled(awx_saml_settings, self.DAB_SAML_AUTHENTICATOR_KEYS)
134146
if awx_saml_enabled:
135147
awx_saml_name = awx_saml_settings["ENABLED_IDPS"]
136148
data.append(
@@ -142,21 +154,25 @@ def handle(self, *args, **options):
142154
awx_saml_name,
143155
)
144156
)
157+
else:
158+
data.append({"SAML_missing_fields": saml_missing_fields})
145159

146160
# dump LDAP settings
147161
awx_ldap_group_settings = self.get_awx_ldap_settings()
148-
for awx_ldap_name, awx_ldap_settings in enumerate(awx_ldap_group_settings.values()):
149-
enabled = self.is_enabled(awx_ldap_settings, self.DAB_LDAP_AUTHENTICATOR_KEYS)
150-
if enabled:
162+
for awx_ldap_name, awx_ldap_settings in awx_ldap_group_settings.items():
163+
awx_ldap_enabled, ldap_missing_fields = self.is_enabled(awx_ldap_settings, self.DAB_LDAP_AUTHENTICATOR_KEYS)
164+
if awx_ldap_enabled:
151165
data.append(
152166
self.format_config_data(
153-
enabled,
167+
awx_ldap_enabled,
154168
awx_ldap_settings,
155169
"ldap",
156170
self.DAB_LDAP_AUTHENTICATOR_KEYS,
157-
str(awx_ldap_name),
171+
f"LDAP_{awx_ldap_name}",
158172
)
159173
)
174+
else:
175+
data.append({f"LDAP_{awx_ldap_name}_missing_fields": ldap_missing_fields})
160176

161177
# write to file if requested
162178
if options["output_file"]:

awx/main/tests/unit/commands/test_dump_auth_config.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def setUp(self):
5252
super().setUp()
5353
self.expected_config = [
5454
{
55-
"type": "awx.authentication.authenticator_plugins.saml",
55+
"type": "ansible_base.authentication.authenticator_plugins.saml",
5656
"name": "Keycloak",
5757
"enabled": True,
5858
"create_objects": True,
@@ -94,14 +94,14 @@ def setUp(self):
9494
},
9595
},
9696
{
97-
"type": "awx.authentication.authenticator_plugins.ldap",
98-
"name": "1",
97+
"type": "ansible_base.authentication.authenticator_plugins.ldap",
98+
"name": "LDAP_1",
9999
"enabled": True,
100100
"create_objects": True,
101101
"users_unique": False,
102102
"remove_users": True,
103103
"configuration": {
104-
"SERVER_URI": "SERVER_URI",
104+
"SERVER_URI": ["SERVER_URI"],
105105
"BIND_DN": "BIND_DN",
106106
"BIND_PASSWORD": "BIND_PASSWORD",
107107
"CONNECTION_OPTIONS": {},
@@ -119,4 +119,14 @@ def setUp(self):
119119
def test_json_returned_from_cmd(self):
120120
output = StringIO()
121121
call_command("dump_auth_config", stdout=output)
122-
assert json.loads(output.getvalue()) == self.expected_config
122+
cmmd_output = json.loads(output.getvalue())
123+
124+
# check configured SAML return
125+
assert cmmd_output[0] == self.expected_config[0]
126+
127+
# check configured LDAP return
128+
assert cmmd_output[2] == self.expected_config[1]
129+
130+
# check unconfigured LDAP return
131+
assert "LDAP_0_missing_fields" in cmmd_output[1]
132+
assert cmmd_output[1]["LDAP_0_missing_fields"] == ['SERVER_URI', 'GROUP_TYPE', 'GROUP_TYPE_PARAMS', 'USER_DN_TEMPLATE', 'USER_ATTR_MAP']

0 commit comments

Comments
 (0)