|
| 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.logsdb; |
| 9 | + |
| 10 | +import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; |
| 11 | +import org.elasticsearch.common.settings.Settings; |
| 12 | +import org.elasticsearch.index.mapper.SourceFieldMapper; |
| 13 | +import org.elasticsearch.license.AbstractLicensesIntegrationTestCase; |
| 14 | +import org.elasticsearch.license.GetFeatureUsageRequest; |
| 15 | +import org.elasticsearch.license.GetFeatureUsageResponse; |
| 16 | +import org.elasticsearch.license.License; |
| 17 | +import org.elasticsearch.license.LicenseService; |
| 18 | +import org.elasticsearch.license.LicensedFeature; |
| 19 | +import org.elasticsearch.license.TransportGetFeatureUsageAction; |
| 20 | +import org.elasticsearch.license.XPackLicenseState; |
| 21 | +import org.elasticsearch.plugins.Plugin; |
| 22 | +import org.elasticsearch.test.ESIntegTestCase; |
| 23 | +import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; |
| 24 | +import org.hamcrest.Matcher; |
| 25 | +import org.junit.Before; |
| 26 | + |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.time.LocalDateTime; |
| 29 | +import java.time.ZoneOffset; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST; |
| 34 | +import static org.elasticsearch.xpack.logsdb.SyntheticSourceLicenseServiceTests.createEnterpriseLicense; |
| 35 | +import static org.elasticsearch.xpack.logsdb.SyntheticSourceLicenseServiceTests.createGoldOrPlatinumLicense; |
| 36 | +import static org.hamcrest.Matchers.equalTo; |
| 37 | +import static org.hamcrest.Matchers.not; |
| 38 | +import static org.hamcrest.Matchers.nullValue; |
| 39 | + |
| 40 | +@ESIntegTestCase.ClusterScope(scope = TEST, numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) |
| 41 | +public class LegacyLicenceIntegrationTests extends AbstractLicensesIntegrationTestCase { |
| 42 | + |
| 43 | + @Override |
| 44 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 45 | + return List.of(P.class); |
| 46 | + } |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setup() throws Exception { |
| 50 | + wipeAllLicenses(); |
| 51 | + ensureGreen(); |
| 52 | + License license = createGoldOrPlatinumLicense(); |
| 53 | + putLicense(license); |
| 54 | + ensureGreen(); |
| 55 | + } |
| 56 | + |
| 57 | + public void testSyntheticSourceUsageDisallowed() { |
| 58 | + createIndexWithSyntheticSourceAndAssertExpectedType("test", "STORED"); |
| 59 | + |
| 60 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY, nullValue()); |
| 61 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE, nullValue()); |
| 62 | + } |
| 63 | + |
| 64 | + public void testSyntheticSourceUsageWithLegacyLicense() { |
| 65 | + createIndexWithSyntheticSourceAndAssertExpectedType(".profiling-stacktraces", "synthetic"); |
| 66 | + |
| 67 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY, not(nullValue())); |
| 68 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE, nullValue()); |
| 69 | + } |
| 70 | + |
| 71 | + public void testSyntheticSourceUsageWithLegacyLicensePastCutoff() throws Exception { |
| 72 | + long startPastCutoff = LocalDateTime.of(2025, 11, 12, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli(); |
| 73 | + putLicense(createGoldOrPlatinumLicense(startPastCutoff)); |
| 74 | + ensureGreen(); |
| 75 | + |
| 76 | + createIndexWithSyntheticSourceAndAssertExpectedType(".profiling-stacktraces", "STORED"); |
| 77 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY, nullValue()); |
| 78 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE, nullValue()); |
| 79 | + } |
| 80 | + |
| 81 | + public void testSyntheticSourceUsageWithEnterpriseLicensePastCutoff() throws Exception { |
| 82 | + long startPastCutoff = LocalDateTime.of(2025, 11, 12, 0, 0).toInstant(ZoneOffset.UTC).toEpochMilli(); |
| 83 | + putLicense(createEnterpriseLicense(startPastCutoff)); |
| 84 | + ensureGreen(); |
| 85 | + |
| 86 | + createIndexWithSyntheticSourceAndAssertExpectedType(".profiling-traces", "synthetic"); |
| 87 | + // also supports non-exceptional indices |
| 88 | + createIndexWithSyntheticSourceAndAssertExpectedType("test", "synthetic"); |
| 89 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY, nullValue()); |
| 90 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE, not(nullValue())); |
| 91 | + } |
| 92 | + |
| 93 | + public void testSyntheticSourceUsageTracksBothLegacyAndRegularFeature() throws Exception { |
| 94 | + createIndexWithSyntheticSourceAndAssertExpectedType(".profiling-traces", "synthetic"); |
| 95 | + |
| 96 | + putLicense(createEnterpriseLicense()); |
| 97 | + ensureGreen(); |
| 98 | + |
| 99 | + createIndexWithSyntheticSourceAndAssertExpectedType(".profiling-traces-v2", "synthetic"); |
| 100 | + |
| 101 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE_LEGACY, not(nullValue())); |
| 102 | + assertFeatureUsage(SyntheticSourceLicenseService.SYNTHETIC_SOURCE_FEATURE, not(nullValue())); |
| 103 | + } |
| 104 | + |
| 105 | + private void createIndexWithSyntheticSourceAndAssertExpectedType(String indexName, String expectedType) { |
| 106 | + var settings = Settings.builder().put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "synthetic").build(); |
| 107 | + createIndex(indexName, settings); |
| 108 | + var response = admin().indices().getSettings(new GetSettingsRequest().indices(indexName)).actionGet(); |
| 109 | + assertThat( |
| 110 | + response.getIndexToSettings().get(indexName).get(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey()), |
| 111 | + equalTo(expectedType) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + private List<GetFeatureUsageResponse.FeatureUsageInfo> getFeatureUsageInfo() { |
| 116 | + return client().execute(TransportGetFeatureUsageAction.TYPE, new GetFeatureUsageRequest()).actionGet().getFeatures(); |
| 117 | + } |
| 118 | + |
| 119 | + private void assertFeatureUsage(LicensedFeature.Momentary syntheticSourceFeature, Matcher<Object> matcher) { |
| 120 | + GetFeatureUsageResponse.FeatureUsageInfo featureUsage = getFeatureUsageInfo().stream() |
| 121 | + .filter(f -> f.getFamily().equals(SyntheticSourceLicenseService.MAPPINGS_FEATURE_FAMILY)) |
| 122 | + .filter(f -> f.getName().equals(syntheticSourceFeature.getName())) |
| 123 | + .findAny() |
| 124 | + .orElse(null); |
| 125 | + assertThat(featureUsage, matcher); |
| 126 | + } |
| 127 | + |
| 128 | + public static class P extends LocalStateCompositeXPackPlugin { |
| 129 | + |
| 130 | + public P(final Settings settings, final Path configPath) { |
| 131 | + super(settings, configPath); |
| 132 | + plugins.add(new LogsDBPlugin(settings) { |
| 133 | + @Override |
| 134 | + protected XPackLicenseState getLicenseState() { |
| 135 | + return P.this.getLicenseState(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected LicenseService getLicenseService() { |
| 140 | + return P.this.getLicenseService(); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | +} |
0 commit comments