Search in sources :

Example 1 with Regex

use of org.opensearch.common.regex.Regex in project OpenSearch by opensearch-project.

the class MetadataIndexTemplateService method validateV2TemplateRequest.

public static void validateV2TemplateRequest(Metadata metadata, String name, ComposableIndexTemplate template) {
    if (template.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)) {
        Settings mergedSettings = resolveSettings(metadata, template);
        if (IndexMetadata.INDEX_HIDDEN_SETTING.exists(mergedSettings)) {
            throw new InvalidIndexTemplateException(name, "global composable templates may not specify the setting " + IndexMetadata.INDEX_HIDDEN_SETTING.getKey());
        }
    }
    final Map<String, ComponentTemplate> componentTemplates = metadata.componentTemplates();
    final List<String> missingComponentTemplates = template.composedOf().stream().filter(componentTemplate -> componentTemplates.containsKey(componentTemplate) == false).collect(Collectors.toList());
    if (missingComponentTemplates.size() > 0) {
        throw new InvalidIndexTemplateException(name, "index template [" + name + "] specifies component templates " + missingComponentTemplates + " that do not exist");
    }
}
Also used : NO_LONGER_ASSIGNED(org.opensearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Version(org.opensearch.Version) Regex(org.opensearch.common.regex.Regex) Strings(org.opensearch.common.Strings) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) Operations(org.apache.lucene.util.automaton.Operations) MapperService(org.opensearch.index.mapper.MapperService) Alias(org.opensearch.action.admin.indices.alias.Alias) Locale(java.util.Locale) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) HeaderWarning(org.opensearch.common.logging.HeaderWarning) TimeValue(org.opensearch.common.unit.TimeValue) Automaton(org.apache.lucene.util.automaton.Automaton) Index(org.opensearch.index.Index) Predicate(java.util.function.Predicate) MetadataCreateDataStreamService.validateTimestampFieldMapping(org.opensearch.cluster.metadata.MetadataCreateDataStreamService.validateTimestampFieldMapping) IndicesService(org.opensearch.indices.IndicesService) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) ValidationException(org.opensearch.common.ValidationException) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) Optional(java.util.Optional) XContentType(org.opensearch.common.xcontent.XContentType) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) BytesReference(org.opensearch.common.bytes.BytesReference) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Priority(org.opensearch.common.Priority) HashMap(java.util.HashMap) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) UUIDs(org.opensearch.common.UUIDs) LinkedList(java.util.LinkedList) IOException(java.io.IOException) IndexService(org.opensearch.index.IndexService) CollectionUtil(org.apache.lucene.util.CollectionUtil) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Sets(org.opensearch.common.util.set.Sets) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Regex(org.opensearch.common.regex.Regex) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings)

Example 2 with Regex

use of org.opensearch.common.regex.Regex in project OpenSearch by opensearch-project.

the class MetadataIndexTemplateService method validate.

