|
| 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.action.filter; |
| 9 | + |
| 10 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 11 | + |
| 12 | +import org.elasticsearch.ElasticsearchSecurityException; |
| 13 | +import org.elasticsearch.action.bulk.BulkItemResponse; |
| 14 | +import org.elasticsearch.action.bulk.BulkRequestBuilder; |
| 15 | +import org.elasticsearch.action.bulk.BulkResponse; |
| 16 | +import org.elasticsearch.action.index.IndexRequestBuilder; |
| 17 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 18 | +import org.elasticsearch.common.settings.Settings; |
| 19 | +import org.elasticsearch.index.IndexSettings; |
| 20 | +import org.elasticsearch.index.mapper.InferenceMetadataFieldsMapper; |
| 21 | +import org.elasticsearch.index.mapper.SourceFieldMapper; |
| 22 | +import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper; |
| 23 | +import org.elasticsearch.inference.SimilarityMeasure; |
| 24 | +import org.elasticsearch.license.LicenseSettings; |
| 25 | +import org.elasticsearch.plugins.Plugin; |
| 26 | +import org.elasticsearch.test.ESIntegTestCase; |
| 27 | +import org.elasticsearch.xpack.inference.LocalStateInferencePlugin; |
| 28 | +import org.elasticsearch.xpack.inference.Utils; |
| 29 | +import org.elasticsearch.xpack.inference.mock.TestDenseInferenceServiceExtension; |
| 30 | +import org.elasticsearch.xpack.inference.mock.TestSparseInferenceServiceExtension; |
| 31 | +import org.junit.Before; |
| 32 | + |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Locale; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticTextInput; |
| 41 | +import static org.hamcrest.Matchers.instanceOf; |
| 42 | + |
| 43 | +public class ShardBulkInferenceActionFilterBasicLicenseIT extends ESIntegTestCase { |
| 44 | + public static final String INDEX_NAME = "test-index"; |
| 45 | + |
| 46 | + private final boolean useLegacyFormat; |
| 47 | + private final boolean useSyntheticSource; |
| 48 | + |
| 49 | + public ShardBulkInferenceActionFilterBasicLicenseIT(boolean useLegacyFormat, boolean useSyntheticSource) { |
| 50 | + this.useLegacyFormat = useLegacyFormat; |
| 51 | + this.useSyntheticSource = useSyntheticSource; |
| 52 | + } |
| 53 | + |
| 54 | + @ParametersFactory |
| 55 | + public static Iterable<Object[]> parameters() throws Exception { |
| 56 | + return List.of( |
| 57 | + new Object[] { true, false }, |
| 58 | + new Object[] { true, true }, |
| 59 | + new Object[] { false, false }, |
| 60 | + new Object[] { false, true } |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @Before |
| 65 | + public void setup() throws Exception { |
| 66 | + Utils.storeSparseModel(client()); |
| 67 | + Utils.storeDenseModel( |
| 68 | + client(), |
| 69 | + randomIntBetween(1, 100), |
| 70 | + // dot product means that we need normalized vectors; it's not worth doing that in this test |
| 71 | + randomValueOtherThan(SimilarityMeasure.DOT_PRODUCT, () -> randomFrom(SimilarityMeasure.values())), |
| 72 | + // TODO: Allow element type BIT once TestDenseInferenceServiceExtension supports it |
| 73 | + randomValueOtherThan(DenseVectorFieldMapper.ElementType.BIT, () -> randomFrom(DenseVectorFieldMapper.ElementType.values())) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 79 | + return Settings.builder().put(LicenseSettings.SELF_GENERATED_LICENSE_TYPE.getKey(), "basic").build(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 84 | + return Arrays.asList(LocalStateInferencePlugin.class); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Settings indexSettings() { |
| 89 | + var builder = Settings.builder() |
| 90 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) |
| 91 | + .put(InferenceMetadataFieldsMapper.USE_LEGACY_SEMANTIC_TEXT_FORMAT.getKey(), useLegacyFormat); |
| 92 | + if (useSyntheticSource) { |
| 93 | + builder.put(IndexSettings.RECOVERY_USE_SYNTHETIC_SOURCE_SETTING.getKey(), true); |
| 94 | + builder.put(IndexSettings.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), SourceFieldMapper.Mode.SYNTHETIC.name()); |
| 95 | + } |
| 96 | + return builder.build(); |
| 97 | + } |
| 98 | + |
| 99 | + public void testLicenseInvalidForInference() { |
| 100 | + prepareCreate(INDEX_NAME).setMapping( |
| 101 | + String.format( |
| 102 | + Locale.ROOT, |
| 103 | + """ |
| 104 | + { |
| 105 | + "properties": { |
| 106 | + "sparse_field": { |
| 107 | + "type": "semantic_text", |
| 108 | + "inference_id": "%s" |
| 109 | + }, |
| 110 | + "dense_field": { |
| 111 | + "type": "semantic_text", |
| 112 | + "inference_id": "%s" |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + """, |
| 117 | + TestSparseInferenceServiceExtension.TestInferenceService.NAME, |
| 118 | + TestDenseInferenceServiceExtension.TestInferenceService.NAME |
| 119 | + ) |
| 120 | + ).get(); |
| 121 | + |
| 122 | + BulkRequestBuilder bulkRequest = client().prepareBulk(); |
| 123 | + int totalBulkReqs = randomIntBetween(2, 100); |
| 124 | + for (int i = 0; i < totalBulkReqs; i++) { |
| 125 | + Map<String, Object> source = new HashMap<>(); |
| 126 | + source.put("sparse_field", rarely() ? null : randomSemanticTextInput()); |
| 127 | + source.put("dense_field", rarely() ? null : randomSemanticTextInput()); |
| 128 | + |
| 129 | + bulkRequest.add(new IndexRequestBuilder(client()).setIndex(INDEX_NAME).setId(Long.toString(i)).setSource(source)); |
| 130 | + } |
| 131 | + |
| 132 | + BulkResponse bulkResponse = bulkRequest.get(); |
| 133 | + for (BulkItemResponse itemResponse : bulkResponse) { |
| 134 | + assertTrue(itemResponse.isFailed()); |
| 135 | + assertThat(itemResponse.getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class)); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments