Skip to content
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

Fix omitting credentials when CLI has already been instantiated #14449

Merged
merged 3 commits into from
Apr 25, 2024

Conversation

lhellebr
Copy link
Contributor

Problem Statement

Omitting credentials doesn't work when CLI has already been instantiated

Solution

If it's been instantiated, set its omitting attribute to True for all the classes

@lhellebr lhellebr added CherryPick PR needs CherryPick to previous branches 6.15.z Introduced in or relating directly to Satellite 6.15 labels Mar 19, 2024
@lhellebr lhellebr self-assigned this Mar 19, 2024
@lhellebr lhellebr requested a review from a team as a code owner March 19, 2024 13:40
@lhellebr
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled

@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 6124
Build Status: SUCCESS
PRT Comment: pytest tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled --external-logging
Test Result : ================= 2 passed, 335 warnings in 5737.68s (1:35:37) =================

@Satellite-QE Satellite-QE added the PRT-Passed Indicates that latest PRT run is passed for the PR label Mar 19, 2024
Copy link
Contributor

@jameerpathan111 jameerpathan111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Co-reviewed with @ogajduse

getattr(self._cli, name).omitting_credentials = True
except AttributeError:
# not everything has an mro method, we don't care about them
pass
yield
self.omitting_credentials = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lhellebr should we check self._cli._configured is configured in the teardown phase too? what happens if we don't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if someone does

with parametrized_enrolled_sat.omit_credentials():
  pass

Then CLI won't get instantiated at all (because parametrized_enrolled_sat.cli) hasn't been called. I haven't tested that though. In any case, I employed this just as a defensive programming technique to avoid error in cases we may not even think about.

Copy link
Member

@ogajduse ogajduse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

co-reviewed with @jameerpathan111

Comment on lines 1853 to 1862
for file in Path('robottelo/cli/').iterdir():
if file.suffix == '.py' and not file.name.startswith('_'):
cli_module = importlib.import_module(f'robottelo.cli.{file.stem}')
for name, obj in cli_module.__dict__.items():
try:
if Base in obj.mro():
getattr(self._cli, name).omitting_credentials = True
except AttributeError:
# not everything has an mro method, we don't care about them
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, @jameerpathan111 and I had some concerns about code repetition. But looking at it further, we concluded that it could be possibly remade into the following:

Suggested change
for file in Path('robottelo/cli/').iterdir():
if file.suffix == '.py' and not file.name.startswith('_'):
cli_module = importlib.import_module(f'robottelo.cli.{file.stem}')
for name, obj in cli_module.__dict__.items():
try:
if Base in obj.mro():
getattr(self._cli, name).omitting_credentials = True
except AttributeError:
# not everything has an mro method, we don't care about them
pass
for name, obj in self._cli.__dict__.items():
with contextlib.suppress(AttributeError): # not everything has an mro method, we don't care about them
if Base in obj.mro():
getattr(self._cli, name).omitting_credentials = True

With this change, it does not seem that much repetitive. 😃 We are not sure if it would work, though. But if the omit_credentials property assumes self._cli._configured, then we can rely on the classes being already populated there instead of going over robottelo/cli/.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think cycling through robottelo/cli ensures we won't run into some attribute of cli that is for some reason descendant of Base - but it's not an entity we should instantiate cli for. I don't know if any such exists and more so, I don't know if any will exist in the future. I understand your point about repetition. Please consider what I just wrote and if you don't change your opinion, I can test your version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best person to address the

I don't know if any such exists and more so, I don't know if any will exist in the future.

part would be @JacobCallahan since he created the initial implementation of cli.

I do not see how your comment relates to the changes I suggested. The implementation of cli property ensures (through calling mro()) that only descendants of Base will be attributes of cli. If cli contains something that should not have been assigned as its attribute, then implementation of cli is the one to blame.

getattr(self._cli, name).omitting_credentials = True
except AttributeError:
# not everything has an mro method, we don't care about them
pass
yield
self.omitting_credentials = False
Copy link
Member

@rplevka rplevka Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Sat object attribute is not necessarily False every time. One can initialize Satellite with True (by explicitly setting it in kwargs). In such scenario, this wouldn't reset the attribute to the correct original version.
Perhaps this contextmanager function could check if the ommiting_credentials is set to False first, otherwise, it has no reason to run and could be skipped straight away.

@lhellebr
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled tests/foreman/destructive/test_ldap_authentication.py::test_positive_autonegotiate

