Don't rely on order in trust agent/controller role check#357
Merged
rcritten merged 1 commit intofreeipa:masterfrom Jun 23, 2025
Merged
Don't rely on order in trust agent/controller role check#357rcritten merged 1 commit intofreeipa:masterfrom
rcritten merged 1 commit intofreeipa:masterfrom
Conversation
The code expected that the local server would always be the first one returned. Instead loop through the returned list to find the current server and set the state based on that. Fixes: freeipa#356 Signed-off-by: Rob Crittenden <rcritten@redhat.com>
There was a problem hiding this comment.
Hey @rcritten - I've reviewed your changes - here's some feedback:
- Add a test case covering the trust_controller scenario so you validate has_role works correctly for both agent and controller roles.
- Consider renaming has_role to something like is_current_host_enabled_role to make its intent clearer and easier to maintain.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Add a test case covering the trust_controller scenario so you validate has_role works correctly for both agent and controller roles.
- Consider renaming has_role to something like is_current_host_enabled_role to make its intent clearer and easier to maintain.
## Individual Comments
### Comment 1
<location> `tests/test_ipa_trust.py:1383` </location>
<code_context>
+
+ assert reg.has_role(roles) is True
+
+ def test_no_role(self):
+ self.results = Results()
+ reg = IPARegistry()
+
+ roles = [
+ {
+ "role_servrole": "AD trust agent",
+ "server_server": "server.ipa.example",
+ "status": "absent",
+ },
+ {
+ "role_servrole": "AD trust agent",
+ "server_server": "replica.ipa.example",
+ "status": "enabled",
+ },
+ ]
+
+ assert reg.has_role(roles) is False
</code_context>
<issue_to_address>
Consider adding a test for an empty roles list.
Adding a test with an empty roles list will verify that has_role correctly returns False and handles this case without errors.
</issue_to_address>
### Comment 2
<location> `tests/test_ipa_trust.py:1345` </location>
<code_context>
+ between an agent and a trust in the way they are stored in
+ a server role.
+ """
+ def test_role_last(self):
+ self.results = Results()
+ reg = IPARegistry()
+
+ roles = [
+ {
+ "role_servrole": "AD trust agent",
+ "server_server": "replica.ipa.example",
+ "status": "absent",
+ },
+ {
+ "role_servrole": "AD trust agent",
+ "server_server": "server.ipa.example",
+ "status": "enabled",
+ },
+ ]
+
+ assert reg.has_role(roles) is True
+
+ def test_role_first(self):
</code_context>
<issue_to_address>
Tests assume api.env.host is 'server.ipa.example', which may not always be true.
Mock or patch api.env.host in the test setup to avoid environment-dependent failures and ensure consistent results.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
flo-renaud
approved these changes
Jun 20, 2025
Contributor
flo-renaud
left a comment
There was a problem hiding this comment.
@rcritten
thanks for the PR, works for me. Tested on an installation with master = controller, replica = agent:
- with the current version of ipa-healthcheck:
# ipa-healthcheck --source ipahealthcheck.ipa.trust
[
{
"source": "ipahealthcheck.ipa.trust",
"check": "IPATrustControllerPrincipalCheck",
"result": "ERROR",
"uuid": "ba7a9972-2231-4c10-b49e-5a72234b81aa",
"when": "20250620125938Z",
"duration": "0.000814",
"kw": {
"key": "krbprincipalname=cifs/replica.testrelm.test@TESTRELM.TEST,cn=services,cn=accounts,dc=testrelm,dc=test",
"error": "no such entry",
"msg": "Error retrieving ldap entry {key}: {error}"
}
},
{
"source": "ipahealthcheck.ipa.trust",
"check": "IPATrustControllerServiceCheck",
"result": "ERROR",
"uuid": "66d376ba-6c91-416d-b494-fc71e23a6541",
"when": "20250620125938Z",
"duration": "0.000064",
"kw": {
"key": "cn=ADTRUST,cn=replica.testrelm.test,cn=masters,cn=ipa,cn=etc,dc=testrelm,dc=test",
"error": "no such entry",
"msg": "Error retrieving ldap entry {key}: {error}"
}
},
{
"source": "ipahealthcheck.ipa.trust",
"check": "IPATrustControllerConfCheck",
"result": "ERROR",
"uuid": "25012c86-b38e-442e-bf1d-23735a6f97a4",
"when": "20250620125938Z",
"duration": "0.045160",
"kw": {
"key": "net conf list",
"error": "No section: 'global'",
"section": "global",
"option": "passdb backend",
"msg": "Unable to read '{option}' in section {section} in {key} output: {error}"
}
}
]
- with the version from the PR:
# git clone http://github.com/rcritten/freeipa-healthcheck
# cd freeipa-healthcheck/
# git checkout issue_356
# python3 -m venv --system-site-packages venv
# venv/bin/pip install -e .
# source venv/bin/activate
(venv) [root@replica freeipa-healthcheck]# ipa-healthcheck --source ipahealthcheck.ipa.trust
[]
The new test is also green.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The code expected that the local server would always be the first one returned. Instead loop through the returned list to find the current server and set the state based on that.
Fixes: #356
Summary by Sourcery
Loop through server-role entries to detect trust agent and controller roles by matching the local host instead of assuming fixed positions in the list.
Bug Fixes:
Enhancements:
has_rolehelper inIPARegistryto abstract role detection logic.has_roleto settrust_agentandtrust_controllerflags dynamically.Tests:
TestHasRolewith scenarios covering enabled/absent statuses in different list orders.