Skip to content

Commit 470e4ee

Browse files
committed
Used UTF-8 bytes for length
Signed-off-by: Owais <[email protected]>
1 parent 5ea651c commit 470e4ee

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2424
### Changed
2525
- Migrate BC libs to their FIPS counterparts ([#14912](https://github.com/opensearch-project/OpenSearch/pull/14912))
2626
- Increase the floor segment size to 16MB ([#17699](https://github.com/opensearch-project/OpenSearch/pull/17699))
27-
- Add validation for Ingest and Search pipeline length ([#17786](https://github.com/opensearch-project/OpenSearch/pull/17786))
27+
- Introduce 512 byte limit to search and ingest pipeline IDs ([#17786](https://github.com/opensearch-project/OpenSearch/pull/17786))
2828

2929
### Dependencies
3030
- Bump `com.nimbusds:nimbus-jose-jwt` from 9.41.1 to 10.0.2 ([#17607](https://github.com/opensearch-project/OpenSearch/pull/17607), [#17669](https://github.com/opensearch-project/OpenSearch/pull/17669))

server/src/main/java/org/opensearch/ingest/IngestService.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.logging.log4j.LogManager;
3636
import org.apache.logging.log4j.Logger;
3737
import org.apache.logging.log4j.message.ParameterizedMessage;
38+
import org.apache.lucene.util.UnicodeUtil;
3839
import org.opensearch.ExceptionsHelper;
3940
import org.opensearch.OpenSearchParseException;
4041
import org.opensearch.ResourceNotFoundException;
@@ -108,7 +109,7 @@ public class IngestService implements ClusterStateApplier, ReportingService<Inge
108109
public static final String NOOP_PIPELINE_NAME = "_none";
109110

110111
public static final String INGEST_ORIGIN = "ingest";
111-
private static final int MAX_PIPELINE_ID_LENGTH = 20;
112+
private static final int MAX_PIPELINE_ID_BYTES = 512;
112113

113114
/**
114115
* Defines the limit for the number of processors which can run on a given document during ingestion.
@@ -514,13 +515,16 @@ void validatePipeline(Map<DiscoveryNode, IngestInfo> ingestInfos, PutPipelineReq
514515
throw new IllegalStateException("Ingest info is empty");
515516
}
516517

517-
if (request.getId().length() > MAX_PIPELINE_ID_LENGTH) {
518+
int pipelineIdLength = UnicodeUtil.calcUTF16toUTF8Length(request.getId(), 0, request.getId().length());
519+
520+
if (pipelineIdLength > MAX_PIPELINE_ID_BYTES) {
518521
throw new IllegalArgumentException(
519522
String.format(
520523
Locale.ROOT,
521-
"Pipeline id [%s] exceeds maximum length of %d characters",
524+
"Pipeline id [%s] exceeds maximum length of %d UTF-8 bytes (actual: %d bytes)",
522525
request.getId(),
523-
MAX_PIPELINE_ID_LENGTH
526+
MAX_PIPELINE_ID_BYTES,
527+
pipelineIdLength
524528
)
525529
);
526530
}

server/src/main/java/org/opensearch/search/pipeline/SearchPipelineService.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.lucene.util.UnicodeUtil;
1314
import org.opensearch.ExceptionsHelper;
1415
import org.opensearch.OpenSearchParseException;
1516
import org.opensearch.ResourceNotFoundException;
@@ -74,7 +75,7 @@ public class SearchPipelineService implements ClusterStateApplier, ReportingServ
7475
public static final String SEARCH_PIPELINE_ORIGIN = "search_pipeline";
7576
public static final String AD_HOC_PIPELINE_ID = "_ad_hoc_pipeline";
7677
public static final String NOOP_PIPELINE_ID = "_none";
77-
private static final int MAX_PIPELINE_ID_LENGTH = 20;
78+
private static final int MAX_PIPELINE_ID_BYTES = 512;
7879
private static final Logger logger = LogManager.getLogger(SearchPipelineService.class);
7980
private final ClusterService clusterService;
8081
private final ScriptService scriptService;
@@ -281,13 +282,16 @@ void validatePipeline(Map<DiscoveryNode, SearchPipelineInfo> searchPipelineInfos
281282
throw new IllegalStateException("Search pipeline info is empty");
282283
}
283284

284-
if (request.getId().length() > MAX_PIPELINE_ID_LENGTH) {
285+
int pipelineIdLength = UnicodeUtil.calcUTF16toUTF8Length(request.getId(), 0, request.getId().length());
286+
287+
if (pipelineIdLength > MAX_PIPELINE_ID_BYTES) {
285288
throw new IllegalArgumentException(
286289
String.format(
287290
Locale.ROOT,
288-
"Search pipeline id [%s] exceeds maximum length of %d characters",
291+
"Search Pipeline id [%s] exceeds maximum length of %d UTF-8 bytes (actual: %d bytes)",
289292
request.getId(),
290-
MAX_PIPELINE_ID_LENGTH
293+
MAX_PIPELINE_ID_BYTES,
294+
pipelineIdLength
291295
)
292296
);
293297
}

server/src/test/java/org/opensearch/ingest/IngestServiceTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import java.util.Comparator;
9090
import java.util.HashMap;
9191
import java.util.List;
92+
import java.util.Locale;
9293
import java.util.Map;
9394
import java.util.Objects;
9495
import java.util.Set;
@@ -397,8 +398,10 @@ public void testValidateNoIngestInfo() throws Exception {
397398

398399
public void testValidatePipelineId_WithNotValidLength_ShouldThrowException() throws Exception {
399400
IngestService ingestService = createWithProcessors();
401+
402+
String longId = "a".repeat(512) + "a";
400403
PutPipelineRequest putRequest = new PutPipelineRequest(
401-
"_idwyebdjgeiwnddhekebddmd",
404+
longId,
402405
new BytesArray(
403406
"{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}},"
404407
+ "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}"
@@ -418,7 +421,12 @@ public void testValidatePipelineId_WithNotValidLength_ShouldThrowException() thr
418421
IllegalArgumentException.class,
419422
() -> ingestService.validatePipeline(Collections.singletonMap(discoveryNode, ingestInfo), putRequest)
420423
);
421-
assertEquals("Pipeline id [_idwyebdjgeiwnddhekebddmd] exceeds maximum length of 20 characters", e.getMessage());
424+
String errorMessage = String.format(
425+
Locale.ROOT,
426+
"Pipeline id [%s] exceeds maximum length of 512 UTF-8 bytes (actual: 513 bytes)",
427+
longId
428+
);
429+
assertEquals(errorMessage, e.getMessage());
422430
}
423431

424432
public void testGetProcessorsInPipeline() throws Exception {

server/src/test/java/org/opensearch/search/pipeline/SearchPipelineServiceTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import java.util.Comparator;
7171
import java.util.HashMap;
7272
import java.util.List;
73+
import java.util.Locale;
7374
import java.util.Map;
7475
import java.util.concurrent.ExecutorService;
7576
import java.util.concurrent.atomic.AtomicReference;
@@ -892,8 +893,9 @@ public void testValidatePipeline() throws Exception {
892893
MediaTypeRegistry.JSON
893894
);
894895

896+
String longId = "a".repeat(512) + "a";
895897
PutSearchPipelineRequest maxLengthIdPutRequest = new PutSearchPipelineRequest(
896-
"_idwyebdjgeiwnddhekebddmd",
898+
longId,
897899
new BytesArray("{\"request_processors\" : [ { \"scale_request_size\": { \"scale\" : \"foo\" } } ] }"),
898900
MediaTypeRegistry.JSON
899901
);
@@ -916,7 +918,12 @@ public void testValidatePipeline() throws Exception {
916918
IllegalArgumentException.class,
917919
() -> searchPipelineService.validatePipeline(Map.of(n1, completePipelineInfo), maxLengthIdPutRequest)
918920
);
919-
assertEquals("Search pipeline id [_idwyebdjgeiwnddhekebddmd] exceeds maximum length of 20 characters", e.getMessage());
921+
String errorMessage = String.format(
922+
Locale.ROOT,
923+
"Search Pipeline id [%s] exceeds maximum length of 512 UTF-8 bytes (actual: 513 bytes)",
924+
longId
925+
);
926+
assertEquals(errorMessage, e.getMessage());
920927

921928
// Invalid configuration in request
922929
PutSearchPipelineRequest badPutRequest = new PutSearchPipelineRequest(

0 commit comments

Comments
 (0)