@Satellite-QE Satellite-QE removed the PRT-Passed Indicates that latest PRT run is passed for the PR label Apr 15, 2024
@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 6506
Build Status: SUCCESS
PRT Comment: pytest tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled tests/foreman/destructive/test_ldap_authentication.py::test_positive_autonegotiate --external-logging
Test Result : ================= 4 passed, 864 warnings in 9264.29s (2:34:24) =================

@Satellite-QE Satellite-QE added the PRT-Passed Indicates that latest PRT run is passed for the PR label Apr 15, 2024
Copy link
Member

@JacobCallahan JacobCallahan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can likely simplify the reversion by adding all modified objects into a list called modified, then iterate over that list post-yield to update omitting_credentials again.

AttributeError
): # not everything has an mro method, we don't care about them
if Base in obj.mro():
getattr(self._cli, name).omitting_credentials = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be True or False, if it's a reversion of the previous action?

@lhellebr
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled tests/foreman/destructive/test_ldap_authentication.py::test_positive_autonegotiate

@Satellite-QE Satellite-QE removed the PRT-Passed Indicates that latest PRT run is passed for the PR label Apr 23, 2024
@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 6647
Build Status: UNSTABLE
PRT Comment: pytest tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled tests/foreman/destructive/test_ldap_authentication.py::test_positive_autonegotiate --external-logging
Test Result : =========== 4 passed, 1006 warnings, 1 error in 10758.13s (2:59:18) ============

@Satellite-QE Satellite-QE added the PRT-Failed Indicates that latest PRT run is failed for the PR label Apr 23, 2024
@lhellebr
Copy link
Contributor Author

trigger: test-robottelo
pytest: tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled tests/foreman/destructive/test_ldap_authentication.py::test_positive_autonegotiate

@Satellite-QE
Copy link
Collaborator

PRT Result

Build Number: 6658
Build Status: SUCCESS
PRT Comment: pytest tests/foreman/destructive/test_ldap_authentication.py::test_positive_negotiate_manual_with_autonegotiation_disabled tests/foreman/destructive/test_ldap_authentication.py::test_positive_autonegotiate --external-logging
Test Result : ================ 4 passed, 986 warnings in 10078.11s (2:47:58) =================

@Satellite-QE Satellite-QE added the PRT-Passed Indicates that latest PRT run is passed for the PR label Apr 23, 2024
@Satellite-QE Satellite-QE removed the PRT-Failed Indicates that latest PRT run is failed for the PR label Apr 23, 2024
@JacobCallahan
Copy link
Member

@lhellebr if you need help with my recommended implementation, let me know.

@lhellebr
Copy link
Contributor Author

Let's just merge. This PR is over a month old.

@lhellebr lhellebr requested a review from JacobCallahan April 24, 2024 12:35
@JacobCallahan
Copy link
Member

@lhellebr I don't see any priority labels on this PR, so would still like you to consider my suggestion. Here it is in code form:

    def omit_credentials(self):
        change = not self.omitting_credentials  # if not already set to omit
        changed = []
        if change:
            self.omitting_credentials = True
            # if CLI is already created
            if self._cli._configured:
                for name, obj in self._cli.__dict__.items():
                    with contextlib.suppress(
                        AttributeError
                    ):  # not everything has an mro method, we don't care about them
                        if Base in obj.mro():
                            attr = getattr(self._cli, name)
                            attr.omitting_credentials = True
                            changed.append(attr)
        yield
        if change:
            self.omitting_credentials = False
            for attr in changed:
                attr.omitting_credentials = False

@pondrejk pondrejk merged commit 845ef03 into SatelliteQE:master Apr 25, 2024
13 checks passed
github-actions bot pushed a commit that referenced this pull request Apr 25, 2024
* Fix omitting credentials when CLI has already been instantiated

* Rework based on comments

* Set to False after yield

(cherry picked from commit 845ef03)
Gauravtalreja1 pushed a commit that referenced this pull request Apr 25, 2024
…ted (#14880)

Fix omitting credentials when CLI has already been instantiated (#14449)

* Fix omitting credentials when CLI has already been instantiated

* Rework based on comments

* Set to False after yield

(cherry picked from commit 845ef03)

Co-authored-by: Lukáš Hellebrandt <[email protected]>
jyejare pushed a commit to jyejare/robottelo that referenced this pull request Oct 19, 2024
…lliteQE#14449)

* Fix omitting credentials when CLI has already been instantiated

* Rework based on comments

* Set to False after yield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.15.z Introduced in or relating directly to Satellite 6.15 CherryPick PR needs CherryPick to previous branches PRT-Passed Indicates that latest PRT run is passed for the PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants