脚本和安全性
Elastic Stack Serverless
Painless 和 Elasticsearch 实现了多层安全机制,构建了安全运行脚本的深度防御策略。
Painless 使用细粒度的允许列表。任何不在允许列表中的内容都会导致编译错误。此功能是脚本深度防御策略的第一层安全机制。
第二层安全机制是 Java 安全管理器。 作为其启动序列的一部分,Elasticsearch 启用了 Java 安全管理器,以限制代码部分可以执行的操作。 Painless 使用 Java 安全管理器作为额外的防御层,以防止脚本执行诸如写入文件和侦听套接字之类的操作。
Elasticsearch 在 Linux 中使用 seccomp,在 macOS 中使用 Seatbelt,在 Windows 中使用 ActiveProcessLimit 作为额外的安全层,以防止 Elasticsearch 分叉或运行其他进程。
最后,脚本度量聚合中使用的脚本可以限制为定义的脚本列表,或者完全禁止。 这可以防止用户运行特别慢或资源密集型的聚合查询。
您可以修改以下脚本设置以限制允许运行的脚本类型,并控制脚本可以运行的可用 上下文。 要在您的深度防御策略中实现其他层,请遵循 Elasticsearch 安全原则。
Elasticsearch 支持两种脚本类型:inline
和 stored
。 默认情况下,Elasticsearch 配置为运行这两种类型的脚本。 要限制运行的脚本类型,请将 script.allowed_types
设置为 inline
或 stored
。 要阻止任何脚本运行,请将 script.allowed_types
设置为 none
。
如果您使用 Kibana,请将 script.allowed_types
设置为 both 或仅设置为 inline
。 某些 Kibana 功能依赖于内联脚本,如果 Elasticsearch 不允许内联脚本,则这些功能将无法按预期运行。
例如,要运行 inline
脚本,但不运行 stored
脚本
script.allowed_types: inline
默认情况下,允许所有脚本上下文。 使用 script.allowed_contexts
设置来指定允许的上下文。 要指定不允许任何上下文,请将 script.allowed_contexts
设置为 none
。
例如,要仅允许脚本在 scoring
和 update
上下文中运行
script.allowed_contexts: score, update
默认情况下,脚本度量聚合中允许所有脚本。 要限制允许的脚本集,请将 search.aggs.only_allowed_metric_scripts
设置为 true
,并使用 search.aggs.allowed_inline_metric_scripts
和/或 search.aggs.allowed_stored_metric_scripts
提供允许的脚本。
要禁止某些脚本类型,请省略相应的脚本列表 (search.aggs.allowed_inline_metric_scripts
或 search.aggs.allowed_stored_metric_scripts
) 或将其设置为空数组。 当两个脚本列表都不为空时,将允许给定的存储脚本和给定的内联脚本。
以下示例仅允许使用 4 个特定的存储脚本,而不允许使用任何内联脚本
search.aggs.only_allowed_metric_scripts: true
search.aggs.allowed_inline_metric_scripts: []
search.aggs.allowed_stored_metric_scripts:
- script_id_1
- script_id_2
- script_id_3
- script_id_4
相反,下一个示例允许使用特定的内联脚本,但不允许使用任何存储脚本
search.aggs.only_allowed_metric_scripts: true
search.aggs.allowed_inline_metric_scripts:
- 'state.transactions = []'
- 'state.transactions.add(doc.some_field.value)'
- 'long sum = 0; for (t in state.transactions) { sum += t } return sum'
- 'long sum = 0; for (a in states) { sum += a } return sum'
search.aggs.allowed_stored_metric_scripts: []