Skip to content

Commit b5f1040

Browse files
authored
[AAP-52121] Fix attribute handling with 'and' condition (#830)
1 parent 259cae5 commit b5f1040

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

ansible_base/authentication/utils/claims.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,15 @@ def process_user_attributes(trigger_condition: dict, attributes: dict, map_id: i
459459
# Check if user has the attribute
460460
user_value = attributes.get(attribute, None)
461461
if user_value is None:
462-
_prefixed_debug(map_id, tracking_id, f"Attr [{attribute}] is not present in user attributes, skipping")
462+
# if condition is not "and", the attribute value is not required, just move on
463+
if join_condition != 'and':
464+
_prefixed_debug(map_id, tracking_id, f"Attr [{attribute}] is not present in user attributes, skipping")
465+
# else, condition is "and" which means the attribute value IS required, set access to False
466+
else:
467+
_prefixed_debug(
468+
map_id, tracking_id, f"Attr [{attribute}] is not present in user attributes but is required by condition 'and' changing access to false"
469+
)
470+
has_access = has_access_with_join(has_access, False, join_condition)
463471
continue
464472

465473
# Normalize user value and process

test_app/tests/authentication/utils/test_claims.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,27 @@ def test_has_access_with_join(current_access, new_access, condition, expected):
890890
claims.TriggerResult.SKIP,
891891
id="in operator with string value (invalid) should be ignored",
892892
),
893+
pytest.param(
894+
{"cn": {"ends_with": "_admin"}, "employeeType": {"equals": "manager"}, "join_condition": "and"},
895+
{"cn": ["ldap_admin"]},
896+
False,
897+
claims.TriggerResult.SKIP,
898+
id="missing attribute required by 'and' condition should result in skip",
899+
),
900+
pytest.param(
901+
{"cn": {"ends_with": "_admin"}, "employeeType": {"equals": "manager"}, "join_condition": "or"},
902+
{"cn": ["ldap_admin"]},
903+
False,
904+
claims.TriggerResult.ALLOW,
905+
id="missing attribute when using 'or' condition should result in allow",
906+
),
907+
pytest.param(
908+
{"cn": {"ends_with": "_admin"}, "employeeType": {"equals": "manager"}, "join_condition": "and"},
909+
{"cn": ["ldap_org_admin"], "employeeType": ["manager"]},
910+
False,
911+
claims.TriggerResult.ALLOW,
912+
id="all attribute required by 'and' condition should result in allow",
913+
),
893914
],
894915
)
895916
@pytest.mark.django_db

0 commit comments

Comments
 (0)