|
| 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 | + * this file has been contributed to by a Generative AI |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.xpack.inference; |
| 11 | + |
| 12 | +import org.elasticsearch.client.Request; |
| 13 | +import org.elasticsearch.client.Response; |
| 14 | +import org.elasticsearch.client.ResponseException; |
| 15 | +import org.elasticsearch.common.Strings; |
| 16 | +import org.elasticsearch.common.settings.SecureString; |
| 17 | +import org.elasticsearch.common.settings.Settings; |
| 18 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 19 | +import org.elasticsearch.core.TimeValue; |
| 20 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 21 | +import org.elasticsearch.test.cluster.local.distribution.DistributionType; |
| 22 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 23 | +import org.elasticsearch.xcontent.XContentFactory; |
| 24 | +import org.elasticsearch.xcontent.XContentType; |
| 25 | +import org.elasticsearch.xpack.core.inference.action.CCMEnabledActionResponse; |
| 26 | +import org.elasticsearch.xpack.core.inference.action.PutCCMConfigurationAction; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.ClassRule; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | + |
| 32 | +import static org.elasticsearch.xpack.core.inference.action.CCMEnabledActionResponse.ENABLED_FIELD_NAME; |
| 33 | +import static org.elasticsearch.xpack.inference.InferenceBaseRestTest.assertStatusOkOrCreated; |
| 34 | +import static org.elasticsearch.xpack.inference.rest.Paths.INFERENCE_CCM_PATH; |
| 35 | +import static org.hamcrest.Matchers.containsString; |
| 36 | +import static org.hamcrest.Matchers.instanceOf; |
| 37 | + |
| 38 | +public class CCMCrudDisallowedIT extends ESRestTestCase { |
| 39 | + |
| 40 | + static final String PUT_METHOD = "PUT"; |
| 41 | + static final String GET_METHOD = "GET"; |
| 42 | + |
| 43 | + @ClassRule |
| 44 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 45 | + .distribution(DistributionType.DEFAULT) |
| 46 | + .setting("xpack.license.self_generated.type", "basic") |
| 47 | + .setting("xpack.security.enabled", "true") |
| 48 | + .setting("xpack.inference.elastic.allow_configuring_ccm", "false") |
| 49 | + // This plugin is located in the inference/qa/test-service-plugin package, look for TestInferenceServicePlugin |
| 50 | + .plugin("inference-service-test") |
| 51 | + .user("x_pack_rest_user", "x-pack-test-password") |
| 52 | + .build(); |
| 53 | + |
| 54 | + @Override |
| 55 | + protected String getTestRestCluster() { |
| 56 | + return cluster.getHttpAddresses(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected Settings restClientSettings() { |
| 61 | + String token = basicAuthHeaderValue("x_pack_rest_user", new SecureString("x-pack-test-password".toCharArray())); |
| 62 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 63 | + } |
| 64 | + |
| 65 | + // TODO always register the ccm endpoints but throw in the transport action logic saying that they are disabled |
| 66 | + |
| 67 | + @After |
| 68 | + public void cleanup() throws IOException { |
| 69 | + // Disable CCM after each test to ensure a clean state |
| 70 | + putCCMConfiguration(PutCCMConfigurationAction.Request.createDisabled(TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS)); |
| 71 | + } |
| 72 | + |
| 73 | + public void testEnablesCCM_Succeeds() throws IOException { |
| 74 | + var response = putCCMConfiguration( |
| 75 | + PutCCMConfigurationAction.Request.createEnabled( |
| 76 | + new SecureString("key".toCharArray()), |
| 77 | + TimeValue.THIRTY_SECONDS, |
| 78 | + TimeValue.THIRTY_SECONDS |
| 79 | + ) |
| 80 | + ); |
| 81 | + |
| 82 | + assertTrue(response.isEnabled()); |
| 83 | + } |
| 84 | + |
| 85 | + private static CCMEnabledActionResponse putCCMConfiguration(PutCCMConfigurationAction.Request request) throws IOException { |
| 86 | + var response = putRawRequest(INFERENCE_CCM_PATH, request); |
| 87 | + assertStatusOkOrCreated(response); |
| 88 | + var responseAsMap = entityAsMap(response); |
| 89 | + |
| 90 | + var enabled = responseAsMap.get(ENABLED_FIELD_NAME); |
| 91 | + |
| 92 | + assertThat(enabled, instanceOf(Boolean.class)); |
| 93 | + return new CCMEnabledActionResponse((Boolean) enabled); |
| 94 | + } |
| 95 | + |
| 96 | + private static Response putRawRequest(String endpoint, PutCCMConfigurationAction.Request actionRequest) throws IOException { |
| 97 | + var builder = XContentFactory.contentBuilder(XContentType.JSON); |
| 98 | + actionRequest.toXContent(builder, null); |
| 99 | + |
| 100 | + var request = new Request(PUT_METHOD, endpoint); |
| 101 | + request.setJsonEntity(Strings.toString(builder)); |
| 102 | + return client().performRequest(request); |
| 103 | + } |
| 104 | + |
| 105 | + public void testEnableCCM_WithNullApiKey_NullEnabled_ThrowsException() { |
| 106 | + var request = new PutCCMConfigurationAction.Request(null, null, TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS); |
| 107 | + |
| 108 | + var exception = expectThrows(IOException.class, () -> putCCMConfiguration(request)); |
| 109 | + assertThat(exception.getMessage(), containsString("At least one of [api_key] or [enabled] must be provided")); |
| 110 | + } |
| 111 | + |
| 112 | + public void testEnableCCM_WithoutBody_ThrowsException() { |
| 113 | + var request = new Request(PUT_METHOD, INFERENCE_CCM_PATH); |
| 114 | + |
| 115 | + var exception = expectThrows(ResponseException.class, () -> client().performRequest(request)); |
| 116 | + assertThat(exception.getMessage(), containsString("The body must be specified")); |
| 117 | + } |
| 118 | + |
| 119 | + public void testDisableCCM_WithEnabledTrue_ThrowsException() { |
| 120 | + var request = new PutCCMConfigurationAction.Request(null, true, TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS); |
| 121 | + |
| 122 | + var exception = expectThrows(IOException.class, () -> putCCMConfiguration(request)); |
| 123 | + assertThat(exception.getMessage(), containsString("The [enabled] field must be set to [false] when disabling CCM")); |
| 124 | + } |
| 125 | + |
| 126 | + public void testDisableCCM_Succeeds() throws IOException { |
| 127 | + var response = putCCMConfiguration(PutCCMConfigurationAction.Request.createDisabled(TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS)); |
| 128 | + |
| 129 | + assertFalse(response.isEnabled()); |
| 130 | + } |
| 131 | + |
| 132 | + public void testGetCCMConfiguration_WhenCCMDisabled_ReturnsDisabled() throws IOException { |
| 133 | + assertFalse(getCCMConfiguration().isEnabled()); |
| 134 | + } |
| 135 | + |
| 136 | + private CCMEnabledActionResponse getCCMConfiguration() throws IOException { |
| 137 | + var getRequest = new Request(GET_METHOD, INFERENCE_CCM_PATH); |
| 138 | + var getResponse = client().performRequest(getRequest); |
| 139 | + assertStatusOkOrCreated(getResponse); |
| 140 | + var responseAsMap = entityAsMap(getResponse); |
| 141 | + |
| 142 | + var enabled = responseAsMap.get(ENABLED_FIELD_NAME); |
| 143 | + assertThat(enabled, instanceOf(Boolean.class)); |
| 144 | + return new CCMEnabledActionResponse((Boolean) enabled); |
| 145 | + } |
| 146 | + |
| 147 | + public void testEnablesCCM_ThenDisable() throws IOException { |
| 148 | + var response = putCCMConfiguration( |
| 149 | + PutCCMConfigurationAction.Request.createEnabled( |
| 150 | + new SecureString("key".toCharArray()), |
| 151 | + TimeValue.THIRTY_SECONDS, |
| 152 | + TimeValue.THIRTY_SECONDS |
| 153 | + ) |
| 154 | + ); |
| 155 | + |
| 156 | + assertTrue(response.isEnabled()); |
| 157 | + assertTrue(getCCMConfiguration().isEnabled()); |
| 158 | + |
| 159 | + response = putCCMConfiguration(PutCCMConfigurationAction.Request.createDisabled(TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS)); |
| 160 | + |
| 161 | + assertFalse(response.isEnabled()); |
| 162 | + assertFalse(getCCMConfiguration().isEnabled()); |
| 163 | + } |
| 164 | +} |
0 commit comments