Skip to content

Conversation

@abhidnya13
Copy link
Contributor

@abhidnya13 abhidnya13 commented Jul 30, 2020

Fixes #64
For Keychain:
Scenario:

  • Cache file already exists before using MSAL extensions for the very first time.
  • Gives an error as cannot find corresponding service name and account name entry in Keychain
  • Returned error is KeychainError : ITEM_NOT_FOUND

For Windows:

  • Empty cache file already exists before using MSAL extensions
  • Adds a check to not make call to DPAPI if input is an empty file.
  • Existing code returns an error which is OS error: WinError 87

Added on 8/4/2020: Wiki page summarizing migration scenario from MSAL Python to MSAL Extensions using same file.

@abhidnya13 abhidnya13 requested a review from rayluo July 30, 2020 20:05
Copy link
Contributor

@rayluo rayluo left a comment

Choose a reason for hiding this comment

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

Thanks for your productive investigation! With the better understanding, we can now:

  • Update the corresponding issue (#64) to describe how to reproduce the issue. Something along these lines: "Cache file previously used with MSAL already exists before using MSAL EXtensions for the very first time. Exception ... and ... would be raised on Mac and Windows."

  • Is the current PR's description precise?

    "... errors raised if input is empty ..." and "Empty cache file already exists before using MSAL extensions ...".

    Wouldn't a non-empty cache file still trigger this issue?

  • In terms of the actual adjustment, let's try adjust it at there

@abhidnya13
Copy link
Contributor Author

abhidnya13 commented Jul 31, 2020

Hey Ray,

Thanks for reviewing !

  1. Added the description to the issue
  2. Yes non empty cache file would cause the issue, I have added this in description, this fix however would not solve that error.
    The application developer will have to still use serializing of previous token_cache and save in new extensions persistence in the same file(the migration scenario we talked about) before using extensions for the first time. I can add a wiki page to mention how this can be done.
  3. For having it in persistence layer, I did think about that option, however since this is an macOS specific error, I think having it is osx.py makes more sense. Also, we already do such kind of handling in the same file here

@abhidnya13 abhidnya13 requested a review from rayluo August 3, 2020 20:16
Copy link
Contributor

@rayluo rayluo left a comment

Choose a reason for hiding this comment

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

Your updated description in the original issue #64 looks good!

Is it possible to prepare some test case to mimic those 3 situations, then? Those test cases won't be automated on Travis CI, but it would probably be valuable to have them for our manual testing.

@abhidnya13 abhidnya13 requested a review from rayluo August 4, 2020 08:42
@abhidnya13 abhidnya13 requested a review from rayluo August 6, 2020 23:35
Comment on lines 91 to 92
# Make sure key chain entry does not already exist for below service name
persistence = KeychainPersistence(cache_file, "my_service_name", "my_account_name")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Make sure key chain entry does not already exist for below service name
persistence = KeychainPersistence(cache_file, "my_service_name", "my_account_name")
persistence = KeychainPersistence(
cache_file, "nonexist_service_name", "nonexist_account_name")

with open(self._location, 'rb') as handle:
return self._dp_agent.unprotect(handle.read())
data = handle.read()
return self._dp_agent.unprotect(data) if data else None
Copy link
Member

@bgavrilMS bgavrilMS Aug 10, 2020

Choose a reason for hiding this comment

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

If data is None, it means the cache is corrupt. Because even if you log out all the users, the cache file should not be empty (it should be an empty json). So you can handle this here, but I would also handle the more general case of "cache is corrupt" (i.e. MSAL fails to parse the cache) and delete the cache file / keychain item / keyring entry

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, it will be good to have a general recovery method in the case of cache being corrupt. Created an issue to track this.

from .osx import KeychainError
if exp.exit_status != KeychainError.ITEM_NOT_FOUND:
raise
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

This implementation does not address similar exceptions from other platforms, does it?

And, I have a feeling that, even after we complete all those platform-dependent logic here, this would not look as good as the #67 approach.

Feel free to explore more, and then we discuss.

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 remember closing the conversation on the fact that we just need to address the Keychain error for now and so for the time being we do it in the token cache layer. Similar errors in other platforms can be addressed by creating a new Exception at the persistence layer later. I remember the reason we do not want to introduce a None return value is because persistence layer is exposed as an API and so introducing a new exception at that layer is also something we possibly need to discuss. So eventually once we have had that conversation we can go ahead and visit #67. @henrik-me Am I missing something ?

@rayluo rayluo closed this in #67 Aug 29, 2020
@rayluo rayluo deleted the os_errors_handling branch August 29, 2020 00:14
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.

Investigate OS errors returned for encryption scenarios on Windows and MacOS

6 participants