|
5 | 5 |
|
6 | 6 | package org.opensearch.ml.common.utils;
|
7 | 7 |
|
| 8 | +import static org.opensearch.ml.common.utils.StringUtils.validateSchema; |
| 9 | + |
8 | 10 | import java.io.IOException;
|
9 | 11 | import java.net.URL;
|
| 12 | +import java.util.HashMap; |
10 | 13 | import java.util.Map;
|
11 | 14 |
|
12 | 15 | import com.google.common.base.Charsets;
|
@@ -40,20 +43,99 @@ public class IndexUtils {
|
40 | 43 | public static final Map<String, Object> UPDATED_DEFAULT_INDEX_SETTINGS = Map.of("index.auto_expand_replicas", "0-1");
|
41 | 44 | public static final Map<String, Object> UPDATED_ALL_NODES_REPLICA_INDEX_SETTINGS = Map.of("index.auto_expand_replicas", "0-all");
|
42 | 45 |
|
| 46 | + // Schema that validates system index mappings |
| 47 | + public static final String MAPPING_SCHEMA_PATH = "index-mappings/schema.json"; |
| 48 | + |
| 49 | + // Placeholders to use within the json mapping files |
| 50 | + private static final String USER_PLACEHOLDER = "USER_MAPPING_PLACEHOLDER"; |
| 51 | + private static final String CONNECTOR_PLACEHOLDER = "CONNECTOR_MAPPING_PLACEHOLDER"; |
| 52 | + public static final Map<String, String> MAPPING_PLACEHOLDERS = Map |
| 53 | + .of(USER_PLACEHOLDER, "index-mappings/placeholders/user.json", CONNECTOR_PLACEHOLDER, "index-mappings/placeholders/connector.json"); |
| 54 | + |
43 | 55 | public static String getMappingFromFile(String path) throws IOException {
|
44 | 56 | URL url = IndexUtils.class.getClassLoader().getResource(path);
|
45 | 57 | if (url == null) {
|
46 | 58 | throw new IOException("Resource not found: " + path);
|
47 | 59 | }
|
48 | 60 |
|
49 | 61 | String mapping = Resources.toString(url, Charsets.UTF_8).trim();
|
50 |
| - if (mapping.isEmpty() || !StringUtils.isJson(mapping)) { |
51 |
| - throw new IllegalArgumentException("Invalid or non-JSON mapping at: " + path); |
| 62 | + if (mapping.isEmpty()) { |
| 63 | + throw new IllegalArgumentException("Empty mapping found at: " + path); |
52 | 64 | }
|
53 | 65 |
|
| 66 | + mapping = replacePlaceholders(mapping); |
| 67 | + validateMapping(mapping); |
| 68 | + |
54 | 69 | return mapping;
|
55 | 70 | }
|
56 | 71 |
|
| 72 | + public static String replacePlaceholders(String mapping) throws IOException { |
| 73 | + if (mapping == null || mapping.isBlank()) { |
| 74 | + throw new IllegalArgumentException("Mapping cannot be null or empty"); |
| 75 | + } |
| 76 | + |
| 77 | + // Preload resources into memory to avoid redundant I/O |
| 78 | + Map<String, String> loadedPlaceholders = new HashMap<>(); |
| 79 | + for (Map.Entry<String, String> placeholder : MAPPING_PLACEHOLDERS.entrySet()) { |
| 80 | + URL url = IndexUtils.class.getClassLoader().getResource(placeholder.getValue()); |
| 81 | + if (url == null) { |
| 82 | + throw new IOException("Resource not found: " + placeholder.getValue()); |
| 83 | + } |
| 84 | + |
| 85 | + loadedPlaceholders.put(placeholder.getKey(), Resources.toString(url, Charsets.UTF_8)); |
| 86 | + } |
| 87 | + |
| 88 | + StringBuilder result = new StringBuilder(mapping); |
| 89 | + for (Map.Entry<String, String> entry : loadedPlaceholders.entrySet()) { |
| 90 | + String placeholder = entry.getKey(); |
| 91 | + String replacement = entry.getValue(); |
| 92 | + |
| 93 | + // Replace all occurrences of the placeholder |
| 94 | + int index; |
| 95 | + while ((index = result.indexOf(placeholder)) != -1) { |
| 96 | + result.replace(index, index + placeholder.length(), replacement); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return result.toString(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Checks if the provided mapping is a valid JSON and validates it against a schema. |
| 105 | + * |
| 106 | + * <p>The schema is located at <code>mappings/schema.json</code> and enforces the following validations:</p> |
| 107 | + * |
| 108 | + * <ul> |
| 109 | + * <li>Mandatory fields: |
| 110 | + * <ul> |
| 111 | + * <li><code>_meta</code></li> |
| 112 | + * <li><code>_meta.schema_version</code></li> |
| 113 | + * <li><code>properties</code></li> |
| 114 | + * </ul> |
| 115 | + * </li> |
| 116 | + * <li>No additional fields are allowed at the root level.</li> |
| 117 | + * <li>No additional fields are allowed in the <code>_meta</code> object.</li> |
| 118 | + * <li><code>properties</code> must be an object type.</li> |
| 119 | + * <li><code>_meta</code> must be an object type.</li> |
| 120 | + * <li><code>_meta.schema_version</code> must be an integer.</li> |
| 121 | + * </ul> |
| 122 | + * |
| 123 | + * <p><strong>Note:</strong> Validation can be made stricter if a specific schema is defined for each index.</p> |
| 124 | + */ |
| 125 | + public static void validateMapping(String mapping) throws IOException { |
| 126 | + if (mapping.isBlank() || !StringUtils.isJson(mapping)) { |
| 127 | + throw new IllegalArgumentException("Invalid or non-JSON mapping found: " + mapping); |
| 128 | + } |
| 129 | + |
| 130 | + URL url = IndexUtils.class.getClassLoader().getResource(MAPPING_SCHEMA_PATH); |
| 131 | + if (url == null) { |
| 132 | + throw new IOException("Resource not found: " + MAPPING_SCHEMA_PATH); |
| 133 | + } |
| 134 | + |
| 135 | + String schema = Resources.toString(url, Charsets.UTF_8); |
| 136 | + validateSchema(schema, mapping); |
| 137 | + } |
| 138 | + |
57 | 139 | public static Integer getVersionFromMapping(String mapping) {
|
58 | 140 | if (mapping == null || mapping.isBlank()) {
|
59 | 141 | throw new IllegalArgumentException("Mapping cannot be null or empty");
|
|
0 commit comments