Skip to content

Commit 8209612

Browse files
authored
Merge pull request #220697 from shohei1029/shohei-auth
Update endpoint authentication article to have both key/token and CLI/SDK equivalent
2 parents 75d0a22 + 6834ad3 commit 8209612

File tree

1 file changed

+102
-7
lines changed

1 file changed

+102
-7
lines changed

articles/machine-learning/how-to-authenticate-online-endpoint.md

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ ms.subservice: mlops
88
author: dem108
99
ms.author: sehan
1010
ms.reviewer: mopeakande
11-
ms.date: 11/04/2022
11+
ms.date: 12/07/2022
1212
ms.topic: how-to
13-
ms.custom: how-to, devplatv2, cliv2, event-tier1-build-2022, ignite-2022
13+
ms.custom: how-to, devplatv2, cliv2, sdkv2, event-tier1-build-2022, ignite-2022
1414
---
1515

1616
# Key and token-based authentication for online endpoints
@@ -35,15 +35,110 @@ Access to retrieve the key or token for an online endpoint is restricted by Azur
3535

3636
For more information on using Azure RBAC with Azure Machine Learning, see [Manage access to Azure Machine Learning](how-to-assign-roles.md).
3737

38-
To get the key, use [az ml online-endpoint get-credentials](/cli/azure/ml/online-endpoint#az-ml-online-endpoint-get-credentials). This command returns a JSON document that contains the key or token. __Keys__ will be returned in the `primaryKey` and `secondaryKey` fields. __Tokens__ will be returned in the `accessToken` field. Additionally, the `expiryTimeUtc` and `refreshAfterTimeUtc` fields contain the token expiration and refresh times. The following example shows how to use the `--query` parameter to return only the primary key:
38+
# [Azure CLI](#tab/azure-cli)
3939

40-
:::code language="azurecli" source="~/azureml-examples-main/cli/deploy-managed-online-endpoint.sh" ID="test_endpoint_using_curl_get_key":::
40+
To get the key or token, use [az ml online-endpoint get-credentials](/cli/azure/ml/online-endpoint#az-ml-online-endpoint-get-credentials). This command returns a JSON document that contains the key or token.
4141

42-
## Score data using the token
42+
__Keys__ will be returned in the `primaryKey` and `secondaryKey` fields. The following example shows how to use the `--query` parameter to return only the primary key:
4343

44-
When calling the online endpoint for scoring, pass the key or token in the authorization header. The following example shows how to use the curl utility to call the online endpoint using a key (if using a token, replace `$ENDPOINT_KEY` with the token value):
44+
```azurecli
45+
ENDPOINT_CRED=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -o tsv --query primaryKey)
46+
```
4547

46-
::: code language="azurecli" source="~/azureml-examples-main/cli/deploy-managed-online-endpoint.sh" ID="test_endpoint_using_curl" :::
48+
__Tokens__ will be returned in the `accessToken` field:
49+
50+
```azurecli
51+
ENDPOINT_CRED=$(az ml online-endpoint get-credentials -n $ENDPOINT_NAME -o tsv --query accessToken)
52+
```
53+
54+
Additionally, the `expiryTimeUtc` and `refreshAfterTimeUtc` fields contain the token expiration and refresh times.
55+
56+
# [Python](#tab/python)
57+
58+
To get the key or token, use the [get_keys](/python/api/azure-ai-ml/azure.ai.ml.operations.onlineendpointoperations#azure-ai-ml-operations-onlineendpointoperations-get-keys) method in the `OnlineEndpointOperations` Class.
59+
60+
__Keys__ will be returned in the `primary_key` and `secondary_key` fields:
61+
62+
```Python
63+
endpoint_cred = ml_client.online_endpoints.get_keys(name=endpoint_name).primary_key
64+
```
65+
66+
__Tokens__ will be returned in the `accessToken` field:
67+
68+
```Python
69+
endpoint_cred = ml_client.online_endpoints.get_keys(name=endpoint_name).access_token
70+
```
71+
72+
Additionally, the `expiry_time_utc` and `refresh_after_time_utc` fields contain the token expiration and refresh times.
73+
74+
For example, to get the `expiry_time_utc`:
75+
```Python
76+
print(ml_client.online_endpoints.get_keys(name=endpoint_name).expiry_time_utc)
77+
```
78+
79+
---
80+
81+
## Score data using the key or token
82+
83+
# [Azure CLI](#tab/azure-cli)
84+
85+
When calling the online endpoint for scoring, pass the key or token in the authorization header. The following example shows how to use the curl utility to call the online endpoint using a key/token:
86+
87+
```azurecli
88+
SCORING_URI=$(az ml online-endpoint show -n $ENDPOINT_NAME -o tsv --query scoring_uri)
89+
90+
curl --request POST "$SCORING_URI" --header "Authorization: Bearer $ENDPOINT_CRED" --header 'Content-Type: application/json' --data @endpoints/online/model-1/sample-request.json
91+
```
92+
93+
# [Python](#tab/python)
94+
95+
When calling the online endpoint for scoring, pass the key or token in the authorization header. The following example shows how to call the online endpoint using a key/token in Python. In the example, replace the `api_key` variable with your key/token you obtained.
96+
97+
```Python
98+
import urllib.request
99+
import json
100+
import os
101+
import ssl
102+
103+
def allowSelfSignedHttps(allowed):
104+
# bypass the server certificate verification on client side
105+
if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
106+
ssl._create_default_https_context = ssl._create_unverified_context
107+
108+
allowSelfSignedHttps(True) # this line is needed if you use self-signed certificate in your scoring service
109+
110+
# Request data goes here
111+
# The example below assumes JSON formatting which may be updated
112+
# depending on the format your endpoint expects
113+
# More information can be found here:
114+
# https://docs.microsoft.com/azure/machine-learning/how-to-deploy-advanced-entry-script
115+
data = {}
116+
117+
body = str.encode(json.dumps(data))
118+
119+
url = 'https://endpt-auth-token.eastus.inference.ml.azure.com/score'
120+
api_key = '<endpoint_cred>' # Replace this with the key or token you obtained
121+
assert api_key != "<endpoint_cred>", "key should be provided to invoke the endpoint"
122+
123+
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}
124+
125+
req = urllib.request.Request(url, body, headers)
126+
127+
try:
128+
response = urllib.request.urlopen(req)
129+
130+
result = response.read()
131+
print(result)
132+
except urllib.error.HTTPError as error:
133+
print("The request failed with status code: " + str(error.code))
134+
135+
# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
136+
print(error.info())
137+
print(error.read().decode("utf8", 'ignore'))
138+
```
139+
140+
141+
---
47142

48143
## Next steps
49144

0 commit comments

Comments
 (0)