private void validate(String name, @Nullable Settings settings, List<String> indexPatterns, List<Alias> aliases) {
    List<String> validationErrors = new ArrayList<>();
    if (name.contains(" ")) {
        validationErrors.add("name must not contain a space");
    }
    if (name.contains(",")) {
        validationErrors.add("name must not contain a ','");
    }
    if (name.contains("#")) {
        validationErrors.add("name must not contain a '#'");
    }
    if (name.contains("*")) {
        validationErrors.add("name must not contain a '*'");
    }
    if (name.startsWith("_")) {
        validationErrors.add("name must not start with '_'");
    }
    if (name.toLowerCase(Locale.ROOT).equals(name) == false) {
        validationErrors.add("name must be lower cased");
    }
    for (String indexPattern : indexPatterns) {
        if (indexPattern.contains(" ")) {
            validationErrors.add("index_patterns [" + indexPattern + "] must not contain a space");
        }
        if (indexPattern.contains(",")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain a ','");
        }
        if (indexPattern.contains("#")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain a '#'");
        }
        if (indexPattern.contains(":")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain a ':'");
        }
        if (indexPattern.startsWith("_")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not start with '_'");
        }
        if (Strings.validFileNameExcludingAstrix(indexPattern) == false) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain the following characters " + Strings.INVALID_FILENAME_CHARS);
        }
    }
    if (settings != null) {
        try {
            // templates must be consistent with regards to dependencies
            indexScopedSettings.validate(settings, true);
        } catch (IllegalArgumentException iae) {
            validationErrors.add(iae.getMessage());
            for (Throwable t : iae.getSuppressed()) {
                validationErrors.add(t.getMessage());
            }
        }
        List<String> indexSettingsValidation = metadataCreateIndexService.getIndexSettingsValidationErrors(settings, true);
        validationErrors.addAll(indexSettingsValidation);
    }
    if (indexPatterns.stream().anyMatch(Regex::isMatchAllPattern)) {
        if (settings != null && IndexMetadata.INDEX_HIDDEN_SETTING.exists(settings)) {
            validationErrors.add("global templates may not specify the setting " + IndexMetadata.INDEX_HIDDEN_SETTING.getKey());
        }
    }
    if (validationErrors.size() > 0) {
        ValidationException validationException = new ValidationException();
        validationException.addValidationErrors(validationErrors);
        throw new InvalidIndexTemplateException(name, validationException.getMessage());
    }
    for (Alias alias : aliases) {
        // we validate the alias only partially, as we don't know yet to which index it'll get applied to
        aliasValidator.validateAliasStandalone(alias);
        if (indexPatterns.contains(alias.name())) {
            throw new IllegalArgumentException("alias [" + alias.name() + "] cannot be the same as any pattern in [" + String.join(", ", indexPatterns) + "]");
        }
    }
}
Also used : ValidationException(org.opensearch.common.ValidationException) Regex(org.opensearch.common.regex.Regex) Alias(org.opensearch.action.admin.indices.alias.Alias) ArrayList(java.util.ArrayList) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException)

Example 3 with Regex

use of org.opensearch.common.regex.Regex in project OpenSearch by opensearch-project.

the class MetadataIndexTemplateService method addComponentTemplate.

