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

pylint: Fix R1732 consider-using-with #937

Merged
merged 4 commits into from
Mar 29, 2022

Conversation

stefanberger
Copy link
Contributor

Signed-off-by: Stefan Berger stefanb@linux.ibm.com

Copy link
Member

@THS-on THS-on left a comment

Choose a reason for hiding this comment

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

I do not see why the e2e are failing on this PR. Can you rebase on the latest master?

keylime/cmd_exec.py Outdated Show resolved Hide resolved
f = open(secdir + "/" + self.server.enc_keyname, 'w', encoding="utf-8")
f.write(base64.b64encode(self.server.K).decode())
f.close()
with open(secdir + "/" + self.server.enc_keyname, 'w', encoding="utf-8") as f:
Copy link
Member

Choose a reason for hiding this comment

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

Can you fix the string formatting to os.path.join?.

I know that this is not directly related to this issue, but I try to convert them if I change the line anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed locally.

@@ -20,7 +20,7 @@
def check_mounted(secdir):
"""Inspect mountinfo to detect if a directory is mounted."""
secdir_escaped = secdir.replace(" ", r"\040")
for line in open("/proc/self/mountinfo", "r", encoding="utf-8"):
for line in open("/proc/self/mountinfo", "r", encoding="utf-8"): #pylint: disable=consider-using-with
Copy link
Member

Choose a reason for hiding this comment

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

Can we not open the file with with and then iterate over readline()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems to work fine for me with python 3.10 just running this here:

>>> with open("/proc/self/mountinfo", "r", encoding="utf-8") as f:
...     for line in f:
...         print(line)

On the CI/CD system we now get this here:

Error in test.test_secure_mount.TestSecureMount.test_check_mounted_found
  File "/usr/lib64/python3.9/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib64/python3.9/unittest/case.py", line 592, in run
    self._callTestMethod(testMethod)
  File "/usr/lib64/python3.9/unittest/case.py", line 550, in _callTestMethod
    method()
  File "/usr/lib64/python3.9/unittest/mock.py", line 1336, in patched
    return func(*newargs, **newkeywargs)
  File "/__w/keylime/keylime/test/test_secure_mount.py", line 31, in test_check_mounted_found
    self.assertTrue(secure_mount.check_mounted("/secdir"))
  File "/__w/keylime/keylime/keylime/secure_mount.py", line 23, in check_mounted
    with open("/proc/self/mountinfo", "r", encoding="utf-8") as f:
AttributeError: __enter__

Copy link
Member

Choose a reason for hiding this comment

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

This is beacuse the open call is mocked, but the __enter__ and __exit__ functions are not: https://github.com/keylime/keylime/blob/master/test/test_secure_mount.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So odd...

>>> subprocess.Popen.__enter__
<function Popen.__enter__ at 0x6fffffdf2d30>
>>> open.__enter__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute '__enter__'
>>> os.open.__enter__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute '__enter__'

Copy link
Member

Choose a reason for hiding this comment

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

open(..) and os.open(..) are two different functions.

Also the __enter__ function seems to be only there when the function is called:

>>> open.__enter__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute '__enter__'. Did you mean: '__ne__'?
>>> open("tesfile", "wb").__enter__
<built-in method __enter__ of _io.BufferedWriter object at 0x7f8266d521f0>

Copy link
Member

Choose a reason for hiding this comment

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

There seems to be a mocking object in the library for that: https://docs.python.org/3.7/library/unittest.mock.html#mock-open

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in additional patch. What a struggle!

@THS-on
Copy link
Member

THS-on commented Mar 23, 2022

The warning is called R1732 not W1732.

@THS-on THS-on mentioned this pull request Mar 23, 2022
39 tasks
@stefanberger stefanberger changed the title pylint: Fix W1732 consider-using-with pylint: Fix R1732 consider-using-with Mar 23, 2022
@stefanberger
Copy link
Contributor Author

When testing 447ef9f there is one test-failure in testing-farm:fedora-35-x86_64:

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   -c delete
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: [ 10:34:35 ] :: [  BEGIN   ] :: Running 'lime_keylime_tenant -c delete'
Reading configuration from ['/etc/keylime.conf']
2022-03-23 10:34:36.296 - keylime.tpm - INFO - TPM2-TOOLS Version: 5.2
2022-03-23 10:34:36.302 - keylime.tenant - INFO - Setting up client TLS in /var/lib/keylime/cv_ca
2022-03-23 10:34:36.302 - keylime.tenant - WARNING - Using default UUID d432fbb3-d2f1-4a97-9ef7-75bd81c00000
2022-03-23 10:34:36.329 - keylime.tenant - INFO - Agent Info:
{"d432fbb3-d2f1-4a97-9ef7-75bd81c00000": {"operational_state": "Get Quote", "v": "5YiO9Kc1QK9h2OSdfiD0+uVbZ7imZhZmQsLcshl0QsY=", "ip": "127.0.0.1", "port": 9002, "tpm_policy": "{\"22\": [\"0000000000000000000000000000000000000001\", \"0000000000000000000000000000000000000000000000000000000000000001\", \"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001\", \"ffffffffffffffffffffffffffffffffffffffff\", \"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"], \"15\": [\"0000000000000000000000000000000000000000\", \"0000000000000000000000000000000000000000000000000000000000000000\", \"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\"], \"mask\": \"0x408400\"}", "vtpm_policy": "{\"23\": [\"ffffffffffffffffffffffffffffffffffffffff\", \"0000000000000000000000000000000000000000\"], \"15\": [\"0000000000000000000000000000000000000000\"], \"mask\": \"0x808000\"}", "meta_data": "{}", "allowlist_len": 6, "mb_refstate_len": 0, "accept_tpm_hash_algs": ["sha512", "sha384", "sha256", "sha1"], "accept_tpm_encryption_algs": ["ecc", "rsa"], "accept_tpm_signing_algs": ["ecschnorr", "rsassa"], "hash_alg": "sha1", "enc_alg": "rsa", "sign_alg": "rsassa", "verifier_id": "default", "verifier_ip": "127.0.0.1", "verifier_port": 8881, "severity_level": null, "last_event_id": null}}
2022-03-23 10:34:41.387 - keylime.tenant - ERROR - Timed out waiting for delete of agent d432fbb3-d2f1-4a97-9ef7-75bd81c00000 to complete at CV
:: [ 10:34:41 ] :: [   PASS   ] :: Command 'lime_keylime_tenant -c delete' (Expected 0, got 0)
:: [ 10:34:41 ] :: [   FAIL   ] :: File '/var/tmp/rlRun_LOG.AoxQaNdP' should contain 'INFO - CV completed deletion of agent d432fbb3-d2f1-4a97-9ef7-75bd81c00000' 
:: [ 10:34:41 ] :: [  BEGIN   ] :: Running 'lime_keylime_tenant -c cvstatus'
Reading configuration from ['/etc/keylime.conf']
2022-03-23 10:34:42.204 - keylime.tpm - INFO - TPM2-TOOLS Version: 5.2
2022-03-23 10:34:42.209 - keylime.tenant - INFO - Setting up client TLS in /var/lib/keylime/cv_ca
2022-03-23 10:34:42.209 - keylime.tenant - WARNING - Using default UUID d432fbb3-d2f1-4a97-9ef7-75bd81c00000
2022-03-23 10:34:42.227 - keylime.tenant - INFO - Agent Info:
{"d432fbb3-d2f1-4a97-9ef7-75bd81c00000": {"operational_state": "Get Quote", "v": "5YiO9Kc1QK9h2OSdfiD0+uVbZ7imZhZmQsLcshl0QsY=", "ip": "127.0.0.1", "port": 9002, "tpm_policy": "{\"22\": [\"0000000000000000000000000000000000000001\", \"0000000000000000000000000000000000000000000000000000000000000001\", \"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001\", \"ffffffffffffffffffffffffffffffffffffffff\", \"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\", \"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"], \"15\": [\"0000000000000000000000000000000000000000\", \"0000000000000000000000000000000000000000000000000000000000000000\", \"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\"], \"mask\": \"0x408400\"}", "vtpm_policy": "{\"23\": [\"ffffffffffffffffffffffffffffffffffffffff\", \"0000000000000000000000000000000000000000\"], \"15\": [\"0000000000000000000000000000000000000000\"], \"mask\": \"0x808000\"}", "meta_data": "{}", "allowlist_len": 6, "mb_refstate_len": 0, "accept_tpm_hash_algs": ["sha512", "sha384", "sha256", "sha1"], "accept_tpm_encryption_algs": ["ecc", "rsa"], "accept_tpm_signing_algs": ["ecschnorr", "rsassa"], "hash_alg": "sha1", "enc_alg": "rsa", "sign_alg": "rsassa", "verifier_id": "default", "verifier_ip": "127.0.0.1", "verifier_port": 8881, "severity_level": null, "last_event_id": null}}
:: [ 10:34:42 ] :: [   PASS   ] :: Command 'lime_keylime_tenant -c cvstatus' (Expected 0, got 0)
:: [ 10:34:42 ] :: [   FAIL   ] :: File '/var/tmp/rlRun_LOG.gSGAYMcd' should contain 'Verifier at 127.0.0.1 with Port 8881 does not have agent d432fbb3-d2f1-4a97-9ef7-75bd81c00000' 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::   Duration: 7s
::   Assertions: 2 good, 2 bad
::   RESULT: FAIL (-c delete)

All other tests are passing. I prefer to ignore this test failure but something is flaky...

@stefanberger stefanberger force-pushed the stefanberger/fix-w1732 branch 4 times, most recently from 4201c74 to cfa277e Compare March 23, 2022 14:33
@mpeters
Copy link
Member

mpeters commented Mar 23, 2022

/packit test

@stefanberger
Copy link
Contributor Author

I wonder what that is when Fedora 34 fails and all other ones pass...

:: [ 19:42:43 ] :: [   FAIL   ] :: Command 'rlWaitForCmd 'tail /var/tmp/limeLib/agent.log | grep -q "Executing revocation action"' -m 10 -d 1 -t 10' (Expected 0, got 1)

@THS-on
Copy link
Member

THS-on commented Mar 24, 2022

I wonder what that is when Fedora 34 fails and all other ones pass...

@kkaarreell seems this like a timing issue?

@stefanberger just use /packit test to rerun the tests.

@stefanberger
Copy link
Contributor Author

/packit test

@kkaarreell
Copy link
Contributor

I wonder what that is when Fedora 34 fails and all other ones pass...

@kkaarreell seems this like a timing issue?

I can increase the timeout... so far 10 seconds for revocation action initiation was enough. What should be the reasonable timeout?

@THS-on
Copy link
Member

THS-on commented Mar 24, 2022

I can increase the timeout... so far 10 seconds for revocation action initiation was enough. What should be the reasonable timeout?

10 seconds seem actually reasonable, but if it flaky again in the future we need to take a closer look what is causing this.

@kkaarreell
Copy link
Contributor

Maybe some networking glitch on the actual virtual system... That would explain the above failure in the keylime tenant.
Having logs with timestamps would help. I will try to find a way how to incorporate more logs into the test output when test have failed.

@kkaarreell
Copy link
Contributor

I will try to find a way how to incorporate more logs into the test output when test have failed.

Hopefully this would help next time
RedHat-SP-Security/keylime-tests#70

@stefanberger stefanberger force-pushed the stefanberger/fix-w1732 branch 2 times, most recently from 5ea7dd4 to b7903be Compare March 25, 2022 20:25
Fix the following issue detected by pylint 2.13.0:

keylime/cmd/ima_emulator_adapter.py:73:12: E4702: Iterated dict 'position' is being modified inside for loop body, iterate through a copy of it instead. (modified-iterating-dict)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Fix the following issue detected by pylint 2.13.0:

keylime/tenant.py:1146:19: E0601: Using variable 'response' before assignment (used-before-assignment)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
@mpeters mpeters merged commit e7dcad7 into keylime:master Mar 29, 2022
@stefanberger stefanberger deleted the stefanberger/fix-w1732 branch March 29, 2022 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants