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

[Eventgrid] Improve error message when providing wrong authentication #15939

Closed
rakshith91 opened this issue Dec 29, 2020 · 3 comments · Fixed by #16206
Closed

[Eventgrid] Improve error message when providing wrong authentication #15939

rakshith91 opened this issue Dec 29, 2020 · 3 comments · Fixed by #16206
Assignees
Labels
blocking-release Blocks release Client This issue points to a problem in the data-plane of the library. Event Grid Messaging Messaging crew

Comments

@rakshith91
Copy link
Contributor

Upon providing wrong key for authentication during publisher client, we get

UnboundLocalError: local variable 'authentication_policy' referenced before assignment on providing wrong credential in the publisher client

This message must improve - perhaps start with insisting on providing an AzureKeyCredential?

@rakshith91 rakshith91 added Client This issue points to a problem in the data-plane of the library. Event Grid labels Dec 29, 2020
@lmazuel
Copy link
Member

lmazuel commented Dec 31, 2020

Can you paste a full example with the full stacktrace of this?

@rakshith91
Copy link
Contributor Author

Can you paste a full example with the full stacktrace of this?

>>> e  = EventGridPublisherClient("https://rakshith-eg.westus-1.eventgrid.azure.net/api/events", "dsf")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Administrator\Documents\Workspace\azure-sdk-for-python\envtrace\lib\site-packages\azure\eventgrid\_publisher_client.py", line 75, in __init__
    policies=EventGridPublisherClient._policies(credential, **kwargs),
  File "C:\Users\Administrator\Documents\Workspace\azure-sdk-for-python\envtrace\lib\site-packages\azure\eventgrid\_publisher_client.py", line 82, in _policies
    auth_policy = _get_authentication_policy(credential)
  File "C:\Users\Administrator\Documents\Workspace\azure-sdk-for-python\envtrace\lib\site-packages\azure\eventgrid\_helpers.py", line 87, in _get_authentication_policy
    return authentication_policy
UnboundLocalError: local variable 'authentication_policy' referenced before assignment

@lmazuel
Copy link
Member

lmazuel commented Jan 4, 2021

That's a bug in this code:

def _get_authentication_policy(credential):
if credential is None:
raise ValueError("Parameter 'self._credential' must not be None.")
if isinstance(credential, AzureKeyCredential):
authentication_policy = AzureKeyCredentialPolicy(credential=credential, name=constants.EVENTGRID_KEY_HEADER)
if isinstance(credential, EventGridSharedAccessSignatureCredential):
authentication_policy = EventGridSharedAccessSignatureCredentialPolicy(
credential=credential,
name=constants.EVENTGRID_TOKEN_HEADER
)
return authentication_policy

If all if are wrong, we reach a return on an undeclared variable.

Correct code:

def _get_authentication_policy(credential):
    if credential is None:
        raise ValueError("Parameter 'self._credential' must not be None.")
    if isinstance(credential, AzureKeyCredential):
        return AzureKeyCredentialPolicy(credential=credential, name=constants.EVENTGRID_KEY_HEADER)
    if isinstance(credential, EventGridSharedAccessSignatureCredential):
        return EventGridSharedAccessSignatureCredentialPolicy(
            credential=credential,
            name=constants.EVENTGRID_TOKEN_HEADER
        )
    raise ValueError("The provided credential should be an instance of EventGridSharedAccessSignatureCredentialPolicy or AzureKeyCredentialPolicy")

@lmazuel lmazuel added this to the [2021] February milestone Jan 4, 2021
@lmazuel lmazuel added the blocking-release Blocks release label Jan 4, 2021
@lmazuel lmazuel added the Messaging Messaging crew label Apr 12, 2021
openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-python that referenced this issue Sep 15, 2021
[Hub Generated] Review request for Microsoft.LabServices to add version preview/2021-10-01-preview (Azure#15939)

* Adds base for updating Microsoft.LabServices from version stable/2018-10-15 to version 2021-10-01-preview

* Updates readme

* Updates API version in new specs and examples

* remove 2018 api spec files

* add 2021-10-01-preview spec files from generation

* update preview api files in readme.md input

* update 2020 -> 2021 in new api version in readme

* spelling fixes

* fix object body mutability

* add type:object where missing

* added default error response

* tracker resource operation naming and description adding

* fix shutdownOnIdleEnabled in examples

* fix shutdownonIdle enum in examples

* examples update username to adminUser object and remove locations from vmprofile

* fix examples for vmprofile

* rest of model validaiton fixes

* prettier fix

* removed skus api from 2021-10-01 version since it's not finished

* fix operationresults

* small images fix

Co-authored-by: Nick Depinet <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocking-release Blocks release Client This issue points to a problem in the data-plane of the library. Event Grid Messaging Messaging crew
Projects
None yet
2 participants