// Package visible for testing
ClusterState addComponentTemplate(final ClusterState currentState, final boolean create, final String name, final ComponentTemplate template) throws Exception {
    final ComponentTemplate existing = currentState.metadata().componentTemplates().get(name);
    if (create && existing != null) {
        throw new IllegalArgumentException("component template [" + name + "] already exists");
    }
    CompressedXContent mappings = template.template().mappings();
    String stringMappings = mappings == null ? null : mappings.string();
    // We may need to normalize index settings, so do that also
    Settings finalSettings = template.template().settings();
    if (finalSettings != null) {
        finalSettings = Settings.builder().put(finalSettings).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
    }
    // Collect all the composable (index) templates that use this component template, we'll use
    // this for validating that they're still going to be valid after this component template
    // has been updated
    final Map<String, ComposableIndexTemplate> templatesUsingComponent = currentState.metadata().templatesV2().entrySet().stream().filter(e -> e.getValue().composedOf().contains(name)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    // if we're updating a component template, let's check if it's part of any V2 template that will yield the CT update invalid
    if (create == false && finalSettings != null) {
        // if the CT is specifying the `index.hidden` setting it cannot be part of any global template
        if (IndexMetadata.INDEX_HIDDEN_SETTING.exists(finalSettings)) {
            List<String> globalTemplatesThatUseThisComponent = new ArrayList<>();
            for (Map.Entry<String, ComposableIndexTemplate> entry : templatesUsingComponent.entrySet()) {
                ComposableIndexTemplate templateV2 = entry.getValue();
                if (templateV2.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)) {
                    // global templates don't support configuring the `index.hidden` setting so we don't need to resolve the settings as
                    // no other component template can remove this setting from the resolved settings, so just invalidate this update
                    globalTemplatesThatUseThisComponent.add(entry.getKey());
                }
            }
            if (globalTemplatesThatUseThisComponent.isEmpty() == false) {
                throw new IllegalArgumentException("cannot update component template [" + name + "] because the following global templates would resolve to specifying the [" + IndexMetadata.SETTING_INDEX_HIDDEN + "] setting: [" + String.join(",", globalTemplatesThatUseThisComponent) + "]");
            }
        }
    }
    // Mappings in component templates don't include _doc, so update the mappings to include this single type
    if (stringMappings != null) {
        Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, stringMappings);
        if (parsedMappings.size() > 0) {
            stringMappings = Strings.toString(XContentFactory.jsonBuilder().startObject().field(MapperService.SINGLE_MAPPING_NAME, parsedMappings).endObject());
        }
    }
    final Template finalTemplate = new Template(finalSettings, stringMappings == null ? null : new CompressedXContent(stringMappings), template.template().aliases());
    final ComponentTemplate finalComponentTemplate = new ComponentTemplate(finalTemplate, template.version(), template.metadata());
    if (finalComponentTemplate.equals(existing)) {
        return currentState;
    }
    validateTemplate(finalSettings, stringMappings, indicesService, xContentRegistry);
    validate(name, finalComponentTemplate);
    // Validate all composable index templates that use this component template
    if (templatesUsingComponent.size() > 0) {
        ClusterState tempStateWithComponentTemplateAdded = ClusterState.builder(currentState).metadata(Metadata.builder(currentState.metadata()).put(name, finalComponentTemplate)).build();
        Exception validationFailure = null;
        for (Map.Entry<String, ComposableIndexTemplate> entry : templatesUsingComponent.entrySet()) {
            final String composableTemplateName = entry.getKey();
            final ComposableIndexTemplate composableTemplate = entry.getValue();
            try {
                validateCompositeTemplate(tempStateWithComponentTemplateAdded, composableTemplateName, composableTemplate, indicesService, xContentRegistry);
            } catch (Exception e) {
                if (validationFailure == null) {
                    validationFailure = new IllegalArgumentException("updating component template [" + name + "] results in invalid composable template [" + composableTemplateName + "] after templates are merged", e);
                } else {
                    validationFailure.addSuppressed(e);
                }
            }
        }
        if (validationFailure != null) {
            throw validationFailure;
        }
    }
    logger.info("{} component template [{}]", existing == null ? "adding" : "updating", name);
    return ClusterState.builder(currentState).metadata(Metadata.builder(currentState.metadata()).put(name, finalComponentTemplate)).build();
}
Also used : NO_LONGER_ASSIGNED(org.opensearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Version(org.opensearch.Version) Regex(org.opensearch.common.regex.Regex) Strings(org.opensearch.common.Strings) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) Operations(org.apache.lucene.util.automaton.Operations) MapperService(org.opensearch.index.mapper.MapperService) Alias(org.opensearch.action.admin.indices.alias.Alias) Locale(java.util.Locale) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) HeaderWarning(org.opensearch.common.logging.HeaderWarning) TimeValue(org.opensearch.common.unit.TimeValue) Automaton(org.apache.lucene.util.automaton.Automaton) Index(org.opensearch.index.Index) Predicate(java.util.function.Predicate) MetadataCreateDataStreamService.validateTimestampFieldMapping(org.opensearch.cluster.metadata.MetadataCreateDataStreamService.validateTimestampFieldMapping) IndicesService(org.opensearch.indices.IndicesService) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) ValidationException(org.opensearch.common.ValidationException) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) Optional(java.util.Optional) XContentType(org.opensearch.common.xcontent.XContentType) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) BytesReference(org.opensearch.common.bytes.BytesReference) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Priority(org.opensearch.common.Priority) HashMap(java.util.HashMap) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) UUIDs(org.opensearch.common.UUIDs) LinkedList(java.util.LinkedList) IOException(java.io.IOException) IndexService(org.opensearch.index.IndexService) CollectionUtil(org.apache.lucene.util.CollectionUtil) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Sets(org.opensearch.common.util.set.Sets) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterState(org.opensearch.cluster.ClusterState) ArrayList(java.util.ArrayList) ValidationException(org.opensearch.common.ValidationException) UncheckedIOException(java.io.UncheckedIOException) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) IOException(java.io.IOException) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) Regex(org.opensearch.common.regex.Regex) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Map(java.util.Map) HashMap(java.util.HashMap) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings)

Aggregations

ArrayList (java.util.ArrayList)3 Alias (org.opensearch.action.admin.indices.alias.Alias)3 ValidationException (org.opensearch.common.ValidationException)3 Regex (org.opensearch.common.regex.Regex)3 InvalidIndexTemplateException (org.opensearch.indices.InvalidIndexTemplateException)3 ObjectCursor (com.carrotsearch.hppc.cursors.ObjectCursor)2 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2