|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.services.elastic.authorization; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.client.internal.Client; |
| 12 | +import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue; |
| 13 | +import org.elasticsearch.core.TimeValue; |
| 14 | +import org.elasticsearch.inference.TaskType; |
| 15 | +import org.elasticsearch.tasks.TaskId; |
| 16 | +import org.elasticsearch.test.ESTestCase; |
| 17 | +import org.elasticsearch.xpack.inference.external.http.sender.Sender; |
| 18 | +import org.elasticsearch.xpack.inference.registry.ModelRegistry; |
| 19 | +import org.elasticsearch.xpack.inference.registry.StoreInferenceEndpointsAction; |
| 20 | +import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceServiceComponents; |
| 21 | +import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceServiceSettingsTests; |
| 22 | +import org.elasticsearch.xpack.inference.services.elastic.InternalPreconfiguredEndpoints; |
| 23 | +import org.elasticsearch.xpack.inference.services.elastic.response.ElasticInferenceServiceAuthorizationResponseEntity; |
| 24 | +import org.junit.Before; |
| 25 | +import org.mockito.ArgumentCaptor; |
| 26 | + |
| 27 | +import java.util.EnumSet; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +import static org.elasticsearch.xpack.inference.services.ServiceComponentsTests.createWithEmptySettings; |
| 33 | +import static org.hamcrest.Matchers.is; |
| 34 | +import static org.mockito.ArgumentMatchers.any; |
| 35 | +import static org.mockito.ArgumentMatchers.eq; |
| 36 | +import static org.mockito.Mockito.doAnswer; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.never; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | +import static org.mockito.Mockito.when; |
| 41 | + |
| 42 | +public class AuthorizationPollerTests extends ESTestCase { |
| 43 | + private DeterministicTaskQueue taskQueue; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void init() throws Exception { |
| 47 | + taskQueue = new DeterministicTaskQueue(); |
| 48 | + } |
| 49 | + |
| 50 | + public void testDoesNotSendAuthorizationRequest_WhenModelRegistryIsNotReady() { |
| 51 | + var mockRegistry = mock(ModelRegistry.class); |
| 52 | + when(mockRegistry.isReady()).thenReturn(false); |
| 53 | + |
| 54 | + var authorizationRequestHandler = mock(ElasticInferenceServiceAuthorizationRequestHandler.class); |
| 55 | + |
| 56 | + var poller = new AuthorizationPoller( |
| 57 | + new AuthorizationPoller.TaskFields(0, "abc", "abc", "abc", new TaskId("abc", 0), Map.of()), |
| 58 | + createWithEmptySettings(taskQueue.getThreadPool()), |
| 59 | + authorizationRequestHandler, |
| 60 | + mock(Sender.class), |
| 61 | + ElasticInferenceServiceSettingsTests.create(null, TimeValue.timeValueMillis(1), TimeValue.timeValueMillis(1), true), |
| 62 | + new ElasticInferenceServiceComponents(""), |
| 63 | + mockRegistry, |
| 64 | + mock(Client.class), |
| 65 | + null |
| 66 | + ); |
| 67 | + |
| 68 | + poller.sendAuthorizationRequest(); |
| 69 | + |
| 70 | + verify(authorizationRequestHandler, never()).getAuthorization(any(), any()); |
| 71 | + } |
| 72 | + |
| 73 | + public void testSendsAuthorizationRequest_WhenModelRegistryIsReady() { |
| 74 | + var mockRegistry = mock(ModelRegistry.class); |
| 75 | + when(mockRegistry.isReady()).thenReturn(true); |
| 76 | + when(mockRegistry.getInferenceIds()).thenReturn(Set.of("id1", "id2")); |
| 77 | + |
| 78 | + var mockAuthHandler = mock(ElasticInferenceServiceAuthorizationRequestHandler.class); |
| 79 | + doAnswer(invocation -> { |
| 80 | + ActionListener<ElasticInferenceServiceAuthorizationModel> listener = invocation.getArgument(0); |
| 81 | + listener.onResponse( |
| 82 | + ElasticInferenceServiceAuthorizationModel.of( |
| 83 | + new ElasticInferenceServiceAuthorizationResponseEntity( |
| 84 | + List.of( |
| 85 | + new ElasticInferenceServiceAuthorizationResponseEntity.AuthorizedModel( |
| 86 | + InternalPreconfiguredEndpoints.DEFAULT_ELSER_2_MODEL_ID, |
| 87 | + EnumSet.of(TaskType.SPARSE_EMBEDDING) |
| 88 | + ) |
| 89 | + ) |
| 90 | + ) |
| 91 | + ) |
| 92 | + ); |
| 93 | + return Void.TYPE; |
| 94 | + }).when(mockAuthHandler).getAuthorization(any(), any()); |
| 95 | + |
| 96 | + var mockClient = mock(Client.class); |
| 97 | + when(mockClient.threadPool()).thenReturn(taskQueue.getThreadPool()); |
| 98 | + |
| 99 | + var eisComponents = new ElasticInferenceServiceComponents(""); |
| 100 | + |
| 101 | + var poller = new AuthorizationPoller( |
| 102 | + new AuthorizationPoller.TaskFields(0, "abc", "abc", "abc", new TaskId("abc", 0), Map.of()), |
| 103 | + createWithEmptySettings(taskQueue.getThreadPool()), |
| 104 | + mockAuthHandler, |
| 105 | + mock(Sender.class), |
| 106 | + ElasticInferenceServiceSettingsTests.create(null, TimeValue.timeValueMillis(1), TimeValue.timeValueMillis(1), true), |
| 107 | + eisComponents, |
| 108 | + mockRegistry, |
| 109 | + mockClient, |
| 110 | + null |
| 111 | + ); |
| 112 | + |
| 113 | + var requestArgCaptor = ArgumentCaptor.forClass(StoreInferenceEndpointsAction.Request.class); |
| 114 | + |
| 115 | + poller.sendAuthorizationRequest(); |
| 116 | + verify(mockClient).execute(eq(StoreInferenceEndpointsAction.INSTANCE), requestArgCaptor.capture(), any()); |
| 117 | + var capturedRequest = requestArgCaptor.getValue(); |
| 118 | + assertThat( |
| 119 | + capturedRequest.getModels(), |
| 120 | + is( |
| 121 | + List.of( |
| 122 | + PreconfiguredEndpointModelAdapter.createModel( |
| 123 | + InternalPreconfiguredEndpoints.getWithInferenceId(InternalPreconfiguredEndpoints.DEFAULT_ELSER_ENDPOINT_ID_V2), |
| 124 | + eisComponents |
| 125 | + ) |
| 126 | + ) |
| 127 | + ) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + public void testDoesNotAttemptToStoreModelIds_ThatDoNotExistInThePreconfiguredMapping() { |
| 132 | + var mockRegistry = mock(ModelRegistry.class); |
| 133 | + when(mockRegistry.isReady()).thenReturn(true); |
| 134 | + when(mockRegistry.getInferenceIds()).thenReturn(Set.of("id1", "id2")); |
| 135 | + |
| 136 | + var mockAuthHandler = mock(ElasticInferenceServiceAuthorizationRequestHandler.class); |
| 137 | + doAnswer(invocation -> { |
| 138 | + ActionListener<ElasticInferenceServiceAuthorizationModel> listener = invocation.getArgument(0); |
| 139 | + listener.onResponse( |
| 140 | + ElasticInferenceServiceAuthorizationModel.of( |
| 141 | + new ElasticInferenceServiceAuthorizationResponseEntity( |
| 142 | + List.of( |
| 143 | + new ElasticInferenceServiceAuthorizationResponseEntity.AuthorizedModel( |
| 144 | + // This is a model id that does not exist in the preconfigured endpoints map so it will not be stored |
| 145 | + "abc", |
| 146 | + EnumSet.of(TaskType.SPARSE_EMBEDDING) |
| 147 | + ) |
| 148 | + ) |
| 149 | + ) |
| 150 | + ) |
| 151 | + ); |
| 152 | + return Void.TYPE; |
| 153 | + }).when(mockAuthHandler).getAuthorization(any(), any()); |
| 154 | + |
| 155 | + var mockClient = mock(Client.class); |
| 156 | + when(mockClient.threadPool()).thenReturn(taskQueue.getThreadPool()); |
| 157 | + |
| 158 | + var eisComponents = new ElasticInferenceServiceComponents(""); |
| 159 | + |
| 160 | + var poller = new AuthorizationPoller( |
| 161 | + new AuthorizationPoller.TaskFields(0, "abc", "abc", "abc", new TaskId("abc", 0), Map.of()), |
| 162 | + createWithEmptySettings(taskQueue.getThreadPool()), |
| 163 | + mockAuthHandler, |
| 164 | + mock(Sender.class), |
| 165 | + ElasticInferenceServiceSettingsTests.create(null, TimeValue.timeValueMillis(1), TimeValue.timeValueMillis(1), true), |
| 166 | + eisComponents, |
| 167 | + mockRegistry, |
| 168 | + mockClient, |
| 169 | + null |
| 170 | + ); |
| 171 | + |
| 172 | + var requestArgCaptor = ArgumentCaptor.forClass(StoreInferenceEndpointsAction.Request.class); |
| 173 | + |
| 174 | + poller.sendAuthorizationRequest(); |
| 175 | + verify(mockClient, never()).execute(eq(StoreInferenceEndpointsAction.INSTANCE), requestArgCaptor.capture(), any()); |
| 176 | + } |
| 177 | + |
| 178 | + public void testDoesNotAttemptToStoreModelIds_ThatHaveATaskTypeThatTheEISIntegration_DoesNotSupport() { |
| 179 | + var mockRegistry = mock(ModelRegistry.class); |
| 180 | + when(mockRegistry.isReady()).thenReturn(true); |
| 181 | + when(mockRegistry.getInferenceIds()).thenReturn(Set.of("id1", "id2")); |
| 182 | + |
| 183 | + var mockAuthHandler = mock(ElasticInferenceServiceAuthorizationRequestHandler.class); |
| 184 | + doAnswer(invocation -> { |
| 185 | + ActionListener<ElasticInferenceServiceAuthorizationModel> listener = invocation.getArgument(0); |
| 186 | + listener.onResponse( |
| 187 | + ElasticInferenceServiceAuthorizationModel.of( |
| 188 | + new ElasticInferenceServiceAuthorizationResponseEntity( |
| 189 | + List.of( |
| 190 | + new ElasticInferenceServiceAuthorizationResponseEntity.AuthorizedModel( |
| 191 | + InternalPreconfiguredEndpoints.DEFAULT_ELSER_2_MODEL_ID, |
| 192 | + // EIS does not yet support completions so this model will be ignored |
| 193 | + EnumSet.of(TaskType.COMPLETION) |
| 194 | + ) |
| 195 | + ) |
| 196 | + ) |
| 197 | + ) |
| 198 | + ); |
| 199 | + return Void.TYPE; |
| 200 | + }).when(mockAuthHandler).getAuthorization(any(), any()); |
| 201 | + |
| 202 | + var mockClient = mock(Client.class); |
| 203 | + when(mockClient.threadPool()).thenReturn(taskQueue.getThreadPool()); |
| 204 | + |
| 205 | + var eisComponents = new ElasticInferenceServiceComponents(""); |
| 206 | + |
| 207 | + var poller = new AuthorizationPoller( |
| 208 | + new AuthorizationPoller.TaskFields(0, "abc", "abc", "abc", new TaskId("abc", 0), Map.of()), |
| 209 | + createWithEmptySettings(taskQueue.getThreadPool()), |
| 210 | + mockAuthHandler, |
| 211 | + mock(Sender.class), |
| 212 | + ElasticInferenceServiceSettingsTests.create(null, TimeValue.timeValueMillis(1), TimeValue.timeValueMillis(1), true), |
| 213 | + eisComponents, |
| 214 | + mockRegistry, |
| 215 | + mockClient, |
| 216 | + null |
| 217 | + ); |
| 218 | + |
| 219 | + var requestArgCaptor = ArgumentCaptor.forClass(StoreInferenceEndpointsAction.Request.class); |
| 220 | + |
| 221 | + poller.sendAuthorizationRequest(); |
| 222 | + verify(mockClient, never()).execute(eq(StoreInferenceEndpointsAction.INSTANCE), requestArgCaptor.capture(), any()); |
| 223 | + } |
| 224 | + |
| 225 | + public void testSendsTwoAuthorizationRequests() { |
| 226 | + fail("TODO"); |
| 227 | + } |
| 228 | +} |
0 commit comments