Skip to content

Commit ab546ae

Browse files
authored
Update script supports java.lang.String.sha1() and java.lang.String.sha256() methods (#16923)
* Update script supports java.lang.String.sha1() and java.lang.String.sha256() methods Signed-off-by: Gao Binlong <gbinlong@amazon.com> * Modify change log Signed-off-by: Gao Binlong <gbinlong@amazon.com> --------- Signed-off-by: Gao Binlong <gbinlong@amazon.com>
1 parent 7a0e8fb commit ab546ae

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2121
- Support prefix list for remote repository attributes([#16271](https://github.com/opensearch-project/OpenSearch/pull/16271))
2222
- Add new configuration setting `synonym_analyzer`, to the `synonym` and `synonym_graph` filters, enabling the specification of a custom analyzer for reading the synonym file ([#16488](https://github.com/opensearch-project/OpenSearch/pull/16488)).
2323
- Add stats for remote publication failure and move download failure stats to remote methods([#16682](https://github.com/opensearch-project/OpenSearch/pull/16682/))
24+
- Update script supports java.lang.String.sha1() and java.lang.String.sha256() methods ([#16923](https://github.com/opensearch-project/OpenSearch/pull/16923))
2425
- Added a precaution to handle extreme date values during sorting to prevent `arithmetic_exception: long overflow` ([#16812](https://github.com/opensearch-project/OpenSearch/pull/16812)).
2526
- Add search replica stats to segment replication stats API ([#16678](https://github.com/opensearch-project/OpenSearch/pull/16678))
2627
- Introduce a setting to disable download of full cluster state from remote on term mismatch([#16798](https://github.com/opensearch-project/OpenSearch/pull/16798/))

modules/lang-painless/src/main/java/org/opensearch/painless/PainlessModulePlugin.java

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.opensearch.script.ScriptContext;
6767
import org.opensearch.script.ScriptEngine;
6868
import org.opensearch.script.ScriptService;
69+
import org.opensearch.script.UpdateScript;
6970
import org.opensearch.search.aggregations.pipeline.MovingFunctionScript;
7071
import org.opensearch.threadpool.ThreadPool;
7172
import org.opensearch.watcher.ResourceWatcherService;
@@ -109,6 +110,11 @@ public final class PainlessModulePlugin extends Plugin implements ScriptPlugin,
109110
ingest.add(AllowlistLoader.loadFromResourceFiles(Allowlist.class, "org.opensearch.ingest.txt"));
110111
map.put(IngestScript.CONTEXT, ingest);
111112

113+
// Functions available to update scripts
114+
List<Allowlist> update = new ArrayList<>(Allowlist.BASE_ALLOWLISTS);
115+
update.add(AllowlistLoader.loadFromResourceFiles(Allowlist.class, "org.opensearch.update.txt"));
116+
map.put(UpdateScript.CONTEXT, update);
117+
112118
// Functions available to derived fields
113119
List<Allowlist> derived = new ArrayList<>(Allowlist.BASE_ALLOWLISTS);
114120
derived.add(AllowlistLoader.loadFromResourceFiles(Allowlist.class, "org.opensearch.derived.txt"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# The OpenSearch Contributors require contributions made to
5+
# this file be licensed under the Apache-2.0 license or a
6+
# compatible open source license.
7+
#
8+
9+
# This file contains an allowlist for the update scripts
10+
11+
class java.lang.String {
12+
String org.opensearch.painless.api.Augmentation sha1()
13+
String org.opensearch.painless.api.Augmentation sha256()
14+
}

modules/lang-painless/src/yamlRestTest/resources/rest-api-spec/test/painless/15_update.yml

+36
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,39 @@
123123
- match: { error.root_cause.0.type: "illegal_argument_exception" }
124124
- match: { error.type: "illegal_argument_exception" }
125125
- match: { error.reason: "Iterable object is self-referencing itself" }
126+
127+
# update script supports java.lang.String.sha1() and java.lang.String.sha256() methods
128+
# related issue: https://github.com/opensearch-project/OpenSearch/issues/16423
129+
---
130+
"Update script supports sha1() and sha256() method for strings":
131+
- skip:
132+
version: " - 2.18.99"
133+
reason: "introduced in 2.19.0"
134+
- do:
135+
index:
136+
index: test_1
137+
id: 1
138+
body:
139+
foo: bar
140+
141+
- do:
142+
update:
143+
index: test_1
144+
id: 1
145+
body:
146+
script:
147+
lang: painless
148+
source: "ctx._source.foo_sha1 = ctx._source.foo.sha1();ctx._source.foo_sha256 = ctx._source.foo.sha256();"
149+
150+
- match: { _index: test_1 }
151+
- match: { _id: "1" }
152+
- match: { _version: 2 }
153+
154+
- do:
155+
get:
156+
index: test_1
157+
id: 1
158+
159+
- match: { _source.foo: bar }
160+
- match: { _source.foo_sha1: "62cdb7020ff920e5aa642c3d4066950dd1f01f4d" }
161+
- match: { _source.foo_sha256: "fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9" }

modules/reindex/src/yamlRestTest/resources/rest-api-spec/test/reindex/85_scripting.yml

+38
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,41 @@
440440
lang: painless
441441
source: syntax errors are fun!
442442
- match: {error.reason: 'compile error'}
443+
444+
# script in reindex supports java.lang.String.sha1() and java.lang.String.sha256() methods
445+
# related issue: https://github.com/opensearch-project/OpenSearch/issues/16423
446+
---
447+
"Script supports sha1() and sha256() method for strings":
448+
- skip:
449+
version: " - 2.18.99"
450+
reason: "introduced in 2.19.0"
451+
- do:
452+
index:
453+
index: twitter
454+
id: 1
455+
body: { "user": "foobar" }
456+
- do:
457+
indices.refresh: {}
458+
459+
- do:
460+
reindex:
461+
refresh: true
462+
body:
463+
source:
464+
index: twitter
465+
dest:
466+
index: new_twitter
467+
script:
468+
lang: painless
469+
source: ctx._source.user_sha1 = ctx._source.user.sha1();ctx._source.user_sha256 = ctx._source.user.sha256()
470+
- match: {created: 1}
471+
- match: {noops: 0}
472+
473+
- do:
474+
get:
475+
index: new_twitter
476+
id: 1
477+
478+
- match: { _source.user: foobar }
479+
- match: { _source.user_sha1: "8843d7f92416211de9ebb963ff4ce28125932878" }
480+
- match: { _source.user_sha256: "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2" }

modules/reindex/src/yamlRestTest/resources/rest-api-spec/test/update_by_query/80_scripting.yml

+35
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,38 @@
432432
lang: painless
433433
source: syntax errors are fun!
434434
- match: {error.reason: 'compile error'}
435+
436+
# script in update_by_query supports java.lang.String.sha1() and java.lang.String.sha256() methods
437+
# related issue: https://github.com/opensearch-project/OpenSearch/issues/16423
438+
---
439+
"Script supports sha1() and sha256() method for strings":
440+
- skip:
441+
version: " - 2.18.99"
442+
reason: "introduced in 2.19.0"
443+
- do:
444+
index:
445+
index: twitter
446+
id: 1
447+
body: { "user": "foobar" }
448+
- do:
449+
indices.refresh: {}
450+
451+
- do:
452+
update_by_query:
453+
index: twitter
454+
refresh: true
455+
body:
456+
script:
457+
lang: painless
458+
source: ctx._source.user_sha1 = ctx._source.user.sha1();ctx._source.user_sha256 = ctx._source.user.sha256()
459+
- match: {updated: 1}
460+
- match: {noops: 0}
461+
462+
- do:
463+
get:
464+
index: twitter
465+
id: 1
466+
467+
- match: { _source.user: foobar }
468+
- match: { _source.user_sha1: "8843d7f92416211de9ebb963ff4ce28125932878" }
469+
- match: { _source.user_sha256: "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2" }

0 commit comments

Comments
 (0)