【发布时间】:2019-02-14 02:20:55
【问题描述】:
我正在尝试使用 reindex api 创建一组文档的副本。文档的字段之一 (uuid) 是 UUID。我需要复制的文档为uuid 字段设置新的 UUID。
根据 [1] 和 [2],方法 java.util.UUID.randomUUID() 未列入白名单以用于无痛脚本。
问题:
1) 如何在无痛中生成 UUID?
2) 为什么UUID.randomUUID() 被认为是不安全的操作?还是它没有被列入白名单只是一个疏忽?
3) 如何在"reindex" 上下文中将UUID.randomUUID() 列入白名单?我尝试根据 [3] 中的示例构建自己的 elasticsearch 无痛扩展/插件来执行此操作。问题是它只适用于"SearchScript" 上下文。似乎没有等效的"ReindexContext"。
这是我正在尝试的简化版本:
curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "product1"
},
"dest": {
"index": "product2"
},
"script": {
"source": "ctx._source.uuid = java.util.UUID.randomUUID().toString()",
"lang": "painless"
}
}
'
这会产生以下错误:
{
"error" : {
"root_cause" : [
{
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... rce.uuid = java.util.UUID.randomUUID().toString()",
" ^---- HERE"
],
"script" : "ctx._source.uuid = java.util.UUID.randomUUID().toString()",
"lang" : "painless"
}
],
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... rce.uuid = java.util.UUID.randomUUID().toString()",
" ^---- HERE"
],
"script" : "ctx._source.uuid = java.util.UUID.randomUUID().toString()",
"lang" : "painless",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "method [java.util.UUID, randomUUID/0] not found"
}
},
"status" : 500
}
我知道我的方法是有效的,并且上述是一个轻松的白名单问题,因为当我尝试不同的方法 (fromString()) 时,我没有收到任何错误:
curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "product1"
},
"dest": {
"index": "product2"
},
"script": {
"source": "ctx._source.uuid = java.util.UUID.fromString(\u0027ad139caa-5b54-4179-b812-5015daecad1e\u0027).toString()",
"lang": "painless"
}
}
'
参考文献:
[1] - https://discuss.elastic.co/t/generate-a-uuid-using-randomuuid-in-painless/144354/3
[2] - https://www.elastic.co/guide/en/elasticsearch/painless/6.6/painless-api-reference.html
[3] - https://github.com/elastic/elasticsearch/tree/v6.6.0/plugins/examples/painless-whitelist
其他说明:
Elasticsearch 6.6
我已经在弹性搜索论坛上问过这个问题(没有回复): https://discuss.elastic.co/t/need-to-generate-uuid-in-painless-script/165318
我还询问了有关将
UUID.randomUUID()方法列入白名单的问题(没有回复): https://discuss.elastic.co/t/question-about-painless-whitelist/165523
【问题讨论】:
-
可以在脚本中定义您自己的自定义实现/方法,以根据您的要求生成 uuid 并使用该方法。基本上,解决依赖关系
-
我解决此问题的功能请求已在此处接受并实施:github.com/elastic/elasticsearch/issues/39080
标签: elasticsearch elasticsearch-painless