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

Instruct Azure CLI to not color output #11362

Merged
merged 4 commits into from
Jun 29, 2020
Merged

Instruct Azure CLI to not color output #11362

merged 4 commits into from
Jun 29, 2020

Conversation

NVolcz
Copy link
Contributor

@NVolcz NVolcz commented May 10, 2020

The Azure CLI supports colored output by using colorama which resets the color after execution by printing "[0m" if the terminal supports color. This can in some cases cause the AzureCliCredential to fail for example when developing in PyCharm: https://github.com/Azure/azure-cli/issues/9903.
Azure CLI allows color output to be disabled by setting the environment variable:AZURE_CORE_NO_COLOR.

The PR in azure-cli: Azure/azure-cli#12601

@chlowell
Copy link
Member

Hi @NVolcz, thanks for opening this! I was able to reproduce the problem locally with an older version of the CLI but not with the latest version (2.5.1). Do you still see this problem with the latest CLI?

@NVolcz
Copy link
Contributor Author

NVolcz commented May 13, 2020

Upgrading to Azure CLI to 2.5.1 fixed this, thanks! I was running version 2.3.1 of the CLI.

@chlowell
Copy link
Member

Great, then it seems that while adding AZURE_CORE_NO_COLOR, someone made the default behavior of get-access-token what we want. I'll close this then. Thanks again for opening it!

@chlowell chlowell closed this May 13, 2020
@NVolcz
Copy link
Contributor Author

NVolcz commented Jun 15, 2020

It seems that this behavior has once again changed. I am now running azure-cli 2.7.0 and are getting the color reset sequence in the end of the JSON.

@chlowell
Copy link
Member

That's interesting, I don't see it from 2.7.0 either. @jiasli, what default coloring behavior should we expect from get-access-token?

@jiasli jiasli self-requested a review June 16, 2020 02:10
@jiasli
Copy link
Member

jiasli commented Jun 16, 2020

I am able to repro. The testing script is very simple:

from azure.identity import AzureCliCredential

cred = AzureCliCredential()
token = cred.get_token('https://management.azure.com/')
print(token)

Add a breakpoint at:

image

In a shell, the issue doesn't occur:

> python D:\cli\testproj\demo.py
AccessToken(token='....', expires_on=1592293455)

This issue should always be there. We didn't change anything when color does get enabled.

When AzureCliCredential doesn't explicitly tell Azure CLI to disable color, Azure CLI will initiate colorama. However, when colorama detects PYCHARM_HOSTED env var is set, it doesn't query on stream.isatty anymore.

https://github.com/tartley/colorama/blob/bfa8c22e8f9b51ad3428c165087540cea5d44a1f/colorama/ansitowin32.py#L45-L47

        if 'PYCHARM_HOSTED' in os.environ:
            if stream is not None and (stream is sys.__stdout__ or stream is sys.__stderr__):
                return True

