Skip to content

Commit 3ec0066

Browse files
jonathan-buttnerelasticsearchmachine
andauthored
[9.2] [Inference API] Fix auth exception listener not called bug (#139966) (#140234)
* [Inference API] Fix auth exception listener not called bug (#139966) * Fixing auth catch bug * Update docs/changelog/139966.yaml * Fixing compile issue * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <infra-root+elasticsearchmachine@elastic.co>
1 parent 72cc6df commit 3ec0066

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

docs/changelog/139966.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 139966
2+
summary: "[Inference API] Fix auth exception listener not called bug"
3+
area: Inference
4+
type: bug
5+
issues: []

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elastic/authorization/ElasticInferenceServiceAuthorizationRequestHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public void getAuthorization(ActionListener<ElasticInferenceServiceAuthorization
113113
} catch (Exception e) {
114114
logger.warn(Strings.format("Retrieving the authorization information encountered an exception: %s", e));
115115
requestCompleteLatch.countDown();
116+
listener.onFailure(e);
116117
}
117118
}
118119

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/elastic/authorization/ElasticInferenceServiceAuthorizationRequestHandlerTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
import static org.hamcrest.Matchers.instanceOf;
4545
import static org.hamcrest.Matchers.is;
4646
import static org.mockito.ArgumentMatchers.any;
47+
import static org.mockito.ArgumentMatchers.anyString;
4748
import static org.mockito.Mockito.doAnswer;
49+
import static org.mockito.Mockito.doThrow;
4850
import static org.mockito.Mockito.mock;
4951
import static org.mockito.Mockito.times;
5052
import static org.mockito.Mockito.verify;
@@ -161,6 +163,44 @@ private void queueWebServerResponsesForRetries(String responseJson) {
161163
}
162164
}
163165

166+
public void testGetAuthorization_ReturnsFailure_WhenExceptionOccurs() throws IOException {
167+
var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, clientManager);
168+
var eisGatewayUrl = getUrl(webServer);
169+
170+
var exceptionToThrow = new IllegalStateException("exception");
171+
var logger = mock(Logger.class);
172+
doThrow(exceptionToThrow).when(logger).debug(anyString());
173+
174+
var authHandler = new ElasticInferenceServiceAuthorizationRequestHandler(eisGatewayUrl, threadPool, logger);
175+
176+
try (var sender = senderFactory.createSender()) {
177+
String responseJson = """
178+
{
179+
"models": [
180+
{
181+
"model_name": "model-a",
182+
"task_types": ["embed/text/sparse", "chat"]
183+
},
184+
{
185+
"model_name": "model-b",
186+
"task_types": ["embed/text/dense"]
187+
}
188+
]
189+
}
190+
""";
191+
192+
webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson));
193+
194+
PlainActionFuture<ElasticInferenceServiceAuthorizationModel> listener = new PlainActionFuture<>();
195+
authHandler.getAuthorization(listener, sender);
196+
197+
var exception = expectThrows(IllegalStateException.class, () -> listener.actionGet(TIMEOUT));
198+
assertThat(exception, is(exceptionToThrow));
199+
200+
assertThat(webServer.requests().size(), is(0));
201+
}
202+
}
203+
164204
public void testGetAuthorization_ReturnsAValidResponse() throws IOException {
165205
var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, clientManager);
166206
var eisGatewayUrl = getUrl(webServer);

0 commit comments

Comments
 (0)