Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Switch to pure ADAL #94

Merged
merged 13 commits into from
Aug 1, 2018
Merged

Switch to pure ADAL #94

merged 13 commits into from
Aug 1, 2018

Conversation

lmazuel
Copy link
Member

@lmazuel lmazuel commented May 7, 2018

Switch to ADAL implementation. Will require a msrestazure 0.5.0, since there is some internal changes that I can't assume no one is using.
Deprecate at the same time some old stuff, like china=True

Needs AzureAD/azure-activedirectory-library-for-python#142 to be iso-functional

Edit: Since adal 0.6.0 I have everything I need to migrate in a iso-functional way

@lmazuel lmazuel requested a review from yugangw-msft May 7, 2018 22:35
@lmazuel lmazuel force-pushed the pureadal branch 2 times, most recently from c1896a7 to 083a544 Compare May 7, 2018 22:39
@lmazuel lmazuel force-pushed the pureadal branch 2 times, most recently from 45670fa to 35ae5aa Compare May 16, 2018 21:26
@lmazuel
Copy link
Member Author

lmazuel commented May 16, 2018

@yugangw-msft It's done, testing is ok, I'd appreciate if you can take a look :)

I didn't tested SDK yet, doing it right now.

@lmazuel
Copy link
Member Author

lmazuel commented May 16, 2018

ChangeLog:

Breaking changes

These breaking changes applies to ServicePrincipalCredentials, UserPassCredentials, AADTokenCredentials

  • Remove "auth_uri" attribute and parameter. This was unused.
  • Remove "state" attribute. This was unused.
  • Remove "client" attribute. This was exposed by mistake and should have been internal. No replacement is possible.
  • Remove "token_uri" attribute and parameter. Use "cloud_environment" and "tenant" to impact the login url now.

Features

  • Implementation is now using ADAL and not request-oauthlib. This allows more AD scenarios (like federated)
  • Expose ADAL token cache system as API

Copy link
Contributor

@yugangw-msft yugangw-msft left a comment

Choose a reason for hiding this comment

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

Please test it with real credentials. If works, then all good.

# Beware that ADAL returns a pointer to its own dict, do
# NOT change it in place
token = token.copy()

Copy link

Choose a reason for hiding this comment

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

I think, I'm running into this bug with the following code. Can this change be hotfixed on the latest release? It appears this PR may take some time.

AUTHORITY_URL = "https://login.microsoftonline.com" + '/' + os.environ["TENANT"]
auth_context = adal.AuthenticationContext(AUTHORITY_URL, api_version=None)
resource = "https://vault.azure.net"
client_id = os.environ["CLIENT_ID"]
client_secret = os.environ["CLIENT_SECRET"]
token = auth_context.acquire_token_with_client_credentials(resource, client_id, client_secret)
credentials = AADTokenCredentials(token, client_id)
print("Got one token. Trying to get the token again")
token = auth_context.acquire_token_with_client_credentials(resource, client_id, client_secret)
credentials = AADTokenCredentials(token, client_id)
print("Got one token second time as well")

The second print never gets printed and I get this exception.

  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 76, in __init__
    '{itype}'.format(itype=instream.__class__.__name__))
TypeError: Parser must be a string or character stream, not float

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @prburgu
Sounds plausible, but could I get the complete stacktrace please? Not just the last line.

Also, this PR will be merged as 0.5.0, but this creates some conflicts right now with some packages that requires 0.4.x. I hope all will be merged and released by this summer.

Copy link

Choose a reason for hiding this comment

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

Actually, I've worked around this issue in my code, so there is no urgent need for hotfix and I'll leave it up to you on that. Here is the complete stacktrace.

Traceback (most recent call last):
  File "adal_test.py", line 13, in <module>
    token = auth_context.acquire_token_with_client_credentials(resource, client_id, client_secret)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/adal/authentication_context.py", line 179, in acquire_token_with_client_credentials
    return self._acquire_token(token_func)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/adal/authentication_context.py", line 128, in _acquire_token
    return token_func(self)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/adal/authentication_context.py", line 177, in token_func
    return token_request.get_token_with_client_credentials(client_secret)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/adal/token_request.py", line 306, in get_token_with_client_credentials
    token = self._find_token_from_cache()
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/adal/token_request.py", line 128, in _find_token_from_cache
    return self._cache_driver.find(cache_query)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/adal/cache_driver.py", line 201, in find
    is_resource_tenant_specific)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/adal/cache_driver.py", line 167, in _refresh_entry_if_necessary
    expiry_date = parser.parse(entry[TokenResponseFields.EXPIRES_ON])
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 1356, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 645, in parse
    res, skipped_tokens = self._parse(timestr, **kwargs)
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 721, in _parse
    l = _timelex.split(timestr)         # Splits the timestr into tokens
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 207, in split
    return list(cls(s))
  File "/Users/prburgu/Downloads/test1/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 76, in __init__
    '{itype}'.format(itype=instream.__class__.__name__))
TypeError: Parser must be a string or character stream, not float

Copy link
Member Author

Choose a reason for hiding this comment

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

I was looking for the _find_token_from_cache and it's there :). You're right, that's the correct fix.
I'd rather wait for this PR to be merged if it's ok for you.

@lmazuel
Copy link
Member Author

lmazuel commented Aug 1, 2018

Merging in the 0.6.0 branch

@lmazuel lmazuel merged commit 39359bc into dev0.6.0 Aug 1, 2018
@lmazuel lmazuel deleted the pureadal branch August 1, 2018 20:36
@lmazuel
Copy link
Member Author

lmazuel commented Aug 1, 2018

For some reason I called the branch 0.6.0 and 0.5.0.... I renamed the branch.

lmazuel added a commit that referenced this pull request Aug 2, 2018
* Switch to pure ADAL (#94)

* Switch to pure ADAL

* ADAL 0.6.0 and some tests

* Full ADAL testing

* Bring back china=True since easy to support

* Restore set token on __init__ behavior

* Typo

* Adding back attribute as property

* Clarify AADTokenCredentials doc

* Add AzureStack support to credentials

* Fix doc

* Add cache support

* Current 0.5.0 and ChangeLog

* Remove keyring (#112)

* Remove keyring

* keyring changelog

* Deserialize addtionalInfo in ARM error (#102)

* ChangeLog

* Docstring cleanup
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants