- 
                Notifications
    
You must be signed in to change notification settings  - Fork 25.6k
 
[ML] Support revoking inference default endpoint authorization #121326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            jonathan-buttner
  merged 10 commits into
  elastic:main
from
jonathan-buttner:ml-handle-auth-revoke
  
      
      
   
  Feb 6, 2025 
      
    
  
     Merged
                    Changes from 9 commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      25828f5
              
                Starting revoke
              
              
                jonathan-buttner 7b760b8
              
                Adding integration tests
              
              
                jonathan-buttner b9e20b6
              
                More integration tests
              
              
                jonathan-buttner 0a04210
              
                Adding test for deleting default inference endpoint via rest call
              
              
                jonathan-buttner 93fbeeb
              
                Merge branch 'main' of github.com:elastic/elasticsearch into ml-handl…
              
              
                jonathan-buttner a61cf62
              
                Merge branch 'main' into ml-handle-auth-revoke
              
              
                jonathan-buttner 464a370
              
                Merge branch 'main' into ml-handle-auth-revoke
              
              
                jonathan-buttner e9a9e2c
              
                Merge branch 'main' of github.com:elastic/elasticsearch into ml-handl…
              
              
                jonathan-buttner 7e452a3
              
                Removing task type any
              
              
                jonathan-buttner c5f8ef6
              
                Addressing feedback and adding test
              
              
                jonathan-buttner File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            271 changes: 271 additions & 0 deletions
          
          271 
        
  ...java/org/elasticsearch/xpack/inference/integration/InferenceRevokeDefaultEndpointsIT.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
| 
     | 
||
| package org.elasticsearch.xpack.inference.integration; | ||
| 
     | 
||
| import org.elasticsearch.ResourceNotFoundException; | ||
| import org.elasticsearch.action.support.PlainActionFuture; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.inference.InferenceService; | ||
| import org.elasticsearch.inference.MinimalServiceSettings; | ||
| import org.elasticsearch.inference.Model; | ||
| import org.elasticsearch.inference.TaskType; | ||
| import org.elasticsearch.inference.UnparsedModel; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.reindex.ReindexPlugin; | ||
| import org.elasticsearch.test.ESSingleNodeTestCase; | ||
| import org.elasticsearch.test.http.MockResponse; | ||
| import org.elasticsearch.test.http.MockWebServer; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.xpack.inference.external.http.HttpClientManager; | ||
| import org.elasticsearch.xpack.inference.external.http.sender.HttpRequestSenderTests; | ||
| import org.elasticsearch.xpack.inference.logging.ThrottlerManager; | ||
| import org.elasticsearch.xpack.inference.registry.ModelRegistry; | ||
| import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceService; | ||
| import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceServiceComponents; | ||
| import org.elasticsearch.xpack.inference.services.elastic.authorization.ElasticInferenceServiceAuthorizationHandler; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| 
     | 
||
| import java.util.Collection; | ||
| import java.util.EnumSet; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| 
     | 
||
| import static org.elasticsearch.xpack.inference.Utils.inferenceUtilityPool; | ||
| import static org.elasticsearch.xpack.inference.Utils.mockClusterServiceEmpty; | ||
| import static org.elasticsearch.xpack.inference.external.http.Utils.getUrl; | ||
| import static org.elasticsearch.xpack.inference.services.ServiceComponentsTests.createWithEmptySettings; | ||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.mockito.Mockito.mock; | ||
| 
     | 
||
| public class InferenceRevokeDefaultEndpointsIT extends ESSingleNodeTestCase { | ||
| private static final TimeValue TIMEOUT = new TimeValue(30, TimeUnit.SECONDS); | ||
| 
     | 
||
| private ModelRegistry modelRegistry; | ||
| private final MockWebServer webServer = new MockWebServer(); | ||
| private ThreadPool threadPool; | ||
| private String gatewayUrl; | ||
| 
     | 
||
| @Before | ||
| public void createComponents() throws Exception { | ||
| threadPool = createThreadPool(inferenceUtilityPool()); | ||
| webServer.start(); | ||
| gatewayUrl = getUrl(webServer); | ||
| modelRegistry = new ModelRegistry(client()); | ||
| } | ||
| 
     | 
||
| @After | ||
| public void shutdown() { | ||
| terminate(threadPool); | ||
| webServer.close(); | ||
| } | ||
| 
     | 
||
| @Override | ||
| protected boolean resetNodeAfterTest() { | ||
| return true; | ||
| } | ||
| 
     | 
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> getPlugins() { | ||
| return pluginList(ReindexPlugin.class); | ||
| } | ||
| 
     | 
||
| public void testDefaultConfigs_Returns_DefaultChatCompletion_V1_WhenTaskTypeIsCorrect() throws Exception { | ||
| String responseJson = """ | ||
| { | ||
| "models": [ | ||
| { | ||
| "model_name": "rainbow-sprinkles", | ||
| "task_types": ["chat"] | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
| 
     | 
||
| webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson)); | ||
| 
     | 
||
| try (var service = createElasticInferenceService()) { | ||
| service.waitForAuthorizationToComplete(TIMEOUT); | ||
| assertThat(service.supportedStreamingTasks(), is(EnumSet.of(TaskType.CHAT_COMPLETION))); | ||
| assertThat( | ||
| service.defaultConfigIds(), | ||
| is( | ||
| List.of( | ||
| new InferenceService.DefaultConfigId(".rainbow-sprinkles-elastic", MinimalServiceSettings.chatCompletion(), service) | ||
| ) | ||
| ) | ||
| ); | ||
| assertThat(service.supportedTaskTypes(), is(EnumSet.of(TaskType.CHAT_COMPLETION))); | ||
| 
     | 
||
| PlainActionFuture<List<Model>> listener = new PlainActionFuture<>(); | ||
| service.defaultConfigs(listener); | ||
| assertThat(listener.actionGet(TIMEOUT).get(0).getConfigurations().getInferenceEntityId(), is(".rainbow-sprinkles-elastic")); | ||
| } | ||
| } | ||
| 
     | 
||
| public void testRemoves_DefaultChatCompletion_V1_WhenAuthorizationReturnsEmpty() throws Exception { | ||
| { | ||
| String responseJson = """ | ||
| { | ||
| "models": [ | ||
| { | ||
| "model_name": "rainbow-sprinkles", | ||
| "task_types": ["chat"] | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
| 
     | 
||
| webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson)); | ||
| 
     | 
||
| try (var service = createElasticInferenceService()) { | ||
| service.waitForAuthorizationToComplete(TIMEOUT); | ||
| assertThat(service.supportedStreamingTasks(), is(EnumSet.of(TaskType.CHAT_COMPLETION))); | ||
| assertThat( | ||
| service.defaultConfigIds(), | ||
| is( | ||
| List.of( | ||
| new InferenceService.DefaultConfigId( | ||
| ".rainbow-sprinkles-elastic", | ||
| MinimalServiceSettings.chatCompletion(), | ||
| service | ||
| ) | ||
| ) | ||
| ) | ||
| ); | ||
| assertThat(service.supportedTaskTypes(), is(EnumSet.of(TaskType.CHAT_COMPLETION))); | ||
| 
     | 
||
| PlainActionFuture<List<Model>> listener = new PlainActionFuture<>(); | ||
| service.defaultConfigs(listener); | ||
| assertThat(listener.actionGet(TIMEOUT).get(0).getConfigurations().getInferenceEntityId(), is(".rainbow-sprinkles-elastic")); | ||
| 
     | 
||
| var getModelListener = new PlainActionFuture<UnparsedModel>(); | ||
| // persists the default endpoints | ||
| modelRegistry.getModel(".rainbow-sprinkles-elastic", getModelListener); | ||
| 
     | 
||
| var inferenceEntity = getModelListener.actionGet(TIMEOUT); | ||
| assertThat(inferenceEntity.inferenceEntityId(), is(".rainbow-sprinkles-elastic")); | ||
| assertThat(inferenceEntity.taskType(), is(TaskType.CHAT_COMPLETION)); | ||
| } | ||
| } | ||
| { | ||
| String noAuthorizationResponseJson = """ | ||
| { | ||
| "models": [] | ||
| } | ||
| """; | ||
| 
     | 
||
| webServer.enqueue(new MockResponse().setResponseCode(200).setBody(noAuthorizationResponseJson)); | ||
| 
     | 
||
| try (var service = createElasticInferenceService()) { | ||
| service.waitForAuthorizationToComplete(TIMEOUT); | ||
| assertThat(service.supportedStreamingTasks(), is(EnumSet.noneOf(TaskType.class))); | ||
| assertTrue(service.defaultConfigIds().isEmpty()); | ||
| assertThat(service.supportedTaskTypes(), is(EnumSet.noneOf(TaskType.class))); | ||
| 
     | 
||
| var getModelListener = new PlainActionFuture<UnparsedModel>(); | ||
| modelRegistry.getModel(".rainbow-sprinkles-elastic", getModelListener); | ||
| 
     | 
||
| var exception = expectThrows(ResourceNotFoundException.class, () -> getModelListener.actionGet(TIMEOUT)); | ||
| assertThat(exception.getMessage(), is("Inference endpoint not found [.rainbow-sprinkles-elastic]")); | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| public void testRemoves_DefaultChatCompletion_V1_WhenAuthorizationDoesNotReturnAuthForIt() throws Exception { | ||
| { | ||
| String responseJson = """ | ||
| { | ||
| "models": [ | ||
| { | ||
| "model_name": "rainbow-sprinkles", | ||
| "task_types": ["chat"] | ||
| }, | ||
| { | ||
| "model_name": "elser-v2", | ||
| "task_types": ["embed/text/sparse"] | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
| 
     | 
||
| webServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseJson)); | ||
| 
     | 
||
| try (var service = createElasticInferenceService()) { | ||
| service.waitForAuthorizationToComplete(TIMEOUT); | ||
| assertThat(service.supportedStreamingTasks(), is(EnumSet.of(TaskType.CHAT_COMPLETION))); | ||
| assertThat( | ||
| service.defaultConfigIds(), | ||
| is( | ||
| List.of( | ||
| new InferenceService.DefaultConfigId( | ||
| ".rainbow-sprinkles-elastic", | ||
| MinimalServiceSettings.chatCompletion(), | ||
| service | ||
| ) | ||
| ) | ||
| ) | ||
| ); | ||
| assertThat(service.supportedTaskTypes(), is(EnumSet.of(TaskType.CHAT_COMPLETION, TaskType.SPARSE_EMBEDDING))); | ||
| 
     | 
||
| PlainActionFuture<List<Model>> listener = new PlainActionFuture<>(); | ||
| service.defaultConfigs(listener); | ||
| assertThat(listener.actionGet(TIMEOUT).get(0).getConfigurations().getInferenceEntityId(), is(".rainbow-sprinkles-elastic")); | ||
| 
     | 
||
| var getModelListener = new PlainActionFuture<UnparsedModel>(); | ||
| // persists the default endpoints | ||
| modelRegistry.getModel(".rainbow-sprinkles-elastic", getModelListener); | ||
| 
     | 
||
| var inferenceEntity = getModelListener.actionGet(TIMEOUT); | ||
| assertThat(inferenceEntity.inferenceEntityId(), is(".rainbow-sprinkles-elastic")); | ||
| assertThat(inferenceEntity.taskType(), is(TaskType.CHAT_COMPLETION)); | ||
| } | ||
| } | ||
| { | ||
| String noAuthorizationResponseJson = """ | ||
| { | ||
| "models": [ | ||
| { | ||
| "model_name": "elser-v2", | ||
| "task_types": ["embed/text/sparse"] | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
| 
     | 
||
| webServer.enqueue(new MockResponse().setResponseCode(200).setBody(noAuthorizationResponseJson)); | ||
| 
     | 
||
| try (var service = createElasticInferenceService()) { | ||
| service.waitForAuthorizationToComplete(TIMEOUT); | ||
| assertThat(service.supportedStreamingTasks(), is(EnumSet.noneOf(TaskType.class))); | ||
| assertTrue(service.defaultConfigIds().isEmpty()); | ||
| assertThat(service.supportedTaskTypes(), is(EnumSet.of(TaskType.SPARSE_EMBEDDING))); | ||
| 
     | 
||
| var getModelListener = new PlainActionFuture<UnparsedModel>(); | ||
| modelRegistry.getModel(".rainbow-sprinkles-elastic", getModelListener); | ||
| 
     | 
||
| var exception = expectThrows(ResourceNotFoundException.class, () -> getModelListener.actionGet(TIMEOUT)); | ||
| assertThat(exception.getMessage(), is("Inference endpoint not found [.rainbow-sprinkles-elastic]")); | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| private ElasticInferenceService createElasticInferenceService() { | ||
| var httpManager = HttpClientManager.create(Settings.EMPTY, threadPool, mockClusterServiceEmpty(), mock(ThrottlerManager.class)); | ||
| var senderFactory = HttpRequestSenderTests.createSenderFactory(threadPool, httpManager); | ||
| 
     | 
||
| return new ElasticInferenceService( | ||
| senderFactory, | ||
| createWithEmptySettings(threadPool), | ||
| new ElasticInferenceServiceComponents(gatewayUrl), | ||
| modelRegistry, | ||
| new ElasticInferenceServiceAuthorizationHandler(gatewayUrl, threadPool) | ||
| ); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -88,6 +88,17 @@ private void doExecuteForked( | |
| ClusterState state, | ||
| ActionListener<DeleteInferenceEndpointAction.Response> masterListener | ||
| ) { | ||
| if (modelRegistry.containsDefaultConfigId(request.getInferenceEndpointId())) { | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change will prevent REST calls from deleting default inference endpoints.  | 
||
| masterListener.onFailure( | ||
| new ElasticsearchStatusException( | ||
| "[{}] is a reserved inference endpoint. Cannot delete a reserved inference endpoint.", | ||
| RestStatus.BAD_REQUEST, | ||
| request.getInferenceEndpointId() | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
| 
     | 
||
| SubscribableListener.<UnparsedModel>newForked(modelConfigListener -> { | ||
| // Get the model from the registry | ||
| 
     | 
||
| 
          
            
          
           | 
    ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's another API that does the implicit typecasting magic, if you prefer: