Skip to content

Commit

Permalink
[metricsadvisor] support key rotation for MetricsAdvisorKeyCredential (
Browse files Browse the repository at this point in the history
…#16276)

* allow rotation of the api/sub keys for MetricsAdvisorKeyCredential

* fix tests

* fix tests

* update changelog
  • Loading branch information
kristapratico authored Jan 21, 2021
1 parent 9043478 commit 95b40c2
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 2 deletions.
1 change: 1 addition & 0 deletions sdk/metricsadvisor/azure-ai-metricsadvisor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**New Features**

- AAD support authentication #15922
- `MetricsAdvisorKeyCredential` support for rotating the subscription and api keys to update long-lived clients

## 1.0.0b2 (2020-11-10)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,45 @@ def __init__(self, subscription_key, api_key):
# type: (str, str) -> None
if not (isinstance(subscription_key, six.string_types) and isinstance(api_key, six.string_types)):
raise TypeError("key must be a string.")
self.subscription_key = subscription_key # type: str
self.api_key = api_key # type: str
self._subscription_key = subscription_key # type: str
self._api_key = api_key # type: str

@property
def subscription_key(self):
# type: () -> str
"""The value of the subscription key.
:rtype: str
"""
return self._subscription_key

@property
def api_key(self):
# type: () -> str
"""The value of the api key.
:rtype: str
"""
return self._api_key

def update(self, subscription_key=None, api_key=None):
# type: (str, str) -> None
"""Update the subscription and/or api key.
This can be used when you've regenerated your service keys and want
to update long-lived clients.
:param str subscription_key: The subscription key
:param str api_key: The api key
:raises: ValueError or TypeError
"""
if not subscription_key and not api_key:
raise ValueError("Pass at least one non-empty key for updating.")
if subscription_key:
if not isinstance(subscription_key, six.string_types):
raise TypeError("The subscription_key used for updating must be a string.")
self._subscription_key = subscription_key
if api_key:
if not isinstance(api_key, six.string_types):
raise TypeError("The api_key used for updating must be a string.")
self._api_key = api_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"feedbackId":"feedback_id","createdTime":"2020-10-21T20:53:05.91Z","userPrincipal":"[email protected]","metricId":"metric_id","dimensionFilter":{"dimension":{"dimension_name":"Mumbai","category":"Shoes
Handbags & Sunglasses"}},"feedbackType":"Anomaly","startTime":"2020-10-01T00:00:00Z","endTime":"2020-10-01T00:00:00Z","value":{"anomalyValue":"NotAnomaly"},"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","anomalyDetectionConfigurationSnapshot":{"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","name":"detection-config","description":"","metricId":"metric_id","wholeMetricConfiguration":{"smartDetectionCondition":{"sensitivity":100.0,"anomalyDetectorDirection":"Both","suppressCondition":{"minNumber":1,"minRatio":100.0}}},"dimensionGroupOverrideConfigurations":[],"seriesOverrideConfigurations":[]}}'
headers:
apim-request-id:
- ff207a4a-c67b-4009-b4de-3f32d0e144ed
content-length:
- '927'
content-type:
- application/json; charset=utf-8
date:
- Thu, 21 Jan 2021 00:19:44 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '202'
x-request-id:
- ff207a4a-c67b-4009-b4de-3f32d0e144ed
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"code":"Unauthorized","message":"Access denied due to invalid API
key or wrong API endpoint. Make sure to provide a valid key and use a correct
regional API endpoint."}'
headers:
apim-request-id:
- 722f2e0a-c248-4a9b-8e14-0575b51d0010
content-type:
- application/json; charset=utf-8
date:
- Thu, 21 Jan 2021 00:19:44 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
transfer-encoding:
- chunked
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '86'
status:
code: 400
message: Bad Request
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"feedbackId":"feedback_id","createdTime":"2020-10-21T20:53:05.91Z","userPrincipal":"[email protected]","metricId":"metric_id","dimensionFilter":{"dimension":{"dimension_name":"Mumbai","category":"Shoes
Handbags & Sunglasses"}},"feedbackType":"Anomaly","startTime":"2020-10-01T00:00:00Z","endTime":"2020-10-01T00:00:00Z","value":{"anomalyValue":"NotAnomaly"},"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","anomalyDetectionConfigurationSnapshot":{"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","name":"detection-config","description":"","metricId":"metric_id","wholeMetricConfiguration":{"smartDetectionCondition":{"sensitivity":100.0,"anomalyDetectorDirection":"Both","suppressCondition":{"minNumber":1,"minRatio":100.0}}},"dimensionGroupOverrideConfigurations":[],"seriesOverrideConfigurations":[]}}'
headers:
apim-request-id:
- 2107621b-7bb9-403a-b461-a199eb738921
content-length:
- '927'
content-type:
- application/json; charset=utf-8
date:
- Thu, 21 Jan 2021 00:19:45 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '230'
x-request-id:
- 2107621b-7bb9-403a-b461-a199eb738921
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"feedbackId":"feedback_id","createdTime":"2020-10-21T20:53:05.91Z","userPrincipal":"[email protected]","metricId":"metric_id","dimensionFilter":{"dimension":{"dimension_name":"Mumbai","category":"Shoes
Handbags & Sunglasses"}},"feedbackType":"Anomaly","startTime":"2020-10-01T00:00:00Z","endTime":"2020-10-01T00:00:00Z","value":{"anomalyValue":"NotAnomaly"},"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","anomalyDetectionConfigurationSnapshot":{"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","name":"detection-config","description":"","metricId":"metric_id","wholeMetricConfiguration":{"smartDetectionCondition":{"sensitivity":100.0,"anomalyDetectorDirection":"Both","suppressCondition":{"minNumber":1,"minRatio":100.0}}},"dimensionGroupOverrideConfigurations":[],"seriesOverrideConfigurations":[]}}'
headers:
apim-request-id:
- bd25f508-276f-4a03-9c4f-9c01ca86cb09
content-length:
- '927'
content-type:
- application/json; charset=utf-8
date:
- Thu, 21 Jan 2021 00:19:45 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '105'
x-request-id:
- bd25f508-276f-4a03-9c4f-9c01ca86cb09
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"error":{"code":"401","message":"Access denied due to invalid subscription
key or wrong API endpoint. Make sure to provide a valid key for an active
subscription and use a correct regional API endpoint for your resource."}}'
headers:
content-length:
- '224'
date:
- Thu, 21 Jan 2021 00:19:45 GMT
status:
code: 401
message: PermissionDenied
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"feedbackId":"feedback_id","createdTime":"2020-10-21T20:53:05.91Z","userPrincipal":"[email protected]","metricId":"metric_id","dimensionFilter":{"dimension":{"dimension_name":"Mumbai","category":"Shoes
Handbags & Sunglasses"}},"feedbackType":"Anomaly","startTime":"2020-10-01T00:00:00Z","endTime":"2020-10-01T00:00:00Z","value":{"anomalyValue":"NotAnomaly"},"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","anomalyDetectionConfigurationSnapshot":{"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","name":"detection-config","description":"","metricId":"metric_id","wholeMetricConfiguration":{"smartDetectionCondition":{"sensitivity":100.0,"anomalyDetectorDirection":"Both","suppressCondition":{"minNumber":1,"minRatio":100.0}}},"dimensionGroupOverrideConfigurations":[],"seriesOverrideConfigurations":[]}}'
headers:
apim-request-id:
- 0e520a36-d235-4784-b83c-c7fc43a1a47c
content-length:
- '927'
content-type:
- application/json; charset=utf-8
date:
- Thu, 21 Jan 2021 00:19:46 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '106'
x-request-id:
- 0e520a36-d235-4784-b83c-c7fc43a1a47c
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"feedbackId":"feedback_id","createdTime":"2020-10-21T20:53:05.91Z","userPrincipal":"[email protected]","metricId":"metric_id","dimensionFilter":{"dimension":{"dimension_name":"Mumbai","category":"Shoes
Handbags & Sunglasses"}},"feedbackType":"Anomaly","startTime":"2020-10-01T00:00:00Z","endTime":"2020-10-01T00:00:00Z","value":{"anomalyValue":"NotAnomaly"},"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","anomalyDetectionConfigurationSnapshot":{"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","name":"detection-config","description":"","metricId":"metric_id","wholeMetricConfiguration":{"smartDetectionCondition":{"sensitivity":100.0,"anomalyDetectorDirection":"Both","suppressCondition":{"minNumber":1,"minRatio":100.0}}},"dimensionGroupOverrideConfigurations":[],"seriesOverrideConfigurations":[]}}'
headers:
apim-request-id:
- 1b8c4f41-01bc-49dc-a6fb-a52a9d5b5e38
content-length:
- '927'
content-type:
- application/json; charset=utf-8
date:
- Thu, 21 Jan 2021 00:19:46 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '100'
x-request-id:
- 1b8c4f41-01bc-49dc-a6fb-a52a9d5b5e38
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"error":{"code":"401","message":"Access denied due to invalid subscription
key or wrong API endpoint. Make sure to provide a valid key for an active
subscription and use a correct regional API endpoint for your resource."}}'
headers:
content-length:
- '224'
date:
- Thu, 21 Jan 2021 00:19:46 GMT
status:
code: 401
message: PermissionDenied
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-metricsadvisor/1.0.0b3 Python/3.9.0 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://js-metrics-advisor.cognitiveservices.azure.com/metricsadvisor/v1.0/feedback/metric/feedback_id
response:
body:
string: '{"feedbackId":"feedback_id","createdTime":"2020-10-21T20:53:05.91Z","userPrincipal":"[email protected]","metricId":"metric_id","dimensionFilter":{"dimension":{"dimension_name":"Mumbai","category":"Shoes
Handbags & Sunglasses"}},"feedbackType":"Anomaly","startTime":"2020-10-01T00:00:00Z","endTime":"2020-10-01T00:00:00Z","value":{"anomalyValue":"NotAnomaly"},"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","anomalyDetectionConfigurationSnapshot":{"anomalyDetectionConfigurationId":"e17f32d4-3ddf-4dc7-84ee-b4130c7e1777","name":"detection-config","description":"","metricId":"metric_id","wholeMetricConfiguration":{"smartDetectionCondition":{"sensitivity":100.0,"anomalyDetectorDirection":"Both","suppressCondition":{"minNumber":1,"minRatio":100.0}}},"dimensionGroupOverrideConfigurations":[],"seriesOverrideConfigurations":[]}}'
headers:
apim-request-id:
- 5bbd22d2-10b9-4d29-ac53-47bb62ff2588
content-length:
- '927'
content-type:
- application/json; charset=utf-8
date:
- Thu, 21 Jan 2021 00:19:46 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '116'
x-request-id:
- 5bbd22d2-10b9-4d29-ac53-47bb62ff2588
status:
code: 200
message: OK
version: 1
Loading

0 comments on commit 95b40c2

Please sign in to comment.