Skip to content

Commit 5a3fb46

Browse files
fix: Speech service API issue fixed (#2202)
1 parent 22bdf46 commit 5a3fb46

2 files changed

Lines changed: 38 additions & 17 deletions

File tree

code/create_app.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,15 +742,32 @@ def speech_config():
742742
"""Get the speech config for Azure Speech."""
743743
try:
744744
logger.info("Method speech_config started")
745-
speech_key = env_helper.AZURE_SPEECH_KEY or get_speech_key(env_helper)
746745

747-
response = requests.post(
748-
f"{env_helper.AZURE_SPEECH_REGION_ENDPOINT}sts/v1.0/issueToken",
749-
headers={
750-
"Ocp-Apim-Subscription-Key": speech_key,
751-
},
752-
timeout=5,
753-
)
746+
if env_helper.AZURE_AUTH_TYPE == "rbac":
747+
credential = get_azure_credential(
748+
env_helper.MANAGED_IDENTITY_CLIENT_ID
749+
)
750+
token = credential.get_token(
751+
"https://cognitiveservices.azure.com/.default"
752+
)
753+
response = requests.post(
754+
f"{env_helper.AZURE_SPEECH_REGION_ENDPOINT}sts/v1.0/issueToken",
755+
headers={
756+
"Authorization": f"Bearer {token.token}",
757+
},
758+
timeout=5,
759+
)
760+
else:
761+
speech_key = env_helper.AZURE_SPEECH_KEY or get_speech_key(
762+
env_helper
763+
)
764+
response = requests.post(
765+
f"{env_helper.AZURE_SPEECH_REGION_ENDPOINT}sts/v1.0/issueToken",
766+
headers={
767+
"Ocp-Apim-Subscription-Key": speech_key,
768+
},
769+
timeout=5,
770+
)
754771

755772
if response.status_code == 200:
756773
return {

code/tests/test_create_app.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def env_helper_mock():
9292
AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG
9393
)
9494
env_helper.SHOULD_STREAM = True
95+
env_helper.AZURE_AUTH_TYPE = "keys"
9596
env_helper.is_auth_type_keys.return_value = True
9697
env_helper.CONVERSATION_FLOW = ConversationFlow.CUSTOM.value
9798

@@ -128,25 +129,24 @@ def test_returns_speech_token_using_keys(
128129
timeout=5,
129130
)
130131

131-
@patch("create_app.CognitiveServicesManagementClient")
132+
@patch("create_app.get_azure_credential")
132133
@patch("create_app.requests")
133134
def test_returns_speech_token_using_rbac(
134135
self,
135136
requests: MagicMock,
136-
CognitiveServicesManagementClientMock: MagicMock,
137+
get_azure_credential_mock: MagicMock,
137138
env_helper_mock: MagicMock,
138139
client: FlaskClient,
139140
):
140141
"""Test that the speech token is returned correctly when using RBAC."""
141142
# given
143+
env_helper_mock.AZURE_AUTH_TYPE = "rbac"
142144
env_helper_mock.AZURE_SPEECH_KEY = None
145+
env_helper_mock.MANAGED_IDENTITY_CLIENT_ID = "mock-client-id"
143146

144-
mock_cognitive_services_client_mock = (
145-
CognitiveServicesManagementClientMock.return_value
146-
)
147-
mock_cognitive_services_client_mock.accounts.list_keys.return_value = MagicMock(
148-
key1="mock-key1", key2="mock-key2"
149-
)
147+
mock_credential = MagicMock()
148+
mock_credential.get_token.return_value = MagicMock(token="mock-aad-token")
149+
get_azure_credential_mock.return_value = mock_credential
150150

151151
mock_response: MagicMock = requests.post.return_value
152152
mock_response.text = "speech-token"
@@ -163,10 +163,14 @@ def test_returns_speech_token_using_rbac(
163163
"languages": AZURE_SPEECH_RECOGNIZER_LANGUAGES,
164164
}
165165

166+
get_azure_credential_mock.assert_called_once_with("mock-client-id")
167+
mock_credential.get_token.assert_called_once_with(
168+
"https://cognitiveservices.azure.com/.default"
169+
)
166170
requests.post.assert_called_once_with(
167171
f"{AZURE_SPEECH_REGION_ENDPOINT}sts/v1.0/issueToken",
168172
headers={
169-
"Ocp-Apim-Subscription-Key": "mock-key1",
173+
"Authorization": "Bearer mock-aad-token",
170174
},
171175
timeout=5,
172176
)

0 commit comments

Comments
 (0)