ElasticSearch 劫持类型安全配置文件内容

ElasticSearch hijacking typesafe config file contents

我正在尝试为弹性插件加载自定义配置,myConfig.conf,因此:

conf = ConfigFactory.load("myConfig.conf");

其中只有内容:

myInteger: 1234

当我尝试访问变量 myInteger 时,它失败了:

int bar1 = conf.getInt("myInteger");

有错误信息:

com.typesafe.config.ConfigException$Missing: system properties: No configuration setting found for key 'myInteger'

当我打印出 myConfig.conf 的内容时,它显示了 Elastic 配置的转储,如下所示:

Config(SimpleConfigObject({"es":{"bundled_jdk":"false","distribution":{"flavor":"oss","type":"zip"},"logs":{"base_path":"/Users/me/Downloads/project/build/testclusters/integTest-0/logs","cluster_name":"integTest","node_name":"integTest-0"},"networkaddress":{"cache":{"negative":{"ttl":"10"},"ttl":"60"}},"path":{"conf":"/Users/me/Downloads/project/build/testclusters/integTest-0/config","home":"/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST"}},"file":{"encoding":"UTF-8","separator":"/"},"ftp":{"nonProxyHosts":"local|*.local|169.254/16|*.169.254/16"},"http":{"nonProxyHosts":"local|*.local|169.254/16|*.169.254/16"},"io":{"netty":{"allocator":{"numDirectArenas":"0"},"noKeySetOptimization":"true","noUnsafe":"true","recycler":{"maxCapacityPerThread":"0"}}},"java":{"awt":{"headless":"true"},"class":{"path":"/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-queries-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/hppc-0.8.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-core-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/log4j-api-2.11.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-suggest-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-analyzers-common-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jopt-simple-5.0.2.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-highlighter-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-dataformat-cbor-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-spatial3d-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-secure-sm-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-join-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/log4j-core-2.11.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/java-version-checker-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-cli-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-x-content-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-spatial-extras-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/snakeyaml-1.26.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-queryparser-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-geo-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-dataformat-smile-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-plugin-classloader-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/t-digest-3.2.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-misc-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-sandbox-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-core-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jna-4.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-backward-codecs-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/spatial4j-0.7.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-grouping-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-dataformat-yaml-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-memory-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-launchers-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/HdrHistogram-2.1.9.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-core-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jts-core-1.15.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/joda-time-2.10.4.jar","version":"59.0"},"home":"/usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home","io":{"tmpdir":"/Users/me/Downloads/project/build/testclusters/integTest-0/tmp"},"library":{"path":"/Users/me/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:."},"locale":{"providers":"SPI,COMPAT"},"runtime":{"name":"OpenJDK Runtime Environment","version":"15.0.1+9"},"specification":{"name":"Java Platform API Specification","vendor":"Oracle Corporation","version":"15"},"vendor":{"url":{"bug":"https://bugreport.java.com/bugreport/"}},"version":"15.0.1","vm":{"compressedOopsMode":"Zero based","info":"mixed mode, sharing","name":"OpenJDK 64-Bit Server VM","specification":{"name":"Java Virtual Machine Specification","vendor":"Oracle Corporation","version":"15"},"vendor":"Oracle Corporation","version":"15.0.1+9"}},"jdk":{"debug":"release"},"jna":{"loaded":"true","nosys":"true","platform":{"library":{"path":"/usr/lib:/usr/lib"}}},"jnidispatch":{"path":"/Users/me/Downloads/project/build/testclusters/integTest-0/tmp/jna-518060194/jna19175516516881411.tmp"},"line":{"separator":"\n"},"log4j":{"shutdownHookEnabled":"false"},"log4j2":{"disable":{"jmx":"true"}},"os":{"arch":"x86_64","name":"Mac OS X","version":"10.15.5"},"path":{"separator":":"},"socksNonProxyHosts":"local|*.local|169.254/16|*.169.254/16","sun":{"arch":{"data":{"model":"64"}},"boot":{"library":{"path":"/usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home/lib"}},"cpu":{"endian":"little"},"io":{"unicode":{"encoding":"UnicodeBig"}},"java":{"command":"org.elasticsearch.bootstrap.Elasticsearch","launcher":"SUN_STANDARD"},"jnu":{"encoding":"UTF-8"},"management":{"compiler":"HotSpot 64-Bit Tiered Compilers"},"nio":{"ch":{"bugLevel":""}}},"user":{"country":"GB","dir":"/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST","home":"/Users/me","language":"en","name":"me","timezone":"Europe/London"}}))

它成功地识别出该文件存在(如果我伪造一个文件路径,它将不起作用)。

但无法识别/阅读 myConfig.conf 的任何内容。

这是为什么?我该如何解决这个问题?


编辑

我还应该注意,要在没有 Elastic 抱怨的情况下读取配置文件,我必须执行以下操作:

AccessController.doPrivileged((PrivilegedAction<AssignmentConfig>) () -> {
  try {
    return AssignmentConfig.configure();
  } catch (Exception e) {
    e.printStackTrace();
  }
  return null;
});

在 elasticsearch 插件中使用外部配置文件是个坏主意。 ES 提供了一种扩展 elasticsearch 配置的机制。您所有的自定义配置都应与插件中的自定义设置注册一起放在 elasticsearch.yml 中,如下所示:

public class MyESPlugin extends Plugin implements ... {

@Override
public List<Setting<?>> getSettings() {
    return Arrays.asList(new Setting<>("setting1", "", Function.identity(), Setting.Property.NodeScope),
        new Setting<>("setting2", "", Function.identity(), Setting.Property.NodeScope), ...);
}

然后,在您的 elasticsearch.yml 中您可以添加:

setting1: ...
setting2: ...

但请注意,在启动节点之前必须安装插件,否则节点将无法启动,因为它无法识别自定义设置。