-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow saml_admin_attr to work in conjunction with SAML Org Map #14285
Allow saml_admin_attr to work in conjunction with SAML Org Map #14285
Conversation
b5a8031
to
83d2071
Compare
Not getting it. def test__update_user_orgs_with_role_name(self, backend, users):
u1, u2, u3 = users
# Test user membership logic with regular expressions
backend.setting('ORGANIZATION_MAP')['Default']['role_name'] = 'auditor_role'
backend.setting('ORGANIZATION_MAP')['Default']['users'] = re.compile('.*')
# desired_org_state = {'Default': {'role_name': 'auditor_role'}}
desired_org_state = {}
orgs_to_create = []
print((backend, desired_org_state, orgs_to_create, u1))
_update_user_orgs(backend, desired_org_state, orgs_to_create, u1)
_update_user_orgs(backend, desired_org_state, orgs_to_create, u2)
_update_user_orgs(backend, desired_org_state, orgs_to_create, u3)
assert desired_org_state == {'Default': {'member_role': False, 'admin_role': False, 'auditor_role': True}} Trying and failing to write a unit test at Here's thing thing - even if I artificially put in desired_org_state[organization_name][role_name] = desired_org_state[organization_name].get('role_name', False) or has_role means that if the first condition is met, it gives data like |
@AlanCoding The way the data structure works we need What I am doing here to solve this is creating an or condition so that if the field is already set to True from the first method and False from the second we still end up with True in the end. So take the existing value already set in So a test, imho, would look more like: @pytest.mark.parameterize(
"desired_org_state,update_m2m_return.expected",
(
# Set the existing auditor_role to either true or false and the call to _update_m2m_from_expression to either return true or faslse
( {'Default': {'auditor_role': False} }, True, True, ),
( {'Default': {'auditor_role': True} }, True, True, ),
( {'Default': {'auditor_role': False} }, False, False, ),
( {'Default': {'auditor_role': True} }, False, True, ),
)
)
def test__update_user_orgs_with_role_name(self, backend, users, desired_org_state, update_m2m_return, expected):
u1, u2, u3 = users
# Test user membership logic with regular expressions
backend.setting('ORGANIZATION_MAP')['Default']['role_name'] = 'auditor_role'
backend.setting('ORGANIZATION_MAP')['Default']['users'] = re.compile('.*')
with mock.patch('awx.sso.saml_pipeline._update_m2m_from_expression', return_value=update_m2m_return):
_update_user_orgs(backend, desired_org_state, [], u1)
assert desired_org_state['Default']['auditor_role'] == expected Note: didn't test this to make sure it worked, there might be more you have to mock |
In the line before what you change: has_role = _update_m2m_from_expression(user, is_member_expression, remove_members) Logically, I look at So why would the test set this? backend.setting('ORGANIZATION_MAP')['Default']['role_name'] = 'auditor_role' Just based on variable type, we established that Instead of desired_org_state[organization_name][role_name] = desired_org_state[organization_name].get(role_name, False) or has_role If I squint and try not to think too hard about it, that seems like it might hit at your objective:
|
Yea, the test should probably make that look like a valid value for |
yeah, so maybe we can get a test that more faithfully does a set up according to
So the input itself is a the settings that makes a user admin via one method and auditor via the other method. Just thinking out loud now. |
I think I might have gotten it. Here is the fix along with a new test for it: https://github.com/ansible/awx/compare/devel...AlanCoding:saml_admin_option2?expand=1 This test will fail on the line assert desired_org_state['o1_alias']['admin_role'] is True This is because the 2nd rule which adds the user as a member will revert the first rule that added the user as an admin. This changes |
Hi Alan, I think the link you shared is missing the PR part. |
It depends on whether John wants to keep this PR or close it and open a new one. |
Also, I still don't have an issue for this. |
…n Map From the SAML redesign, we found a regression where an admin specified by SOCIAL_AUTH_SAML_ORGANIZATION_ATTR but the user was not a member of the admin role from the SOCIAL_AUTH_SAML_ORGANIZATION_MAP the user would not be an admin of the organization. We fixed this by doing an or condition between the existing desired_org_state and the returned values
83d2071
to
b023fcf
Compare
@AlanCoding thanks for catching this! |
this changes the role_name use from a literal string to a variable reference, which should make the fix more correct this adds a unit test which mocks the expected reproducer the problem occured when the "org mapping" made a user an org member but the "saml attrs" made the same user an admin - expectation is that one does not undo the other
Current status is that I believe this is a good and valid patch. However, we have not obtained confirmation by any experiencing the bug that it fixed their problem. |
…le#14285) (ansible#6487) Co-authored-by: John Westcott IV <[email protected]> Co-authored-by: Alan Rominger <[email protected]>
…le#14285) Co-authored-by: Alan Rominger <[email protected]>
SUMMARY
From the SAML redesign, we found a regression where an admin specified by
saml_admin_attr
property ofSOCIAL_AUTH_SAML_ORGANIZATION_ATTR
setting but the user was not a member of the admin role from theadmins
property of theSOCIAL_AUTH_SAML_ORGANIZATION_MAP
setting the user would not be an admin of the organization. We fixed this by doing an or condition between the existing desired_org_state and the returned values.ISSUE TYPE
COMPONENT NAME
AWX VERSION
ADDITIONAL INFORMATION