【问题标题】:Reading a file in an Elasticsearch plugin在 Elasticsearch 插件中读取文件
【发布时间】:2016-05-25 22:09:45
【问题描述】:

我正在编写一个弹性搜索插件,它依赖于从磁盘上的文件中读取数据。当我尝试在我的代码中访问此文件时,出现以下异常。

Caused by: java.security.AccessControlException: access denied ("java.io.FilePermission" "patient_similarity/codes.txt" "read")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.security.AccessController.checkPermission(AccessController.java:884)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
at java.io.FileInputStream.<init>(FileInputStream.java:127)
at org.gatech.lucene.search.store.DotProductStore.<init>(DotProductStore.java:22)
at org.gatech.lucene.search.store.DotProductStore.newInstance(DotProductStore.java:71)
at org.gatech.elasticsearch.CommonsPlugin.onModule(CommonsPlugin.java:39)

有没有推荐的方法来访问 elasticsearch 插件中的文件?是否有任何快速的解决方法可以访问我的插件中的文件?

【问题讨论】:

    标签: java elasticsearch elasticsearch-plugin accesscontrolexception


    【解决方案1】:

    一种方法是通过禁用安全管理器来启动 Elasticsearch 进程,如下所示:

     bin/elasticsearch -Dsecurity.manager.enabled=false
    

    从 ES 2.x 开始,Java 安全管理器默认是启用的,之前它被禁用了。但请注意,此选项将为 removed in 2.3,因为它会使您的 ES 进程易受攻击。

    这样做的正确方法是自定义您的security policy 并使用policy files 指定您要访问的文件:

    grant { 
        permission java.io.FilePermission "/tmp/patient_similarity/codes.txt", "read,write";
    };
    

    您可以在四个不同的位置添加此策略:

    1. $JAVA_HOME/lib/security/java.policy 中的任一系统范围
    2. 或仅适用于 /home/elasticsearch/.java.policy 中的 elasticsearch 用户
    3. 或从命令行指定的文件:-Djava.security.policy=someURL
    4. 或在plugin-security.policy 文件中included in your plugin

    既然你正在开发一个插件,你当然应该使用选项 4。

    【讨论】:

    • 在 ES 6.x 文档中提到了安全管理器的东西,但细节已被删除。添加策略文件现在是执行此操作的唯一方法。
    • 我们使用的是 ES 版本 7.x 和 Java 12,我们需要在目录中读写。请提供解决方案。
    • @HardikDobariya 此问题已结束,请随时参考此问题提出新问题。
    • @Val:谢谢。我打开了一个新问题。 stackoverflow.com/questions/58326513/…
    • 使用puppet时可以实现选项3:elasticsearch::jvm_options: - '-Djava.security.policy=someUrl'
    【解决方案2】:

    我在我的 elasticsearch RestHandler 插件中遇到了类似的问题,我正在从某个 url 读取文件内容说“http://google.com/content.json”我通过以下方法解决了它 -

     `
    String url = "http://google.com/content.json";
    URL fileUrl = new URL(url);
    InputStream is = fileUrl.openStream();
    `
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 2017-06-18
      • 2016-10-18
      • 2018-11-18
      相关资源
      最近更新 更多