Azure / azure-cli

Azure Command-Line Interface
MIT License
4.01k stars 2.98k forks source link

"cognitiveservices account create" has variable lag in keys? #29674

Closed thomasnield closed 2 months ago

thomasnield commented 2 months ago

Describe the bug

Hi Azure CLI team,

I've developed some Azure interactive training scenarios for O'Reilly Media on Azure CLI. My colleagues and I have noticed that after running a Cognitive Services account creation like this...

az cognitiveservices account create \
    --name lesson21translater \
    --resource-group $resource \
    --kind TextTranslation \
    --sku F0 \
    --location eastus \
    --yes

And we successfully extract the key like this:

my_key=$(az cognitiveservices account keys list \
    --name lesson21translater \
    --resource-group $resource \
    --query 'key1')

echo $my_key

And we have a Python file to use the service like this:

import requests, uuid, json, sys

# Add your subscription key and endpoint
subscription_key = sys.argv[1].strip('"')

endpoint = "https://api.cognitive.microsofttranslator.com"

# Add your location, also known as region. The default is global.
# This is required if using a Cognitive Services resource.
location = "eastus"

path = '/translate'
constructed_url = endpoint + path

params = {
    'api-version': '3.0',
    'from': 'en',
    'to': ['de', 'it']
}
constructed_url = endpoint + path

headers = {
    'Ocp-Apim-Subscription-Key': subscription_key,
    'Ocp-Apim-Subscription-Region': location,
    'Content-type': 'application/json',
    'X-ClientTraceId': str(uuid.uuid4())
}

# You can pass more than one object in body.
body = [{
    'text': 'Hello World!'
}]

request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()

print(json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))

I get an error object with "code" 401001 indicating the key does not exist. However, if we wait for a variable amount of time, anywhere from 2 minutes to as much of an hour, the key will then be seen. It's like there is an unpredictable, variable lag for when the key is ready.

It's one thing if there's a predictable lag, but a lag with that variance is not ideal to document. Is this a bug or just how the design on the backend works?

Related command

cognitiveservices account create

Errors

{ "error": { "code": 401001, "message": "The request is not authorized because credentials are missing or invalid." } }

Issue script & Debug output

python3 translate.py $my_key --debug /usr/lib/python3/dist-packages/requests/init.py:89: RequestsDependencyWarning: urllib3 (2.2.2) or chardet (3.0.4) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported " { "error": { "code": 401001, "message": "The request is not authorized because credentials are missing or invalid." } }

Expected behavior

Ideally the cognitiveservices account create should not complete until the keys are ready to use. If they cannot, the lag should at least take a deterministic amount of time. With any lag, it should be documented to the user to expect the lag so they don't think the service is broken or they did something wrong.

Environment Summary

azure-cli 2.62.0 *

core 2.62.0 * telemetry 1.1.0

Extensions: account 0.2.5 ad 0.1.0 aem 0.3.0 arcdata 1.5.16 authV2 0.1.3 azure-devops 1.0.1 azure-firewall 1.1.0 azure-iot 0.24.0 baremetal-infrastructure 2.0.1 communication 1.9.3 connectedk8s 1.9.0 connectedmachine 1.0.0b1 costmanagement 0.3.0 customlocation 0.1.3 databox 1.1.0 databricks 1.0.0 datafactory 1.0.2 desktopvirtualization 1.0.0 dev-spaces 1.0.6 express-route-cross-connection 0.1.1 front-door 1.2.0 healthcareapis 0.4.0 hpc-cache 0.1.5 image-copy-extension 0.2.13 k8s-configuration 2.0.0 k8s-extension 1.6.1 kusto 0.5.0 managementpartner 0.1.3 ml 2.28.0 monitor-control-service 1.2.0 peering 0.2.1 rdbms-connect 1.0.6 redisenterprise 1.0.0 resource-graph 2.1.0 sap-hana 0.6.5 sentinel 0.2.0 spring 1.25.0 spring-cloud 3.1.8 ssh 2.0.5 stack-hci 1.1.0 storagesync 1.0.0 support 2.0.0 timeseriesinsights 1.0.0b1 vm-repair 1.0.7 vmware 6.0.1 webpubsub 1.4.0

Dependencies: msal 1.28.1 azure-mgmt-resource 23.1.1

Python location '/opt/az/bin/python3' Extensions directory '/root/.azure/cliextensions'

Python (Linux) 3.11.8 (main, Jul 4 2024, 02:23:43) [GCC 9.4.0]

Legal docs and information: aka.ms/AzureCliLegal

You have 2 update(s) available. Consider updating your CLI installation with 'az upgrade'

Additional context

We are running this with an O'Reilly training environment. https://learning.oreilly.com/interactive-lab/cognitive-services-ai/9781098131371/lab/

azure-client-tools-bot-prd[bot] commented 2 months ago

Hi @thomasnield,

This is not the official Azure CLI published by Microsoft.

How to tell if the installed Azure CLI is unofficial:

Please follow https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux to install Microsoft official Azure CLI.

If you feel that further discussion is needed, please add a comment with the text /clibot unresolve to reopen the issue.

yonzhan commented 2 months ago

Thank you for opening this issue, we will look into it.