So, colorama will print the control sequence \033[0m when Azure CLI exits.

https://github.com/tartley/colorama/blob/bfa8c22e8f9b51ad3428c165087540cea5d44a1f/colorama/ansitowin32.py#L170-L174

    def reset_all(self):
        if self.convert:
            self.call_win32('m', (0,))
        elif not self.strip and not self.stream.closed:
            self.wrapped.write(Style.RESET_ALL)

I think this is more of an issue with colorama and PyCharm. For Azure CLI and Azure Identity, the best approach is to explicitly disable color in Azure CLI so that colorama never gets initiated.

I have created issue tartley/colorama#263 in colorama's repo.

@NVolcz
Copy link
Contributor Author

NVolcz commented Jun 16, 2020

Would it be crazy to suggest to reconsider my pull request in light of this impediment with colorama?

@chlowell
Copy link
Member

Not at all, that would be entirely reasonable because it seems we must do something here 😄

If you want to fix up this pull request, my feedback on its current state is:

  • the fix should also apply to non-Windows platforms and the async AzureCliCredential
  • the sync credential must support Python 2.7 (e.g. no f-strings)
  • I prefer using the env keyword argument to check_output and create_subprocess_exec

If not, no worries, we'll get this fixed in the next release.

@NVolcz
Copy link
Contributor Author

NVolcz commented Jun 20, 2020

I have updated my PR but haven't verified that it works on Linux yet.

@chlowell
Copy link
Member

Thanks for the update. There are still a couple things left to get this over the finish line: I'd rather set the environment variable with the env keyword argument when starting the subprocess, and the async AzureCliCredential needs the same fix.

This needs to merge early next week to make the next release. Is that too soon for you? I can push a couple commits to finish this if you like.

@NVolcz NVolcz force-pushed the master branch 4 times, most recently from 4e8c2d4 to d225585 Compare June 27, 2020 19:03
The Azure CLI supports colored output by using colorama which resets the color
after execution by printing "[0m" if the terminal supports color. This can in
some cases cause the AzureCliCredential to fail for example when developing
in PyCharm: Azure/azure-cli#9903
Azure CLI allows color output to be disabled by setting the environment
variable:AZURE_CORE_NO_COLOR.

The PR in azure-cli: Azure/azure-cli#12601
@NVolcz
Copy link
Contributor Author

NVolcz commented Jun 27, 2020

How about now?

@chlowell
Copy link
Member

Looks good to me, I'll take it 😄

Thanks for reporting this, and for fixing it!

chlowell
chlowell previously approved these changes Jun 29, 2020
@chlowell chlowell self-requested a review June 29, 2020 16:07
@chlowell chlowell merged commit ffefbbb into Azure:master Jun 29, 2020
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Jun 30, 2020
…into regenerate_certs

* 'master' of https://github.com/Azure/azure-sdk-for-python: (27 commits)
  Set http_logging_policy in Configuration (Azure#12218)
  Sync eng/common directory with azure-sdk-tools repository (Azure#11990)
  AzureCliCredential instructs CLI not to color output (Azure#11362)
  Sdk automation/track2 azure mgmt storage (Azure#12238)
  Fix changelog of CustomVision (Azure#12225)
  Doc update for conf file name (Azure#12224)
  update doc for content_type (Azure#12220)
  update API version to use 2020-06-30 (Azure#12208)
  Use MSAL's custom transport API (Azure#11892)
  add breadcumbs for training filter (Azure#12196)
  [formrecognizer] arch board feedback renames (Azure#12207)
  Dataplane autogeneration (Azure#12210)
  update version number and API_version support (Azure#12154)
  Update SECURITY.md (Azure#12209)
  adding a pip freeze to ensure we fully understand what our environment has (Azure#12173)
  FaceAPI 0.4.1 (Azure#12199)
  [ServiceBus] Enable Async Unittest (Azure#12001)
  add search owver (Azure#12190)
  update training docstring (Azure#12168)
  don't use mgmt track2 (Azure#12183)
  ...
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Jun 30, 2020
…into regenerate_keys

* 'master' of https://github.com/Azure/azure-sdk-for-python:
  Set http_logging_policy in Configuration (Azure#12218)
  Sync eng/common directory with azure-sdk-tools repository (Azure#11990)
  AzureCliCredential instructs CLI not to color output (Azure#11362)
  Sdk automation/track2 azure mgmt storage (Azure#12238)
  Fix changelog of CustomVision (Azure#12225)
  Doc update for conf file name (Azure#12224)
  update doc for content_type (Azure#12220)
  update API version to use 2020-06-30 (Azure#12208)
  Use MSAL's custom transport API (Azure#11892)
  add breadcumbs for training filter (Azure#12196)
  [formrecognizer] arch board feedback renames (Azure#12207)
  Dataplane autogeneration (Azure#12210)
  update version number and API_version support (Azure#12154)
  Update SECURITY.md (Azure#12209)
  adding a pip freeze to ensure we fully understand what our environment has (Azure#12173)
  FaceAPI 0.4.1 (Azure#12199)
  [ServiceBus] Enable Async Unittest (Azure#12001)
  add search owver (Azure#12190)
  update training docstring (Azure#12168)
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Jun 30, 2020
…into regenerate_secrets

* 'master' of https://github.com/Azure/azure-sdk-for-python: (36 commits)
  Install dev dependency when running apistub (Azure#12268)
  SharedTokenCacheCredential lazily loads the cache (Azure#12172)
  Changes in docs [Form Recognizer] (Azure#12216)
  [formrecognizer] adjust text angle to fit in specified interval (Azure#12248)
  Set http_logging_policy in Configuration (Azure#12218)
  Sync eng/common directory with azure-sdk-tools repository (Azure#11990)
  AzureCliCredential instructs CLI not to color output (Azure#11362)
  Sdk automation/track2 azure mgmt storage (Azure#12238)
  Fix changelog of CustomVision (Azure#12225)
  Doc update for conf file name (Azure#12224)
  update doc for content_type (Azure#12220)
  update API version to use 2020-06-30 (Azure#12208)
  Use MSAL's custom transport API (Azure#11892)
  add breadcumbs for training filter (Azure#12196)
  [formrecognizer] arch board feedback renames (Azure#12207)
  Dataplane autogeneration (Azure#12210)
  update version number and API_version support (Azure#12154)
  Update SECURITY.md (Azure#12209)
  adding a pip freeze to ensure we fully understand what our environment has (Azure#12173)
  FaceAPI 0.4.1 (Azure#12199)
  ...
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.

3 participants