| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Codecov Report
@@ Coverage Diff @@
## master #665 +/- ##
==========================================
+ Coverage 65.05% 65.06% +<.01%
==========================================
Files 102 102
Lines 25670 25676 +6
==========================================
+ Hits 16700 16705 +5
- Misses 8970 8971 +1
Continue to review full report at Codecov.
|
Sorry, something went wrong.
|
Oh also, the method for creating tempfiles on Windows does not do anything with the delete_tmpfiles flag, but my though was that it's probably OK to just leave them and let IIS clean them up, no? |
Sorry, something went wrong.
|
You can even run a schedular to delete .xml files older than 10minutes |
Sorry, something went wrong.
|
the delete_tmpfiles is simply a flag that is passed to Python's NamedTemporaryFile function in order to trigger the automatic deletion. In your case it seems like NamedTemporaryFile is not working at all the way it should be. You mentioned two errors:
Can you identify what was that fixed the first error and led to the second error? The first issue sounds like file permissions and the second sound like a strange race condition with file locks. On the diff per se, using uuid may sound like a fail proof solution but there is no mechanism to control what will happen if the uuid named file already exists. And this is even more possible since there is no automatic deletion mechanism so the files on the TEMP folder will be deleted when the server restarts (or some cron job deletion script runs). Both of the above two scenarios are handled by NamedTemporaryFile. But there seems to be an issue with Windows NT and later versions that probably affects us mentioned in Python's docs:
This might probably affect us since the file is opened, written, and then passed around. It seems from reading this issue that the real problem is that when the temp files are created, because the system is on windows the O_TEMPORARY flag must be set. This is mentioned in the issue and you can also see it in the source code of the NamedTemporaryFile: # Setting O_TEMPORARY in the flags causes the OS to delete
# the file when it is closed. This is only supported by Windows.
if _os.name == 'nt' and delete:
flags |= _os.O_TEMPORARYSo I would suggest if possible to test if the issue still exists with the O_TEMPORARY flag set on Windows and modifying the _run_xmlsec function here to also include this flag. If this works what we have to do is update the documentation and set the _run_xmlsec function when using Popen to have this env flag set to whatever the system has it set. |
Sorry, something went wrong.
|
Thanks for doing the research and the detailed response. I don't see a way to pass the O_TEMPORARY flag anywhere in the code (I think you're suggesting to pass it to Popen()? I don't think that will work, and NamedTemporaryFile does not provide a way to pass flags). If we use mkstemp, do we really need to delete the tempfiles? Most OSes have some mechanism for deleting tempfiles on a scheduled basis, right? It just seems really difficult to do with the way that open file objects are passed around and read from in the code. I guess I could write a context manager class that does this though? |
Sorry, something went wrong.
The first error was fixed by removing NamedTemporaryFile from make_temp, which was used to make the temporary PEM and XML files. The second was fixed by using the new _make_temp function (the same mechanism) in _run_xmlsec, which was used to make the file that xmlsec uses for --output. |
Sorry, something went wrong.
When you say "removing NamedTemporaryFile" you mean that you replaced it with some other file creation function?
Pass it to Popen by writing Popen(com_list, stderr=PIPE, stdout=PIPE,env={'O_TEMPORARY': 1}). It should also be set to the environment that is running/using pysaml
Since we provide a way to *nix systems to automatically delete these temp files (and don't rely to some third party script/cron job to do it) for the sake of consistency I think we should also provide a way to do it in Windows as well |
Sorry, something went wrong.
This does not function as expected in Windows and this issue is referenced in the python documentation.
Essentially, we could not open this file again on our windows environment, which meant that xmlsec was unable to use the file for verifying the signature. |
Sorry, something went wrong.
| # `NamedTemporaryFile` is not very reliable on Windows, so we'll make a | ||
| # tempfile a different way. | ||
| if sys.platform == 'win32': | ||
| return open(os.path.join(gettempdir(), '%s.%s' % (uuid4(), suffix)), 'w+b') |
There was a problem hiding this comment.
will this ever be cleaned up?
Sorry, something went wrong.
|
This is very nasty on Python's part. I am really sad for this state of things. The option being discussed, is to implement "NamedTemporaryFiles" some other way. To be complete, this should support deleting/cleaning-up those files, when closed. Is there something like that out there? What do other projects do? Another option is to stop using files and use actual bindings to the xmlsec lib. I would prefer we invest time in this option. There is already a wrapper for xmlsec: https://github.com/mehcode/python-xmlsec Another lib we can look into is signxml: https://github.com/XML-Security/signxml Would someone like to see how we can use this to sign/verify/encrypt/decrypt an xml document? |
Sorry, something went wrong.
|
I've encountered this issue as well, it seems to be entirely a Windows problem in so much that temporary files created with the O_TEMPORARY flag seem to be only accessible by the creating process and any subprocesses (i.e. xmlsec) are denied access. I ended up solving it by monkey patching tempfile._TemporaryFileCloser and tempfile.NamedTemporaryFile so that O_TEMPORARY was never set and that closing the file would manually delete it, instead. Naturally, I wouldn't recommend that solution to everyone, though. |
Sorry, something went wrong.
thank you for the feedback since I didn't have a windows vm to try this solution out. As it seems, the fact that we use a subprocess call to xmlsec beats the windows flag workaround. So we'll have to find some other way to do this -not monkeypatching internal libraries of course but something different in terms of handling these files. |
Sorry, something went wrong.
|
Having this same issue on windows, any ETA when this will be merged? |
Sorry, something went wrong.
Not yet to be honest. This needs some discussion on how to handle and when to free up these files. We should come up (if possible) with a common solution for all OS instead of adding flags to check if it's windows or some other *nix OS |
Sorry, something went wrong.
|
i ran into this problem. NamedTemporaryFile should never be used on windows or linux. it is fully bugged and can even result in an an infinite loop. uuid mechanism is the best mechanism. check out: python/cpython#66305 and https://stackoverflow.com/a/58955530/627042 |
Sorry, something went wrong.
|
hi @earonesty, and thanks for commenting back on this. I think using uuid is a viable way forward. The code needs to be reworked to clean up the files. But your gist is also interesting to hide the inner details behind a familiar interface. The alternative choice would be to start using the xmlsec bindings and avoid creating files altogether, but that would go into a separate backend. |
Sorry, something went wrong.
|
python 3.12 finally has a better fix for this one; |
Sorry, something went wrong.
|
⚠️ Please install the Codecov Report❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
@@ Coverage Diff @@
## master #665 +/- ##
=======================================
Coverage 65.05% 65.06%
=======================================
Files 102 102
Lines 25670 25676 +6
=======================================
+ Hits 16700 16705 +5
- Misses 8970 8971 +1 ☔ View full report in Codecov by Sentry.
|
Sorry, something went wrong.
|
Hi @c00kiemon5ter @ioparaskev i am running into similar problem on Windows after trying to integrate sso using djangosaml2 where i am getting Error: failed to load public key from "C:\Windows\TEMP\tmps0w7s2ph.pem". Error: keys manager creation failed` upon looking at above discussions as well as shared hyperlinks i am a bit confused on how can this be resolved ? |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
We were having issues with the temporary files created by this library on Windows not being readable by the xmlsec process. I don't totally understand the problem, but it seems like IIS was retaining some kind of lock on the created files, so the xmlsec process couldn't open them for reading. This resulted in 2 different errors:
I also see that NamedTemporaryFile has some odd behavior on Windows as noted in the Python documentation: https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
This PR changes the way tempfiles are written on Windows. It still writes them to the temp directory as determined by tempfile.gettempdir(), but just uses regular open instead of NamedTemporaryFile. uuid4() is used to generate a unique-ish filename. This fix seems to work well on our Windows server and locally on Windows machines. I'm not sure if this is the best way to do it though---open to other implementations if you don't like this one.
Thanks!