gpt4 book ai didi

具有特权的JVM中的Java文件setWritable

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:52 24 4
gpt4 key购买 nike

我使用带有图像 java:8u111-jdk 的 Gitlab CI 从源代码构建 Apache Flink,发现文件权限测试失败,因为文件权限未得到遵守。

其中一个单元测试如下:

    @Test
public void testDeleteDirectory() throws Exception {

// deleting a non-existent file should not cause an error

File doesNotExist = new File(tmp.newFolder(), "abc");
FileUtils.deleteDirectory(doesNotExist);

// deleting a write protected file should throw an error

File cannotDeleteParent = tmp.newFolder();
File cannotDeleteChild = new File(cannotDeleteParent, "child");

try {
assumeTrue(cannotDeleteChild.createNewFile());
assumeTrue(cannotDeleteParent.setWritable(false));
assumeTrue(cannotDeleteChild.setWritable(false));

FileUtils.deleteDirectory(cannotDeleteParent);
fail("this should fail with an exception");
}
catch (AccessDeniedException ignored) {
// this is expected
}
finally {
//noinspection ResultOfMethodCallIgnored
cannotDeleteParent.setWritable(true);
//noinspection ResultOfMethodCallIgnored
cannotDeleteChild.setWritable(true);
}
}

测试结果为:

testDeleteDirectory(org.apache.flink.util.FileUtilsTest)  Time elapsed: 0.022 sec  <<< FAILURE!
java.lang.AssertionError: this should fail with an exception
at org.junit.Assert.fail(Assert.java:88)
at org.apache.flink.util.FileUtilsTest.testDeleteDirectory(FileUtilsTest.java:129)

当我深入研究代码时,我在 java.io.File 中发现了以下语句:

On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to modify files that disallow write operations.

所以我怀疑是CI环境出了问题。我怎样才能进一步调试问题?谢谢!

环境:

  • docker镜像:java:8u111-jdk
  • 操作系统内核:Linux 4.9.0-8-amd64 x86_64
  • 发行版:Debian 8
  • JDK版本:openjdk 1.8.0_111

最佳答案

问题是我们以 root 用户身份运行此代码,因此我们能够删除写保护的文件。

关于具有特权的JVM中的Java文件setWritable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com